Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

Can we maintain multiple transactions inside a procedure?

Asked by Julian Springer Jan 7, 2020 3.8K views 2 answers
Share

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on May 28, 2024

Yes, it is possible to maintain multiple transactions inside a stored procedure, but this largely depends on the database management system (DBMS) you are using, as different DBMSs have different levels of support for nested transactions and transaction management within stored procedures. Here’s how you can handle multiple transactions in some popular DBMSs:


SQL Server

SQL Server supports nested transactions. You can start a transaction within another transaction, but the commit or rollback behavior can be complex. Here’s an example:

CREATE PROCEDURE MultiTransactionProcedure
AS
BEGIN
    BEGIN TRANSACTION Tran1
    BEGIN TRY
        -- Your first set of operations
        INSERT INTO Table1 (Column1) VALUES ('Value1');
        -- Nested transaction
        BEGIN TRANSACTION Tran2
        BEGIN TRY
            -- Your second set of operations
            INSERT INTO Table2 (Column1) VALUES ('Value2');
            COMMIT TRANSACTION Tran2;
        END TRY
        BEGIN CATCH
            ROLLBACK TRANSACTION Tran2;
            THROW;
        END CATCH;
        COMMIT TRANSACTION Tran1;
    END TRY
    BEGIN CATCH
        ROLLBACK TRANSACTION Tran1;
        THROW;
    END CATCH;
END;

PostgreSQL

PostgreSQL supports savepoints, which allow you to create rollback points within a transaction. This is useful for handling partial rollbacks within a single transaction:

CREATE OR REPLACE FUNCTION multi_transaction_function()
RETURNS void AS $$
BEGIN
    BEGIN;
    -- First set of operations
    INSERT INTO table1 (column1) VALUES ('Value1');

    SAVEPOINT savepoint1;
    BEGIN
        -- Second set of operations
        INSERT INTO table2 (column1) VALUES ('Value2');
    EXCEPTION
        WHEN OTHERS THEN
            ROLLBACK TO savepoint1;
            -- Handle exception
    END;
    COMMIT;
END;
$$ LANGUAGE plpgsql;

MySQL

MySQL does not support nested transactions in the way SQL Server does, but you can use a similar approach with savepoints:

DELIMITER //
CREATE PROCEDURE MultiTransactionProcedure()
BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
        ROLLBACK      -- Handle the exception
    END;
    START TRANSACTION;
    -- First set of operations
    INSERT INTO Table1 (Column1) VALUES ('Value1');
    SAVEPOINT sp1;
    -- Second set of operations
    BEGIN
        -- If an error occurs here, we can rollback to savepoint sp1
        INSERT INTO Table2 (Column1) VALUES ('Value2');
    END;
    -- More operations
    SAVEPOINT sp2;
    -- Third set of operations
    BEGIN
        -- If an error occurs here, we can rollback to savepoint sp2
        INSERT INTO Table3 (Column1) VALUES ('Value3');
    END;
    COMMIT;
END //
DELIMITER ;Oracle

Oracle also supports savepoints within a single transaction, similar to PostgreSQL and MySQL:

CREATE OR REPLACE PROCEDURE MultiTransactionProcedure IS

BEGIN
    -- Start the main transaction
    SAVEPOINT sp_start;
    BEGIN
        -- First set of operations
        INSERT INTO Table1 (Column1) VALUES ('Value1');
        SAVEPOINT sp1;
        BEGIN
            -- Second set of operations
            INSERT INTO Table2 (Column1) VALUES ('Value2');
        EXCEPTION
            WHEN OTHERS THEN
                ROLLBACK TO sp1;
                -- Handle exception
        END;
        SAVEPOINT sp2;
        BEGIN
            -- Third set of operations
            INSERT INTO Table3 (Column1) VALUES ('Value3');
        EXCEPTION
            WHEN OTHERS THEN
                ROLLBACK TO sp2;
                -- Handle exception
        END;
    EXCEPTION
        WHEN OTHERS THEN
            ROLLBACK TO sp_start;
            -- Handle exception
    END;
    COMMIT;
END;

Key Points to Remember

Transaction Control: Always ensure proper transaction control by using COMMIT and ROLLBACK statements appropriately.

Error Handling: Use try-catch blocks or equivalent constructs to handle errors and perform rollbacks where necessary.

Savepoints: Utilize savepoints to create rollback points within a transaction, enabling finer control over transaction management.

By following these principles and understanding the specific capabilities of your DBMS, you can effectively manage multiple transactions within stored procedures.


Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.