0

I would like to start a transaction through a sql procedure, run other 2 procedure, and then run the first procedure with command: 'commit'. Do you believe that this could be possible? I tried but received an error. Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.

Alexa Adrian
  • 1,778
  • 2
  • 23
  • 38
  • 1
    possible duplicate of [Nested stored procedures containing TRY CATCH ROLLBACK pattern?](http://stackoverflow.com/questions/2073737/nested-stored-procedures-containing-try-catch-rollback-pattern) – gbn May 08 '12 at 08:10

2 Answers2

0

This happens because SQL Server does not really support nested transactions.

If you commit or rollback in a nested stored proc (not transaction), then you'll generate error 266 because of a @@TRANCOUNT mismatch on start and entry.

You should pair up your BEGIN TRAN and COMMITs in the same SPROC.

If there is no concept of nested transaction then you need to do rollback/commit in same sproc. You can use SET XACT_ABORT ON suppresses error 266 caused by mismatched @@TRANCOUNT.

Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
  • Pairing them doesn't help if you ROLLBACK. See http://stackoverflow.com/a/2074139/27535 for a lot more – gbn May 08 '12 at 08:11
-1

not sure about nested transactions in sql server but you can try this one

Begin Try
 Begin Transaction1
  Call Proc1
   Call Proc2
   Call Proc3
 Commit
 End Transaction
Catch
 Rollback
Joy
  • 92
  • 1
  • 9