我有一个具有可空字段的临时表(在SQL SERVER 2008中) 我想将登台表中的记录插入或更新到主表 在此期间,我想要进行比较
Update main
set main.field1 = (
if(staging.field1 isnull)
then ----
else if(staging.field2 isnull)
then ----
else
then
)
如何在插入和更新语句中嵌入上述条件?
答案 0 :(得分:10)
(某种)等价物是使用CASE
表达式:
UPDATE main
SET main.field1 =
CASE
WHEN staging.field1 IS NULL
THEN --
WHEN staging.field2 IS NULL
THEN --
ELSE --
END;
答案 1 :(得分:0)
MERGE INTO main
USING staging
ON main.id = staging.id
WHEN MATCHED THEN
UPDATE
SET field1 = COALESCE( staging.field1 , main.field1 ),
SET field2 = COALESCE( staging.field2 , main.field2 ),
... ;