总体作为一个事务,整体提交或整体回滚
BEGIN tran Price
DECLARE @tran_error int;
SET @tran_error=0;
BEGIN try
UPDATE t_PortGrant SET PortPrice ='200' WHERE StageID=73 AND StaffID='18440' and BasePID='1501'
set @tran_error=@tran_error+@@error;
update t_BaseProduct SET ImagePrice='200' where StageID=73 and BasePID='1544401'
set @tran_error=@tran_error+@@error;
end try
BEGIN CATCH
PRINT '出现异常,错误编号:' + convert(varchar,error_number()) + ',错误消息:' + error_message()
SET @tran_error = @tran_error + 1
END CATCH
IF(@tran_error > 0)
BEGIN
--执行出错,回滚事务
ROLLBACK TRAN;
PRINT '价格修改失败!';
END
ELSE
BEGIN
--没有异常,提交事务
COMMIT TRAN;
PRINT '价格修改成功!';
END
SET XACT_ABORT ON
BEGIN TRAN
--要执行的语句
UPDATE t_PortGrant SET PortPrice='200' WHERE StageID='73' AND StaffID='18440' AND BasePID=1501
UPDATE t_BaseProduct SET ImagePrice='200' WHERE StageID='73' AND BasePID='1234'
COMMIT TRAN
GO
其中某一条SQL语句执行出错时,则事物回滚,两天语句都不会执行操作