使用Inserted Table进行MSSQL触发器更新

时间:2013-03-26 09:46:12

标签: sql-server triggers

我正在尝试编写一个只有在更新列等于特定值时才会运行的触发器。

基本上,只有当我将列名“状态”更新为值“付费”时,我才希望触发器运行。

if (Update(Status) AND  inserted.Status = 'Paid')
begin
end

目前我收到此错误 “多部分标识符”inserted.Status“无法绑定。”

提前致谢!

1 个答案:

答案 0 :(得分:1)

您需要将其分为两个步骤。 1.仅在状态更新时作出反应,2。对插入状态为Paid

的所有元素执行所需操作
if update(status)
begin
    -- Do the stuff to the Paid elements in inserted
    select * from inserted as i where i.status = 'Paid'

end

请记住,您的状态也可以从付费更新为付费,您可能不想对此做出反应。所以你可以这样做:

select * from
inserted as i
inner join deleted as d on i.id = d.id
where i.status <> d.status and i.status = 'Paid'

现在结果只包含已更新为付费的行以及尚未等于付费的行

相关问题