77 lines
953 B
Markdown
77 lines
953 B
Markdown
# 存储过程
|
||
|
||
|
||
|
||
## 一、创建存储过程(create procedure)
|
||
|
||
### 1.创建无参的存储过程
|
||
|
||
```mysql
|
||
delimiter //
|
||
create procedure proc()
|
||
begin
|
||
select from fruits;
|
||
end //
|
||
```
|
||
|
||
```mysql
|
||
修改数据库命令结束标志
|
||
delimiter //
|
||
end //
|
||
```
|
||
|
||
### 2.带输出参数的存储过程
|
||
|
||
```mysql
|
||
delimiter $$
|
||
create procedure proc1(out num int)
|
||
begin
|
||
select count(*)into num from fruits;
|
||
end $$
|
||
```
|
||
|
||
### 3.创建带输入输出参数的存储过程
|
||
|
||
```mysql
|
||
delimiter $$
|
||
create procedure proc2(in sid int,out n int)
|
||
begin
|
||
select count(*)into n from fruits where s id=sid;
|
||
end$$
|
||
```
|
||
|
||
|
||
|
||
## 二、调用存储过程(call)
|
||
|
||
### 1.调用无参的存储过程
|
||
|
||
```mysql
|
||
call proc();
|
||
```
|
||
|
||
### 2.带输出参数的存储过程
|
||
|
||
```mysql
|
||
call proc(@num);$$
|
||
|
||
select @num; $$
|
||
```
|
||
|
||
### 3.调用带输入输出参数的存储过程
|
||
|
||
```mysql
|
||
call proc2(101,@n);$$
|
||
|
||
select @n;$$
|
||
```
|
||
|
||
|
||
|
||
## 三、删除存储过程
|
||
|
||
```mysql
|
||
drop procedure 存储名
|
||
```
|
||
|