dbTalk Databases Forums  

help with an update statement using with and "(values"

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss help with an update statement using with and "(values" in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
gimme_this_gimme_that@yahoo.com
 
Posts: n/a

Default help with an update statement using with and "(values" - 01-06-2012 , 02:19 PM






I'd like to update six rows of table "scores" at once using a "with"
and a "(values" construct as shown below.

Actually I'm open to any construct that does the job.

This statement returns a "SQL0104N An unexpected token "a" was found
following scores" error message.

Can it be tweaked to work?

Thanks.

with b(p_id,r_id,z_id,score_no)
as(values (201101954,3,1,0.20),
(201101954,3,4,0.20),
(201101954,3,5,0.10),
(201101954,3,6,0.05),
(201101954,3,7,0.07),
(201101954,3,8,0.09))
update scores a,b
set a.score_no=b.score_no
where
a.p_id=b.p_id
and a.r_id=b.r_id
and a.z_id=b.z_id

Reply With Quote
  #2  
Old   
Tonkuma
 
Posts: n/a

Default Re: help with an update statement using with and "(values" - 01-07-2012 , 01:13 AM






Your update statement violated syntax of UPDATE statenet in DB2.

Please try...

UPDATE scores a
SET score_no
= (SELECT b.score_no
FROM (VALUES (201101954 , 3 , 1 , 0.20)
, (201101954 , 3 , 4 , 0.20)
, (201101954 , 3 , 5 , 0.10)
, (201101954 , 3 , 6 , 0.05)
, (201101954 , 3 , 7 , 0.07)
, (201101954 , 3 , 8 , 0.09)
) b(p_id , r_id , z_id , score_no)
WHERE a.p_id = b.p_id
AND a.r_id = b.r_id
AND a.z_id = b.z_id
)
;

Reply With Quote
  #3  
Old   
Tonkuma
 
Posts: n/a

Default Re: help with an update statement using with and "(values" - 01-07-2012 , 07:45 PM



score_no would be set to null in my last example, if the row in a
doesn't match with b.

To keep values of score_no at the time the row doesn't match with b.
UPDATE scores a
SET score_no
= COALESCE(
(SELECT b.score_no
FROM (VALUES (201101954 , 3 , 1 , 0.20)
, (201101954 , 3 , 4 , 0.20)
, (201101954 , 3 , 5 , 0.10)
, (201101954 , 3 , 6 , 0.05)
, (201101954 , 3 , 7 , 0.07)
, (201101954 , 3 , 8 , 0.09)
) b(p_id , r_id , z_id , score_no)
WHERE a.p_id = b.p_id
AND a.r_id = b.r_id
AND a.z_id = b.z_id
)
, a.score_no
)
;

Or, it would be better to use a MERGE statement.

MERGE scores a
USING (VALUES (201101954 , 3 , 1 , 0.20)
, (201101954 , 3 , 4 , 0.20)
, (201101954 , 3 , 5 , 0.10)
, (201101954 , 3 , 6 , 0.05)
, (201101954 , 3 , 7 , 0.07)
, (201101954 , 3 , 8 , 0.09)
) b(p_id , r_id , z_id , score_no)
ON b.p_id = a.p_id
AND b.r_id = a.r_id
AND b.z_id = a.z_id
WHEN MATCHED THEN
UPDATE
SET score_no = b.score_no
;

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.