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

标签 mysql stored-procedures cursor

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

还有一个null的cust_email(见表customers),但是它什么都不返回只是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

帮助我,谢谢!

最佳答案

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

关于Mysql存储过程使用游标返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43038608/

相关文章:

mysql - 根据最畅销产品选择客户类型

mysql - 唯一索引允许重复条目,以防其中一个条目为 NULL

mysql - 优化 MySQL 查询时出现问题

sql - PL/SQL : Encountered the symbol "END"

java - Uri 引起的 CursorIndexOutOfBoundsException

php - 计数数据取决于句子id和数据频率

sql - 查找存储过程中未引用的所有表

linq - 使用 LINQ to SQL 从存储过程中获取多维结果

Java 不释放 oracle 游标

sql-server - t-sql : difference in performance between cursor and cursor static