MySQL Forums
Forum List  »  UDF

How to return temp table in mysql function
Posted by: piyush patel
Date: June 01, 2016 12:56AM

I have Converted sql function which split the delimited string in rows, my code is as below, when i query select * from tmp, it gives correct result, how can i get the result by calling that function

DELIMITER $$



DROP FUNCTION IF EXISTS `split`$$

CREATE AGGREGATE FUNCTION `split`(`String` VARCHAR(8000), `Delimiter` CHAR(1)) RETURNS VARCHAR(8000) CHARSET latin1
BEGIN
DECLARE idx INT;
DECLARE slice VARCHAR(8000);
DECLARE result INT DEFAULT 0;
DROP TEMPORARY TABLE IF EXISTS tmp;
CREATE TEMPORARY TABLE `tmp` (items VARCHAR(8000));
SET @tmptab = tmp;
SET idx = 1;

label: WHILE LENGTH(`String`)<1 OR `String` IS NULL DO
LEAVE label;
END WHILE;
loop_lable: WHILE (idx<>0) DO

SET idx = LOCATE(`Delimiter`,`String`);
IF (idx<>0) THEN

SET slice = LEFT(`String`,idx - 1);
ELSE
SET slice = `String`;
END IF;
IF(LENGTH(slice)>0) THEN
INSERT INTO tmp(Items) VALUES(slice) ;
SET result = 1;
END IF;
SET `String` = RIGHT(`String`,LENGTH(`String`) - idx);
IF LENGTH(`String`) = 0 THEN
LEAVE loop_lable;
END IF;
END WHILE;
RETURN result;

END$$

DELIMITER ;

Options: ReplyQuote


Subject
Views
Written By
Posted
How to return temp table in mysql function
3263
June 01, 2016 12:56AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.