MySQL:从函数调用存储过程

时间:2014-03-12 12:19:51

标签: mysql stored-procedures

我在MySQL String_Split中存储过程。

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

1 个答案:

答案 0 :(得分: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