select * top 3 from t_a
order by asc

解决方案 »

  1.   

    declare @2 table (y int,b char(10))
    insert @2 values (12,'a')
    insert @2 values (12,'d')
    insert @2 values (3,'c')
    insert @2 values (44,'d')
    insert @2 values (44,'d')
    insert @2 values (454,'d')declare @Temp As varchar(1000)    
    Declare @tmp table (y int,b Varchar(1000))    
    Declare @i VarChar(8000)    --存放查询表中的“y”字段值
    Declare @len int
    Declare @j int --用于存放上一次出现逗号的位置
    Set @i=''select @i=@i+ltrim(rtrim(cast(a.y as char)))+',' From
    (Select distinct y from @2) aSet @len=1
    Set @j=charindex(',',@i,0)
    select @i=@i+'Why,'While @j<=len(@i)-2
    begin
    set @Temp=''
    Select @len=@j-@lenSelect @Temp=@Temp+rtrim(ltrim(a.b))+',' From @2 a 
    Where y=cast(right(left(@i,@j-1),@len) as int)insert @tmp values (cast(right(left(@i,@j-1),@len) as int),@temp)set @len=@j+1
    Set @j=charindex(',',@i,@len)end 
    select * from @tmp
      

  2.   

    to luohzad
        你把以上代码全部复制到SQL查询分析器中运行看看,如果是类似你想要的结果,就麻烦你再根据自己的要求修改吧。
      

  3.   

    如果不用逗号分隔,则用以下代码
    declare @2 table (y int,b char(10))
    insert @2 values (12,'a')
    insert @2 values (12,'d')
    insert @2 values (3,'c')
    insert @2 values (44,'d')
    insert @2 values (44,'d')
    insert @2 values (454,'d')declare @Temp As varchar(1000)    
    Declare @tmp table (y int,b Varchar(1000))    
    Declare @i VarChar(8000)    --存放查询表中的“y”字段值
    Declare @len int
    Declare @j int --用于存放上一次出现逗号的位置
    Set @i=''select @i=@i+ltrim(rtrim(cast(a.y as char)))+',' From
    (Select distinct y from @2) aSet @len=1
    Set @j=charindex(',',@i,0)
    select @i=@i+'Why,'While @j<=len(@i)-2
    begin
    set @Temp=''
    Select @len=@j-@lenSelect @Temp=@Temp+rtrim(ltrim(a.b)) From @2 a 
    Where y=cast(right(left(@i,@j-1),@len) as int)insert @tmp values (cast(right(left(@i,@j-1),@len) as int),@temp)set @len=@j+1
    Set @j=charindex(',',@i,@len)end 
    select * from @tmp
      

  4.   

    谢谢 yiyanFengZi(依伊尘埃) 的答复...
      

  5.   

    to   yiyanFengZi(依伊尘埃)
       结果没有什么大问题..
       在有' , ' 的这种情况下最后会多出一次逗号....
       y       b
    ______________________________
        3     c,
       12     a,d,
       44     d,d, 
      454     d,
      
    如果是下面的结果就更加的完美了
       y       b
    ______________________________
        3     c
       12     a,d
       44     d,d 
      454     d