declare @s varchar(8000)
set @s=''
select @s=@s+','+cast(id as varchar)
table where id=1 or id=2 or id=3 
select 结果=substring(@s,2,8000)

解决方案 »

  1.   

    declare @Ids varchar(300)
    set @Ids=''
    select @Ids=@Ids+id+',' from 
    (
    select 1 as id
    union all
    select 2 as id
    union all
    select 3 as id
    ) as x left join table1 t
    on x.id=t.id
    where t.id is nullif @ids<>''
       set @ids=left(@ids,len(@ids)-1)print @ids
      

  2.   

    declare @str varchar(1000)
    set @str=''select @str=@str+','+cast(ID as varchar)
    from Table
    where not(id=1 or id=2 or id=3)select @str
      

  3.   

    declare @str varchar(1000)
    set @str=''select @str=@str+','+cast(ID as varchar)
    from Table
    where not(id=1 or id=2 or id=3)select substring(@str,2,8000)
      

  4.   

    更正:declare @Ids varchar(300)
    set @Ids=''
    select @Ids=@Ids+cast(id as varchar(10))+',' from 
    (
    select 1 as id
    union all
    select 2 as id
    union all
    select 3 as id
    ) as x left join table1 t
    on x.id=t.id
    where t.id is nullif @ids<>''
       set @ids=left(@ids,len(@ids)-1)print @ids
      

  5.   

    zjcxc(: 邹建 :) 和progress99(如履薄冰) 的方法都不对
    因为这样会出来很多不需要的数据,这样会拉出来我查询条件中没有但是表中有的所有数据。
    CCEO(CSDN的CEO??) 的方法不好实现!?