mysql> show create procedure proc_6\G
*************************** 1. row ***************************
Procedure: proc_6
sql_mode: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
Create Procedure: CREATE DEFINER=`root`@`192.168.6.%` PROCEDURE `proc_6`()
LANGUAGE SQL
begin
DECLARE code CHAR(5) DEFAULT '00000';
DECLARE msg TEXT;
declare pout double;
DECLARE continue HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
--經過GET DIAGNOSTICS這樣的方式獲取sqlstate message_text(固然錯誤信息錯誤編號能夠本身定義)
--參考個人另一個示例
------------------------------
drop FUNCTION if exists test.fn_get_random_str;
create function fn_get_random_str(f_len int)
returns varchar(4000)
no sql
begin
declare v_str varchar(52);
declare v_cnt int;
declare f_result varchar(4000);
/*自定義異常編碼40001:value_to_large*/
declare value_to_large condition for sqlstate '40001';
if f_len>4000 then
begin
signal sqlstate '40001'
set message_text='value_to_large';/*輸出錯誤信息40001:value_to_large*/
end ;
end if ;
set v_str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
set v_cnt=1;
set f_result='';
while v_cnt<=f_len do
set f_result=concat(substr(v_str,round(rand()*52,0),1),f_result);
set v_cnt=v_cnt+1;
end while ;
return f_result ;
end ;
mysql> select fn_get_random_str(5000);
ERROR 1644 (40001): value_to_large
------------------------------
code = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
END;
/*set pout= 1/0;*/
select 1/0 into pout from test.ttt ;
--模擬了一個不存在的表錯誤
select concat(code,msg) ;
end
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (0.28 sec)
mysql> call test.proc_6();
+-------------------------------------+
| concat(code,msg) |
+-------------------------------------+
| 42S02Table 'test.ttt' doesn't exist |
--看到了吧錯誤信息打印出來了
+-------------------------------------+
1 row in set (0.99 sec)
Query OK, 0 rows affected (0.99 sec)
咋樣知足需求了吧!