简体   繁体   中英

SQL Transaction Handling in stored procedure

I have written a stored procedure to check the how transaction working in stored procedure.

Is this correct? How can I check this is correct or not?

What I want to do is if second table data not deleted ; both the table data should not be delete.

CREATE PROCEDURE DeleteDepartment
(
   @DepartmentID    int
)
AS
   BEGIN TRANSACTION

   DELETE FROM Employees
   WHERE DepartmentID = @DepartmentID

   IF @@ERROR <> 0
   BEGIN
        -- Rollback the transaction
        ROLLBACK

        -- Raise an error and return
        RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
        RETURN
   END

   DELETE FROM Departments
   WHERE DepartmentID = @DepartmentID

   IF @@ERROR <> 0
   BEGIN
       -- Rollback the transaction
       ROLLBACK

       -- Raise an error and return
       RAISERROR ('Error in deleting department in DeleteDepartment.', 16, 1)
       RETURN
   END

   COMMIT
CREATE PROCEDURE DeleteDepartment
(
   @DepartmentID    int
)
AS


BEGIN TRY 

BEGIN TRANSACTION

DELETE FROM Employees
WHERE DepartmentID = @DepartmentID

--Test Code Start
--For testing purpose Add an Insert statement with passing value in the identity column.
declare @table1 as table(ID Identity(1,1),Test varchar(10))

insert into @table1(ID, Test)
Values(1,'Failure Test')
--Test Code end

DELETE FROM Departments
WHERE DepartmentID = @DepartmentID


COMMIT TRANSACTION
END TRY

BEGIN CATCH
        ROLLBACK TRANSACTION
        RETURN ERROR_MESSAGE()
END CATCH

First things first, Commit transaction appears ahead of Rollback Transaction

And to test if the transactions work, what you can do is, try adding an INSERT statement in the query between 2 delete statements and try adding value for the identity column in it. So that the first delete is successful, but transaction fails. Now you can check if the first delete is reflected in the Table or not.

COMMIT is supposed to be before ROLLBACK. and i advice using try/catch blocks it should look like something like this

BEGIN TRY
    declare @errorNumber as int
BEGIN TRANSACTION 

     --do 1st statement
     IF @@ERROR<>0
     BEGIN
       SET @errorNumber=1
     END

     --do 2nd statement
     IF @@ERROR<>0
     BEGIN
       SET @errorNumber=2
     END

 COMMIT

END TRY

BEGIN CATCH

    IF @@TRANCOUNT > 0

ROLLBACK

END CATCH

Your stored procedure will work but I have added few bits

CREATE PROCEDURE DeleteDepartment
(  @DepartmentID    int
)
AS

BEGIN TRANSACTION
BEGIN TRY
   DELETE FROM Employees 
   WHERE DepartmentID = @DepartmentID

   DELETE FROM Departments 
   WHERE DepartmentID = @DepartmentID

   COMMIT   
END TRY
BEGIN CATCH
   ROLLBACK

    -- Raise an error and return
    RAISERROR (ERROR_MESSAGE(), 16, 1)
END CATCH

END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM