各位大哥帮帮忙分析一下。
/*  借用SQL 77 的表结构*/
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go 
create table #TB([A] varchar(3),[B] int)
insert #TB
select 'bcd',1 union all
select 'bcde',2 union all
select 'cd',3
----------------------
/* bcd   1
 bcde  2
 cd    3*/----------------------------
想要的结果是:
---------------------
b  1
c  1
d  1
b  2
c  2
d  2
e  2
c  3
d  3

解决方案 »

  1.   

    http://blog.csdn.net/ws_hgo/archive/2010/01/22/5224723.aspx
      

  2.   

    拆分一个字符串/*
    ****拆分一个字符串*********
    问题描述:
    @str='fds,bbbf,eee,ddd,fff,hhhfg,dddde'  拆分成
    col
    ------------------------
    fds
    bbbf
    eee
    ddd
    fff
    hhhfg
    dddde
    */
    ---方法1:动态
    declare  @str  varchar(500),@aaa varchar(8000)
    set  @str='fds,bbbf,eee,ddd,fff,hhhfg,dddde'  
    set @aaa='select * from (select '''+REPLACE(@str,',',''' as str union all select ''')+''') a '
    exec(@aaa)  
    go
    --方法2:循环
    create  table  #t(  
    id  varchar(10))  
    declare  @str  varchar(300)  
    set  @str='fds,bbbf,eee,ddd,fff,hhhfg,dddde'  
    declare  @i  int  
    declare  @len  int  
    set  @i  =  1  
    while  @i  <  len(@str+',')  
    begin  
       insert  #t  select  substring(@str+',',@i,charindex(',',@str+',',@i)-@i)  
       set  @i  =  charindex(',',@str+',',@i)+1  
    end  
    select * from #t
    drop table #t
    go
    --or
    CREATE FUNCTION f_splitSTR(
    @s   varchar(8000),   --待分拆的字符串
    @split varchar(10)     --数据分隔符
    )RETURNS @re TABLE(col varchar(100))
    AS
    BEGIN
    DECLARE @splitlen int
    SET @splitlen=LEN(@split+'a')-2
    WHILE CHARINDEX(@split,@s)>0
    BEGIN
    INSERT @re VALUES(LEFT(@s,CHARINDEX(@split,@s)-1))
    SET @s=STUFF(@s,1,CHARINDEX(@split,@s)+@splitlen,'')
    END
    INSERT @re VALUES(@s)
    RETURN
    END
    GO
    --方法3:使用临时性分拆辅助表法
    CREATE FUNCTION f_splitSTR(
    @s   varchar(8000),  --待分拆的字符串
    @split varchar(10)     --数据分隔符
    )RETURNS @re TABLE(col varchar(100))
    AS
    BEGIN
    --创建分拆处理的辅助表(用户定义函数中只能操作表变量)
    DECLARE @t TABLE(ID int IDENTITY,b bit)
    INSERT @t(b) SELECT TOP 8000 0 FROM syscolumns a,syscolumns b INSERT @re SELECT SUBSTRING(@s,ID,CHARINDEX(@split,@s+@split,ID)-ID)
    FROM @t
    WHERE ID<=LEN(@s+'a') 
    AND CHARINDEX(@split,@split+@s,ID)=ID
    RETURN
    END
    GO
    declare  @str  varchar(500),@aaa varchar(8000)
    set  @str='fds,bbbf,eee,ddd,fff,hhhfg,dddde'  
    select * from dbo.f_splitSTR(@str,',')
      

  3.   

    拆分表的字段
    /*
    -----------------------------------
     -------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
    */
      

  4.   

    if object_id('tempdb.dbo.#TB') is not null drop table #TB 
    go 
    create table #TB([A] varchar(10),[B] int) 
    insert #TB 
    select 'bcd',1 union all 
    select 'bcde',2 union all 
    select 'cd',3 select substring(a.a,t.n,1) as a,a.b
    from #TB a,(
    select 1 as n union all select 2 union all select 3 union all select 4 union all select 5
    union all select 6 union all select 7 union all select 8 union all select 9 union all select 10
    ) as t
    where t.n<=len(a.a) 
      

  5.   

    if object_id('tb') is not null drop table tb 
    go 
    create table tb([A] varchar(10),[B] int) 
    insert tb 
    select 'bcd',1 union all 
    select 'bcde',2 union all 
    select 'cd',3 
    goselect 
    substring(a.A,b.number,1) as a,a.b
    from tb a,
    master..spt_values b
    where b.type='P' and b.number>0
    and b.number<=len(a.a) /**
    a    b           
    ---- ----------- 
    b    1
    c    1
    d    1
    b    2
    c    2
    d    2
    e    2
    c    3
    d    3(所影响的行数为 9 行)
    **/
      

  6.   

    if object_id('tempdb.dbo.#TB') is not null drop table #TB 
    go 
    create table #TB([A] varchar(10),[B] int) 
    insert #TB 
    select 'bcd',1 union all 
    select 'bcde',2 union all 
    select 'cd',3 
    select 
        substring(a.[A],b.number+1,1)[A],
        [B]
    from #TB a,master..spt_values b
    where b.type='P' and b.number<len(a.[A])
    /*
    A    B
    ---- -----------
    b    1
    c    1
    d    1
    b    2
    c    2
    d    2
    e    2
    c    3
    d    3(9 個資料列受到影響)
    */
      

  7.   

    if object_id('tempdb.dbo.#TB') is not null drop table #TB 
    go 
    create table #TB([A] varchar(4),[B] int) 
    insert #TB 
    select 'bcd',1 union all 
    select 'bcde',2 union all 
    select 'cd',3 SELECT TOP 100 ID=IDENTITY(INT,1,1) INTO #NUM FROM SYS.SYSCOLUMNS A,SYS.SYSCOLUMNS B
    SELECT 
        A.b,[VALUE]=SUBSTRING(A.a,B.ID,1) 
    FROM 
        #TB A,#NUM B
    WHERE
        b.id<=len(a.a)
    /*
    b           VALUE
    ----------- -----
    1           b
    1           c
    1           d
    2           b
    2           c
    2           d
    2           e
    3           c
    3           d
      

  8.   

    if object_id('tempdb.dbo.#TB') is not null drop table #TB 
    go 
    create table #TB([A] varchar(4),[B] int) 
    insert #TB 
    select 'bcd',1 union all 
    select 'bcde',2 union all 
    select 'cd',3 SELECT TOP 100 ID=IDENTITY(INT,1,1) INTO #NUM FROM SYS.SYSCOLUMNS A,SYS.SYSCOLUMNS B 
    SELECT 
        A.b,[VALUE]=SUBSTRING(A.a,B.ID,1) 
    FROM 
        #TB A,#NUM B 
    WHERE 
        b.id <=len(a.a) 
    /* 
    b          VALUE 
    ----------- ----- 
    1          b 
    1          c 
    1          d 
    2          b 
    2          c 
    2          d 
    2          e 
    3          c 
    3          d