我是SQL新手,我想更新一个" big" db,包含来自临时的较小数据库的数据。
CREATE PROCEDURE sp_update_DimSomeone
AS
BEGIN
MERGE DimSomeone AS V
USING DimSomeone_temp AS T
ON V.AltKey = T.Altkey
WHEN NOT MATCHED BY TARGET AND T.AltKey <> '' THEN
INSERT VALUES (AltKey, Param1, Param2, Param3)
WHEN MATCHED AND T.Param1 <> V.Param1 THEN
UPDATE SET V.Param1 = T.Param1
WHEN MATCHED AND T.Param2 <> V.Param2 THEN
UPDATE SET V.Param2 = T.Param2
WHEN MATCHED AND T.Param3 <> V.Param3 THEN
UPDATE SET V.Param3 = T.Param3
END
我收到了错误:类型&#39;匹配时的行为&#39;在“更新”中不能出现多次MERGE声明的条款。
我理解这条消息,但我不想更新整行,只有属性,这是不一样的。 (例如,客户使用AltKey 001更改您的临时地址,我只想更新Param3(例如地址),而不是整行。)
感谢您的回答!
答案 0 :(得分:1)
为什么不总是更新param1,param2和param3?结果会一样!
CREATE PROCEDURE sp_update_DimSomeone
AS
BEGIN
MERGE DimSomeone AS V
USING DimSomeone_temp AS T
ON V.AltKey = T.Altkey
WHEN NOT MATCHED BY TARGET AND T.AltKey <> '' THEN
INSERT VALUES (AltKey, Param1, Param2, Param3)
WHEN MATCHED THEN
UPDATE SET V.Param1 = T.Param1,
V.Param2 = T.Param2,
V.Param3 = T.Param3
END
编辑: 您至少可以检查一下,如果这3列中的任何一列发生了变化,那么您将跳过完全相同的行:
CREATE PROCEDURE sp_update_DimSomeone
AS
BEGIN
MERGE DimSomeone AS V
USING DimSomeone_temp AS T
ON V.AltKey = T.Altkey
WHEN NOT MATCHED BY TARGET AND T.AltKey <> '' THEN
INSERT VALUES (AltKey, Param1, Param2, Param3)
WHEN MATCHED AND (T.Param1 <> V.Param1 OR T.Param2 <> V.Param2 OR T.Param3 <> V.Param3) THEN
UPDATE SET V.Param1 = T.Param1,
V.Param2 = T.Param2,
V.Param3 = T.Param3
END