August 14, 2007 at 4:08 pm
Any idea how to do an update that contains an inner join in DB2?
and I need to update them with the values from Table2.Column2 (cubiscan.CFFIHGTH)
where Table1.Column1(FFIITMAP.FFIITMN) = Table2.Column1(cubiscan.CFFIITMN)>
--DB2 Doesn't like the From>
set FFIHGTH = CFFIHGTH
From idsdemo.FFIITMAP
inner join idsdemo.cubiscan on ltrim(rtrim(idsdemo.cubiscan.CFFIITMN)) = ltrim(rtrim(idsdemo.FFIITMAP.FFIITMN))>
Update idsdemo.FFIITMAP
inner join idsdemo.cubiscan on ltrim(rtrim(idsdemo.cubiscan.CFFIITMN)) = ltrim(rtrim(idsdemo.FFIITMAP.FFIITMN))
set FFIHGTH = CFFIHGTH >
Update idsdemo.FFIITMAP
set FFIHGTH =
(select CFFIHGTH from idsdemo.cubiscan
inner join idsdemo.FFIITMAP on ltrim(rtrim(idsdemo.FFIITMAP.FFIITMN)) = ltrim(rtrim(idsdemo.cubiscan.CFFIITMN))
)>
August 15, 2007 at 8:08 am
the table idsdemo.FFIITMAP is already part of the query, so you don't want to say FROM <itself>
you want to say FROM <other table> and then add the join conditions.
I think this is what you are looking for:
Update idsdemo.FFIITMAP
set idsdemo.FFIITMAP.FFIHGTH = idsdemo.cubiscan.CFFIHGTH
From idsdemo.cubiscan
WHERE ltrim(rtrim(idsdemo.cubiscan.CFFIITMN)) = ltrim(rtrim(idsdemo.FFIITMAP.FFIITMN))
Lowell
August 15, 2007 at 8:18 am
I gave that a try but it still gives me an error dealing with the FROM causing a problem.
Keyword FROM not expected. Valid tokens: <END-OF-STATEMENT>.
August 15, 2007 at 8:26 am
wierd...it's ok syntax wise, so I suspect is has to do with teh multi-part-name.
I'd suggest aliasing the table to fix it:
Update idsdemo.FFIITMAP
set idsdemo.FFIITMAP.FFIHGTH = A.CFFIHGTH
From idsdemo.cubiscan A
WHERE ltrim(rtrim(A.CFFIITMN)) = ltrim(rtrim(idsdemo.FFIITMAP.FFIITMN))
Lowell
August 15, 2007 at 8:39 am
I tried that as well but I got another error: Column qualifier or table A undefined.
I actually run without the table name prefix in the set and it still gives me an error on the FROM. Is the syntax that much different in DB2 than SQL2000? I'm used to working with SQL2000 but not so much with DB2.
Update idsdemo.FFIITMAP
set FFIHGTH = CFFIHGTH
From idsdemo.cubiscan
WHERE ltrim(rtrim(idsdemo.cubiscan.CFFIITMN)) = ltrim(rtrim(idsdemo.FFIITMAP.FFIITMN))
August 15, 2007 at 8:55 am
since this is db2 instead of sql, does that mean the idsdemo.FFIITMAP is not databasename.tablename? so the tablename has a period in it? ie [idsdemo.FFIITMAP]
if that's true, then we'd just add [] brackets to allow the name to be evaluated:
Update [idsdemo.FFIITMAP]
set FFIHGTH = CFFIHGTH
From [idsdemo.cubiscan]
WHERE ltrim(rtrim([idsdemo.cubiscan].CFFIITMN)) = ltrim(rtrim([idsdemo.FFIITMAP].FFIITMN))
Lowell
August 15, 2007 at 9:15 am
No, the DB name is idsdemo and table is FFIITMAP the same as SQL is. I tried the suggestion anyways and it produced an error on the [
I think it is just unfamiliar with a FROM within an Update Statement for some reason. I tried to work around the problem and not use the FROM by using a inner select but it says it returns more than 1 record (which I'm aware of and trying to do) however it doesn't give me a syntax error. Either way I can't get it to update the data.
Update idsdemo.FFIITMAP
set FFIHGTH =
(select CFFIHGTH from idsdemo.cubiscan
inner join idsdemo.FFIITMAP on ltrim(rtrim(idsdemo.FFIITMAP.FFIITMN)) = ltrim(rtrim(idsdemo.cubiscan.CFFIITMN))
)
August 16, 2007 at 7:40 am
Okay in an attempt to accomplish the same goal I've written a cursor however DB2 seems to have a problem with my variables that are declared. Anyone familiar with DB2 syntax or how I need to write a cursor to get it to execute?
declare @CFFIITMN as varchar (10)
declare @CFFIHGTH as varchar (10)
declare Cubiscan_Cursor cursor for
select ltrim(rtrim(CFFIITMN)), CFFIHGTH
FROM idsdemo.cubiscan
open Cubiscan_Cursor
fetch next from Cubiscan_Cursor into @CFFIITMN, @CFFIHGTH
while @@fetch_status = 0
begin
Update idsdemo.FFIITMAP
Set FFIHGTH = @CFFIHGTH
WHERE ltrim(rtrim(FFIITMN)) = @CFFIITMN
fetch next from Cubiscan_Cursor into @CFFIITMN, @CFFIHGTH
end
close Cubiscan_Cursor
deallocate Cubiscan_Cursor
August 16, 2007 at 11:06 am
I think the following is standard ANSI so it may work:
UPDATE FFIITMAP
SET FFIHGTH = (
SELECT MAX(C.CFFIHGTH)
FROM cubiscan C
WHERE C.CFFIITMN = FFIITMAP.FFIITMN
GROUP BY C.CFFIITMN
)
WHERE EXISTS (
SELECT *
FROM cubiscan C1
WHERE C1.CFFIITMN = FFIITMAP.FFIITMN
)
September 13, 2007 at 10:19 am
I forgot to post what I used that finally allowed me to update the field. The script is below.
UPDATE idsdemo.FFIITMAP
SET FFIHGTH = (SELECT FFIHGTH
FROM idsdemo.cubiscan1
WHERE ltrim(rtrim(idsdemo.cubiscan1.FFIITMN)) = ltrim(rtrim(idsdemo.FFIITMAP.FFIITMN)))
Where ltrim(rtrim(FFIITMN)) in (select ltrim(rtrim(FFIITMN)) from idsdemo.cubiscan1)
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply