@@TRANCOUNT Query on INSERT
-
Wednesday, November 12, 2008 4:44 PMModerator
Can anyone explain the following?
Code Snippetcreate
table trancount(
dt datetime, trn int)select
getdate(), @@trancount -- 0 hereinsert
into trancount (dt, trn)select
getdate(), @@trancountselect
dt, trn -- 2 here!!!from
trancountdrop
table trancountI don't understand why the @@trancount is 2.
All Replies
-
Wednesday, November 12, 2008 5:01 PM
Hi,
The insert statement is actually made up of two transactions, a selection that is subsequently placed into another transaction that performs the INSERT part.
Code Snippetinsert into trancount (dt, trn)
select
getdate(), @@trancountYou can see that the above code block is responsible for two transactions by wrapping the section in an explicit transaction.
Code Snippetcreate
table trancount(
dt datetime, trn int)go
BEGIN
TRANSACTIONselect
getdate(), @@trancount -- 0 hereBEGIN
TRANSACTIONinsert
into trancount (dt, trn)select
getdate(), @@trancountCOMMIT
TRANSACTIONselect
dt, trn -- 2 here!!!from
trancountGO
COMMIT
transactiondrop
table trancountGO
-
Wednesday, November 12, 2008 5:03 PM
@@trancount is typically used with explicit transaction not implicit.
A.D.T.

