我有这个MERGE语句正常工作:
MERGE INTO product_replenishment PR
USING ( SELECT * FROM traxs_temp..__MinMaxImport WHERE session_id = @session_id ) T
ON T._product = PR.product AND T._branch_no = PR.branch_no
WHEN MATCHED AND T._stock_warehouse IS NOT NULL THEN
UPDATE SET
date_time_updated = GETDATE(),
user_no_updated = @user_no,
stock_warehouse = T._stock_warehouse
WHEN NOT MATCHED BY TARGET AND T._stock_warehouse IS NOT NULL THEN
INSERT
(date_time_created,
date_time_updated,
user_no_created,
user_no_updated,
branch_no,
product,
stock_warehouse,
archive)
VALUES
(GETDATE(),
GETDATE(),
@user_no,
@user_no,
T._branch_no,
T._product,
T._stock_warehouse,
0);
我想添加另一个WHEN MATCHED
语句,如下所示:
WHEN MATCHED AND T._stock_warehouse IS NOT NULL THEN
UPDATE SET
date_time_updated = GETDATE(),
user_no_updated = @user_no,
stock_warehouse = T._stock_warehouse
WHEN MATCHED AND T._stock_warehouse IS NULL THEN
UPDATE SET
date_time_updated = GETDATE(),
user_no_updated = @user_no,
archive = 1
但我收到错误:An action of type 'WHEN MATCHED' cannot appear more than once in a 'UPDATE' clause of a MERGE statement.
难以实现我想做的事吗?
答案 0 :(得分:1)
一般情况下,可以使用this answer here或this blog post here中所示的CASE
表达式来模拟。但是您的情况更具体,因为这两个子句之间的区别比一般情况更简单。您可以将它们合并为一个子句:
WHEN MATCHED THEN UPDATE SET
-- These are always updated the same way, regardless of the WHEN MATCHED AND predicate
date_time_updated = GETDATE(),
user_no_updated = @user_no,
-- These depend on the relevant WHEN MATCHED AND predicate, so use CASE
stock_warehouse = CASE
WHEN T._stock_warehouse IS NOT NULL THEN T._stock_warehouse
ELSE stock_warehouse
END,
archive = CASE WHEN T._stock_warehouse IS NULL THEN 1 ELSE archive END