问题
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