我有以下示例代码,但在处理异常时,我看到了 varchar 和整数列的不同行为。
我正在从表中获取姓名和工资列值,但我已声明 过程中小于表列大小的变量。
因此对于整数列,我收到以下错误
1264 |错误 1264 (22003):“v_sal”列的值超出范围
它正在进入我在过程中编码的异常 block
但是对于 varchar 列,我收到以下错误
错误 1265 (01000):第 1 行“v_ename”列的数据被截断
但它没有进入我的异常 block 而是抛出错误 并退出程序
为什么会有这样的行为,我必须如何处理这个 varchar 场景,它应该进入我的异常 block 而不是突然出现。
我使用的是MySQL 5.7
Table structure
empid int(4)
ename varchar(10)
sal smallint(6)
CREATE PROCEDURE samp_proc(in p_empno int(4),
out p_sal smallint,
out p_error_code INT,
out p_errmsg VARCHAR(500)
)
BEGIN
declare v_ename varchar(3);
declare v_sal tinyint;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
begin
GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE,
@errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;
SET @full_error = CONCAT("ERROR ", @errno, " (", @sqlstate, "): ", @text);
set p_error_code = @errno;
set p_errmsg = @full_error ;
select p_error_code,p_errmsg;
rollback;
end;
select ename,sal into v_ename,v_sal from kk_chk where empid = p_empno;
/* since ename is first in the fetch the varchar behavior takes the precedence
if we make sal column as first in the fetch the integer behavior takes the precdednce */
end $$
delimiter ;
感谢和问候
Karthikeyan.R
最佳答案
我认为错误 block 的行为符合预期。 给定
MariaDB [sandbox]> select * from users;
+------+------+----------+-------+-----------+----------+---------+
| id | name | password | email | firstname | lastname | sal |
+------+------+----------+-------+-----------+----------+---------+
| 1 | aaa | NULL | NULL | aaa | aaa | 1000000 |
| 2 | bbbb | NULL | NULL | NULL | NULL | 1000000 |
+------+------+----------+-------+-----------+----------+---------+
2 rows in set (0.00 sec)
这个程序略有修改
drop procedure if exists samp_proc;
delimiter $$
CREATE PROCEDURE samp_proc(
in p_empno int(4)
#,out p_sal smallint,out p_error_code INT,out p_errmsg VARCHAR(500)
)
BEGIN
declare v_ename varchar(3);
declare v_sal tinyint;
declare v_id int;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
begin
GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE,
@errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;
SET @full_error = CONCAT("ERROR ", @errno, " (", @sqlstate, "): ", @text);
#set p_error_code = @errno;
#set p_errmsg = @full_error ;
#select p_error_code,p_errmsg;
select 'error block message - ' ,@errno,@full_error;
# rollback;
end;
select name,sal,id into v_ename,v_sal,v_id from users where id = p_empno;
/* since ename is first in the fetch the varchar behavior takes the precedence
if we make sal column as first in the fetch the integer behavior takes the precdednce */
end $$
delimiter ;
产生这些结果
MariaDB [sandbox]> call samp_proc(1);
+------------------------+--------+--------------------------------------------------------------------+
| error block message - | @errno | @full_error |
+------------------------+--------+--------------------------------------------------------------------+
| error block message - | 1264 | ERROR 1264 (22003): Out of range value for column 'v_sal' at row 1 |
+------------------------+--------+--------------------------------------------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> call samp_proc(2);
+------------------------+--------+-----------------------------------------------------------------+
| error block message - | @errno | @full_error |
+------------------------+--------+-----------------------------------------------------------------+
| error block message - | 1406 | ERROR 1406 (22001): Data too long for column 'v_ename' at row 2 |
+------------------------+--------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
也许我的模型有误,但对我来说看起来不错。
关于mysql - 错误代码 1265 没有进入异常 block 而是从程序中出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44492770/