在存储过程中,如何获取执行的错误信息

hzc191025 2011-11-17 02:04:35
---------------------------------------------------------------------------------------------
消息 515,级别 16,状态 2,过程 xxx,第 77 行
不能将值 NULL 插入列 'Id',表 'xxx.xxx.xxx';列不允许有 Null 值。INSERT 失败。
---------------------------------------------------------------------------------------------

手动执行存储过程时会出现以上的错误,但是我如何在运行期间获取这样的错误日志,保存到临时表里呢?


谢谢答复
...全文
336 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-11-17
  • 打赏
  • 举报
回复
---raiserror的用法
begin try
raiserror('生成一个错误消息',11,1)
end try
begin catch
select error_message() as 错误消息,
error_severity() as严重级别,
error_state() as state;
end catch
唐诗三百首 2011-11-17
  • 打赏
  • 举报
回复

-- 建测试表
create table xxx(i int, Id int not null)

-- 建存储错误信息的临时表
create table errlog(dtime datetime,errnum int,errmsg varchar(500))

begin try
insert into xxx select 1,null
end try
begin catch
insert into errlog(dtime,errnum,errmsg)
select getdate(),
error_number() as error_number ,
error_message() as error_message
end catch

-- 结果
select * from errlog

dtime errnum errmsg
----------------------- ----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2011-11-17 14:28:08.327 515 Cannot insert the value NULL into column 'Id', table 'DBAP.dbo.xxx'; column does not allow nulls. INSERT fails.

(1 row(s) affected)
hzc191025 2011-11-17
  • 打赏
  • 举报
回复
@error只是数值,但是并不详细,不能指出错误详细内容
-晴天 2011-11-17
  • 打赏
  • 举报
回复
调用后用@@error 函数获得错误消息.

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧