1

我正在使用 MySQL。我可以在 MySQL 程序中获得更多行,但不能在函数中获得。我想通过返回不止一行的值来编写函数。我附上我的部分代码:

drop function if exists test_splitfn;
DELIMITER $$

CREATE Function test_splitfn( sSepar VARCHAR(255), saVal TEXT )
Returns @MyTable table 
body:

BEGIN

  DROP TEMPORARY TABLE IF EXISTS lib_Explode;
  CREATE TEMPORARY TABLE lib_Explode(
    pos int unsigned NOT NULL auto_increment,
    val VARCHAR(255) NOT NULL,
    PRIMARY KEY  (pos)
  ) ENGINE=Memory COMMENT='Explode() results.';

  IF sSepar IS NULL OR saVal IS NULL THEN LEAVE body; END IF;

  SET @saTail = saVal;
  SET @iSeparLen = LENGTH( sSepar );

  create_layers:
  WHILE @saTail != '' DO

    # Get the next value
    SET @sHead = SUBSTRING_INDEX(@saTail, sSepar, 1);
    SET @saTail = SUBSTRING( @saTail, LENGTH(@sHead) + 1 + @iSeparLen );
    INSERT INTO lib_Explode SET val = @sHead;

  END WHILE;

     Insert @MyTable
      SELECT val  FROM lib_Explode;


    return @MyTable;
END; $$
DELIMITER ;
4

2 回答 2

3

在 MySQL 函数中,您无法返回表..

于 2012-11-26T17:11:45.937 回答
0

尝试这样的事情:

DROP FUNCTION IF EXISTS test_splitfn;
DELIMITER $$

CREATE PROCEDURE test_splitfn (IN sSepar VARCHAR(255), saVal TEXT)
body:BEGIN

    DROP TEMPORARY TABLE IF EXISTS lib_Explode;
    CREATE TEMPORARY TABLE lib_Explode(
        pos int unsigned NOT NULL auto_increment,
        val VARCHAR(255) NOT NULL,
        PRIMARY KEY  (pos)
    ) ENGINE=Memory COMMENT='Explode() results.';

    IF sSepar IS NULL OR saVal IS NULL THEN
        LEAVE body;
    END IF;

    SET @saTail = saVal;
    SET @iSeparLen = LENGTH( sSepar );

    WHILE (@saTail != '') DO
        SET @sHead = SUBSTRING_INDEX(@saTail, sSepar, 1);
        SET @saTail = SUBSTRING( @saTail, LENGTH(@sHead) + 1 + @iSeparLen );
        INSERT INTO lib_Explode SET val = @sHead;
    END WHILE;

    SELECT val FROM lib_Explode;
END$$
DELIMITER ;
于 2012-11-06T11:24:53.583 回答