0

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

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

IE

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

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

提前致谢!

4

1 回答 1

1

你需要把它分成两个步骤。1. 仅在状态更新时做出反应, 2. 对插入的所有具有已支付状态的元素执行您想要的操作

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'

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

于 2013-03-26T09:52:38.353 回答