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> 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM