Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用多个集合参数更新表_Sql_Tsql - Fatal编程技术网

Sql 使用多个集合参数更新表

Sql 使用多个集合参数更新表,sql,tsql,Sql,Tsql,我得到了一张表的修改列表。我想知道如何在一个脚本中进行所有更改…我尝试了以下方法 UPDATE tableA SET col1 = 'somedata' WHERE col2 = 'somereference' SET col1 = 'someotherdata' WHERE col2 = 'someotherreference' SET col1 = 'evenmoredata' WHERE col2 = 'anotherreference' 但这不起作用。我是否可以使用特定的语法来实现这一

我得到了一张表的修改列表。我想知道如何在一个脚本中进行所有更改…我尝试了以下方法

UPDATE tableA
SET col1 = 'somedata' WHERE col2 = 'somereference'
SET col1 = 'someotherdata' WHERE col2 = 'someotherreference'
SET col1 = 'evenmoredata' WHERE col2 = 'anotherreference'
但这不起作用。我是否可以使用特定的语法来实现这一点,或者我是否坚持这样做:-

UPDATE tableA
SET col1 = 'somedata' WHERE col2 = 'somereference'

UPDATE tableA
SET col1 = 'someotherdata' WHERE col2 = 'someotherreference'

对于我要进行的每个更改?

在单集语句中使用case语句:

UPDATE tableA
SET col1 = case col2
    when 'somereference' then 'somedata'
    when 'someotherreference' then 'someotherdata'
    when 'anotherreference' then 'evenmoredata'
    else col1
    end

在使用mis触发的where子句中,最好将原始值的默认值放入其中(无论如何,您都应该使用where子句,否则您将更新所有行)

使用case语句。在之前的帖子中被问到并回答:我认为这不仅仅是一个好主意,也是必不可少的。如果您不将原始值放入
else
,它实际上将
NULL
列中与其他条件不匹配的行。@MichaelBerkowski:我更愿意假设使用where子句来限制更新的范围,但正如您所说,如果这与case语句不完全匹配,那么您将得到空更新。@MichaelBerkowski我宁愿在col1中添加一个
('somereference','someotherreference','anotherreference')
,以避免不必要的更新