Link to home
Start Free TrialLog in
Avatar of SharonBrindley
SharonBrindley

asked on

Convert SQL to Interbase

I have a SQL statement which I need to convert to work with Interbasse:

UPDATE Table1 SET Table1.AField = Table2.AField FROM Table1 INNER JOIN Table2 ON Table1.FieldID = Table2.FieldID WHERE Table1.AField IS NULL OR Table1.AField = ''

the query updates a field in table 1 from the same field in table 2 only on records where table 1 does not already have data populated in that field
ASKER CERTIFIED SOLUTION
Avatar of BAlexandrov
BAlexandrov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vogonPoet
vogonPoet




 UPDATE Table1 SET Table1.AField =

 ( select Table2.AField
   FROM Table1 INNER JOIN Table2
   ON Table1.FieldID = Table2.FieldID )

 WHERE Table1.AField IS NULL OR Table1.AField = ''


Pseudo - SQL :

update all rows in Table1  where AField is null OR AField is empty string.

Table1.AField gets the value of Table2.AField where there is one row that matches FieldID between both.

The relationship between 'Table1.FieldID = Table2.FieldID'
must be 1 to 1, or you'll may probably get a "multiple rows in a singleton select" error.

I see that  BAlexandrov has recommended a stored procedure, which is probably the best way to handle it in a controlled manner.

Good Luck!
As you(vogonPoet) have wrote:

UPDATE Table1 SET Table1.AField =
( select Table2.AField
  FROM Table1 INNER JOIN Table2
  ON Table1.FieldID = Table2.FieldID )

And if we have the case that in Table1.AField = '' but there is no data in Table2 either to what value will be set
Table1.AField if the inner select does not return nothing?
May be it will become null, there was a case in IB 6.2 that when you join with stored procedure that does not return nothing it causes server to restart...
For this reason I have added the second select in the where clause that checks if there is data in table2.
Good point, BAlexandrov.

I was looking at it much too late at night.
Hi SharonBrindley,

if you got the needed answer, please accept it by clicking on Accept in the header of the good answer. By this way you can express thanks for expert's support

with best regards

Janos

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       to accept BAlexandrov's answer
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

kacor
EE Cleanup Volunteer