PS,1880后程序員

          看不完的牙,寫不完的程序,跑不完的步。
          隨筆 - 97, 文章 - 34, 評論 - 10, 引用 - 0
          數(shù)據(jù)加載中……

          MySQL 定義異常


          MySQL5.6,使用SIGNAL可以根據(jù)變量的值,拋出Exception

          測試數(shù)據(jù)

          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

           

          執(zhí)行

          call my_test(2);

          輸出:





          posted on 2013-10-24 10:24 amenglai 閱讀(196) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 南宁市| 扶绥县| 札达县| 万源市| 江阴市| 南岸区| 镶黄旗| 县级市| 忻州市| 响水县| 修水县| 怀集县| 余江县| 高唐县| 涟源市| 夏邑县| 萍乡市| 凌源市| 虎林市| 顺昌县| 敦化市| 吉隆县| 舞钢市| 通州区| 定结县| 南昌县| 贺兰县| 舞阳县| 安达市| 宣恩县| 中西区| 西丰县| 壤塘县| 民丰县| 乳山市| 孝感市| 北碚区| 洪雅县| 石嘴山市| 门源| 阜宁县|