Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Help needed to write SQL query (Update)

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
prasun dhara

New User


Joined: 01 Mar 2008
Posts: 49
Location: kolkata

PostPosted: Wed Aug 17, 2011 5:18 pm    Post subject: Help needed to write SQL query (Update)
Reply with quote

Hi,

I have follwoing requirement.There are two table( table1 and table2)

table1
PK1 emp_num field1 field2 ...
11 12 10 a
10 13 13 b
12 12 44 c

table2
PK1 emp_num field2 ...
11 10
12 11
13 33

Now I need to write a query to update emp_num of table1 with the value of emp_num from table2 for all matching PK1 (table1.PK1 =table2.PK1)
So after execution of the query table1 should be like below

table1
PK1 emp_num field1 field2 ...
11 10 10 a
10 13 13 b
12 11 44 c

Thanks
Back to top
View user's profile Send private message

GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1278
Location: Belgium

PostPosted: Thu Aug 18, 2011 12:35 pm    Post subject:
Reply with quote

this stil is basic SQL:

Code:
update tableX X
   set (colX1,colX2) = (select y.ColX1,y.colX2 from tableY where x.PK = y.pk)
  where         exists (select y.ColX1,y.colX2 from tableY where x.PK = y.pk)


Just make sure the subselect returns maximum 1 Row.

the "where exists" is to make sure that for rows which aren't in tableY the update does not set colX1,colx2 to NULL


ps.: plz don't sollicit for answer thru PM's
Back to top
View user's profile Send private message
gylbharat

Active Member


Joined: 31 Jul 2009
Posts: 565
Location: Bangalore

PostPosted: Thu Aug 18, 2011 7:38 pm    Post subject:
Reply with quote

Hi GuyC,

Why you have selected both the columns (primary key and Empno) from tabley?

cant we re write as
Code:

update tableX X
   set colX2 = (select colX2 from tableY where x.PK = y.pk)
  where         exists (select y.colX2 from tableY where x.PK = y.pk)
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1278
Location: Belgium

PostPosted: Mon Aug 29, 2011 7:13 pm    Post subject:
Reply with quote

ooh, I thought I answered this, before my absence.

I do not select PK and empno.
I just gave an example for updating two columns from a subselect.
It can be simplified to 1 column like you did, but then whenever it becomes two or more columns , people start to write the strangest things like :
Bad example below (good example two posts up)
Code:
update tableX X
   set colX1 = (select colX1 from tableY where x.PK = y.pk)
      , colX2 = (select colX2 from tableY where x.PK = y.pk)
      , colX3 = (select colX3 from tableY where x.PK = y.pk)
  where         exists (select y.colX2 from tableY where x.PK = y.pk)
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
No new posts -913/-911 Deadlock during UPDATE stat... NoSleep319 DB2 5 Fri Nov 18, 2016 12:37 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us