MySQL--带有out的存储过程
Posted dongyaotou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL--带有out的存储过程相关的知识,希望对你有一定的参考价值。
带有out 的存储过程,同样可以返回一个值,也可以返回多个值
下面分别进行介绍
案例一:根据女神名,返回对应的男神名
1 delimiter $ 2 create PROCEDURE myp7(in beautyName VARCHAR(20),out boyName VARCHAR(20)) 3 begin 4 select bo.boyName into boyName 5 from boys bo 6 INNER JOIN beauty b on bo.id=b.boyfriend_id 7 where b.name=beautyName; 8 end $ 9 #调用 10 #set @bName$ #其实这个定义用户变量过程是不用写的,直接按照下面调用的写法就行 11 call myp7(\'小昭\',@bName)$ 12 select @bName$
这里我要强调一点就是,在使用dos窗口执行存储过程的时候,我的电脑也不知道是怎么搞的,始终不能粘贴内容,有时候及时粘贴进去内容之后,回车执行的时候,总是出现mysql-> 就好像还让你输入下一行了。这里,我推荐大家使用navicat,在navicat中打开命令执行窗口,同样能够看到我们想要的效果,同时我们也不用再手动的设置字符集的编码格式了,方便多了。
下面是运行结果:
案例二:根据女神名,返回对应的男神名和魅力值
1 delimiter $ 2 create PROCEDURE myp8(in beautyName VARCHAR(20),out boyName VARCHAR(20),out userCP INT) 3 begin 4 select bo.boyName,bo.userCP into boyName,userCP 5 from boys bo 6 INNER JOIN beauty b on bo.id=b.boyfriend_id 7 where b.name=beautyName; 8 end $ 9 10 #调用 11 call myp7(\'小昭\',@bName)$ 12 select @bName,@userCP$
运行结果:
1 mysql> delimiter $ 2 create PROCEDURE myp8(in beautyName VARCHAR(20),out boyName VARCHAR(20),out userCP INT) 3 begin 4 select bo.boyName,bo.userCP into boyName,userCP 5 from boys bo 6 INNER JOIN beauty b on bo.id=b.boyfriend_id 7 where b.name=beautyName; 8 end $ 9 Query OK, 0 rows affected (0.00 sec) 10 mysql> call myp7(\'小昭\',@bName)$ 11 Query OK, 1 row affected (0.00 sec) 12 13 mysql> select @bName$,@userCP$ 14 +--------+ 15 | @bName | 16 +--------+ 17 | 张无忌 | 18 +--------+ 19 1 row in set (0.06 sec) 20 21 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'@userCP\' at line 1 22 mysql> call myp7(\'小昭\',@bName,@userCP)$ 23 1318 - Incorrect number of arguments for PROCEDURE girls.myp7; expected 2, got 3 24 mysql> call myp8(\'小昭\',@bName,@userCP)$ 25 Query OK, 1 row affected (0.00 sec) 26 mysql> select @bName$,@userCP$ 27 +--------+ 28 | @bName | 29 +--------+ 30 | 张无忌 | 31 +--------+ 32 1 row in set (0.05 sec) 33 34 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'@userCP\' at line 1 35 mysql> SELECT @userCP$ 36 +---------+ 37 | @userCP | 38 +---------+ 39 | 100 | 40 +---------+ 41 1 row in set (0.06 sec) 42 43 mysql> SELECT @bName,@userCP$ 44 +--------+---------+ 45 | @bName | @userCP | 46 +--------+---------+ 47 | 张无忌 | 100 | 48 +--------+---------+ 49 1 row in set (0.06 sec) 50 51 mysql>
以上是关于MySQL--带有out的存储过程的主要内容,如果未能解决你的问题,请参考以下文章