If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to use temporarytable in such scenario, sql query DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-26-09, 12:47
cprash.aggarwal cprash.aggarwal is offline
Registered User
 
Join Date: Mar 2009
Posts: 24
How to use temporarytable in such scenario, sql query DB2

i want to perform query like this in which i select some fields like ITM_ID, EC_MC_ID, EC_RL_DT, DES_SEQNUM in inner select query and store tehm in temporary table. now i want to use values from that temp table to update some other table.
Please guide me how can i perform such task. here i m getting error as temp table TEMP_PNEC is created afterwards but i m using it to update some table before its declaration

UPDATE ERE2.OBJECT_DATA SET Q_DATREL = TEMP_PNEC.EC_RL_DT, Q_DESIGNSEQ = TEMP_PNEC.DES_SEQNUM
WHERE EXISTS
(SELECT *
FROM (SELECT ITEM_OBJID, ITM_ID, EC_MC_ID, EC_RL_DT, DES_SEQNUM FROM "join of some tables" ) AS TEMP_PNEC
WHERE
OBJECT_DATA.ITEM_OBJID = TEMP_PNEC.ITEM_OBJID
AND OBJECT_DATA.S_IDCNG = TEMP_PNEC.EC_MC_ID
AND OBJECT_DATA.Q_PMSYNC='Y' AND OBJECT_DATA.S_DC='RD' AND OBJECT_DATA.Q_DATEPMSYNC < OBJECT_DATA.Q_LASTMOD
)
;

Thanks
Prashant Aggarwal
Reply With Quote
  #2 (permalink)  
Old 03-26-09, 16:39
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Please use MERGE statement.

Problem while doing Update using temporary table
Quote:
You can not use Common Table Expression in an UPDATE statement.

Use EXISTS predicate with Nested Table Expression.
Or MERGE statement, if it is neccesary to use column(s) of CTE in SET clause.

.....
Reply With Quote
  #3 (permalink)  
Old 03-27-09, 08:02
cprash.aggarwal cprash.aggarwal is offline
Registered User
 
Join Date: Mar 2009
Posts: 24
Quote:
Originally Posted by tonkuma
Can you please illustrate using Merge statement?
Reply With Quote
  #4 (permalink)  
Old 03-27-09, 09:24
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
There are plenty of examples in the manual
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On