Home >php教程 >php手册 >mysql 定义方法(function) 存储过程(procedure)

mysql 定义方法(function) 存储过程(procedure)

WBOY
WBOYOriginal
2016-06-06 20:01:391289browse

首先需要查看一下创建函数的功能是否开启: mysql show variables like '%func%'; +-----------------------------------------+-------+ | Variable_name | Value | +-----------------------------------------+-------+ | log_bin_trust_function_creator

首先需要查看一下创建函数的功能是否开启:
mysql> show variables like '%func%';
+-----------------------------------------+-------+
| Variable_name                            | Value |
+-----------------------------------------+-------+
| log_bin_trust_function_creators | ON    |
+-----------------------------------------+-------+
1 row in set (0.02 sec)
如果Value处值为OFF,则需将其开启。
mysql> set global log_bin_trust_function_creators=1;

 

创建函数时,先选择数据库,
mysql> use xxx;
Database changed
delimiter $$是设置 $$为命令终止符号,代替分号,因为分号在begin...end中会用到;
mysql> delimiter $$
CREATE FUNCTION first_func(param1 varchar(5),parmam2 varchar(5),param3 varchar(10))
RETURNS TINYINT
BEGIN
   RETURN 1;
END
函数创建成功后需恢复分号为命令终止符号。
mysql> delimiter ;
测试:
mysql> select first_func('aaa','bbb','ccc');
+-------------------------------+
| first_func('aaa','bbb','ccc') |
+-------------------------------+
|                             1 |
+-------------------------------+
1 row in set (0.47 sec)
删除函数:
mysql> drop function first_func ;
Query OK, 0 rows affected (0.11 sec)
查看函数
1) show function status
显示数据库中所有函数的基本信息 
2)查看某个具体函数
 mysql>show create function bobj.first_func; ......  

 

 

#存储过程

CREATE DEFINER=`root`@`%` PROCEDURE `chens`(in param1 varchar(50))
begin
 select * from admin;
end

 

#调用

call chens('chen');

 

 

 

 

 

 

 

 

 

 

 

begin
declare no varchar(20); 
declare title varchar(30); 
set no='101010',title='存储过程中定义变量与赋值'; 
end

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn