MySQL-存储过程代码笔记-in、out、inout、declare局部变量和if...else语句

mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> use test;
Database changed
mysql> create table testA
    -> (
    -> id int not null,
    -> num int not null
    -> );
Query OK, 0 rows affected (0.09 sec)

mysql> delimiter $$
mysql> insert into testA values (1,10),(2,20),(3,30)$$
Query OK, 3 rows affected (0.86 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> create procedure protest1()        //不带参数的函数过程
    -> begin
    -> select id, num from testA;
    -> end $$
Query OK, 0 rows affected (0.87 sec)

mysql> call protest1 $$
+----+-----+
| id | num |
+----+-----+
|  1 |  10 |
|  2 |  20 |
|  3 |  30 |
+----+-----+
3 rows in set (0.03 sec)

Query OK, 0 rows affected (0.05 sec)

mysql> create procedure protest2 (in cid int, in cnum int)       
                             //使用in 带参数的函数过程
    -> begin
    -> update testA set num = cnum where id = cid;
    -> end $$
Query OK, 0 rows affected (0.84 sec)

mysql> select * from testA $$
+----+-----+
| id | num |
+----+-----+
|  1 |  10 |
|  2 |  20 |
|  3 |  30 |
+----+-----+
3 rows in set (0.00 sec)

mysql> call protest2(3,100) $$
Query OK, 1 row affected (0.00 sec)

mysql> select * from testA $$
+----+-----+
| id | num |
+----+-----+
|  1 |  10 |
|  2 |  20 |
|  3 | 100 |
+----+-----+
3 rows in set (0.00 sec)

mysql> create procedure pro_test3(in cia int,out cnum int)   //带in、out
    -> begin
    -> select cnum from testA where id = cia;              //查询参数写错以至于查询结果出错
    -> end $$
Query OK, 0 rows affected (0.00 sec)

mysql> call pro_test3(2,@cnum) $$
+------+
| cnum |
+------+
| NULL |
+------+
1 row in set (0.03 sec)

Query OK, 0 rows affected (0.03 sec)

mysql> drop procedure pro_test3;
    -> $$
Query OK, 0 rows affected (0.00 sec)

mysql> create procedure pro_test3(in cid int,out cnum int)
    -> begin
    -> select num from testA where id = cid;
    -> end $$
Query OK, 0 rows affected (0.00 sec)

mysql> call pro_test3(2,@cnum) $$
+-----+
| num |
+-----+
|  20 |
+-----+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> delimiter $$
mysql> create procedure pro_test4(inout cid int)        //inout
    -> begin
    -> declare v_count int;                             //declare 局部变量
    -> if cid > 3 then                                  //使用if...else 语句
    -> set v_count = 100;
    -> else
    -> set v_count = 180;
    -> end if;
    -> set cid = v_count;
    -> end $$
Query OK, 0 rows affected (0.03 sec)

mysql> set @cid = 2;
    -> call pro_test4(@cid);
    -> select @cid $$
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+------+
| @cid |
+------+
|  180 |
+------+
1 row in set (0.00 sec)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值