使用游标的Mysql存储过程返回null

时间:2017-03-27 05:35:11

标签: mysql stored-procedures cursor

代码如下:当我调用noemail时,它返回null,出了什么问题?

那里有一个null cust_email(参见表customer),但是它只返回null,它应该返回一个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

帮助我,谢谢你!

2 个答案:

答案 0 :(得分:0)

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

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 ;

答案 1 :(得分:0)

您正在使用相同的名称" 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 ;