如何在MySQL 5.5中的存储过程中获取异常消息

前端之家收集整理的这篇文章主要介绍了如何在MySQL 5.5中的存储过程中获取异常消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用MySQL 5.5.要在MySQL 5.6上获取异常消息,请使用GET DIAGNOSTIC函数. MysqL 5.5中是否有类似的功能?我正在使用的项目已经使用MysqL 5.5版.

最佳答案
您可以尝试使用SHOW ERROR和SHOW WARNING.要查看上一个错误或警告,您可以将其用作:

SHOW ERRORS LIMIT 1   -- for sql-state > 2
SHOW WARNINGS LIMIT 1 -- for sql-state 1,2

为了防止列出每个错误,您可以处理一类sql错误,如下所示:

sqlWARNING is shorthand for the class of sqlSTATE values that begin
with ’01’.

NOT FOUND is shorthand for the class of sqlSTATE values that begin
with ’02’. This is relevant only within the context of cursors and is
used to control what happens when a cursor reaches the end of a data
set. If no more rows are available,a No Data condition occurs with
sqlSTATE value 02000. To detect this condition,you can set up a
handler for it (or for a NOT FOUND condition). An example is shown in
Section 12.7.5,“Cursors”. This condition also occurs for SELECT …
INTO var_list statements that retrieve no rows.

sqlEXCEPTION is shorthand for the class of sqlSTATE values that do not
begin with ’00’,’01’,or ’02’.

因此,要处理异常,您只需执行以下操作:

DECLARE EXIT HANDLER FOR sqlSTATE sqlEXCEPTION .....;

链接

http://dev.mysql.com/doc/refman/5.5/en/signal.html

http://dev.mysql.com/doc/refman/5.0/en/declare-handler.html

原文链接:https://www.f2er.com/mysql/433096.html

猜你在找的MySQL相关文章