如何在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