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
SharonBrindleyAsked:
Who is Participating?
 
BAlexandrovConnect With a Mentor Commented:
Infortunatelly this is not standart ANSI SQL and IB does not support it.
Alternative is:

UPDATE Table1
SET Table1.AField =
 (Select Table2.AField
  FROM Table2
  WHERE Table2.FieldID = Table1.FieldID)
Where
 Exists(Select 1
        FROM Table2
        WHERE Table2.FieldID = Table1.FieldID) and
 (Table1.AField IS NULL OR Table1.AField = '')

This is more easy done in Stored procedure. If you are not familiar with that - ask again.


0
 
vogonPoetCommented:



 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!
0
 
BAlexandrovCommented:
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
vogonPoetCommented:
Good point, BAlexandrov.

I was looking at it much too late at night.
0
 
kacorretiredCommented:
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
0
 
kacorretiredCommented:

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
0
All Courses

From novice to tech pro — start learning today.