Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can there be multiple commits to one transaction regardless of scope of while loop

Tags:

sql

sql-server

Following is a SQL Server stored procedure. There is on start of transaction and there are two commits as you can see below. Is this valid (start is in while loop and 1st commit is in same while loop, but 2nd commit is in 2nd while loop)? If not what could be the solution to do it?

Please help.

IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'DELETE_COBOC_DATA')
DROP PROCEDURE DELETE_COBOC_DATA
GO

CREATE PROCEDURE DELETE_COBOC_DATA @ORGDN VARCHAR(100), @CHUNK VARCHAR(10) 
AS
BEGIN
    -- some code that executes before while           
    WHILE (@NUM_ROWS_TMPTRADMIN > 0) 
    BEGIN               

        BEGIN TRANSACTION
            -- executes some code
        COMMIT TRANSACTION                              
    END
    -- some more code
    WHILE @NUM_ROWS_TMPDIR > 0
    BEGIN
        -- code code code
        COMMIT TRANSACTION
        -- code code code
    END
    -- some more code here as well
END

As I know this is allowed in MySQL

like image 408
Ramesh Avatar asked Dec 14 '12 09:12

Ramesh


1 Answers

You cannot commit the same transaction twice. You can have nested transactions, but you cannot have "partial" commits, that is contradictory to the notion of a transaction (it's all-or-nothing). If both commits are executed, the second one will throw an error

The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION
like image 124
SWeko Avatar answered Oct 31 '22 04:10

SWeko