Update using where exists

Ask question on - DataBase Concepts, IBM Mainframe DB2, DB2/UDB, DB2 Tools, SQL , DB2 programming with languages like COBOL, PL1.

Moderators: Kalicharan, Moderator Group

Post Reply
pbouzaid
Member
Posts: 5
Joined: Mon Jul 03, 2006 6:09 am

Update using where exists

Post by pbouzaid » Fri Feb 27, 2009 3:34 am

Howdy,

I am trying to update a table using the where exists and I'm not getting the expected updates.

My SQL is as follows:

UPDATE FF.TABLE1
SET COL6 = 'Y',
COL7 = 'Y'
WHERE EXISTS
(SELECT *
FROM FF.TABLE1 A, FF.TABLE2 B
WHERE (B.COL9 = 101030
OR B.COL9 = 101031)
AND B.COL2 = A.COL2);

I expect COL6 and COL7 in the TABLE1 to update only when the where exists returns a match. If I run the where exists select on its own I get 5 rows returned which is what I expect. If I run this whole SQL then every row in TABLE1 is updated, instead of the 5 I want updated.

Any thoughts on how to fix this?

Regards,

Pete.


Edit:

Someone suggested I change the SQL to the following:

UPDATE FF.TABLE1
SET COL6 = 'Y',
COL7 = 'Y'
WHERE EXISTS (SELECT A.COL6, A.COL7
FROM FF.TABLE1 A, FF.TABLE2 B
WHERE (B.COL9 = 101030
OR B.COL9 = 101031)
AND B.COL2 = A.COL2);

but still every row in the table is being updated. It almost seems the SQL execution is stopping at where exists and the select is not being executed.....

pbouzaid
Member
Posts: 5
Joined: Mon Jul 03, 2006 6:09 am

Post by pbouzaid » Fri Feb 27, 2009 9:39 am

never mind guys. i worked it out using a different method.

all good now.

User avatar
Natarajan
Moderator
Posts: 537
Joined: Fri Oct 10, 2008 12:57 pm
Location: chennai
Contact:

Post by Natarajan » Fri Feb 27, 2009 10:34 am

Hi Pete,

can you update us, what is that different method.
Natarajan
Chennai

pbouzaid
Member
Posts: 5
Joined: Mon Jul 03, 2006 6:09 am

Post by pbouzaid » Mon Mar 02, 2009 3:39 am

I did it this way:

UPDATE FF.TABLE1 A
SET COL6 = 'Y',
COL7 = 'Y'
WHERE A.COL2 IN
(SELECT B.COL2 FROM FF.TABLE2 B
WHERE (B.COL9 = 000001
OR B.COL9 = 000002));

Seems to work quite well.

User avatar
Natarajan
Moderator
Posts: 537
Joined: Fri Oct 10, 2008 12:57 pm
Location: chennai
Contact:

Post by Natarajan » Mon Mar 02, 2009 10:57 am

thanks.
Natarajan
Chennai

Post Reply

FREE TUTORIALS

Tutorials
Free tutorials from mainframegurukul
  • JCL Tutorial
    Covers all important JCL concepts.
  • Cobol Tutorial
    This tutorials covers all Cobol Topics from STRING to COMP-3.
  • DB2 Tutorial
    DB2 Tutorial focuses on DB2 COBOL Programming.
  • SORT Tutorial
    This Tutorial covers all important aspects of DFSORT with examples
  • CICS Tutorial
    This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions



Other References
Mainframe Tools and others