怎样得到SQL语句执行失败的异常信息
怎样得到SQL语句执行失败的错误信息?
sqlserver 2000 or 2005;
比如:我运行use tmptest;
提示: 消息 911,级别 16,状态 1,第 1 行
未能在 sysdatabases 中找到数据库 'tmptest' 所对应的条目。没有找到具有该名称的条目。请确保正确地输入了名称。
请问各位牛人,怎么获得查询分析器中的错误信息?可以获得到得话,我就可以在前台提示给用户了。
不吝赐教啊,大家。
注:1.在网上也看到过使用@@ERROR,能获得错误id;
2.然后master数据库中的sysmessages表中查找错误id对应的错误信息,但是,比如3013错误代码在表中对应的信息是%hs is terminating abnormally. 显然%hs占位符不是我想要的,替换了以后才是我想要的信息。
------解决方案--------------------
2005用try..catch..
------解决方案--------------------
sqlserver 2000 or 2005;
比如:我运行use tmptest;
提示: 消息 911,级别 16,状态 1,第 1 行
未能在 sysdatabases 中找到数据库 'tmptest' 所对应的条目。没有找到具有该名称的条目。请确保正确地输入了名称。
请问各位牛人,怎么获得查询分析器中的错误信息?可以获得到得话,我就可以在前台提示给用户了。
不吝赐教啊,大家。
注:1.在网上也看到过使用@@ERROR,能获得错误id;
2.然后master数据库中的sysmessages表中查找错误id对应的错误信息,但是,比如3013错误代码在表中对应的信息是%hs is terminating abnormally. 显然%hs占位符不是我想要的,替换了以后才是我想要的信息。
------解决方案--------------------
2005用try..catch..
------解决方案--------------------
- SQL code
try catch : begin try select 1/0 end try begin catch select * from sys.messages where Message_id=@@error and language_id=2052 end catch
------解决方案--------------------
- SQL code
-- 定义要执行的SQL declare @sql varchar(5000) select @sql='use tmptest' -- 用 try.. catch.. begin try exec(@sql) 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 end catch -- 结果 error_number error_message error_state error_severity ------------ ---------------------------------------------- ----------- -------------- 911 Database 'tmptest' does not exist. Make sure that the name is entered correctly. 1 16 (1 row(s) affected)