我想在SQL DB 里面抽数据在表A里查找数据往表B里写,如果表A查出的字符大于5(表B里第一行放不下就自动放到表B第二行)表A
“1234567890 11 12 13。。”
表B第一行
“12345”表B第二行
“67890” 
表B第三行
“11 12 13”

解决方案 »

  1.   


    -- 建表
    create table tb1(col varchar(1000));
    insert into tb1 values('12345678901112131415161718192021222324252627282930');
    insert into tb1 values('abcdefghijklmnopqrstuvwxyzfdsaklfdjsaklfjdsaklfj');create table tb2(col varchar(50));
    select * from tb1-- 插入第二张表数据的sql
    ;with t as(
    select 
    vt = substring(col,5+1, len(col)),
    st = left(col, 5)
    from tb1
    union all
    select 
    vt = substring(t.vt,5+1, len(t.vt)),
    st = left(t.vt, 5)
    from t
    where len(t.vt)>0
    )
    insert into tb2
    select st from t