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 > DB2 Update Multiple Columns Syntax

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-20-11, 03:25
captainlaw captainlaw is offline
Registered User
 
Join Date: May 2011
Posts: 11
DB2 Update Multiple Columns Syntax

Hello,
I'm trying to update multiple columns with set. However, I'm not sure the correct syntax. Here's what I'm trying to do in SQL.

update prod.opm_exception
set partner=a.PARTNERNAME
, placement=a.PLACEMENTNAME
, partnerorg=a.PARTNERORG
, network=a.NETWORK
, updated=current date
from prod.mapping a
join prod.exception b on a.MktgCode=b.mktg_code

Can someone point me to the correct syntax in DB2?
Much appreciated!
Reply With Quote
  #2 (permalink)  
Old 05-20-11, 06:50
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Please see syntax of UPDATE statement, especially "assignment-clause".
"assignment-clause" includes row-fullselect.

Here is a partial copy from Information Center.
Code:
assignment-clause

   .-,----------------------------------------------------------------------------------------.   
   V                                                                                          |   
|----+-column-name--+----------------------+--=--+-expression-+-----------------------------+-+--|
     |              | .------------------. |     +-NULL-------+                             |     
     |              | V                  | |     '-DEFAULT----'                             |     
     |              '---..attribute-name-+-'                                                |     
     |    .-,-------------------------------------.             .-,------------------.      |     
     |    V                                       |             V                (1) |      |     
     '-(----column-name--+----------------------+-+--)--=--(--+---+-expression-+-----+-+--)-'     
                         | .------------------. |             |   +-NULL-------+       |          
                         | V                  | |             |   '-DEFAULT----'       |          
                         '---..attribute-name-+-'             |                (2)     |          
                                                              '-row-fullselect---------'
You can see more detail in here.
UPDATE - IBM DB2 9.7 for Linux, UNIX, and Windows
Reply With Quote
  #3 (permalink)  
Old 05-20-11, 10:15
captainlaw captainlaw is offline
Registered User
 
Join Date: May 2011
Posts: 11
The diagram is very hard to read and understand.
Can someone provide a simple DB2 syntax based on the query I provided? It is basically updating couple of columns when the two tables' join condition is met.

Your help is appreciated!
Reply With Quote
  #4 (permalink)  
Old 05-20-11, 10:26
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
How do the rows returned from your join equate to the table you are updating? Otherwise it would be more of an insert to get one row of data from your updated table with every row from the join condition.
Dave
Reply With Quote
  #5 (permalink)  
Old 05-20-11, 11:29
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Code:
UPDATE table1 t1 
 SET (col1, col2) = (
  SELECT col3, col4 
  FROM  table2 t2 
  WHERE t1.col8=t2.col9
 )
Reply With Quote
  #6 (permalink)  
Old 05-24-11, 06:08
himanshuadmin himanshuadmin is offline
Registered User
 
Join Date: Mar 2011
Posts: 3
hello,

if u want to update multiple columns in a single update statement then use

then we use syntax:

update tablename set(columnname1,columnname2,columnname3)=(value1,v alue2,value3) where condition

with this u can update multiple columns with single update statement
Reply With Quote
Reply

Tags
update syntax

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