我正在使用 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 ;