是这样的,我有一个SQL字段
他是这样的结构。type
3
4
3
1
1
1
4
2数字的个数和内容都是不固定的。如何提取出这行字段中的不同参数形成一个数组呢?也就是例子中最后形成一个a{3,4,1,2}数组

解决方案 »

  1.   

    select distinct type from tb
      

  2.   

    declare @s varchar(1000)
    select @s=isnull(@s+',','')+ltrim(type)from
    (select distinct type from tb)t
    select @s
      

  3.   

    select distinct type from tb
      

  4.   


    ---------------------------------
    --  Author: htl258(Tony)
    --  Date  : 2009-08-14 09:36:03
    ---------------------------------
    --> 生成测试数据表:tbIf not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([type] int)
    Insert tb
    Select 3 union all
    Select 4 union all
    Select 3 union all
    Select 1 union all
    Select 1 union all
    Select 1 union all
    Select 4 union all
    Select 2
    Go
    --Select * from tb-->SQL查询如下:
    ;with t as 
    (
        select rn=row_number()over(order by getdate()),*
        from tb
    ),t1 as
    (
    select type
    from t a
    where not exists(
    select 1
    from t
    where type=a.type 
    and rn<a.rn)
    )
    select stuff((select ','+ltrim(type) from t1 for xml path('')),1,1,'') as type/*
    type
    ----------------------
    3,4,1,2(1 行受影响)*/
      

  5.   


    create table test(type int)
    insert into test
    select 3 union all
    select 4 union all
    select 3 union all
    select 1 union all
    select 1 union all
    select 4 union all
    select 2 goselect distinct(type) from testtype        
    ----------- 
    1
    2
    3
    4(所影响的行数为 4 行)
      

  6.   

    select top 1 stuff((select ','+ltrim([Type]) from aa group by [Type] for xml path('')),1,1,'') from aa
      

  7.   


    select top 1 stuff((select ','+ltrim([Type]) from 表 group by [Type] for xml path('')),1,1,'') from 表
      

  8.   

    DECLARE @VARCHAR VARCHAR(20)
    SELECT @VARCHAR=ISNULL(@VARCHAR+',','')+LTRIM(TYPE) FROM (SELECT DISTINCT * FROM TB)AS T
    SELECT @VARCHAR