我有一个表,结构如下h   name
2   小
3   大
4   中
...
请问如何将上表中的记录变成字段方式?小弟初学,谢谢,分不多

解决方案 »

  1.   

    declare @str varchar(2000)
    select @str=''select @str=@str + name + ',' from 表
    select @str=left(@str,len(@str) -  1)
    print @str
      

  2.   

    create table kvyd
    (
    h  int ,
    name varchar(10)) insert into kvyd
    select 2 ,  '小' union all select
    3  , '大' union all select
    4 ,  '中'select max(case name when '小' then h else '' end) as 小,
    max(case name when '大' then h else '' end) as 大,
    max(case name when '中' then h else '' end) as 中
    from kvyd
    --------------------
    小        大       中
    2 3 4
      

  3.   

    create table # (
    id int,
    name varchar(20))insert #
    select 1,'jack'
    union all
    select 2,'tom'
    union all
    select 3,'terry'declare @str varchar(2000)
    select @str=''
    select @str = @str + name + ',' from #
    select @str = left(@str,len(@str) - 1 )
    print @str
    drop table #
    --------------------------(所影响的行数为 3 行)jack,tom,terry