SQL触发器。如何为触发器提供查询"变量"?

时间:2015-12-03 01:47:05

标签: azure

我需要一些帮助来理解我如何处理查询传递给触发器的参数。我在下面创建了一个适用于userrId 5的触发器。但是让我说我​​尝试在查询中删除userrId = 4的userr对象,然后启动Trigger,如何将该userrId放在触发器中。 / p>

我在Visual Studio中工作并使用Azure SQL DB。

GO

CREATE TRIGGER [dbo].[Trigger]
    ON [dbo].[userr]
    INSTEAD OF DELETE
    AS
    BEGIN
        DELETE FROM userHouses WHERE userrId = 5;
        DELETE FROM userr WHERE userrId = 5;
    END

1 个答案:

答案 0 :(得分:0)

为什么要发布其他问题?

试试这个

CREATE TRIGGER [dbo].[Trigger]
ON [dbo].[user]
AFTER DELETE
AS
DECLARE @user_id INT
SELECT @user_id = (SELECT userid FROM DELETED)
BEGIN
    DELETE FROM userHouse WHERE userid = @user_id;
END