MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Subselect in the update statement - DB2V7

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
vallishar
Beginner


Joined: 17 Dec 2002
Posts: 53
Topics: 14
Location: BengaLuru

PostPosted: Wed Dec 18, 2002 6:25 pm    Post subject: Subselect in the update statement - DB2V7 Reply with quote

Hi

I am using DB2 V7 on OS-390.

This version of DB2 provides the facility of a subselect in an Update statement,
which is not allowed in the earlier version.

I have tried to execute a step like this:

The following gives the table structures and sample values which I am using.
Code:

TableA            
---------------------           
Col1A  Col2A Timestamp         

AAA      Y         
BBB      N         
CCC      Y         
            

TableB
---------------------------
Col1B   Col2B   Col3B   Col4B   Timestamp

AAA    Y    BBB    Y
CCC    Y    AAA    Y
BBB    Y    CCC    Y
AAA     Y    CCC    Y


The update query looks like this:
The values in the update statement are those at the
time of execution.

Code:


UPDATE TABLEA  A                               
SET    COL2A = 'Y',
       Timestamp  =
       CURRENT TIMESTAMP                   
WHERE  COL1A   = 'AAA'
  AND  COL1A NOT IN                               
         (SELECT B.COL1B FROM                   
             TABLEB B , TABLEA C       
           WHERE B.COL1B = 'AAA'
             AND B.COL2B = 'Y'
             AND B.COL3B = C.COL1A
             AND B.COL4B = C.COL2A )     


If the table has the data as above, the update would be successful.

Code:


TableA         
---------------------           
Col1A  Col2A Timestamp         

AAA      Y         
BBB      Y         
CCC      Y         
            

TableB
---------------------------
Col1B   Col2B   Col3B   Col4B   Timestamp

AAA    Y   BBB    Y
CCC    Y   AAA    Y
BBB    Y   CCC    Y
AAA     Y   CCC    Y



If the data were to be like this, then the update would have failed.

But, prior to execution, if the data were :

Code:


TableA            
---------------------           
Col1A  Col2A Timestamp         

AAA      N         
BBB      N         
CCC      Y         
      

TableB
---------------------------
Col1B   Col2B   Col3B   Col4B   Timestamp

AAA    Y   BBB    Y
CCC    Y   AAA    Y
BBB    Y   CCC    Y
AAA     Y   CCC    Y



And if two different instances of the same program were being executed
one, to update the row with value 'AAA' to 'Y' and another, to update the row with value 'BBB' to 'Y'.

Ideally speaking, the update should have been failed if the updates have occured on different rows were to be seen by the subselect. But it didnt happen. Both the updates were successful.

The DB2 V7 manual says that it makes use of temporary files while performing the subselect on the update.

Can anybody throw some light on :

1. How does the Sub-select work when used in the Update statement like this ?
2. Why did the update as coded above was successful for both the instances ?
3. How will the SQL behave if the sub-select didnt have a join on another table, but may have a sub-select on only the table being updated (Would the result be the same as this or not) ?


Smile

Vallish
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group