create function F_test(@ID int)
returns nvarchar(1000)
as
begin
declare @s nvarchar(1000) 
select @s=isnull(@s+',','')+web2 from T where web1=@ID
return @s
end
go
select
distinct 
web1
dbo.f_test(web1)as web2
from 
t

解决方案 »

  1.   

    -- 1. 创建处理函数CREATE FUNCTION dbo.f_str(@id int)RETURNS varchar(8000)ASBEGIN    DECLARE @r varchar(8000)    SET @r = ''    SELECT @r = @r + ',' + value    FROM tb    WHERE id=@id    RETURN STUFF(@r, 1, 1, '')ENDGO -- 调用函数SELECt id, values=dbo.f_str(id) FROM tb GROUP BY id -- 2. 新的解决方法 -- 示例数据DECLARE @t TABLE(id int, value varchar(10))INSERT @t SELECT 1, 'aa'UNION ALL SELECT 1, 'bb'UNION ALL SELECT 2, 'aaa'UNION ALL SELECT 2, 'bbb'UNION ALL SELECT 2, 'ccc' -- 查询处理SELECT *FROM(    SELECT DISTINCT         id    FROM @t)AOUTER APPLY(    SELECT         [values]= STUFF(REPLACE(REPLACE(            (                SELECT value FROM @t N                WHERE id = A.id                FOR XML AUTO            ), '<N value="', ','), '"/>', ''), 1, 1, ''))N
      

  2.   

    --如果数据不多declare @t table (web1 int,      web2  varchar(20))
    insert @t select
      1,           '12,23,35,36'
    union all select
      2,           '25' declare @sql varchar(8000)
    select @sql=isnull(@sql+' Union all ','')+' select '+rtrim(web1)+' as web1,'''+replace(web2,',',''' as web2 union all select '+rtrim(web1)+' as web1,''')+''''
    from @t
    exec(@sql)--结果
    eb1        web2 
    ----------- ---- 
    1           12
    1           23
    1           35
    1           36
    2           25
      

  3.   

    --本能反应,没看清楚。。哈哈
    用辅助表go
    create  table T(web1 int,      web2  varchar(20))
    insert T select
      1,           '12,23,35,36'
    union all select
      2,           '25' goselect 
    top 100
    id=identity(int,1,1)
    into #
    from 
    syscolumns a,syscolumns bgo
    select
    web1,web2=substring(web2,b.ID,charindex(',',web2+',',b.ID)-b.ID)
    from 
    T, # b 
    where
    charindex(',',','+web2,b.ID)=b.ID
    /*
    web1        web2                 
    ----------- -------------------- 
    1           12
    1           23
    1           35
    1           36
    2           25(所影响的行数为 5 行)
    */DECLARE @t TABLE(id int, [values] varchar(100))INSERT @t SELECT 1, 'aa,bb'UNION ALL SELECT 2, 'aaa,bbb,ccc'
    -- 查询处理SELECT     A.id, B.valueFROM(    SELECT id, [values] = CONVERT(xml,            '<root><v>' + REPLACE([values], ',', '</v><v>') + '</v></root>')    FROM @t)AOUTER APPLY(    SELECT value = N.v.value('.', 'varchar(100)')    FROM A.[values].nodes('/root/v') N(v))B
      

  4.   

    楼上的有点看不明白,能不能说明下吗```有点看头晕`````还有```表的数据比较多,表table 
    web1       web2                web3
      1           12,23,35,36,      001
      2           25                002要查到的结果 
      1           12     001
      1           23     001
      1           35     001
      1           36     001
      2           25     002
    ````````等
      

  5.   

    create     table   T(web1   int,             web2     varchar(20)) 
    insert   T   select 
        1,                       '12,23,35,36' 
    union   all   select 
        2,                       '25'   go select   
    top   100 
    id=identity(int,1,1) 
    into   # 
    from   
    syscolumns   a,syscolumns   b go 
    select 
    web1,web2=substring(web2,b.ID,charindex(',',web2+',',b.ID)-b.ID) 
    from   
    T, #   b   
    where 
    charindex(',',','+web2,b.ID)=b.ID 
      

  6.   

    web1,web2=substring(web2,b.ID,charindex(',',web2+',',b.ID)-b.ID) --改显示列
      

  7.   

    declare @t table (web1 int,      web2  varchar(20))
    insert @t select
      1,           '12,23,35,36'
    union all select
      2,           '25' declare @t1 table(id int,w1 int,w2 varchar(20))
    insert @t1 select 0,web1,web2+',' from @t
    while exists(select 1 from @t1 where id=0 and w2<>'')
    begin 
     insert @t1 select 1,w1,left(w2,charindex(',',w2)-1) 
                from @t1 where charindex(',',w2)>0 and id = 0
     update @t1 set w2=right(w2,len(w2)-charindex(',',w2)) 
    end
    select w1,w2 from @t1 where id = 1 order by w1
    /*
    w1          w2                   
    ----------- -------------------- 
    1           12
    1           23
    1           35
    1           36
    2           25
    */
      

  8.   


    declare @t table (web1 int,      web2  varchar(20))
    insert @t select
      1,           '12,23,35,36'   --数据量比较大啊```不单只是  '12,23,35,36'  后面还 3  32,37 等等
    union all select
      2,           '25'    
    web1               web2                                 web3 
        1                       12,23,35,36,                 001 
        2                       25                           002 
        3                       26,37                        003
      

  9.   


    Create table T(web1 int,web2 varchar(20),web3 varchar(20))
    insert into t(web1,web2,web3) 
    select 1,'12,23,35,36','001'
    union all
    select 2,'25','002'
    union all
    select 3,'26,27','003'--建立临时表#
    select top 100 id=Identity(int,1,1) into #t from sysobjects a,syscolumns bselect a.web1,web2=substring(web2,b.id,charindex(',',web2+',',b.id)-b.id),a.web3 from  T a,#t b
    where substring(','+web2,b.id,1)=','/*
    web1        web2                 web3
    ----------- -------------------- --------------------
    1           12                   001
    1           23                   001
    1           35                   001
    1           36                   001
    2           25                   002
    3           26                   003
    3           27                   003(7 行受影响)
    */
    drop table t
      

  10.   

    好想大家没明白我的意思```还有```表的数据比较多, 表table   
    web1               web2                                 web3 
        1                       12,23,35,36,             001 
        2                       25                       002 
        3                       26,89                    003 
        4                       37                       004 
    ```等等```还5 6 一直下的要查到的结果   
        1                       12           001 
        1                       23           001 
        1                       35           001 
        1                       36           001 
        2                       25           002 
    ````````等数据量比较大的,大家明白了吗``/*
    insert into t(web1,web2,web3) 
    select 1,'12,23,35,36','001'
    union all
    select 2,'25','002'
    union all
    select 3,'26,27','003'
    */不能这么定的,那例如我到了第1000行呢??
      

  11.   

    呵```是我看不明白吧``create     table   T(web1   int,             web2     varchar(20)) 
    insert   T   select 
        1,          '12,23,35,36'  
    --这里为什么要定上固定的值呢,那如果我这里列的值,那样怎么写呢??? 那insert   T   select 1 是什么意思呢??
    union   all   select 
        2,            '25'   
      

  12.   

    这个问题还没解决,谁能帮忙解答下呢??create           table       T(web1       int,                           web2           varchar(20))   
    insert       T       select   
            1,                     '12,23,35,36'     
    --这里为什么要定上固定的值呢,那如果我这里列的值,那样怎么写呢???   那insert       T       select   1   是什么意思呢?? 
    union       all       select   
            2,                         '25'