22,300
社区成员




---raiserror的用法
begin try
raiserror('生成一个错误消息',11,1)
end try
begin catch
select error_message() as 错误消息,
error_severity() as严重级别,
error_state() as state;
end catch
-- 建测试表
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)