SQLServe触发器如何删除?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (51)

我正在尝试创建一个基本数据库触发器,该触发器可以有条件地删除数据库1中的行。表1时从数据库2中的一行。表2被删除。我对触发器还不熟悉。

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
提问于
用户回答回答于

最好使用:

DELETE tbl FROM tbl INNER JOIN deleted ON tbl.key=deleted.key
用户回答回答于
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

扫码关注云+社区

领取腾讯云代金券