如果数据库中记录:1  aaa     111,222,333  22,33,44   55,66,77
2  bbb     111,222 22,33  55,66
转为1  aaa     111 22 55
1  aaa     222 33  66
1  aaa     333 44  77
2  bbb     111 22  55
2  bbb     222 33  66
如何写sql?

解决方案 »

  1.   

    create function dbo.fn_strSplit ( @arg varchar(8000) , @splitchar varchar(8) )
    returns @tb table ( s varchar(64) ) as
    begin
    declare @part varchar(64), @pos int
    set @pos = charindex(@splitchar, @arg)
    while @pos>=1 begin
    set @part = rtrim(left(@arg,@pos-1))
    set @arg = stuff(@arg,1,@pos+len(@splitchar),'')
    if @part>''
    insert into @tb values (@part)
    set @pos = charindex(@splitchar, @arg)
    end
    if @arg>''
    insert into @tb values (@arg)
    return
    end
      

  2.   

    --参考
    拆分表:--> --> (Roy)生成測試數據
     
    if not object_id('Tab') is null
        drop table Tab
    Go
    Create table Tab([Col1] int,[COl2] nvarchar(5))
    Insert Tab
    select 1,N'a,b,c' union all
    select 2,N'd,e' union all
    select 3,N'f'
    GoSQL2000用辅助表:
    if object_id('Tempdb..#Num') is not null
        drop table #Num
    go
    select top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns b
    Select 
        a.Col1,COl2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID) 
    from 
        Tab a,#Num b
    where
        charindex(',',','+a.Col2,b.ID)=b.ID --也可用 substring(','+a.COl2,b.ID,1)=','
    SQL2005用Xml:select 
        a.COl1,b.Col2
    from 
        (select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)a
    outer apply
        (select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b
    SQL05用CTE:;with roy as 
    (select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tab
    union all
    select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>''
    )
    select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)生成结果:
    /*
    Col1        COl2
    ----------- -----
    1           a
    1           b
    1           c
    2           d
    2           e
    3           f
    */
      

  3.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-07-16 20:13:25
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([col1] int,[col2] varchar(3),[col3] varchar(11),[col4] varchar(8),[col5] varchar(8))
    insert [tb]
    select 1,'aaa','111,222,333','22,33,44','55,66,77' union all
    select 2,'bbb','111,222','22,33','55,66'
    --------------开始查询--------------------------
    if object_id('Tempdb..#Num') is not null
        drop table #Num
    go
    select top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns b
    Select 
        a.Col1,a.col2,
        COl3=substring(a.Col3,b.ID,charindex(',',a.Col3+',',b.ID)-b.ID),
        col4=substring(a.Col4,b.ID,charindex(',',a.Col4+',',b.ID)-b.ID),
        col5=substring(a.Col5,b.ID,charindex(',',a.Col5+',',b.ID)-b.ID)
    from 
        tb a,#Num b
    where
        charindex(',',','+a.Col3,b.ID)=b.ID----------------结果----------------------------
    /*Col1        col2 COl3        col4     col5
    ----------- ---- ----------- -------- --------
    1           aaa  111         22       55
    1           aaa  222         3        6
    1           aaa  333                  
    2           bbb  111         22       55
    2           bbb  222         3        6(5 行受影响)
    */
      

  4.   

    如果数据库中记录: 1  aaa    111,222,333  22,33,44  55,66,77 
    2  bbb    111,222 22,33  55,66 
    转为 1  aaa    111 22 55 
    1  aaa    222 33  66 
    1  aaa    333 44  77 
    2  bbb    111 22  55 
    2  bbb    222 33  66 
    如何写sql?
    数据库要加粘贴,复制功能...