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)-----------------------------------------
相关文章推荐
- MySQL删除表数据
- MySQL删除表数据
- MySQL Table Information
- mysql workbench建表时PK,NN,UQ,BIN,UN,ZF,AI
- PDO 与 Mysql Insert Ignore 的细节
- Mysql 语句学习
- mySql数据库的一些操作
- mysql5的字符集和sql mode问题
- PDO 与 MYSQL Replace 的细节
- mysql新增字段、注释、指定位置新增字段、删除字段。
- mysql处理百万级以上的数据时如何提高其查询速度的方法
- mysql学习笔记五----存储过程
- Mysql Cluster 学习
- mysqldump导入导出mysql数据库
- MySQL数据库恢复(使用mysqlbinlog命令)
- mysql中 delete 别名问题
- ERROR 1129 (00000): Host 'XXXXXX' is blocked because of many connection errors; unblock with 'mysqla
- 影响MySQL性能的五大配置参数
- 报错:1130-host ... is not allowed to connect to this MySql server
- mysql常见故障问题汇总