declare @錯誤數量 int, @回傳訊息 nvarchar(4000), @位置 nvarchar(128)
set @錯誤數量=0
set @回傳訊息=N''
set @位置=N''
set nocount on
begin try
begin tran
if 1=0
begin
set @位置=N'區塊一'
throw 50001, N'自己定義的錯誤', 1
end
commit tran
end try
begin catch
if xact_state()<>0
begin
rollback tran
end
set @錯誤數量=@錯誤數量+1
set @回傳訊息=@回傳訊息+N'('+@位置+'):'+error_message()+'。'
end catch
set nocount off
select @錯誤數量 as 錯誤數量, N''+@回傳訊息 as 回傳訊息