sql-server – SQL Server ON DELETE触发器

前端之家收集整理的这篇文章主要介绍了sql-server – SQL Server ON DELETE触发器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个基本的数据库触发器,当数据库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
原文链接:https://www.f2er.com/mssql/83063.html

猜你在找的MsSQL相关文章