Click here to Skip to main content
15,896,063 members
Home / Discussions / Database
   

Database

 
GeneralRe: ADO book Pin
Rickard Andersson2025-Nov-02 21:32
Rickard Andersson2025-Nov-02 21:32 
GeneralRe: ADO book Pin
Christian Graus25-Nov-02 22:02
protectorChristian Graus25-Nov-02 22:02 
GeneralRe: ADO book Pin
Rickard Andersson2025-Nov-02 22:28
Rickard Andersson2025-Nov-02 22:28 
GeneralRe: ADO book Pin
Michael P Butler27-Nov-02 2:33
Michael P Butler27-Nov-02 2:33 
GeneralRe: ADO book Pin
Rickard Andersson2027-Nov-02 3:46
Rickard Andersson2027-Nov-02 3:46 
GeneralRe: ADO book Pin
Jon Sagara3-Dec-02 7:16
Jon Sagara3-Dec-02 7:16 
Generalrollback triggers Pin
dmanoharan24-Nov-02 22:24
dmanoharan24-Nov-02 22:24 
GeneralRe: rollback triggers Pin
Rob Graham25-Nov-02 17:39
Rob Graham25-Nov-02 17:39 
From SQL books Online:
Rollbacks in Stored Procedures and Triggers
If @@TRANCOUNT has a different value when a stored procedure finishes than it had when the procedure was executed, an informational error 266 is generated. This error is not generated by the same condition in triggers.

A 266 error is generated when a stored procedure is called with an @@TRANCOUNT of 1 or greater and the procedure executes a ROLLBACK TRANSACTION or ROLLBACK WORK statement. This is because ROLLBACK rolls back all outstanding transactions and decrements @@TRANCOUNT to 0, which is a lower value than it had when the procedure was called.

If a ROLLBACK TRANSACTION is issued in a trigger:

All data modifications made to that point in the current transaction are rolled back, including any that were made by the trigger.


The trigger continues executing any remaining statements after the ROLLBACK statement. If any of these statements modify data, the modifications are not rolled back. No nested triggers are fired by the execution of these remaining statements.


None of the statements in the batch after the statement that fired the trigger are executed.


A ROLLBACK in a trigger closes and deallocates all cursors that were declared and opened in the batch containing the statement that fired the trigger. This includes cursors declared and opened in stored procedures called by the batch that fired the trigger. Cursors declared in a batch prior to the batch that fired the trigger are only closed, except that STATIC or INSENSITIVE cursors are left open if:
CURSOR_CLOSE_ON_COMMIT is set OFF.


The static cursor is either synchronous, or a fully populated asynchronous cursor.
A trigger always operates as if there were an outstanding transaction in effect when the trigger is executed. This is definitely true if the statement firing the trigger is in an implicit or explicit transaction. It is also true in autocommit mode. When a statement begins executing in autocommit mode, there is an implied BEGIN TRANSACTION to allow the recovery of all modifications generated by the statement if it encounters an error. This implied transaction has no effect on the other statements in the batch because it is either committed or rolled back when the statement completes. This implied transaction is still in effect, however, when a trigger is called.

This means that any time a BEGIN TRANSACTION statement is issued in the trigger, it is actually beginning a nested transaction. Because a nested BEGIN TRANSACTION statement is ignored when rolling back nested transactions, ROLLBACK TRANSACTION issued in the trigger always rolls back past any BEGIN TRANSACTION statements issued by the trigger itself. ROLLBACK rolls back to the outermost BEGIN TRANSACTION.

You must use the SAVE TRANSACTION statement to do a partial rollback in a trigger, even if it is always called in autocommit mode. This is illustrated by the following trigger:

CREATE TRIGGER TestTrig ON TestTab FOR UPDATE AS
SAVE TRANSACTION MyName
INSERT INTO TestAudit
SELECT * FROM inserted
IF (@@error <> 0)
BEGIN
ROLLBACK TRANSACTION MyName
END

This also affects COMMIT TRANSACTION statements that follow a BEGIN TRANSACTION statement in a trigger. Because BEGIN TRANSACTION starts a nested transaction, a subsequent COMMIT statement applies only to the nested transaction. If a ROLLBACK TRANSACTION statement is executed after COMMIT, ROLLBACK rolls back everything to the outermost BEGIN TRANSACTION. This is illustrated by the following trigger:

CREATE TRIGGER TestTrig ON TestTab FOR UPDATE AS
BEGIN TRANSACTION
INSERT INTO TrigTarget
SELECT * FROM inserted
COMMIT TRANSACTION
ROLLBACK TRANSACTION

This trigger will never insert into the TrigTarget table. BEGIN TRANSACTION always starts a nested transaction. COMMIT TRANSACTION commits only the nested transaction, while the following ROLLBACK TRANSACTION rolls everything back to the outermost BEGIN TRANSACTION.



BTW the SQL Books Online can be downloaded from microsoft[^]Smile | :)

I may be somewhat used, but I'm not used up!
R

GeneralLicense Question Pin
Roger Wright24-Nov-02 20:11
professionalRoger Wright24-Nov-02 20:11 
GeneralRe: License Question Pin
SimonS24-Nov-02 20:25
SimonS24-Nov-02 20:25 
GeneralRe: License Question Pin
Roger Wright24-Nov-02 20:43
professionalRoger Wright24-Nov-02 20:43 
Questionhow can i repair and compact the access2000 database using ado in vc? Pin
whelk24-Nov-02 17:34
whelk24-Nov-02 17:34 
AnswerRe: how can i repair and compact the access2000 database using ado in vc? Pin
John Wong27-Nov-02 22:13
John Wong27-Nov-02 22:13 
GeneralRe: how can i repair and compact the access2000 database using ado in vc? Pin
whelk1-Dec-02 16:10
whelk1-Dec-02 16:10 
GeneralRe: how can i repair and compact the access2000 database using ado in vc? Pin
whelk2-Dec-02 14:04
whelk2-Dec-02 14:04 
GeneralRe: how can i repair and compact the access2000 database using ado in vc? Pin
John Wong2-Dec-02 21:34
John Wong2-Dec-02 21:34 
GeneralRe: how can i repair and compact the access2000 database using ado in vc? Pin
whelk2-Dec-02 22:38
whelk2-Dec-02 22:38 
GeneralRe: how can i repair and compact the access2000 database using ado in vc? Pin
John Wong3-Dec-02 22:09
John Wong3-Dec-02 22:09 
GeneralRe: Milli second resolution in DateTime Pin
Rob Graham23-Nov-02 6:12
Rob Graham23-Nov-02 6:12 
GeneralRe: Milli second resolution in DateTime Pin
Majid Shahabfar26-Nov-02 0:34
Majid Shahabfar26-Nov-02 0:34 
QuestionRecordCount Property return me -1? Pin
anju22-Nov-02 2:07
anju22-Nov-02 2:07 
AnswerRe: RecordCount Property return me -1? Pin
Nick Parker22-Nov-02 2:25
protectorNick Parker22-Nov-02 2:25 
GeneralRe: RecordCount Property return me -1? Pin
anju22-Nov-02 20:46
anju22-Nov-02 20:46 
GeneralTrying to close Access DB while app running... Pin
LukeV21-Nov-02 9:11
LukeV21-Nov-02 9:11 
GeneralRe: Trying to close Access DB while app running... Pin
Rob Graham21-Nov-02 10:14
Rob Graham21-Nov-02 10:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.