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 に答える