我有一个表格类型,我在存储过程中作为变量发送。
CREATE TYPE OperationKeysTableType AS TABLE
(
[KeysId] [int] NOT NULL,
[OperationId] [int] NOT NULL,
[IsChecked] [bit] NOT NULL
)
然后我想使用这个表进行搜索,如果它们匹配或不匹配,插入或删除但是只有在没有检查时才这样:
MERGE INTO dbo.tblOperationKeys AS T
USING (SELECT VT.KeysId, VT.OperationId as OperationId FROM @ValuesTable AS VT) AS S
ON T.KeysId = S.KeysId AND T.OperationId = S.OperationId
WHEN MATCHED AND S.IsChecked = 0 THEN --DELETE
DELETE
WHEN NOT MATCHED THEN--INSERT
INSERT (KeysId, OperationId) VALUES (S.KeysId, S.OperationId)
OUTPUT $action AS ChangesMade, inserted.OperationKeysId AS new_OperationKeysId, deleted.OperationKeysId AS old_OperationKeysId;
我的问题是SQL告诉我IsChecked无效。对我做错了什么的想法?
答案 0 :(得分:0)
您没有选择IsChecked。
在这一行:
USING (SELECT VT.KeysId, VT.OperationId as OperationId FROM @ValuesTable AS VT) AS S
你需要:
USING (SELECT VT.KeysId, VT.OperationId as OperationId, VT.IsChecked as IsChecked FROM @ValuesTable AS VT) AS S