[英]One Update statement with multiple cases and where clauses
我有一个包含以下各列的表
june_hours
july_hours
august_hours
...
april_hours
may_hours
我想在我的触发器上说,如果june_hours为null,则将其设置为零;如果sept_hours为null,则将其设置为零。 但是,我想在一个更新语句中执行此操作。
UPDATE a
SET july_hours = 0
From LT_VOLUNTEER_HOURS a
JOIN inserted b ON a.id_key = b.id_key
where a.july_hours is null
UPDATE a
SET sept_hours = 0
From LT_VOLUNTEER_HOURS a
JOIN inserted b ON a.id_key = b.id_key
where a.sept_hours is null
等等...如果可能的话,我想做什么
update table
set
case where june_hours is null = 0
case where sept_hours is null = 0
case where april_hours is null = 0
这可行吗? 我一直在读书,找不到能说是的话。
首先,您实际上应该将每个“小时”作为单独的行存储在表中。 这将使此更新更容易-可能还会进行许多其他查询。
无论如何,您都可以执行以下操作:
update table
set june_hours = coalesce(june_hours, 0),
sept_hours = coalesce(sept_hours, 0),
april_hours = coalesce(april_hours, 0)
where june_hours is null or sept_hours is null or april_hours is null;
编辑:
您的代码看起来像是在触发器中运行。 同样的想法成立:
update vh
set june_hours = coalesce(june_hours, 0),
sept_hours = coalesce(sept_hours, 0),
april_hours = coalesce(april_hours, 0)
from LT_VOLUNTEER_HOURS vh join
inserted i
on vh.id_key = i.id_key
where lv.june_hours is null or lv.sept_hours is null or lv.april_hours is null;
当然,您可以将其扩展到所有月份。 where
子句是可选的,但是它确实使查询的意图更加明显(并可以减少开销)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.