存储过程,in,out,inout的区别

存储过程作用:执行效率和SQL 代码封装

in,out,inout的简单使用:

in:参数in的使用(代表输入,意思说你的参数要传到存过过程的过程里面去)

create procedure p1(in n int)
begin
declare total int default 0; // 申明变量用 declare
declare num int default 0;
while num < n do
set num:=num+1;  // 对一个变量赋值用 set 
set total:=total+num;
end while;
select total;
end

call p1(10)  //调用存储过程用call

out:参数out的使用(代表往外输出)

create procedure p2(in n int,out total int)
begin
declare num int default 0;
set total:=0;
while num < n do
set num:=num+1;
set total:=total+num;
end while;
end

call p2(100,@sum) //这里的@sum就是我定义用来接收处处total的值
select @sum

intout:参数inout的使用(既能输入一个值又能传出来一个值)

create procedure p3(inout age int)
begin
set age:=age+10;
end

set @currentAge=8
call p3(@currentAge)
select @currentAge

第四种情况:不使用前面三种,直接定义存储过程

例子:

CALL studentS("张三","1001");

CALL outr("张三","1001");


CREATE DEFINER=`root`@`localhost` PROCEDURE `outr`(name varchar(40),id varchar(40))
BEGIN
    #Routine body goes here...
SELECT * FROM student WHERE name = name AND sid = id;
END


CREATE DEFINER=`root`@`localhost` PROCEDURE `studentS`(name varchar(40),id varchar(20))
BEGIN
    #Routine body goes here...
SELECT * FROM student WHERE sname = name and id = id ;
END
原文地址:https://www.cnblogs.com/xiaoniuniu886/p/10254087.html