在mySql中 拆分字符串


# DROP TEMPORARY TABLE IF EXISTS test_tim;  删表用
create TEMPORARY table test_tim (
    id int,
    a1 varchar(128),
    a2 decimal(16,2),
    a21 decimal(16,2),
    a3 varchar(1024),
    a4 datetime,
    a5 int,
    a6 int
);

#DROP TEMPORARY TABLE IF EXISTS test;  删表用
create TEMPORARY table test (
    id int,
    a1 varchar(128),
    a2 decimal(16,2),
    a21 decimal(16,2),
    a3 varchar(1024),
    a4 datetime,
    a5 int,
    a6 int
);


#插入样本

insert into test values(136,'test',1234567.8,987654.2,'com.a.b,1,6,error,IOEofFilea,b','2012/10/22 12:00',0,4);
insert into test values(137,'test2',1234567.9,987654.3,'com.a.b,1,6,error,IOEofFileb,c','2012/10/22 13:00',1,5);


#校验

select * from test;

136test1234567.80987654.20com.a.b,1,6,error,IOEofFilea,b2012-10-22 12:00:0004
137test21234567.90987654.30com.a.b,1,6,error,IOEofFileb,c2012-10-22 13:00:0015

#处理逻辑

delimiter $$;
create procedure lhy()
READS SQL DATA
begin
    declare did int;
    declare da1 varchar(128);
    declare da2 decimal(16,2);
    declare da21 decimal(16,2);
    declare da3 varchar(1024);
    declare da4 datetime;
    declare da5 int;
    declare da6 int;    
    DECLARE STOP INT DEFAULT 0;

    declare _cs cursor for     select id, a1, a2, a21, a3,a4,a5,a6 from test;

    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET STOP=1;

    open _cs;
    fetch _cs into did, da1, da2, da21, da3,da4,da5,da6;
    while STOP<>1 DO
        begin
            declare i int;  
            declare s int;
            set i=1 ;
            set s=1  ;
            while(i>0) DO     
               set i=LOCATE(',',da3,s)  ;    
               if(i>0) then             
                       insert test_tim values(did, da1, da2, da21, substring(da3,s,i-s),da4,da5,da6 ) ;       
               end if;         
               if(i<=0) then       
                       insert test_tim values(did, da1, da2, da21, substring(da3,s,length(da3)-s+1),da4,da5,da6) ;
               end if;
               set s = i + 1   ;
            end  while;
        end;
    fetch _cs into did, da1, da2, da21, da3,da4,da5,da6;
    END while;
    close _cs;
end$$;


#drop procedure lhy;删除procedure 用
#call lhy; 呼叫lhy用

#校验结果用--先create ,再call,再校验结果

#select * from test_tim;

136test1234567.80987654.20com.a.b2012-10-22 12:00:0004
136test1234567.80987654.2012012-10-22 12:00:0004
136test1234567.80987654.2062012-10-22 12:00:0004
136test1234567.80987654.20error2012-10-22 12:00:0004
136test1234567.80987654.20IOEofFilea2012-10-22 12:00:0004
136test1234567.80987654.20b2012-10-22 12:00:0004
137test21234567.90987654.30com.a.b2012-10-22 13:00:0015
137test21234567.90987654.3012012-10-22 13:00:0015
137test21234567.90987654.3062012-10-22 13:00:0015
137test21234567.90987654.30error2012-10-22 13:00:0015
137test21234567.90987654.30IOEofFileb2012-10-22 13:00:0015
137test21234567.90987654.30c2012-10-22 13:00:0015


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值