您的位置:首页 > 数据库 > MySQL

mysql高级 存储过程[2] ~之 传入的参数 && 控制结构

2014-04-14 16:39 549 查看

参数的传入:

-- 顺序,选择,循环-- 求1-100的和
delimiter $

create procedure p6()
begin
declare total int default 0;
declare num int default 0;

WHILE num <= 100 DO
-- 语句num加到total长度
set total := total + num;
set num := num + 1;
END WHILE;
select total;

end$

------------------------------------------
mysql> call p6()$
+-------+
| total |
+-------+
|  5050 |
+-------+

------------------------------------------
动态的传入:
-- 在p6的基础上 创造p7
create procedure p7(in n int)
begin
declare num int default 0;
declare total int default 0;

WHILE num <= n DO
set total := total+num;
set num := num+1;
END WHILE;
select total;
end$

' in '
'in是输入'

----------------------------------------
mysql> call p7(100)$
+-------+
| total |
+-------+
|  5050 |
+-------+
----------------------------------------

输出一个变量:null
----------------------------------------
select @total;

mysql> select @total$
+--------+
| @total |
+--------+
| NULL   |
+--------+
----------------------------------------
参数有三个不同的 in out inout:in:
-- 在p6的基础上 创造p7
create procedure p7(in n int)
begin
declare num int default 0;
declare total int default 0;

WHILE num <= n DO
set total := total+num;
set num := num+1;
END WHILE;
select total;
end$

' in '
'in是输入'

----------------------------------------
mysql> call p7(100)$
+-------+
| total |
+-------+
|  5050 |
+-------+
----------------------------------------
out:
----------------------------------------select @total;mysql> select @total$+--------+| @total |+--------+| NULL   |+--------+----------------------------------------create procedure p8(in n int , out total int)begindeclare num int default 0;WHILE num < n DOset num := num+1;set total := total+num;END WHILE;end$call p8(100,)$  -- 第二个参数传什么?call p8(100, @sumary)$  -- 发射给 @sumary 这个值--------------------------------------------------mysql> call p8(100, @sumary)$  -- 发射给 @sumary 这个值Query OK, 0 rows affected (0.00 sec)mysql> select @sumary$+---------+| @sumary |+---------+| NULL    |+---------+1 row in set (0.00 sec)--------------------------------------------------null * 3 等于 nullnull - 3 等于 nullnull != null 等于 nullnull 是大忌||||drop procedure p8$create procedure p8(in n int , out total int)begindeclare num int default 0;set total := 0;WHILE num < n DOset num := num+1;set total := total+num;END WHILE;end$-----------------------------------------------mysql> call p8(100,@sumary)$Query OK, 0 rows affected (0.00 sec)mysql> select @sumary$+---------+| @sumary |+---------+| 5050    |+---------+1 row in set (0.00 sec)-----------------------------------------------
inout: -- 既可以传入又可以传出
inout? -- 既可以传入又可以传出create procedure p9(inout age int)beginset age := age + 20;end$---------------------------------------------mysql> set @currage = 18$Query OK, 0 rows affected (0.00 sec)mysql> call p9(@currage)$Query OK, 0 rows affected (0.00 sec)mysql> select @currage$+----------+| @currage |+----------+| 38       |+----------+1 row in set (0.00 sec)---------------------------------------------

控制结构:

--switch case
-- switch casecreate procedure p10()begindeclare pos int default 0;set pos := floor(5*rand());case poswhen 1 then select ' still flying ';when 2 then select ' fall in sea ';when 3 then select ' in then island ';else select ' i dont know ';end case;end$call p10()$  # 每次不一样
-- repeat 循环
/*repeatsql statement;sql statement;until condition end repeat;*/create procedure p11()begindeclare total int default 0;declare i int default 0;repeatset i := i+1;set total := total+i;until i>=100 end repeat;select total;end$-----------------------------------------mysql> call p11$+-------+| total |+-------+|  5050 |+-------+1 row in set (0.00 sec)-----------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: