select identity(int,1,1)xh,* into #t from 表
select * from #t a left join #t b on a.xh=1 and b.xh=2

解决方案 »

  1.   

    create table test (itemname varchar(100),itemresult varchar(100))
    insert into test values('标题','生化报告单')
    insert into test values('建议','随访')declare @sql varchar(1000),@i int
    select @sql='select convert(varchar,getdate(),111) as time',@i=0
    select @i=@i+1,@sql=@sql+',max(case itemname when '''+convert(varchar(100),itemname)+''' then itemname else ''0'' end) [itemname'+cast(@i as varchar(10))+'],max(case itemresult when '''+convert(varchar(100),itemresult)+''' then itemresult else ''0'' end) [itemresult'+cast(@i as varchar(10))+']' from testselect @sql=@sql+' into #a from test'
    print @sql
    exec(@sql+' select * from #a')
      

  2.   

    你的数据如果不确定的话,就要用动态生成SQL的方法select id=identity(int,1,1),itemname,itemresult into #temp from 你的表declare @i int,@ich varchar(30),@sql varchar(8000)
    select @sql='',@i=max(id) from #tempwhile @i>0
      select @ich=cast(@i as varchar(30))
        ,@sql=',max(case id when '+@ich+' then itemresult end) as itemresult'+@ich
        +char(13)+',max(case id when '+@ich+' then itemname end) as itemname'+@ich
        +char(13)+@sql
        ,@i=@i-1
    set @sql='select '+right(@sql,len(@sql)-1)+' from #temp'
    exec (@sql)
      

  3.   

    http://search.csdn.net/expert/topic/57/5704/2002/11/27/1211043.htm
      

  4.   

    同意zjcxc(邹建) 
    先创建动态序列号,再用该序列值做为标识 循环
      

  5.   

    用此方法.记录过多就失效,变量@SQL不能超过8000字符
      

  6.   

    btw,我怎么结不了贴呀?给分的密码是仕么?