|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 2:05 AM
Points: 1,103,
Visits: 1,200
|
|
Comments posted to this topic are about the item Triggers and Transactions
See, understand, learn, try, use efficient © Dr.Plch
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 3:15 AM
Points: 1,476,
Visits: 1,943
|
|
Good question, didnt know it behaved that way.
Also good that Microsoft change the way that SQL handles this. In 2000 you wouldnt have a clue that your insert just got shafted. Atleast in 2005 and 2008 you get an error back.
However i still wonder in what circumstances that rolling back a transactions inside a trigger is a good thing. Isnt anything i have ever felt the need to use. But then i only use triggers for logging. So curious if anyone has an exempel on when this would be usefull?
/T
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 2:05 AM
Points: 1,103,
Visits: 1,200
|
|
tommyh (10/1/2010)
However i still wonder in what circumstances that rolling back a transactions inside a trigger is a good thing. Isnt anything i have ever felt the need to use. But then i only use triggers for logging. So curious if anyone has an exempel on when this would be usefull?
Rolling back a transaction inside a trigger is used to avoid inconsistent data. Eg.
select @s = somedata, @o = otherdata from inserted if @s < 0 and @o = 1 then rollback transaction
Or you can avoid change in some column
if update(pk) begin raiserror('Update of primary key is not allowed',11,1) rollback transaction end
See, understand, learn, try, use efficient © Dr.Plch
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:49 PM
Points: 5,244,
Visits: 7,063
|
|
tommyh (10/1/2010) Also good that Microsoft change the way that SQL handles this. In 2000 you wouldnt have a clue that your insert just got shafted. Atleast in 2005 and 2008 you get an error back. Exactly what change are you refering to? As far as I know, the effect of ROLLBACK in a trigger in current versions is the same as it was in SQL Server 2000 (and probably even versions before that).
However i still wonder in what circumstances that rolling back a transactions inside a trigger is a good thing. Isnt anything i have ever felt the need to use. But then i only use triggers for logging. So curious if anyone has an exempel on when this would be usefull? I develop a code generator that offers much more constraints that just the standard SQL Server constraints. Triggers are used to check modifications against these constraints. If violations are found, the trigger rolls back the transaction, logs the violation, and sends an error message to the client.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 3:15 AM
Points: 1,476,
Visits: 1,943
|
|
Hugo Kornelis (10/1/2010)
tommyh (10/1/2010) Also good that Microsoft change the way that SQL handles this. In 2000 you wouldnt have a clue that your insert just got shafted. Atleast in 2005 and 2008 you get an error back.Exactly what change are you refering to? As far as I know, the effect of ROLLBACK in a trigger in current versions is the same as it was in SQL Server 2000 (and probably even versions before that). However i still wonder in what circumstances that rolling back a transactions inside a trigger is a good thing. Isnt anything i have ever felt the need to use. But then i only use triggers for logging. So curious if anyone has an exempel on when this would be usefull? I develop a code generator that offers much more constraints that just the standard SQL Server constraints. Triggers are used to check modifications against these constraints. If violations are found, the trigger rolls back the transaction, logs the violation, and sends an error message to the client.
Running the code under 2000 all i get is
(1 row(s) affected) under 2005 and 2008 i get
Server: Msg 3609, Level 16, State 1, Line 1 The transaction ended in the trigger. The batch has been aborted.
(1 row(s) affected)
Server: Msg 3609, Level 16, State 1, Line 2 The transaction ended in the trigger. The batch has been aborted.
The effect is as you say the same the rollback performed.
/T
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:49 PM
Points: 5,244,
Visits: 7,063
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 2:05 AM
Points: 1,103,
Visits: 1,200
|
|
tommyh (10/1/2010)Running the code under 2000 all i get is (1 row(s) affected) under 2005 and 2008 i get Server: Msg 3609, Level 16, State 1, Line 1 The transaction ended in the trigger. The batch has been aborted.
(1 row(s) affected)
Server: Msg 3609, Level 16, State 1, Line 2 The transaction ended in the trigger. The batch has been aborted.
The effect is as you say the same the rollback performed. /T
Thanks for this observation, in time I wrote the question I missed this. You are right the messages are more clear.
See, understand, learn, try, use efficient © Dr.Plch
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 2:05 AM
Points: 1,103,
Visits: 1,200
|
|
Hugo Kornelis (10/1/2010) I develop a code generator that offers much more constraints that just the standard SQL Server constraints. Triggers are used to check modifications against these constraints. If violations are found, the trigger rolls back the transaction, logs the violation, and sends an error message to the client. Yes. You can have constraints in some other application tier too. But triggers are the last and safest barrier.
See, understand, learn, try, use efficient © Dr.Plch
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:53 AM
Points: 1,530,
Visits: 359
|
|
thanks for this nice example...
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Yesterday @ 12:56 AM
Points: 1,972,
Visits: 1,822
|
|
[b]tommyh. Running the code under 2000 all i get is (1 row(s) affected) under 2005 and 2008 i get Server: Msg 3609, Level 16, State 1, Line 1 The transaction ended in the trigger. The batch has been aborted.
(1 row(s) affected)
Server: Msg 3609, Level 16, State 1, Line 2 The transaction ended in the trigger. The batch has been aborted.
The effect is as you say the same the rollback performed. /T tommyh, in sql2000 I think you have this option set: SET IMPLICIT_TRANSACTIONS ON Set it off and then rerun the batch. Also in sql2000 an error message should appear because of COMMIT TRAN when there are not pending transaction.
|
|
|
|