有这样的数据结构Id  int
SerialNumber nvarchar
SubCategories nvarchar和类似下面这样的数据Id SerialNumber SubCategories
1 NO.2009.10 9,11
2 NO.2009.12 20
3 NO.2009.11 空字符串
4 NO.2009.14 7,8,9我希望得到以下数据:
Id SerialNumber SubCategories
1 NO.2009.10 9
1 NO.2009.10 11
2 NO.2009.12 20
3 NO.2009.11 空字符串
4 NO.2009.14 7
4 NO.2009.14 8
4 NO.2009.14 9希望得到高手解答.谢谢.

解决方案 »

  1.   

    /*
    -----------------------------------
     -------T-MAC ---------------------
     ---------------小编---------------
       ---------------love 轩--------
    -----------------------------------
    */
    拆分表:
    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'
    Go--SQL2000用辅助表:
    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)=','
    --2000不使用辅助表
    Select
        a.Col1,COl2=substring(a.Col2,b.number,charindex(',',a.Col2+',',b.number)-b.number) 
    from 
        Tab a join master..spt_values  b 
        ON B.type='p' AND B.number BETWEEN 1 AND LEN(A.col2)
    where
         substring(','+a.COl2,b.number,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:--此法roy博客;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
    */
      

  2.   

    感谢feixianxxx的回复.
    我选择了 SQL05用CTE 这种方法.请问我在这个后面select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)
    加上
    inner join 
    请问这样可以吗?为什么报关键字 'inner' 附近有语法错误。
    像下面这样
    with BrandSplit as 
    (select Id,SerialNumber,BarndName,MenuId,CategoryId,SubCategoryId=cast(left(SubCategories,charindex(',',SubCategories+',')-1) as nvarchar(100)),Split=cast(stuff(SubCategories+',',1,charindex(',',SubCategories+','),'') as nvarchar(100)),UseRange,Hits,Img,Assignee,BuyRegion,OrderValue from Brand
    union all
    select Id,SerialNumber,BarndName,MenuId,CategoryId,SubCategoryId=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)),UseRange,Hits,Img,Assignee,BuyRegion,OrderValue from BrandSplit where split>''
    )
    select Id,SerialNumber,BarndName,MenuId,CategoryId,SubCategoryId,UseRange,Hits,Img,c.CategoryName,Assignee,BuyRegion,OrderValue from BrandSplit option (MAXRECURSION 0)
    inner join Category c on c.Id=CategoryId
    谢谢.
      

  3.   


    create view v_b
    as
    with BrandSplit as 
    (select Id,SerialNumber,BarndName,MenuId,CategoryId,SubCategoryId=cast(left(SubCategories,charindex(',',SubCategories+',')-1) as nvarchar(100)),Split=cast(stuff(SubCategories+',',1,charindex(',',SubCategories+','),'') as nvarchar(100)),UseRange,Hits,Img,Assignee,BuyRegion,OrderValue from Brand
    union all
    select Id,SerialNumber,BarndName,MenuId,CategoryId,SubCategoryId=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)),UseRange,Hits,Img,Assignee,BuyRegion,OrderValue from BrandSplit where split>''
    )
    select Id,SerialNumber,BarndName,MenuId,CategoryId,SubCategoryId,UseRange,Hits,Img,c.CategoryName,Assignee,BuyRegion,OrderValue 
    from BrandSplit  inner join Category c on c.Id=CategoryId option (MAXRECURSION 0)
    这样?
      

  4.   

    --2000不使用辅助表
    Select
        a.Col1,COl2=substring(a.Col2,b.number,charindex(',',a.Col2+',',b.number)-b.number) 
    from 
        Tab a join master..spt_values  b 
        ON B.type='p' AND B.number BETWEEN 1 AND LEN(A.col2)
    where
         substring(','+a.COl2,b.number,1)=','
      

  5.   

    非常小f的回复。我已经使用了cte的方法,不想再改动了。既然无法在视图中使用,我想使用表值函数总是可以吧,可是这样也不行。郁闷的很。create function fn_brandSplit()
    returns table
    as
    begin
    with BrandSplit as 
    (
    select Id,SerialNumber,BarndName,MenuId,CategoryId,SubCategoryId=cast(left(SubCategories,charindex(',',SubCategories+',')-1) as nvarchar(100)),Split=cast(stuff(SubCategories+',',1,charindex(',',SubCategories+','),'') as nvarchar(100)),UseRange,Hits,Img,Assignee,BuyRegion,OrderValue,MenuName=case when MenuId=1 then '精品商标' when MenuId=2 then '商标超市' when MenuId=3 then '商标授权' else '成功案例' end from Brand
    union all
    select Id,SerialNumber,BarndName,MenuId,CategoryId,SubCategoryId=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)),UseRange,Hits,Img,Assignee,BuyRegion,OrderValue,MenuName=case when MenuId=1 then '精品商标' when MenuId=2 then '商标超市' when MenuId=3 then '商标授权' else '成功案例' end from BrandSplit where split>''
    )
    select bs.Id,bs.SerialNumber,bs.BarndName,bs.MenuId,bs.CategoryId,bs.SubCategoryId,bs.UseRange,bs.Hits,bs.Img,c.CategoryName,bs.MenuName,bs.Assignee,bs.BuyRegion,bs.OrderValue from BrandSplit bs 
    inner join dbo.Category c on c.Id=bs.CategoryId 
    order by bs.OrderValue desc,id
    option (MAXRECURSION 0)
    end'BEGIN' 附近有语法错误。
      

  6.   

    WITH前加;; WITH BrandSplit as 
      

  7.   

    create function fn_brandSplit()
    returns table
    as
    return(
    with BrandSplit as 
    (
    select Id,SerialNumber,BarndName,MenuId,CategoryId,SubCategoryId=cast(left(SubCategories,charindex(',',SubCategories+',')-1) as nvarchar(100)),Split=cast(stuff(SubCategories+',',1,charindex(',',SubCategories+','),'') as nvarchar(100)),UseRange,Hits,Img,Assignee,BuyRegion,OrderValue,MenuName=case when MenuId=1 then '精品商标' when MenuId=2 then '商标超市' when MenuId=3 then '商标授权' else '成功案例' end from Brand
    union all
    select Id,SerialNumber,BarndName,MenuId,CategoryId,SubCategoryId=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)),UseRange,Hits,Img,Assignee,BuyRegion,OrderValue,MenuName=case when MenuId=1 then '精品商标' when MenuId=2 then '商标超市' when MenuId=3 then '商标授权' else '成功案例' end from BrandSplit where split>''
    )
    select bs.Id,bs.SerialNumber,bs.BarndName,bs.MenuId,bs.CategoryId,bs.SubCategoryId,bs.UseRange,bs.Hits,bs.Img,c.CategoryName,bs.MenuName,bs.Assignee,bs.BuyRegion,bs.OrderValue from BrandSplit bs 
    inner join dbo.Category c on c.Id=bs.CategoryId 
    order by bs.OrderValue desc,id
    option (MAXRECURSION 0))
      

  8.   

    if object_id('tb') is not null
    drop table tb
    go
    create table tb(name varchar(25))
    insert into tb
    select 'z,y,w,c' union all
    select '1,2,3,4' 
    select name=substring(a.name,b.number,(charindex(',',a.name+',',b.number))-b.number) 
    from tb a,master..spt_values b where b.type='p' and b.number between 1 and len(a.name)
     and  substring(','+a.name,b.number,1)=','
      

  9.   

    哥,表值函数不是这么用的
    CREATE FUNCTION FUN_MU(@ID INT)
    RETURNS TABLE @T(COL1 INT,COL2 INT,....)
    AS
    BEGIN
    INSERT INTO @T
    ......RETURN @TEND