在 SQL Server 中,您需要检查每个 SQL 语句的错误。例如,如果我在一笔交易中有 3 次更新,我需要一些代码,例如:
declare @HasError int
begin tran
Update tab1 set ....
Set @HasError = @@error;
Update tab2 set ...
Set @HasError = @@error;
Update tab3 set ...
Set @HasError = @@error;
If @HasError<>0
rollback tran;
else
commit tran;
对于这种情况,还有更简单的代码的其他解决方案吗?例如,类似 c# 风格的东西:
begin tran
try
{
Update tab1 set ....
Update tab2 set ...
Update tab3 set ...
commit tran;
}catch(error){
rollback tran;
}