成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

MySQL5.5創(chuàng)建存儲過程和函數(shù)

執(zhí)行CREATE PROCEDURE和CREATE FUNCTION語句需要CREATE ROUTINE權(quán)限。

查看neo用戶現(xiàn)有權(quán)限
MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

授權(quán)
MySQL> grant create routine on fire.* to neo;
Query OK, 0 rows affected (0.12 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

創(chuàng)建存儲過程

MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

注意:在命令行縮進(jìn)時,不要用tab,要使用空格,否則會報下面的錯
DATE INNER MULTILINESTRING SET UNICODE warnings
DATEDIFF INNOBASE MULTILINESTRINGFROMTEXT SHA UNION
DATETIME INNODB MULTILINESTRINGFROMWKB SHA1 UNIQUE
DATE_ADD INOUT MULTIPOINT SHARE UNIQUE_USERS
-> Info;
->
Display all 903 possibilities? (y or n)

執(zhí)行存儲過程
MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

授權(quán)
mysql> grant execute on fire.* to neo;
Query OK, 0 rows affected (0.04 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

也可以直接在Navicat里面執(zhí)行
MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

編輯存儲過程
MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

調(diào)用存儲過程
MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

創(chuàng)建不含參數(shù)的存儲過程,和Oracle不同的是,存儲過程名字后面必須要有()

mysql> delimiter $$
mysql> create procedure proc_Subscribers_update()
    -> begin
    -> DECLARE v_count INT;
    -> select ifnull(max(a),0) into v_count from t2;
    -> while v_count < 2 do
    -> select concat('the maximum value is ',v_count);
    -> set v_count = v_count+1;
    -> end while;
    -> end$$
Query OK, 0 rows affected (0.06 sec)

創(chuàng)建包含傳入?yún)?shù)的存儲過程
delimiter $$
create procedure proc_Subscribers_update(IN v_fetch_cnt INT, IN v_sleep_secs INT)
begin
DECLARE v_count INT;
DECLARE v_times INT DEFAULT 1;
DECLARE v_max_value INT;
/*compute the times that the loop runs*/
select ceil(count(MSISDN))/v_fetch_cnt into v_count from tmp_Subscribers_01;
/*compute the maximum rows that have been already updated*/
WHILE v_times < v_count DO
select ifnull(max(id),0) into v_max_value from tmp_Subscribers_02;
if v_max_value < v_fetch_cnt * v_count then
SET v_times = 1 + floor(v_max_value/v_fetch_cnt);
update Subscribers s,tmp_Subscribers_01 t set s.LastAccessTimeStamp=1420066800
where s.MSISDN=t.MSISDN and t.id > v_max_value and t.id <= v_fetch_cnt * v_times;
/*record the processing rows*/
insert into tmp_Subscribers_02 select id, MSISDN, now() from tmp_Subscribers_01 where id = v_fetch_cnt * v_times;
select concat('The job',' has already updated ', v_fetch_cnt * v_times, ' rows..') as Info;
select sleep(v_sleep_secs);
end if;
commit;
END WHILE;
select concat('The job',' is ','finished!') as Info;
commit;
end$$

刪除存儲過程
MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

需要授予alter routine權(quán)限

mysql> grant alter routine on fire.* to neo;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

在mysql客戶端中創(chuàng)建調(diào)用存儲過程

MariaDB [test]> delimiter //
MariaDB [test]> create procedure simpleproc(out param1 int)
    -> begin
    -> select count(*) into param1 from t;
    -> end//
Query OK, 0 rows affected (0.12 sec)
MariaDB [test]> delimiter ;
MariaDB [test]> CALL simpleproc(@a);
Query OK, 1 row affected (0.08 sec)
MariaDB [test]> select @a;
+------+
| @a   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

在調(diào)用的時候,如果參數(shù)不帶@,會報下面的錯
mysql> call proc_test(a,b);
ERROR 1414 (42000): OUT or INOUT argument 1 for routine test.proc_test is not a variable or NEW pseudo-variable in BEFORE trigger

查看存儲過程的狀態(tài)
MariaDB [test]> show procedure status like 'simpleproc'\G
*************************** 1. row ***************************
                  Db: test
                Name: simpleproc
                Type: PROCEDURE
             Definer: root@localhost
            Modified: 2016-07-01 08:16:20
             Created: 2016-07-01 08:16:20
       Security_type: DEFINER
             Comment: 
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8_general_ci
1 row in set (0.00 sec)

通過information_schema的視圖查看存儲過程的相關(guān)信息

MariaDB [test]> select * from information_schema.routines where routine_name='simpleproc'\G
*************************** 1. row ***************************
           SPECIFIC_NAME: simpleproc
         ROUTINE_CATALOG: def
          ROUTINE_SCHEMA: test
            ROUTINE_NAME: simpleproc
            ROUTINE_TYPE: PROCEDURE
               DATA_TYPE: 
CHARACTER_MAXIMUM_LENGTH: NULL
  CHARACTER_OCTET_LENGTH: NULL
       NUMERIC_PRECISION: NULL
           NUMERIC_SCALE: NULL
      DATETIME_PRECISION: NULL
      CHARACTER_SET_NAME: NULL
          COLLATION_NAME: NULL
          DTD_IDENTIFIER: NULL
            ROUTINE_BODY: SQL
      ROUTINE_DEFINITION: begin
select count(*) into param1 from t;
end
           EXTERNAL_NAME: NULL
       EXTERNAL_LANGUAGE: NULL
         PARAMETER_STYLE: SQL
        IS_DETERMINISTIC: NO
         SQL_DATA_ACCESS: CONTAINS SQL
                SQL_PATH: NULL
           SECURITY_TYPE: DEFINER
                 CREATED: 2016-07-01 08:16:20
            LAST_ALTERED: 2016-07-01 08:16:20
                SQL_MODE: NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
         ROUTINE_COMMENT: 
                 DEFINER: root@localhost
    CHARACTER_SET_CLIENT: utf8
    COLLATION_CONNECTION: utf8_general_ci
      DATABASE_COLLATION: utf8_general_ci
1 row in set (0.00 sec)

查看存儲過程的定義

MariaDB [test]> show create procedure simpleproc\G
*************************** 1. row ***************************
           Procedure: simpleproc
            sql_mode: NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `simpleproc`(out param1 int)
begin
select count(*) into param1 from t;
end
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8_general_ci
1 row in set (0.00 sec)

創(chuàng)建函數(shù)

MySQL的傳入?yún)?shù)不能設(shè)置默認(rèn)值,否則會報錯
mysql> delimiter $$
mysql> CREATE FUNCTION format_selectQuery (THE_TABLE_NAME VARCHAR(75), THE_COLUMNS_NAME VARCHAR(75), THE_CONDITION VARCHAR(75) DEFAULT NULL) RETURNS VARCHAR(200) DETERMINISTIC
    -> BEGIN
    ->     /*SELECT concat(' WHERE ', THE_CONDITION) INTO @WHERE_CLAUSE;
   /*>     IF THE_CONDITION IS NULL THEN
   /*> SET @WHERE_CLAUSE = NULL;
   /*> END IF;
   /*>     RETURN concat('SELECT ', THE_COLUMNS_NAME, ' FROM ', THE_TABLE_NAME, @WHERE_CLAUSE);*/
    ->     RETURN 1;
    -> END $$
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT '') RETURNS VARCHAR(200) DETERMINISTIC
BEGIN
    
    RETURN 1;
END' at line 1
mysql> delimiter ;

需要注意的是,在MySQL里,創(chuàng)建函數(shù)中在函數(shù)聲明后面的返回關(guān)鍵字是RETURNS
MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

執(zhí)行函數(shù)
MySQL 5.5 創(chuàng)建存儲過程和函數(shù)


MySQL 5.5 創(chuàng)建存儲過程和函數(shù)

需要注意的是,函數(shù)或存儲過程里面的參數(shù)聲明順序,順序有誤,容易引起語法報錯
delimiter $$
CREATE FUNCTION is_ChangeDescColumnExist (THE_VERSION_LEVEL_TABLE_NAME VARCHAR(35)) RETURNS INT DETERMINISTIC
    BEGIN
/* 聲明變量*/
    DECLARE nbr INT;
/* 聲明異常*/
    DECLARE CONTINUE HANDLER FOR NOT FOUND set nbr = 1;
/* 給變量賦值 */
    SET nbr =0;       
        select count(*) into nbr from information_schema.columns where table_name = THE_VERSION_LEVEL_TABLE_NAME and
        (column_name ='C_CHANGE_DESCRIPTION');
        IF nbr = 1 THEN
            RETURN 1;
        ELSE        
            RETURN 0;
        END IF;
    END$$
delimiter ;

將上面的順序放置錯誤,則會引起報錯
mysql> CREATE FUNCTION is_ChangeDescColumnExist (THE_VERSION_LEVEL_TABLE_NAME VARCHAR(35)) RETURNS INT DETERMINISTIC
    ->     BEGIN
    ->     DECLARE nbr INT;
    ->     SET nbr =0;
    ->     DECLARE CONTINUE HANDLER FOR NOT FOUND set nbr = 10;
    ->           
    ->         select count(*) into nbr from information_schema.columns where table_name = THE_VERSION_LEVEL_TABLE_NAME and
    ->         (column_name ='C_CHANGE_DESCRIPTION');
    ->         IF nbr = 1 THEN
    ->             RETURN 1;
    ->         ELSE
ELSE    ELSEIF  
    ->         ELSE
    ->             RETURN 0;
    ->         END IF;
    ->     END$$
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE CONTINUE HANDLER FOR NOT FOUND set nbr = 10;
          
        select c' at line 5

創(chuàng)建兩個函數(shù),返回兩種變量,一種是DECLARE變量,一種是@變量
delimiter $$
    CREATE FUNCTION is_TableMigrated (THE_VERSION_LEVEL_TABLE_NAME VARCHAR(20)) RETURNS INTEGER DETERMINISTIC
    BEGIN
    DECLARE nbr integer;
    SET nbr =222;
RETURN nbr;
END$$
delimiter ;

delimiter $$
    CREATE FUNCTION is_TableMigrated (THE_VERSION_LEVEL_TABLE_NAME VARCHAR(20)) RETURNS INTEGER DETERMINISTIC
    BEGIN
    DECLARE nbr integer;
    SET @nbr2 =22222;
RETURN @nbr2;
END$$
delimiter ;

網(wǎng)站欄目:MySQL5.5創(chuàng)建存儲過程和函數(shù)
URL分享:http://jinyejixie.com/article44/gdphhe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站App開發(fā)、網(wǎng)站營銷、Google、網(wǎng)站策劃、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)
临漳县| 贵州省| 越西县| 达州市| 巴里| 汪清县| 清新县| 乌苏市| 外汇| 高平市| 昌黎县| 万年县| 洮南市| 宁陵县| 喀喇沁旗| 徐汇区| 南投市| 银川市| 南宁市| 温州市| 曲沃县| 桐柏县| 密云县| 儋州市| 巩留县| 东辽县| 信丰县| 尼勒克县| 聊城市| 乐至县| 玉树县| 游戏| 讷河市| 长丰县| 诸城市| 永顺县| 蓝山县| 云梦县| 云梦县| 金山区| 讷河市|