2017-03-27 74 views
1

的代碼是在這裏如下:?當我打電話noemail返回null,什麼錯誤MySQL存儲過程中使用遊標返回null

而且還有一個空CUST_EMAIL有(見表客戶),但它沒有返回只是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   | [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] | 
    +----------+--------------+--------------------+-----------------------+ 

及以下錯誤:

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

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

幫助我,謝謝

回答

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 ; 
+1

非常感謝你,我覺得我應該更多地瞭解有關SQL的基本原則〜什麼是char的默認長度,我將char改爲char(20)以使其可運行。非常感謝:)我是一名初學者 – EimanTan

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 ; 
+0

我知道,但我想konw什麼錯誤我的尾的 – EimanTan