【问题标题】:Can i update mutiple rows with varying values on various fields in one statement? [duplicate]我可以在一个语句的各个字段上更新具有不同值的多行吗? [复制]
【发布时间】:2011-05-07 01:17:50
【问题描述】:

可能重复:
Is it possible to perform multiple updates with a single UPDATE SQL statement?

我想实现类似于多个 INSERT 行的功能,但这次使用的是 UPDATE 语句。

说使用插入,它可以:

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

我如何对 UPDATE 做同样的事情?

【问题讨论】:

  • 那我可以删除问题吗?你上面给出的链接正是我想要做的

标签: sql sql-server


【解决方案1】:

我不知道你到底想要什么。如果您想要更好的答案,请提供实际数据,说明您想要做什么,并在完成您想要的操作后为结果数据提供示例输出。

我会大胆猜测 MERGE 可能会为您解决问题。例如:

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-28
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    相关资源
    最近更新 更多