开发者

Can i update mutiple rows with varying values on various fields in one statement? [duplicate]

This question already has answers here: 开发者_运维问答 Closed 12 years ago.

Possible Duplicate:

Is it possible to perform multiple updates with a single UPDATE SQL statement?

I'd like to achieve something similar to multiple INSERT rows but this time with an UPDATE statement.

Say using insert, its possible to:

 INSERT INTO table VALUES(
   ('value1','other_value1','row1'),
   ('value2','other_value2','row2'),
   ('value3','other_value3','row3'),
 );

How do i do the same with UPDATE?


I don't know exactly what you want. If you want better answers, then provide actual data, an explanation of what you want to do, and sample output for the resulting data after what you want is accomplished.

I'll take a wild guess that MERGE may do the trick for you. For example:

insert into T values 
  ('value1','other3','abcdef',144),
  ('value2','other4','abcdefg',244),
  ('value3','other3','abcdefgh',344),
  ('value4','other4','abcdefghi',444);

merge into T using (values 
  ('value1','other3',144),
  ('value3','other7',344),
  ('value4','other4',444)
) as V(v,o,i)
on T.keyVal = V.v
when matched then update 
  set other = o, num = i;
go

select * from T;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜