Please help me with this stored function
Posted by:
jim smith
Date: January 13, 2009 08:38AM
I am trying to make my own split function that I can use to create my not like list for a where clause. I have some errors. Can you help?
DELIMITER $$;
CREATE FUNCTION `apache_logs`.`split` (sStringIn text,splitChar varchar(1))
RETURNS text
DETERMINISTIC
BEGIN
DECLARE comma INT DEFAULT 0;
DECLARE mylist TEXT DEFAULT _intlist;
DECLARE temp TEXT DEFAULT '';
DECLARE strlen int DEFAULT LENGTH(_intlist);
DECLARE crlf varchar(5) DEFAULT char(13);
DECLARE tab varchar(5) DEFAULT char(9);
DECLARE outtext text(10000) DEFAULT '';
DECLARE done INT DEFAULT 0;
DECLARE temp1 TEXT DEFAULT '';
DECLARE checker int DEFAULT 0;
CREATE TEMPORARY TABLE TempTable (num int) TYPE=INNODB;
/* find the first instance of the spliting character */
SET comma = LOCATE(splitChar,mylist);
/* Insert each split variable into the temp table */
WHILE strlen > 0 DO
IF comma = 0 THEN
SET temp = TRIM(mylist);
SET mylist = '';
SET strlen = 0;
ELSE
SET temp = TRIM(SUBSTRING(mylist,1,comma));
SET mylist = TRIM(SUBSTRING(mylist FROM comma+1));
SET strlen = LENGTH(mylist);
END IF;
IF CAST(temp as UNSIGNED) != 0
THEN
INSERT INTO TempTable VALUES(CAST(temp as UNSIGNED));
END IF;
SET comma = LOCATE(splitChar,mylist);
END WHILE;
/* open the temp table and loop through the entries */
DECLARE temps CURSOR FOR SELECT * FROM TempTable;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
SET done=0;
set outtext='';
open temps;
temps_loop:LOOP /* Loop through temps*/
fetch temps into temp1;
IF done=1 THEN LEAVE temps_loop; END IF; /*no more rows*/
sub_block: BEGIN
/* append data */
set outtext = concat(outtext,' and Path not like ',char(39),char(37),temp1,char(37),char(39));
END sub_block;
END LOOP temps_loop;
SET done=0;
CLOSE temps;
select outtext;
DROP TEMPORARY TABLE IF EXISTS TempTable;
END$$
DELIMITER ;$$
Subject
Views
Written By
Posted
Please help me with this stored function
2849
January 13, 2009 08:38AM
1820
January 13, 2009 01:54PM
1672
January 14, 2009 08:57AM
1575
January 14, 2009 12:09PM
1718
January 14, 2009 12:04PM
1646
January 14, 2009 12:57PM
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.