zoukankan      html  css  js  c++  java
  • 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> 
  • 相关阅读:
    随笔:判断一个范围内有多少质数,分别是多少
    随笔:判断一个整数是否是质数,如果不是质数,那么因数表达式是什么
    随笔:Python发送SMTP邮件方法封装
    Python基础学习:打印九九乘法表
    随笔:docker学习笔记(包括了基础学习和制作运行jar包的docker镜像,还有centos7防火墙这个坑)
    随笔:测试心得
    随笔:docker安装
    Python基础:Python连接MySQL数据库方法封装2
    随笔:Python打印临时日志、清空临时日志
    radio点击一下选中,再点击恢复未选状态
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/12363308.html
Copyright © 2011-2022 走看看