如何获取错误消息并将其保存在sybase中的表中?

时间:2014-04-23 12:49:30

标签: sybase sybase-ase

drop procedure getTitle

create procedure getTitle @title_id VARCHAR(9)
as

declare @error  int,
@title varchar(100)

begin

insert into tab1 (CODE) VALUES (1) jhghjghj --- the jhghjghj  places to cause an error

SELECT @title = description from master..sysmessages where error = @@error
update ex_employee set info = @title
commit
print "%1!", @title
end
go

如果我尝试编译此程序,它将给我这个错误

Incorrect syntax near 'jhghjghj'.

我想要的是将此消息Incorrect syntax near 'jhghjghj'.保存到表格中。 我试图在某种程度上抓住它。那可能吗 ?

1 个答案:

答案 0 :(得分:2)

这是解决方案。这有点棘手。使查询动态化,以便Syabse在编译时不会抱怨语法错误。你的编译会很好,你会在运行时得到你想要的结果。

create procedure getTitle @title_id VARCHAR(9)
as

declare @error  int,
@title varchar(100)

begin

declare @my_query varchar(500)
select @my_query = "insert into tab1 (CODE) VALUES (1) jhghjghj"  
exec (@my_query )

SELECT @title = description from master..sysmessages where error = @@error
update ex_employee set info = @title
commit
print "%1!", @title
end
go