有一个表,如下:表一:
Hours(Field)02
03
05
12
24通过Select语句转变成为表二样式,如下:
表二:
02   03   05   12   24(Field)简单来说就是把表一的数据作为表二的字段,请各位帮忙!Tks!

解决方案 »

  1.   

    declare @t table(Hours varchar(4))
    insert into @t select '02' union select '03' union select '05' union select '12' union select '24'declare @s varchar(8000)
    select @s=isnull(@s,'')+','''+Hours+'''' from @t
    set @s='select '+stuff(@s,1,1,'')
    exec(@s)/*
    ---- ---- ---- ---- ---- 
    02   03   05   12   24
    */
      

  2.   

    create table ta(id varchar(2))
    insert ta
    select '02' union all
    select '03'union all
    select '05'union all
    select '12'union all
    select '24'declare @sql varchar(1000)
    set @sql=''
    select @sql=@sql+'  '+id from ta
    select ltrim(@sql)
                                                                                                                                                                                                                                                                     
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    02  03  05  12  24(所影响的行数为 1 行)
      

  3.   

    表一的数据不固定呀
    -------------------------------------------------------------------------
    动态SQL,没有固定表一的数据,只不过用示例数据做测试。