Search code examples
mysqlstored-procedurescursor

Mysql stored procedure using cursor returns null


The code is here below: When I call noemail it returns null, what is going wrong?

And there is a null cust_email there(see table customers), but it returns nothing just null, it is supposed to return a cust_name, which part is wrong? Thank you.

    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         | [email protected] |
    | 43333    | USA          | Michelle Green     | NULL                  |
    | 42222    | USA          | Jim Jones          | [email protected]    |
    | 88888    | USA          | Denise L. Stephens | [email protected] |
    | 54545    | USA          | Kim Howard         | [email protected]   |
    +----------+--------------+--------------------+-----------------------+

and the error below:

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

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

Help me and thank you!


Solution

  • You are using same name "cust_name" which is also your column name. When the parameter or variable names conflict with the field names, the parameter or variable names are used.

    I am adding edits :

    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 ;