简体   繁体   English

存储过程中的SQL事务处理

[英]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 首先, Commit transaction出现在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. 为了测试事务是否有效,您可以做的是,尝试在查询中的2条delete语句之间添加INSERT语句,并尝试为其中的identity列添加值。 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. COMMIT应该在ROLLBACK之前。 and i advice using try/catch blocks it should look like something like this 我建议使用try / catch块它应该看起来像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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