Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. mismatching number of BEGIN and COMMIT statements (T_SQL) [modified]

mismatching number of BEGIN and COMMIT statements (T_SQL) [modified]

Scheduled Pinned Locked Moved Database
regexhelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Naunt
    wrote on last edited by
    #1

    Dear all, Please kindly advise this, my storeprocedure structure is as below. The purpose is, While Insert in TableA if there is any error occour Then I want to ROLLBACK TRAN of TableB that related with the current Transaction of TableA, if not COMMIT TRAN. When execute it I have got the error

    Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1159.

    DECLARE Cur_1 Cursor LOCAL FOR
    Select col1,col2,col3 from table1
    OPEN Cur_1
    FETCH NEXT FROM Cur_1
    INTO @col1,@col2,@col3

    WHILE @@FETCH\_STATUS = 0 
    BEGIN
    		
        DELETE FROM FROM TableA Where match with @col1,@col2,@col3
        DELETE FROM FROM TableB Where match with @col1,@col2,@col3
    
            DECLARE Cur\_2 LOCAL FOR		
         Select \* from table2 Where match with @col1,@col2,@col3
    	 OPEN Cur\_2
    	 FETCH NEXT FROM  Cur\_2
    
    	 WHILE @@FETCH\_STATUS = 0 
    	 BEGIN
    

    BEGIN TRAN
    INSERT INTO TableB (data from Cur_2 & Cur_1)

    	    FETCH NEXT FROM  Cur\_2
    	 END
    	 CLOSE Cur\_2
    	 DEALLOCATE Cur\_2					
    		
        INSERT INTO TableA (data fromCur\_1)
    		
        IF @@ERROR = 0
    	COMMIT TRAN
        ELSE
    	ROLLBACK TRAN
    
        FETCH NEXT FROM  Cur\_1
    	INTO @col1,@col2,@col3
    END
    
    CLOSE	Cur\_1
    DEALLOCATE Cur\_1
    

    modified on Friday, September 2, 2011 12:35 AM

    G L G 3 Replies Last reply
    0
    • N Naunt

      Dear all, Please kindly advise this, my storeprocedure structure is as below. The purpose is, While Insert in TableA if there is any error occour Then I want to ROLLBACK TRAN of TableB that related with the current Transaction of TableA, if not COMMIT TRAN. When execute it I have got the error

      Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1159.

      DECLARE Cur_1 Cursor LOCAL FOR
      Select col1,col2,col3 from table1
      OPEN Cur_1
      FETCH NEXT FROM Cur_1
      INTO @col1,@col2,@col3

      WHILE @@FETCH\_STATUS = 0 
      BEGIN
      		
          DELETE FROM FROM TableA Where match with @col1,@col2,@col3
          DELETE FROM FROM TableB Where match with @col1,@col2,@col3
      
              DECLARE Cur\_2 LOCAL FOR		
           Select \* from table2 Where match with @col1,@col2,@col3
      	 OPEN Cur\_2
      	 FETCH NEXT FROM  Cur\_2
      
      	 WHILE @@FETCH\_STATUS = 0 
      	 BEGIN
      

      BEGIN TRAN
      INSERT INTO TableB (data from Cur_2 & Cur_1)

      	    FETCH NEXT FROM  Cur\_2
      	 END
      	 CLOSE Cur\_2
      	 DEALLOCATE Cur\_2					
      		
          INSERT INTO TableA (data fromCur\_1)
      		
          IF @@ERROR = 0
      	COMMIT TRAN
          ELSE
      	ROLLBACK TRAN
      
          FETCH NEXT FROM  Cur\_1
      	INTO @col1,@col2,@col3
      END
      
      CLOSE	Cur\_1
      DEALLOCATE Cur\_1
      

      modified on Friday, September 2, 2011 12:35 AM

      G Offline
      G Offline
      Geoff Williams
      wrote on last edited by
      #2

      Your problem is that your BEGIN TRAN statement is inside a WHILE loop and hence it will be executed every time the loop code is executed. You need to move it outside the loop.

      1 Reply Last reply
      0
      • N Naunt

        Dear all, Please kindly advise this, my storeprocedure structure is as below. The purpose is, While Insert in TableA if there is any error occour Then I want to ROLLBACK TRAN of TableB that related with the current Transaction of TableA, if not COMMIT TRAN. When execute it I have got the error

        Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1159.

        DECLARE Cur_1 Cursor LOCAL FOR
        Select col1,col2,col3 from table1
        OPEN Cur_1
        FETCH NEXT FROM Cur_1
        INTO @col1,@col2,@col3

        WHILE @@FETCH\_STATUS = 0 
        BEGIN
        		
            DELETE FROM FROM TableA Where match with @col1,@col2,@col3
            DELETE FROM FROM TableB Where match with @col1,@col2,@col3
        
                DECLARE Cur\_2 LOCAL FOR		
             Select \* from table2 Where match with @col1,@col2,@col3
        	 OPEN Cur\_2
        	 FETCH NEXT FROM  Cur\_2
        
        	 WHILE @@FETCH\_STATUS = 0 
        	 BEGIN
        

        BEGIN TRAN
        INSERT INTO TableB (data from Cur_2 & Cur_1)

        	    FETCH NEXT FROM  Cur\_2
        	 END
        	 CLOSE Cur\_2
        	 DEALLOCATE Cur\_2					
        		
            INSERT INTO TableA (data fromCur\_1)
        		
            IF @@ERROR = 0
        	COMMIT TRAN
            ELSE
        	ROLLBACK TRAN
        
            FETCH NEXT FROM  Cur\_1
        	INTO @col1,@col2,@col3
        END
        
        CLOSE	Cur\_1
        DEALLOCATE Cur\_1
        

        modified on Friday, September 2, 2011 12:35 AM

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        To add to Geoff's answer, it is always a best programming practise to enclose transactions inside a Try Catch block, like this:

        BEGIN TRY

        BEGIN TRANSACTION

        -- DO YOUR STUFF HERE

        COMMIT TRANSACTION

        END TRY
        BEGIN CATCH
        IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
        DECLARE @ErrMsg VARCHAR(8000), @ErrSeverity INT
        SELECT @ErrMsg = ERROR_MESSAGE(), @ErrSeverity = ERROR_SEVERITY()
        RAISERROR(@ErrMsg,@ErrSeverity,1)
        END CATCH

        1 Reply Last reply
        0
        • N Naunt

          Dear all, Please kindly advise this, my storeprocedure structure is as below. The purpose is, While Insert in TableA if there is any error occour Then I want to ROLLBACK TRAN of TableB that related with the current Transaction of TableA, if not COMMIT TRAN. When execute it I have got the error

          Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1159.

          DECLARE Cur_1 Cursor LOCAL FOR
          Select col1,col2,col3 from table1
          OPEN Cur_1
          FETCH NEXT FROM Cur_1
          INTO @col1,@col2,@col3

          WHILE @@FETCH\_STATUS = 0 
          BEGIN
          		
              DELETE FROM FROM TableA Where match with @col1,@col2,@col3
              DELETE FROM FROM TableB Where match with @col1,@col2,@col3
          
                  DECLARE Cur\_2 LOCAL FOR		
               Select \* from table2 Where match with @col1,@col2,@col3
          	 OPEN Cur\_2
          	 FETCH NEXT FROM  Cur\_2
          
          	 WHILE @@FETCH\_STATUS = 0 
          	 BEGIN
          

          BEGIN TRAN
          INSERT INTO TableB (data from Cur_2 & Cur_1)

          	    FETCH NEXT FROM  Cur\_2
          	 END
          	 CLOSE Cur\_2
          	 DEALLOCATE Cur\_2					
          		
              INSERT INTO TableA (data fromCur\_1)
          		
              IF @@ERROR = 0
          	COMMIT TRAN
              ELSE
          	ROLLBACK TRAN
          
              FETCH NEXT FROM  Cur\_1
          	INTO @col1,@col2,@col3
          END
          
          CLOSE	Cur\_1
          DEALLOCATE Cur\_1
          

          modified on Friday, September 2, 2011 12:35 AM

          G Offline
          G Offline
          Ganu Sharma
          wrote on last edited by
          #4

          try something like this. start your transaction begin transaction a insert into tableA if(@@rowcount > 0) begin insert into tableB commit transaction a end else (@@error > 0) begin rollback transaction a end

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups