在回滚事务SQL之后

时间:2014-10-14 07:12:19

标签: sql-server-2014

大家好我有一个存储过程,我根据要求去了Transaction,这是我的SP

CREATE PROCEDURE ProcName
@Id INT,
@user_id INT
AS
 BEGIN
    BEGIN TRANSACTION [transName]
    BEGIN TRY

        DELETE 
        FROM    table1
        WHERE   UserId= @user_id 

        UPDATE  table2
        SET     DATE_MODIFIED = GETDATE()
        WHERE   ID= @Id

        COMMIT TRANSACTION [transName]

    END TRY
BEGIN CATCH
        ROLLBACK TRANSACTION [transName]
        THROW
END CATCH
END

Iam在THROW附近得到了错误语法等异常可以告诉我哪里出错了

1 个答案:

答案 0 :(得分:0)

得到了答案

CREATE PROCEDURE ProcName
@Id INT,
@user_id INT
AS
 BEGIN
    BEGIN TRANSACTION [transName]
    BEGIN TRY

        DELETE 
        FROM    table1
        WHERE   UserId= @user_id 

        UPDATE  table2
        SET     DATE_MODIFIED = GETDATE()
        WHERE   ID= @Id

        COMMIT TRANSACTION [transName]

    END TRY
BEGIN CATCH
         IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;

DECLARE @ErrorNumber INT = ERROR_NUMBER();
DECLARE @ErrorLine INT = ERROR_LINE();

SELECT  CAST(ERROR_NUMBER() AS NVARCHAR(1000));

THROW;
END CATCH
END