如何在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

dawei

【声明】:淮南站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。