@ Adrian
- 存储过程就是具有名字的一段代码,用来完成一个特定的功能
- 创建的存储过程保存在数据库的数据宝典中
保存在MySQL上的一个别名 => 一坨SQL语句
别名()
用于替代程序员写SQL语句
方式一:
MySQL: 存储过程
程序:调用存储过程
方式二:
MySQL:。。
程序:SQL语句
方式三:
MySQL:。。
程序:类和对象(SQL语句)
- 简单
create procedure p1()
BEGIN
select * from student;
INSERT into teacher(tname) values("ct");
END
call p1()
cursor.callproc('p1')
- 传参数(in,out,inout)
delimiter //
create procedure p2(in n1 int,in n2 int)
BEGIN
select * from student where sid > n1;
END //
delimiter ;
call p2(12,2)
cursor.callproc('p2',(12,2))
- 参数 out
delimiter //
create procedure p3(in n1 int,inout n2 int)
BEGIN
set n2 = 123123;
select * from student where sid > n1;
END //
delimiter ;
set @v1 = 10;
call p2(12,@v1)
select @v1;
set @_p3_0 = 12
ser @_p3_1 = 2
call p3(@_p3_0,@_p3_1)
select @_p3_0,@_p3_1
- INOUT 输入输出参数:
既表示调用者向过程传入值,又表示过程向调用者传出值(值只能是变量)
mysql> delimiter //
mysql> create procedure inout_param(inout p_inout int)
-> begin
-> select p_inout;
-> set p_inout=2;
-> select p_inout;
-> end
-> //
mysql> delimiter ;
mysql> set @p_inout=1;
mysql> call inout_param(@p_inout);
+---------+
| p_inout |
+---------+
| 1 |
+---------+
+---------+
| p_inout |
+---------+
| 2 |
+---------+
mysql> select @p_inout;
+----------+
| @p_inout |
+----------+
| 2 |
+----------+
inout参数就尽量的少用。