将可能会出错的sql 写在begin try...end try 之间,若出错,刚程序就跳到紧接着的begin try...end try 的beign catch...end catch
中,执行beign catch...end catch错误处理SQL。try..catch 是可以嵌套的。
在begin catch ...end catch中我们可以利用系统提供的下面四个函数得到出错信息:
error_number 返回错误代码
error_serverity 返回错误的严重级别
error_state 返回错误状态代码
error_message 返回完整的错误信息
上面的四个函数在同一个begin catch ...end catch可以在多次使用,值是不变的。
--语法
begin try
--SQL
end try
begin catch
--sql (处理出错动作)
end catch
begin try
--SQL
end try
begin catch
--sql (处理出错动作)
end catch
begin try
select 2/0
end try
begin catch
select error_number() as error_number ,
error_message() as error_message,
error_state() as error_state,
error_severity() as error_severity
print error_number()+ error_severity() +error_state()+error_procedure()+error_line()+error_message()
end catch
select 2/0
end try
begin catch
select error_number() as error_number ,
error_message() as error_message,
error_state() as error_state,
error_severity() as error_severity
print error_number()+ error_severity() +error_state()+error_procedure()+error_line()+error_message()
end catch