有一表
ID D1 D2 D3 D4
1  AB B  C  AB
2  B  AB B  BC
现将记录进行拆分,如第一条记录拆分为4条记录
1  A   B   C   B
2  A   B   C   A
3  B   B   C   B
4  B   B   C   A
即每个字段区一个字母进行组合。
第二条记录同样如此拆分。
并将拆分后的记录存到另外的表中!

解决方案 »

  1.   

    有一表
    ID D1 D2 D3 D4
    1 AB B C AB
    2 B AB B BC
    现将记录进行拆分,如第一条记录拆分为4条记录
    ID D1 D2 D3 D4
    1 A B C B
    2 A B C A
    3 B B C B
    4 B B C A
    即每个字段取分别一个字母进行组合成一条记录。
    第二条记录同样如此拆分。
    ID D1 D2 D3 D4
    5 B B B C
    6 B B B B
    7 B A B C
    8 B A B B
    并将拆分后的记录存到另外的表中!
    结果为
    ID D1 D2 D3 D4
    1 A B C B
    2 A B C A
    3 B B C B
    4 B B C A
    5 B B B C
    6 B B B B
    7 B A B C
    8 B A B B
      

  2.   

    --> 测试数据:#
    if object_id('tempdb.dbo.#') is not null drop table #
    create table #(ID int, D1 varchar(8), D2 varchar(8), D3 varchar(8), D4 varchar(8))
    insert into #
    select 1, 'AB', 'B', 'C', 'AB' union all
    select 2, 'B', 'AB', 'B', 'BC'if object_id('tempdb.dbo.#n') is not null drop table #n
    select top 10 n = identity(int,1,1) into #n from syscolumns;
    with t1 as
    (
        select id, d1=substring(d1,n,1) from #, #n where n<=len(d1)
    ),
    t2 as
    (
        select id, d2=substring(d2,n,1) from #, #n where n<=len(d2)
    ),
    t3 as
    (
        select id, d3=substring(d3,n,1) from #, #n where n<=len(d3)
    ),
    t4 as
    (
        select id, d4=substring(d4,n,1) from #, #n where n<=len(d4)
    )
    select id=row_number()over(order by t1.id), d1,d2,d3,d4 from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t3.id and t3.id=t4.id
    /*
    id       d1   d2   d3   d4
    -------- ---- ---- ---- ----
    1        A    B    C    A
    2        A    B    C    B
    3        B    B    C    A
    4        B    B    C    B
    5        B    A    B    B
    6        B    B    B    B
    7        B    A    B    C
    8        B    B    B    C
    */
      

  3.   


    用一下脑,久了不用会生锈的inset your_table select id=row_number()over(order by t1.id), d1,d2,d3,d4 from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t3.id and t3.id=t4.idselect id=row_number()over(order by t1.id), d1,d2,d3,d4 into your_table from t1,t2,t3,t4 where t1.id=t2.id and t2.id=t3.id and t3.id=t4.id