这是我的存储过程,我认为每件事都是正确的
ALTER PROC USP_D_Delete
(
@ID INT=NULL,
@RETURN INT OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRAN
BEGIN
DELETE FROM PE WHERE ID =@ID
END
COMMIT
SET @RETURN = 1
RETURN @RETURN
END TRY
BEGIN CATCH
ROLLBACK
SET @RETURN = 0
RETURN @RETURN
END CATCH
END
当我尝试从UI执行此操作时,我将获取ERROR
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements.Previous count = 0, current count = 1
任何帮助请,我找不到解决方案因为我觉得这个语法是正确的,在ROLLBACK之后尝试了COMMIT而且,它没有用..
答案 0 :(得分:1)
也许你必须在TRAN
和commit
之后添加一个单词rollback
,如下所示:
ALTER PROC USP_D_Delete
(
@ID INT=NULL,
@RETURN INT OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRAN
BEGIN
DELETE FROM PE WHERE ID =@ID
END
COMMIT TRAN
SET @RETURN = 1
RETURN @RETURN
END TRY
BEGIN CATCH
ROLLBACK TRAN
SET @RETURN = 0
RETURN @RETURN
END CATCH
END
答案 1 :(得分:0)
也许我错过了一些东西,但恕我直言,你的SP代码没有任何意义,因为它是:
DELETE
语句是原子的,不需要显式事务DELETE
,它将默默执行,并且不会删除任何内容。唯一可能出错并导致异常的是缺少PE
表。但在这种情况下,你没有任何东西可以回滚。话虽如此,整个SP可以归结为唯一的DELETE
声明