Mysql高级编程_存储过程类型(in/out/inout)

简单实例:

------>存储过程,参数的传入!
delimiter $
create procedure p1()
begin
    declare  i int default 10;
    select concat ('i的取值是:',i) as QuZhi;
end $

delimiter $
create procedure p2(width int, hegit int)
begin
    select concat('它的面积是:',width * hegit) as area;
    if width > hegit then
        select '比较瘦' as Xingzhuang ;
    elseif width < hegit then
        select '比较方' as Xingzhuang ;
    end if;
end $



------>这里求的是1+100之间的和,这里是固定的求和
delimiter $
create procedure p3()
begin
    declare total int default 0 ;
    declare num   int default 0 ;
    while num <= 100 do
        set total := total+num;
        set num := num +1;
    end while;
    select concat('total的大小是:',total) as total;
end $

--->需求:如果这里想要计算1+N ?
--->in型参数表示往存储过程中传输参数
delimiter $
create procedure p4(in n int)  
begin
    declare total int default 0 ;
    declare num   int default 0 ;
    while num <= n do
        set total := total+num;
        set num := num +1;
    end while;
    select concat('total的大小是:',total) as total;
end $

---看看out型参数?
create procedure p5(in n int,out total int)  
begin
    declare num   int default 0 ;
    set total := 0;
    while num <= n do
        set total := total+num;
        set num := num +1;
    end while;
    
end $

--看看inout型参数?
delimiter $
create procedure p6(inout age int)
begin
    set age := age + 10;
end $

--具体操作--
root@localhost 22:57:  [liulin]> set @current=18 $      --这里先要设置一个变量--
Query OK, 0 rows affected (0.00 sec)

root@localhost 22:58:  [liulin]> call p6(@current) $    --然后再将这个变量传入存储过程中inout类型中--
Query OK, 0 rows affected (0.00 sec)

root@localhost 22:58:  [liulin]> select @current $      --最后再查看该变量是否修改--
+----------+
| @current |
+----------+
|       28 |
+----------+
1 row in set (0.00 sec)

 

上一篇:MySQL数据库 *实验18错误处理


下一篇:基于Mysql的编程语言(实验六、七)