how to maintain transactions for multiple insert statements in sql server 2005

  • how to maintain transactions for multiple insert statements in sql server 2005

    ex

    Create Procedure MyProc

    @Id int,

    @Name varchar(50),

    @Fname Varchar(50),

    @Deptno int

    as

    Begin

    Insert into t1 values(@Id,@Name)

    Insert into t2 values(@Id,@FName)

    Insert into t3 values(@Id,@Deptno)

    End

  • Hello

    CREATE PROCEDURE MyProc

    @Id int,

    @Name varchar(50),

    @Fname Varchar(50),

    @Deptno int

    as

    BEGIN TRY

    BEGIN TRANSACTION

    INSERT INTO t1 values(@Id,@Name)

    INSERT INTO t2 values(@Id,@FName)

    INSERT INTO t3 values(@Id,@Deptno)

    COMMIT TRANSACTION

    END TRY

    BEGIN CATCH

    IF (XACT_STATE() = -1)

    ROLLBACK TRANSACTION

    IF (XACT_STATE() = 1)

    COMMIT TRANSACTION

    END CATCH

    Greets

    Flo

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply