2017-06-12 28 views
0

我有以下示例代碼,但是在處理異常時處理varchar和integer列時會看到不同的行爲。錯誤代碼1265沒有進入異常塊,而是出現了程序

我正在從表中提取姓名和薪金列值,但我在程序中聲明瞭 變量,該值小於表列的大小。

所以整列我收到以下錯誤

1264 | ERROR 1264(22003):超出範圍值的列「v_sal」

它進入我的程序編碼我的異常塊

但對於VARCHAR列,我收到以下錯誤

ERROR 1265(01000):數據被截斷列「v_ename」在行1

,但它不是進入我的異常塊,而拋出的錯誤 和出山的過程

爲什麼行爲所以我該如何處理這個應該進入我的異常塊而不是突然出現的varchar場景。

我使用的是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

回答

0

我認爲預期的誤差部件行爲。 給出

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) 

也許我有錯模型,但它看起來好像沒什麼問題。

+0

感謝您的答覆,使用相同的代碼只更改表名稱,但仍然獲得相同的名稱列行爲。 錯誤1265(01000):列'v_ename'的數據被截斷。 但是你得到錯誤代碼1406,但對我來說1265如何在相同的情況下代碼變化任何配置設置? – Keyan

相關問題