简体   繁体   中英

Raising errors in After Triggers Sql Server 2005

If I raise an error in an AFTER UPDATE trigger in Sql Server 2005, will that cause the update which caused the trigger to be fired to roll back, even if the statement was not executed within a transaction?

Thanks.

No, you have to rollback transaction by calling ROLLBACK TRAN :

CREATE TRIGGER trg_au_table
ON  dbo.table
AFTER UPDATE
AS 
BEGIN
    ROLLBACK TRAN
END
GO

This example will prevent from updating any record.

This:

CREATE TRIGGER trg_au_table
ON  dbo.table
AFTER UPDATE
AS 
BEGIN
    RAISERROR('This is a test', 16, 1)
END
GO

will only raise the error but the change will be made in the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM