SQL Trigger for Insert and Update

我是研究僧i 提交于 2019-12-25 11:16:30

问题


Sql newbie in need of help.

I have a table with an SQL trigger that occurs on Insert. It looks like this:

ALTER TRIGGER Holiday_ITrig
ON HolidayDate
FOR INSERT
AS
IF NOT EXISTS (
                SELECT
                        ID
                    FROM
                        WorkingDay w
                    INNER JOIN
                        inserted i ON
                            w.ID = I.WorkingDayId
                    WHERE
                        WorkingDayTypeId = 2
                )
BEGIN
    ROLLBACK TRANSACTION
END

However, I also need the trigger to fire on update. Is this possible within the same trigger or do I have to create a new 'update' trigger?


回答1:


Just add UPDATE to the list of actions.

You will need to check that the same logic applies to an UPDATE as well as the original INSERT.

ALTER TRIGGER Holiday_ITrig
ON HolidayDate
FOR INSERT, UPDATE
AS
IF NOT EXISTS (
                SELECT
                        ID
                    FROM
                        WorkingDay w
                    INNER JOIN
                        inserted i ON
                            w.ID = I.WorkingDayId
                    WHERE
                        WorkingDayTypeId = 2
                )
BEGIN
    ROLLBACK TRANSACTION
END

CREATE TRIGGER MSDN article



来源:https://stackoverflow.com/questions/17143639/sql-trigger-for-insert-and-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!