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

2021/4/18 19:27:02

本文主要是介绍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高级编程_存储过程类型(in/out/inout)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程