MySQL自定義函數

自定義函數的格式:html

CREATE  mysql

     [DEFINER = { user | CURRENT_USER }]  sql

     FUNCTION sp_name ([func_parameter[,...]])  服務器

     RETURNS type  函數

     [characteristic ...] routine_body  htm

func_parameter:  ip

     param_name type     ci

type:  文檔

     Any valid MySQL data type  get

characteristic:  

     LANGUAGE SQL  

   | [NOT] DETERMINISTIC  

   | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }  

   | SQL SECURITY { DEFINER | INVOKER }  

   | COMMENT 'string'  

routine_body:  

     Valid SQL procedure statement  

 

自定義函數與存儲過程的區別

1,函數方法的參數列表只容許IN類型的參數,而且不容許指定IN關鍵字

2,函數方法返回一個單一的值,值的類型在存儲方法的頭部定義

3,函數方法能夠在SQL語句內部調用

4,函數方法不能返回結果集

 

官方文檔:

http://dev.mysql.com/doc/refman/5.1/zh/stored-procedures.html#create-procedure

http://dev.mysql.com/doc/refman/5.1/zh/extending-mysql.html#adding-functions

 

建立自定義函數

        CREATE [AGGREGATE] FUNCTION function_name RETURNS {STRING|INTEGER|REAL}

        BEGIN

            //函數實現的語句

        END;

        aggregate 指定建立的函數是普通的自定義函數,仍是AGGREGATE函數。

        function_name 是用在SQL聲明中以備調用的函數名字。

        RETURNS 子句說明函數返回值的類型。 

        每次服務器啓動的時候會從新加載全部有效函數,除非使用--skip-grant-tables參數啓動mysqld。在這種狀況下, 將跳過UDF的初始化,UDF不可用。

       mysql文檔說明:一個AGGREGATE函數就像一個MySQL固有的集合(總和)函數同樣起做用,好比,SUM或COUNT()函數。要使得AGGREGATE 起做用,mysql.func表必須包括一個type列。若是mysql.func表沒有這一 列,則應該運行mysql_fix_privilege_tables腳原本建立此列。

 

建立自定義函數

mysql> delimiter //

mysql> DROP FUNCTION IF EXISTS fun_rand_key //

mysql> CREATE FUNCTION fun_rand_key(iparam int) RETURNs int

    -> BEGIN

    -> declare i_return int;

    -> set i_return =iparam + floor(rand()*100);

    -> return i_return;

    -> END;

    -> //

mysql> delimiter ;

 

使用自定義函數

mysql> select id from tb;

+----+

| id |

+----+

|  1 |

+----+

1 row in set (0.00 sec)

 

mysql> select fun_rand_key(id) from tb;

+------------------+

| fun_rand_key(id) |

+------------------+

|                9 |

+------------------+

1 row in set (0.00 sec)

 

查看自定義函數

        SHOW CREATE FUNTION function_name;

或   SHOW FUNCTION STATUS [ LIKE '' ];

如:mysql> show function status \G;

*************************** 1. row ***************************

                  Db: testdb

                Name: fun_rand_key

                Type: FUNCTION

             Definer: root@localhost

            Modified: 2013-08-21 14:46:53

             Created: 2013-08-21 14:46:53

       Security_type: DEFINER

             Comment:

character_set_client: gbk

collation_connection: gbk_chinese_ci

  Database Collation: gbk_chinese_ci

1 row in set (0.01 sec)

 

mysql> show function status like '%rand%' \G;

*************************** 1. row ***************************

                  Db: testdb

                Name: fun_rand_key

                Type: FUNCTION

             Definer: root@localhost

            Modified: 2013-08-21 14:46:53

             Created: 2013-08-21 14:46:53

       Security_type: DEFINER

             Comment:

character_set_client: gbk

collation_connection: gbk_chinese_ci

  Database Collation: gbk_chinese_ci

1 row in set (0.00 sec)

 

ERROR:

No query specified

 

mysql> show create function fun_rand_key \G;

*************************** 1. row ***************************

            Function: fun_rand_key

            sql_mode: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITU

TION

     Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION `fun_rand_key`(

iparam int) RETURNS int(11)

BEGIN

declare i_return int;

set i_return =iparam + floor(rand()*100);

return i_return;

END

character_set_client: gbk

collation_connection: gbk_chinese_ci

  Database Collation: gbk_chinese_ci

1 row in set (0.00 sec)

 

刪除自定義函數

        DROP FUNCTION [ IF EXISTS ] function_name;

相關文章
相關標籤/搜索