在SQL SERVER2005中对于错误的处理,引入了try catch块.
例如:
代码
declare @ErrMsg varchar(1000)
begin try
select 5/0
end try
begin catch
set @ErrMsg = ERROR_MESSAGE()
raiserror(@ErrMsg, 14, 1)
print 'Error Msg: ' + error_message()
print 'Error Number: ' + convert(varchar(10), error_number())
end catch
执行结果如下:
(0 row(s) affected)
Msg 50000, Level 14, State 1, Line 7
Divide by zero error encountered.
Error Msg: Divide by zero error encountered.
Error Number: 8134