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: 1280
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: 1280
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 JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts IMS DB-How to update a record (a sing... Nic Clouston IMS DB/DC 9 Thu Mar 09, 2017 4:38 pm
No new posts Write R1 and R2 to OP1 and R1 to OP2 bhaskar_kanteti DFSORT/ICETOOL 10 Fri Mar 03, 2017 11:18 am
No new posts Can you write 2 recs on SORTOUT when ... Sysaron DFSORT/ICETOOL 6 Thu Feb 23, 2017 12:07 am
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm


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