gpt4 book ai didi

sql-server - SQL Server ON DELETE 触发器

转载 作者:行者123 更新时间:2023-12-02 00:53:32 25 4
gpt4 key购买 nike

我正在尝试创建一个基本数据库触发器,当从database2.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

关于sql-server - SQL Server ON DELETE 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9996643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com