declare @error_id begin tran ... your sql set @error_id = @@error if @error_id <> 0 goto errorhandle ... commit tran return 0errorhandle: rollback tran return @error_id--error id --错误信息可依据错误号从master..sysmessages 中去查
可是往往一个存储过程里有很多T-SQL语句,难道只有每句都这样进行检测@@error的返回值吗
declare @LastError int declare @LastRowcount int//其他的操作select @LastError=@@error,@LastRowcount=@@rowcount if @LastError<>0 or @LastRowcount<>1 begin return @LastError end return 0
newdongkui(老乌鸦 )的方法是最常见的!!!!!!!!!!!!!!!!!!!!11111
begin transaction update.... if @@error <>0 goto QuitWithRollbackinsert .... if @@error <>0 goto QuitWithRollback.... if @@error <>0 goto QuitWithRollbackcommit transaction GOTO EndSave QuitWithRollback: IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION EndSave: GO
begin tran
...
your sql
set @error_id = @@error
if @error_id <> 0
goto errorhandle
...
commit tran
return 0errorhandle:
rollback tran
return @error_id--error id
--错误信息可依据错误号从master..sysmessages 中去查
declare @LastRowcount int//其他的操作select @LastError=@@error,@LastRowcount=@@rowcount
if @LastError<>0 or @LastRowcount<>1
begin
return @LastError
end
return 0
if @@error <>0 goto QuitWithRollbackinsert ....
if @@error <>0 goto QuitWithRollback....
if @@error <>0 goto QuitWithRollbackcommit transaction
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO