mysql存储过程之返回多个值的方法示例

2022-05-15 0 179

本文实例讲述了mysql存储过程之返回多个值的方法。分享给大家供大家参考,具体如下:

mysql存储函数只返回一个值。要开发返回多个值的存储过程,需要使用带有INOUT或OUT参数的存储过程。咱们先来看一个orders表它的结构:

mysql> desc orders;
+----------------+-------------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| orderNumber  | int(11)   | NO  | PRI | NULL  |    |
| orderDate   | date    | NO  |   | NULL  |    |
| requiredDate  | date    | NO  |   | NULL  |    |
| shippedDate  | date    | YES |   | NULL  |    |
| status     | varchar(15) | NO  |   | NULL  |    |
| comments    | text    | YES |   | NULL  |    |
| customerNumber | int(11)   | NO  | MUL | NULL  |    |
+----------------+-------------+------+-----+---------+-------+
7 rows in set

然后嘞,咱们来看一个存储过程,它接受客户编号,并返回发货(shipped),取消(canceled),解决(resolved)和争议(disputed)的订单总数:

DELIMITER $$
CREATE PROCEDURE get_order_by_cust(
 IN cust_no INT,
 OUT shipped INT,
 OUT canceled INT,
 OUT resolved INT,
 OUT disputed INT)
BEGIN
 -- shipped
 SELECT
      count(*) INTO shipped
    FROM
      orders
    WHERE
      customerNumber = cust_no
        AND status = 'Shipped';
 -- canceled
 SELECT
      count(*) INTO canceled
    FROM
      orders
    WHERE
      customerNumber = cust_no
        AND status = 'Canceled';
 -- resolved
 SELECT
      count(*) INTO resolved
    FROM
      orders
    WHERE
      customerNumber = cust_no
        AND status = 'Resolved';
 -- disputed
 SELECT
      count(*) INTO disputed
    FROM
      orders
    WHERE
      customerNumber = cust_no
        AND status = 'Disputed';
END

其实,除IN参数之外,存储过程还需要4个额外的OUT参数:shipped, canceled, resolved 和 disputed。 在存储过程中,使用带有count函数的select语句根据订单状态获取相应的订单总数,并将其分配给相应的参数。按着上面的sql,我们如果要使用get_order_by_cust存储过程,可以传递客户编号和四个用户定义的变量来获取输出值。执行存储过程后,我们再使用SELECT语句输出变量值:

+----------+-----------+-----------+-----------+
| @shipped | @canceled | @resolved | @disputed |
+----------+-----------+-----------+-----------+
|    22 |     0 |     1 |     1 |
+----------+-----------+-----------+-----------+
1 row in set

结合实际应用,我们再来看下从PHP程序中调用返回多个值的存储过程:

<?php
/**
 * Call stored procedure that return multiple values
 * @param $customerNumber
 */
function call_sp($customerNumber)
{
  try {
    $pdo = new PDO("mysql:host=localhost;dbname=yiibaidb", 'root', '123456');
    // execute the stored procedure
    $sql = 'CALL get_order_by_cust(:no,@shipped,@canceled,@resolved,@disputed)';
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':no', $customerNumber, PDO::PARAM_INT);
    $stmt->execute();
    $stmt->closeCursor();
    // execute the second query to get values from OUT parameter
    $r = $pdo->query("SELECT @shipped,@canceled,@resolved,@disputed")
         ->fetch(PDO::FETCH_ASSOC);
    if ($r) {
      printf('Shipped: %d, Canceled: %d, Resolved: %d, Disputed: %d',
        $r['@shipped'],
        $r['@canceled'],
        $r['@resolved'],
        $r['@disputed']);
    }
  } catch (PDOException $pe) {
    die("Error occurred:" . $pe->getMessage());
  }
}
call_sp(141);

上述代码中,在@符号之前的用户定义的变量与数据库连接相关联,因此它们可用于在调用之间进行访问。

好啦,本次分享就到这里了。

更多关于MySQL相关内容感兴趣的读者可查看本站专题

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 MySql mysql存储过程之返回多个值的方法示例 https://www.niceym.com/39763.html