旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

mysql存储历程返回多个效果集【MySQL教程】,mysql

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:54评论:0


导读:本篇文章将引见存储历程返回多个效果集时是什么效果,愿望给人人以参考作用。mysql存储函数只返回一个值。要开辟返回多个值的存储历程,须要运用带有INOUT或OUT参数的...
本篇文章将引见存储历程返回多个效果集时是什么效果,愿望给人人以参考作用。

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

以上就是mysql存储历程返回多个效果集的细致内容,更多请关注ki4网别的相干文章!

标签:mysql


欢迎 发表评论: