mysql-get错误值

xj3cbfub  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(238)

我正在使用mysql 5.7.23,我正在尝试将错误记录到存储过程中。为了将其写入日志表,我在获取特定错误方面遇到了一些困难。我想这样做:

BEGIN
DECLARE v_error varchar(50);
DECLARE v_err_no varchar(50) ;
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
BEGIN
set v_err_no= mysql_errno ;

# set v_error= mysql_sqlstate();

call pcd_log ('ERROR', v_err_no ); #my custom procedure to log
end;

insert into temp values (1);#some stuff that might give errors

end

但这是行不通的,我怎样才能得到错误的值(数字或描述)并将其放入变量中呢?
谢谢

yvfmudvl

yvfmudvl1#

仔细阅读https://dev.mysql.com/doc/refman/5.6/en/get-diagnostics.html

drop procedure if exists p;

delimiter $$
create procedure p()
BEGIN
DECLARE v_error varchar(50);
DECLARE v_err_no varchar(50) ;
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
    BEGIN
    get diagnostics condition 1
     v_err_no = returned_sqlstate;

    #call pcd_log ('ERROR', v_err_no ); #my custom procedure to log

    select v_err_no;
end;

insert into t values ('zzz');#some stuff that might give errors

end $$

delimiter ;

call p();

+----------+
| v_err_no |
+----------+
| 22007    |
+----------+
1 row in set (0.00 sec)

相关问题