【问题标题】:Mysql stored procedure using cursor returns nullMysql存储过程使用游标返回null
【发布时间】:2017-03-27 05:35:11
【问题描述】:

代码如下:当我调用noemail时返回null,这是怎么回事?

而且那里有一个空的cust_email(见表customers),但它什么都不返回,只是空,它应该返回一个cust_name,哪一部分错了?谢谢。

    DELIMITER $$
    USE a_schema_in_mysql $$
    create procedure noemail()
    begin
        declare cust_name char;
        declare custcursor cursor for
            select cust_name from customers where cust_email is null;

        open custcursor;
        loop
            fetch custcursor into cust_name;
            select concat('Null email host is',cust_name) as noemailcust;
        end loop;
        close custcursor;
    end$$
    DELIMITER ;

    select * from customers;

    +------------+---------------+----------------------+-----------+------------+
    | cust_id    | cust_name     | cust_address         | cust_city | cust_state |
    +------------+---------------+----------------------+-----------+------------+
    | 1000000001 | Village Toys  | 200 Maple Lane       | Detroit   | MI         |
    | 1000000002 | Kids Place    | 333 South Lake Drive | Columbus  | OH         | 
    | 1000000003 | Fun4All       | 1 Sunny Place        | Muncie    | IN         | 
    | 1000000004 | Fun4All       | 829 Riverside Drive  | Phoenix   | AZ         | 
    | 1000000005 | The Toy Store | 4545 53rd Street     | Chicago   | IL         |
    +------------+---------------+----------------------+-----------+------------+
    +----------+--------------+--------------------+-----------------------+
    | cust_zip | cust_country | cust_contact       | cust_email            |
    +----------+--------------+--------------------+-----------------------+
    | 44444    | USA          | John Smith         | sales@villagetoys.com |
    | 43333    | USA          | Michelle Green     | NULL                  |
    | 42222    | USA          | Jim Jones          | jjones@fun4all.com    |
    | 88888    | USA          | Denise L. Stephens | dstephens@fun4all.com |
    | 54545    | USA          | Kim Howard         | kim@thetoystore.com   |
    +----------+--------------+--------------------+-----------------------+

以及下面的错误:

mysql> call noemail;
+-------------+
| noemailcust |
+-------------+
| NULL        |
+-------------+
1 row in set (0.00 sec)

ERROR 1329 (02000): No data - zero rows fetched, selected, or processed

帮帮我,谢谢!

【问题讨论】:

    标签: mysql stored-procedures cursor


    【解决方案1】:

    您使用的是同名“cust_name”,这也是您的列名。 当参数或变量名称与字段名称冲突时,使用参数或变量名称。

    我正在添加编辑:

    DELIMITER $$
    USE a_schema_in_mysql $$
    create procedure noemail()
    begin
        declare cust_name_s char;
        declare custcursor cursor for
            select cust_name from customers where cust_email is null;
    
        open custcursor;
        loop
            fetch custcursor into cust_name_s;
            select concat('Null email host is',cust_name_s) as noemailcust;
        end loop;
        close custcursor;
    end$$
    DELIMITER ;
    

    【讨论】:

    • 非常感谢,我想我应该多学习一些关于 SQL 的基本原理~以及 char 的默认长度是多少,我将 char 更正为 char(20) 以使其可运行。非常感谢:) 我是初学者
    【解决方案2】:

    我假设你可以在不使用光标的情况下实现你想要的

    DELIMITER $$
    USE a_schema_in_mysql $$
    create procedure noemail()
    begin
        select concat('Null email host is',cust_name) from customers where cust_email is null;
    end$$
    DELIMITER ;
    

    【讨论】:

    • 我知道,但我想知道我的 coda 出了什么问题
    猜你喜欢
    • 2016-10-31
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2014-04-18
    • 2013-12-19
    • 2021-06-11
    • 2012-11-29
    • 2014-08-04
    相关资源
    最近更新 更多