我有 3 个
UPDATE
语句(更新多个列),我想针对 id 列表执行这些语句,而不必针对每个 id
一次一个地运行 3 个更新语句。
这是我需要针对一堆
id
运行的 3 个更新语句:
-- store ContractDate value in temp col
update tval set temp_col = (select val from tval where id = 402280209 and fid = 3782) where id = 402280209 and fid = 3782
-- Replace ContractDate with maturityDate
update tval set val= (select val from tval where fid = 3771 and id = 402280209) where fid = 3782 and id = 402280209
-- set MaturityDate to ContactDate
update tval set val = (select temp_col from tval where id = 402280209 and fid = 3782) where id = 402280209 and fid = 3771
我有一个
id
列表,我需要对其运行上述 3 个更新语句。是否可以批量运行上述内容(即在一个查询中)?
仅供参考,我的
tval
表看起来像这样:
id fid ts val temp_col
402280209 3765 2021-09-20 00:00:00.000 2023-12-19 00:00:00.000
402280209 3771 2021-09-20 00:00:00.000 2023-09-20 00:00:00.000
402280209 3782 2021-09-20 00:00:00.000 2023-12-19 00:00:00.000
我试图避免的是为每个
id
手动运行上述内容。
你可以做
update yourtable
set temp_col = (select val from tval where id = 402280209 and fid = 3782),
val= (select val from tval where fid = 3771 and id = 402280209),
val = (select temp_col from tval where id = 402280209 and fid = 3782)
where fid = 3782 and id = 402280209
也就是说,使用
set
关键字,然后用逗号分隔您实际想要设置的内容。