2014-03-12 192 views
0

我在MySQL String_Split中存儲了過程。MySQL:從函數調用存儲過程

DROP PROCEDURE IF EXISTS String_Split; 
DELIMITER $$ 
CREATE PROCEDURE String_Split(vString VARCHAR(8000),vSeparator VARCHAR(5)) 
BEGIN 

DECLARE vDone tinyint(1) DEFAULT 1; 
DECLARE vIndex INT DEFAULT 1; 
DECLARE vSubString VARCHAR(500); 

DROP TEMPORARY TABLE IF EXISTS tmpIDList; 
CREATE TEMPORARY TABLE IF NOT EXISTS tmpIDList (ID varchar(500)); 

WHILE vDone > 0 DO 
    SET vSubString = SUBSTRING(vString, vIndex, 
        IF(LOCATE(vSeparator, vString, vIndex) > 0, 
         LOCATE(vSeparator, vString, vIndex) - vIndex, 
         LENGTH(vString) 
        )); 
    IF LENGTH(vSubString) > 0 THEN 
     SET vIndex = vIndex + LENGTH(vSubString) + 1; 
     INSERT INTO tmpIDList VALUES (vSubString); 
    ELSE 
     SET vDone = 0; 
    END IF; 
END WHILE; 

END$$ 
DELIMITER ; 

我從函數調用這個存儲過程。

DROP FUNCTION IF EXISTS test; 
DELIMITER $$ 
    CREATE FUNCTION test (IDLIST VARCHAR(255)) 
    RETURNS VARCHAR(2000) 
    BEGIN 
     DECLARE returnData VARCHAR(2000); 

     DROP TEMPORARY TABLE IF EXISTS TEMP1;  
     CREATE TEMPORARY TABLE IF NOT EXISTS TEMP1(id bigint); 

     CALL String_Split(IDLIST,','); 
     INSERT INTO TEMP1 (SELECT * FROM tmpIDList); 

     SELECT GROUP_CONCAT(first_name) INTO returnData 
     FROM user_master 
     WHERE id IN (SELECT id FROM TEMP1); 

     RETURN returnData; 
    End $$ 
DELIMITER ; 

但我發現了以下異常

Error Code: 1146. Table 'backup25dec_latest.tmpidlist' doesn't exist 

回答

0

我無法重現該問題。

確實生成了兩個警告(NOTE),但該行爲記錄在13.1.28 DROP TABLE Syntax中。

mysql> SHOW WARNINGS; 
+-------+------+---------------------------+ 
| LEVEL | CODE | Message     | 
+-------+------+---------------------------+ 
| Note | 1051 | UNKNOWN TABLE 'temp1'  | 
| Note | 1051 | UNKNOWN TABLE 'tmpidlist' | 
+-------+------+---------------------------+ 
2 ROWS IN SET (0.00 sec) 

SQL Fiddle demo

+0

我採用的是MySQL 5.0,我認爲這個功能可能無法在其提供。我升級到5.6,工作正常。感謝您的關注 –