2012-12-08 83 views
1

我一直在MySQL中發出這個警告,並找不到原因。功能如下。MySQL - 沒有數據 - 函數

Warning | 1329 | No data - zero rows fetched, selected, or processed 

功能:

CREATE FUNCTION CONTACT_INFO_EXISTS(
    hp VARCHAR(64), 
    wp VARCHAR(32), 
    mp VARCHAR(32), 
    f VARCHAR(32), 
    pe VARCHAR(64), 
    se VARCHAR(64), 
    c TIMESTAMP 
) 
RETURNS VARCHAR(36) 
READS SQL DATA 
BEGIN 
    DECLARE c_id CHAR(36); 

    SET c_id = NULL; 

    SELECT contact_infouuid INTO c_id FROM contact_info 
     WHERE (home_phone = hp OR hp IS NULL) 
      AND (work_phone = wp OR wp IS NULL) 
      AND (mobile_phone = mp OR mp IS NULL) 
      AND (fax = f OR f IS NULL) 
      AND (primary_email = pe OR pe IS NULL) 
      AND (secondary_email = se OR se IS NULL) 
      AND (last_updated = c OR c IS NULL) 
     LIMIT 1; 

    RETURN IF(c_id IS NOT NULL, c_id, '0'); 
END$$ 
+0

只是一個觀察,SET C_ID = NULL;認爲這是沒有必要的。 – alditis

+0

評論:http://dev.mysql.com/doc/refman/5.0/en/declare-local-variable.html – alditis

+0

我知道,但它只是爲了幫助我們的一些程序員保持清醒,他們只是學習SQL –

回答

0

試試這個:

CREATE FUNCTION CONTACT_INFO_EXISTS(
    hp VARCHAR(64), 
    wp VARCHAR(32), 
    mp VARCHAR(32), 
    f VARCHAR(32), 
    pe VARCHAR(64), 
    se VARCHAR(64), 
    c TIMESTAMP 
) 
RETURNS VARCHAR(36) 
READS SQL DATA 
BEGIN 
    DECLARE c_id CHAR(36); 

    SET c_id = NULL; 

    IF EXISTS (SELECT * FROM contact_info 
        WHERE (home_phone = hp OR hp IS NULL) 
         AND (work_phone = wp OR wp IS NULL) 
         AND (mobile_phone = mp OR mp IS NULL) 
         AND (fax = f OR f IS NULL) 
         AND (primary_email = pe OR pe IS NULL) 
         AND (secondary_email = se OR se IS NULL) 
         AND (last_updated = c OR c IS NULL)) THEN 
     SELECT contact_infouuid INTO c_id FROM contact_info 
       WHERE (home_phone = hp OR hp IS NULL) 
        AND (work_phone = wp OR wp IS NULL) 
        AND (mobile_phone = mp OR mp IS NULL) 
        AND (fax = f OR f IS NULL) 
        AND (primary_email = pe OR pe IS NULL) 
        AND (secondary_email = se OR se IS NULL) 
        AND (last_updated = c OR c IS NULL) 
       LIMIT 1; 
    END IF; 
    RETURN IF(c_id IS NOT NULL, c_id, '0'); 
END$$ 
+0

考慮到這個功能應該總是返回一些東西......我想我迷路了。 「檢查存在」是什麼意思? –