岁月无痕

岁月-人生
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Split in Mysql

Posted on 2008-06-12 15:39  岁月无痕  阅读(520)  评论(0)    收藏  举报

CREATE  PROCEDURE `proc_SplitDemo`(
inputstring   varchar(1000),
delim     char(1)
)
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
begin
 declare strlen int;  
    declare last_index int;
    declare cur_index int;   
    declare cur_char VARCHAR(200);
    set cur_index=1;   
    set last_index=0;
    set strlen=length(inputstring); 
    drop temporary table if exists splittable;/**/
    create TEMPORARY table splittable
    (
     id int AUTO_INCREMENT,
        value VARCHAR(20),
        PRIMARY KEY (`ID`),
    UNIQUE KEY `ID` (`ID`)
    ) ;

 

    WHILE(cur_index<=strlen) DO
      begin   

        if substring(inputstring from cur_index for 1)=',' then 

   insert into splittable

         (`value`) values
            (substring(inputstring from (last_index+1) for (cur_index-last_index-1)));

      set last_index=cur_index;
  end if; 
        set cur_index=cur_index+1;        

      END;

    end while;
end;