如何捕获RAISERROR抛出的错误码?

geodetic 2006-08-04 04:53:49
thx:)
...全文
328 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
geodetic 2006-08-07
  • 打赏
  • 举报
回复
谢谢各位。
hellowork 2006-08-04
  • 打赏
  • 举报
回复
使用INSTEAD OF DELETE触发器试试:
create trigger trg_delete_table on table
instead of delete
as
delete table from table as a inner join deleted as d on a.id = d.id where d.id <> 1
GO
xyxfly 2006-08-04
  • 打赏
  • 举报
回复
事务?
geodetic 2006-08-04
  • 打赏
  • 举报
回复
另外谢谢你对deleted的解释呵呵:)
看了一下,deleted存储了待删的所有记录的所有字段-_-,这样对性能的影响比原来想象的更厉害,对那个防止误删的问题老大有没有更好的办法hoho?
geodetic 2006-08-04
  • 打赏
  • 举报
回复
谢谢双星老大hoho

不过select @@error在我这边不行啊

使用你的那个触发器:
CREATE trigger tr_delete_Test2
on Test2
for delete
as
set nocount on
if exists (select 1 from deleted where id=0)
begin
RAISERROR ('不能删除id=0的记录', 16, 1)
ROLLBACK TRANSACTION
end

之后在存储过程中这么用:
create proc dbo.deltest2
as
delete from test2
if @@error<>0
delete from test2 where id>0


delete from test2 where id>0这句没有被执行。



昵称被占用了 2006-08-04
  • 打赏
  • 举报
回复
在sql server也可以用@@error捕获,这和RAISERROR怎么抛出错误有关

RAISERROR('Message',16,1)
@@error=50000

RAISERROR(msgid,16,1) --msgid是个数字,msgid〉50000
@@error=msgid

可以自己定义错误信息
exec sp_addmessage 50005,@msgtext ='你的信息'

RAISERROR(50005,16,1)
@@error=50005

select description from master..sysmessages
where error=@@error
可以查出信息


昵称被占用了 2006-08-04
  • 打赏
  • 举报
回复
在前端应用程序可以捕获的

与所用的数据库引擎有关

22,209

社区成员

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

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