PS,1880后程序员

看不完的牙,写不完的程序,跑不完的步。
随笔 - 97, 文章 - 34, 评论 - 10, 引用 - 0
数据加载中……

MySQL 定义异常


MySQL5.6,使用SIGNAL可以根据变量的值,抛出Exception

测试数据

create table t1(f1 int);

insert into t1(f1) values(2);

insert into t1(f1) values(12);

insert into t1(f1) values(4);

insert into t1(f1) values(6);

insert into t1(f1) values(7);

 

存储过程定义

-- --------------------------------------------------------------------------------

-- Routine DDL

-- Note: comments before and after the routine body will not be stored by the server

-- --------------------------------------------------------------------------------

DELIMITER $$

 

CREATE PROCEDURE `my_test`

 (

    in_c_t int

 )

BEGIN

 DECLARE my_n_t int;

 DECLARE specialty CONDITION FOR SQLSTATE '45000';

 

SELECT count(1) into my_n_t

    FROM t1

    WHERE f1 = in_c_t;

 

IF my_n_t = 0 THEN

    SIGNAL SQLSTATE '45000'

      SET MESSAGE_TEXT = 'Can not delete';

end IF;

 

 delete from t1 WHERE f1 = in_c_t;

 COMMIT ;

 

END

 

执行

call my_test(2);

输出:





posted on 2013-10-24 10:24 amenglai 阅读(169) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: