Mysql存储过程和函数(六)
来源:互联网 发布:mp4转gif mac 编辑:程序博客网 时间:2024/06/03 18:27
一、创建存储过程和函数
“DELIMITER//” 语句作用是将Mysql的结束符设置为//,以end//结束存储,再使用delimiter 恢复默认结束符
创建存储过程
mysql>delimiter//
mysql> create procedure avgFruitPrice()
-> begin
-> select avg(f_price) as avgprice from fruits;
-> end;
mysql> create procedure Proc()
-> begin
-> select *from fruits;
-> end//
Query OK, 0 rows affected
mysql> delimiter;
创建存储函数
mysql> delimiter //
mysql> create function NameByZip()
-> returns char(30)
-> return (select s_name from suppliers where s_call=’13564’);
-> //
Query OK, 0 rows affected
mysql> delimiter;
二、调用存储过程和函数
存储过程是通过call语句调用的
mysql> delimiter //
mysql> create procedure CountProc1(IN sid int,OUT num int)
-> begin
-> select count(*) into num from fruits where s_id=sid;
-> end //
Query OK, 0 rows affected
mysql> delimiter;
mysql> call CountProc1(101,@num);
Query OK, 1 row affected
mysql> select @num;
+——+
| @num |
+——+
| 2 |
+——+
1 row in set
调用存储函数
存储函数与存储过程定义稍有不同,但可以实现相同的功能
mysql> delimiter //
mysql> create function CountProc2(sid int)
-> returns int
-> begin
-> return (select count(*) from fruits where s_id=sid);
-> end //
Query OK, 0 rows affected
mysql> delimiter;
mysql> select CountProc2(101);
+—————–+
| CountProc2(101) |
+—————–+
| 2 |
+—————–+
1 row in set
三、查看存储过程和函数
使用show status语句查看存储函数与过程的状态
mysql> show procedure status like ‘Count%’;
+——+————+———–+—————-+———————+———————+—————+———+———————-+———————-+——————–+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+——+————+———–+—————-+———————+———————+—————+———+———————-+———————-+——————–+
| test | CountProc1 | PROCEDURE | root@localhost | 2017-08-12 12:16:37 | 2017-08-12 12:16:37 | DEFINER | | utf8 | utf8_general_ci | latin1_swedish_ci |
+——+————+———–+—————-+———————+———————+—————+———+———————-+———————-+——————–+
1 row in set
使用show create语句查看存储过程和函数的定义
mysql> show create function test.CountProc2 ;
+————+——————————————————————————————————————————————-+————————————————————————————————————————————————+———————-+———————-+——————–+
| Function | sql_mode | Create Function | character_set_client | collation_connection | Database Collation |
+————+——————————————————————————————————————————————-+————————————————————————————————————————————————+———————-+———————-+——————–+
| CountProc2 | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=root
@localhost
FUNCTION CountProc2
(sid int) RETURNS int(11)
begin
return (select count(*) from fruits where s_id=sid);
end | utf8 | utf8_general_ci | latin1_swedish_ci |
+————+——————————————————————————————————————————————-+————————————————————————————————————————————————+———————-+———————-+——————–+
1 row in set
四、修改存储过程和函数
修改存储过程
alter procedure CountProc modifies sql data sql security invoker;
修改存储函数
alter function CountProc2 reads sql data comment ‘find name’;
五、删除存储过程和函数
mysql> drop procedure CountProc1;
Query OK, 0 rows affected
mysql> drop function CountProc2;
Query OK, 0 rows affected
- Mysql存储过程和函数(六)
- MySQL优化系列(六)--存储过程和存储函数
- MYSQL存储过程(六)
- mysql(六)存储过程
- MySQL基础笔记(六) 存储过程与函数
- MySQL基础笔记(六) 存储过程与函数
- (六) MySQL存储过程
- mysql存储过程和函数
- mysql存储过程和函数
- Mysql 存储过程和函数
- MySQL-存储过程和函数
- MySQL存储过程和函数
- MySQL存储过程和函数
- Mysql存储过程和函数
- mysql(存储过程和函数)
- MySQL:存储过程和函数
- MySQL 存储过程和函数
- mysql:存储过程和函数
- 2017/8/12
- 阿里路由框架--ARouter 源码解析之Compiler
- 【Linux】-- ufw 端口操作
- osg 添加纹理
- 内存对齐小解
- Mysql存储过程和函数(六)
- 背包九讲第一讲-简单的0/1背包问题有感1.1
- git合并远程分支冲突解决方式
- getWidth deprecated [获取屏幕宽度]
- Linux SElinux
- 解决.net程序com操作实例化提示"不支持的接口"
- 日期类
- Go 接口interface 的用法
- 简单的屏幕滚动切换效果