首頁 >資料庫 >mysql教程 >MySQL存储过程实现split

MySQL存储过程实现split

WBOY
WBOY原創
2016-06-07 14:56:321374瀏覽

MySQL存储过程实现split MySQL drop PROCEDURE if exists procedure_split;CREATE PROCEDURE `procedure_split`( inputstring varchar(1000), delim char(1))begin declare strlen int DEFAULT length(inputstring); declare last_index int DEFAULT 0; decla

MySQL存储过程实现split MySQL MySQL存储过程实现split
drop PROCEDURE if exists procedure_split;
CREATE PROCEDURE `procedure_split`(
    inputstring varchar(1000),
    delim char(1)
)
begin
    declare strlen int DEFAULT length(inputstring);
    declare last_index int DEFAULT 0;
    declare cur_index int DEFAULT 1;
    declare cur_char VARCHAR(200);
    declare len int;
    drop temporary table if exists splittable;
    create TEMPORARY table splittable(
        value VARCHAR(20)
    ) ;
    WHILE(cur_index<=strlen) DO    
    begin
        if substring(inputstring from cur_index for 1)=delim or cur_index=strlen then
            set len=cur_index-last_index-1;
            if cur_index=strlen then
               set len=len+1;
            end if;
            insert into splittable(`value`)values(substring(inputstring from (last_index+1) for len));
            set last_index=cur_index;
        end if;
        set cur_index=cur_index+1;
    END;
    end while;
end ;
call PROCEDURE_split('中国,开源,社区',',');
select * from splittable;
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn