我之前从未在sql服务器中使用过触发器,而且我已经在网上查看但是没有找到我的问题的答案.基本上我正在尝试编写一个触发器,它将在表中更新记录后运行.然后,此触发器将根据第一个表中更新的记录更新另外两个表.
带有触发器的主表将使用如下查询更新一条记录:
UPDATE E.SM_T_RList SET IsActive = 0 WHERE Guid = @Guid
然后我想让触发器做这样的事情:
ALTER TRIGGER [E].[IsActiveUpdate] ON [E].[SM_T_RList] AFTER UPDATE AS BEGIN SET NOCOUNT ON; UPDATE E.SM_T_BInfo SET IsActive = 0 WHERE Guid = @Guid UPDATE E.SM_T_RMachines SET IsActive = 0 WHERE GUID = @GUID END
我希望更新的Guid正在被主表使用.但我无法弄清楚如何将我希望更新的@Guid更新到触发器中?请帮忙.
谢谢
解决方法
已发布的答案都存在同样的问题 – 只要基表上发生任何更新,它们就会将其他行标记为非活动状态
就像是:
ALTER TRIGGER [E].[IsActiveUpdate] ON [E].[SM_T_RList] AFTER UPDATE AS BEGIN SET NOCOUNT ON; UPDATE E.SM_T_BInfo SET IsActive = 0 WHERE Guid IN (SELECT Guid FROM INSERTED where IsActive=0) UPDATE E.SM_T_RMachines SET IsActive = 0 WHERE Guid IN (SELECT Guid FROM INSERTED where IsActive=0) END
会更合适