当前位置: 代码迷 >> Sql Server >> 怎么在SQL server的存储过程中实现回滚
  详细解决方案

怎么在SQL server的存储过程中实现回滚

热度:70   发布时间:2016-04-27 16:49:06.0
如何在SQL server的存储过程中实现回滚?
我写了一个存储过程,用于向数据库中插入数据.
当插入出错时,我想让数据库回滚,该怎么实现了
谢谢

------解决方案--------------------
CREATE PROCEDURE p_test
AS
-- Execute the DELETE statement.
BEGIN TRAN
INSERT INTO ....
-- Test the error value.
IF @@ERROR <> 0
BEGIN
-- Return 99 to the calling program to indicate failure.
PRINT N 'An error occurred inserting information. ';
ROLLBACK TRAN;
END
ELSE
BEGIN
-- Return 0 to the calling program to indicate success.
PRINT N 'The job success. ';
COMMIT TRAN;
END;
GO
  相关解决方案