Unix Technical Forum

update with join of multiple tables

This is a discussion on update with join of multiple tables within the DB2 forums, part of the Database Server Software category; --> Apologize for posting this question.... Yes there were postings on update with join.... My question involves 4 table join... ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 03:30 AM
beena
 
Posts: n/a
Default update with join of multiple tables

Apologize for posting this question....
Yes there were postings on update with join....
My question involves 4 table join... (hopefully qualifies as a new
question)

Need to convert the following sql from SYbase to UDB 8.2 FP8 on AIX.

UPDATE TABLE A
SET A.FLD_SUPV = B.FLD_SUPV
FROM TABLEA A, TABLEB B, TABLEC C,TABLED D
WHERE (A.FLD1= B.FLD1
AND A.FLD_DT >= B.FLD_FM_DT
AND A.FLD_DT <= B.FLD_THRU_DT)
AND A.FLD_DT > D.FLD_THRU_DT
AND A.FLD_DT < C.FLD_EFF_DT

Any suggestions... Thanks in advance....

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 03:30 AM
Knut Stolze
 
Posts: n/a
Default Re: update with join of multiple tables

beena wrote:

> Apologize for posting this question....
> Yes there were postings on update with join....
> My question involves 4 table join... (hopefully qualifies as a new
> question)
>
> Need to convert the following sql from SYbase to UDB 8.2 FP8 on AIX.
>
> UPDATE TABLE A
> SET A.FLD_SUPV = B.FLD_SUPV
> FROM TABLEA A, TABLEB B, TABLEC C,TABLED D
> WHERE (A.FLD1= B.FLD1
> AND A.FLD_DT >= B.FLD_FM_DT
> AND A.FLD_DT <= B.FLD_THRU_DT)
> AND A.FLD_DT > D.FLD_THRU_DT
> AND A.FLD_DT < C.FLD_EFF_DT


DB2 and the SQL standard don't have a FROM clause in an UPDATE statement.
So you have to clearly separate the steps to (a) identify the rows to be
modified and to (b) compute the new value.

UPDATE TABLE A
SET A.FLD_SUPV = ( SELECT B.FLD_SUPV
FROM TABLEA A, TABLEB B, TABLEC C,TABLED D
WHERE A.FLD1= B.FLD1
AND A.FLD_DT >= B.FLD_FM_DT
AND A.FLD_DT <= B.FLD_THRU_DT
AND A.FLD_DT > D.FLD_THRU_DT
AND A.FLD_DT < C.FLD_EFF_DT )
WHERE EXISTS ( SELECT B.FLD_SUPV
FROM TABLEA A, TABLEB B, TABLEC C,TABLED D
WHERE A.FLD1= B.FLD1
AND A.FLD_DT >= B.FLD_FM_DT
AND A.FLD_DT <= B.FLD_THRU_DT
AND A.FLD_DT > D.FLD_THRU_DT
AND A.FLD_DT < C.FLD_EFF_DT )

The optimizer will see that the sub-queries in the SET and the FROM clause
are identical and it should merge them in the internal execution plan.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 03:30 AM
Serge Rielau
 
Posts: n/a
Default Re: update with join of multiple tables

MERGE INTO TABLEA A
USING (SELECT B.FLD_SUPV
FROM TABLEB B, TABLEC C,TABLED D
WHERE (A.FLD1= B.FLD1
AND A.FLD_DT >= B.FLD_FM_DT
AND A.FLD_DT <= B.FLD_THRU_DT)
AND A.FLD_DT > D.FLD_THRU_DT
AND A.FLD_DT < C.FLD_EFF_DT) AS S
ON 1=1
WHEN MATCHED THEN
UPDATE SET A.FLD_SUPV = S.FLD_SUPV

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:29 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com