我有以下字符串,但似乎不喜欢它:
"UPDATE
table
SET a = '', b = '34'
WHERE id = '1000001';
SET a = '1111', b = '11'
WHERE id = '100210';
SET a = '', b = '2'
WHERE id = '8002059';"
在我的asp经典代码中,字符串在一行上。这是执行多个update / where语句的正确方法吗?
答案 0 :(得分:0)
它会更好(3个陈述)。
UPDATE table SET a = '', b = '34' WHERE id = '1000001';
UPDATE table SET a = '1111', b = '11' WHERE id = '100210';
UPDATE table SET a = '', b = '2' WHERE id = '8002059';
或者,您可以在SET中使用两个CASE语句。如果这是一次性情况,你经常需要完成3次条件更新(不太可能),那么它将不会像以前那样可读,但运行速度稍快一点,而不是3次。
UPDATE table SET
a = CASE id WHEN '1000001' THEN '' WHEN '100210' THEN '1111' WHEN '8002059' THEN '' ELSE a END,
b = CASE id WHEN '1000001' THEN '34' WHEN '100210' THEN '11' WHEN '8002059' THEN '2' ELSE b END
WHERE id IN ('1000001', '100210', '8002059');
答案 1 :(得分:0)
不,它会给你一个错误。
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'table'.
你不能在UPDATE陈述中使用多个位置。
请改为尝试:
UPDATE
[table]
set a =
( case when id IN ( '1000001', '8002059') then ''
when id = '100210' then '1111'
end
),
b =
( case when id = '1000001' then '34'
when id = '100210' then '11'
when id = '8002059' then '2'
end
)
where id in ('1000001', '100210', '8002059');