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 > Question on update in SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-21-07, 20:56
sundaram sundaram is offline
Registered User
 
Join Date: Mar 2006
Posts: 104
Question on update in SQL

Hi

DB2 8.2.6 on windows 2000

I have a table T1 with following columns
KEY
COL1
COL2
Would like to perform the following task using a single SQL statement:

when KEY=1 and COL2='A' update COL1='X'
when KEY=1 and COL2 != 'A' update COL1 = 'X' and COL2= 'A'


is it possible to have a single SQL statement doing the above?

Thanks in advance

Hari kumar
Reply With Quote
  #2 (permalink)  
Old 08-21-07, 23:03
nidm nidm is offline
Registered User
 
Join Date: May 2003
Posts: 113
does db2/luw have CASE-WHEN expression? that may work in your case.

Quote:
Originally Posted by sundaram
Hi

DB2 8.2.6 on windows 2000

I have a table T1 with following columns
KEY
COL1
COL2
Would like to perform the following task using a single SQL statement:

when KEY=1 and COL2='A' update COL1='X'
when KEY=1 and COL2 != 'A' update COL1 = 'X' and COL2= 'A'


is it possible to have a single SQL statement doing the above?

Thanks in advance

Hari kumar
Reply With Quote
  #3 (permalink)  
Old 08-22-07, 01:30
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
UPDATE T1
SET COL1 = 'X' , COL2= 'A'
WHERE KEY=1 ;

if COL2 is already 'A' , this will be recognized by DB2 and no change is done to that column so there is no need for extra code.


The other alternative is ( as nidm already mentioned ):

UPDATE T1
SET COL1 = 'X' ,
COL2 = CASE WHEN COL2='A' THEN COL2 ELSE 'A' END
WHERE KEY=1
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