我正在尝试创建一个基本的数据库触发器,当数据库2.table2的行被删除时,有条件地从database1.table1中删除行.我是新来的触发器,希望学习最好的方式来完成这个.这是我到目前为止.建议?
CREATE TRIGGER sampleTrigger ON database1.dbo.table1 FOR DELETE AS IF EXISTS (SELECT foo FROM database2.dbo.table2 WHERE id = deleted.id AND bar = 4) -- If there is a row that exists in database2.dbo.table2 -- matching the id of the deleted row and bar=4,delete -- it as well. -- DELETE STATEMENT? GO
解决方法
CREATE TRIGGER sampleTrigger ON database1.dbo.table1 FOR DELETE AS DELETE FROM database2.dbo.table2 WHERE bar = 4 AND ID IN(SELECT deleted.id FROM deleted) GO