select * from syscolumns A ,sysobjects B where A.id=B.id and B.name='表一'
select A.* from syscolumns A ,sysobjects B where A.id=B.id and B.name='表一' 这样就是所以的表一的字段了。
这需要看你使用的是什么数据库了! Oracle select decode(id,'c1','c1','c2','c2'...) from 表1 into 表2 SQL server select case id when c1 then c1 end, case id when c2 then c2 end, ... from 表1 into 表2
牛人的;create table #表一(FLH1 varchar(100),FLH2 varchar(100)) insert #表一 values('A','Q2') insert #表一 values('B121','8823') insert #表一 values('BQ99','4434C') insert #表一 values('GI12','EE242')declare @参数 int set @参数=3select *,0 flag1,0 flag2 into #临时表 from #表一declare @a int,@b int select @a=0,@b=1update #临时表 set @b=case when @a<@参数 then @b else @b+1 end,@a=case when @a<@参数 then @a+1 else 1 end,flag1=@a,flag2=@b declare @sql varchar(8000) set @sql = 'select ' select @sql = @sql + 'max(case flag1 when '''+cast(flag1 as varchar(10))+''' then FLH1 end) [FLH1'+cast(flag1 as varchar(10))+'],max(case flag1 when '''+cast(flag1 as varchar(10))+''' then FLH2 end) [FLH2'+cast(flag1 as varchar(10))+'],' from (select distinct flag1 from #临时表) a set @sql=left(@sql,len(@sql)-1) exec(@sql+' from #临时表 group by flag2') go go drop table #表一,#临时表 (MSSQL MVP 大力)
from table
你想办法找人把我的贴子给解决了,
就能让你用纯SQL代码解决这个问题了,http://expert.csdn.net/Expert/topic/2146/2146114.xml?temp=.2632868
这样就是所以的表一的字段了。
Oracle
select decode(id,'c1','c1','c2','c2'...) from 表1 into 表2
SQL server
select
case id when c1 then c1 end,
case id when c2 then c2 end,
...
from 表1
into 表2
insert #表一 values('A','Q2')
insert #表一 values('B121','8823')
insert #表一 values('BQ99','4434C')
insert #表一 values('GI12','EE242')declare @参数 int
set @参数=3select *,0 flag1,0 flag2 into #临时表 from #表一declare @a int,@b int
select @a=0,@b=1update #临时表 set @b=case when @a<@参数 then @b else @b+1 end,@a=case when @a<@参数 then @a+1 else 1 end,flag1=@a,flag2=@b
declare @sql varchar(8000)
set @sql = 'select '
select @sql = @sql + 'max(case flag1 when '''+cast(flag1 as varchar(10))+''' then FLH1 end) [FLH1'+cast(flag1 as varchar(10))+'],max(case flag1 when '''+cast(flag1 as varchar(10))+''' then FLH2 end) [FLH2'+cast(flag1 as varchar(10))+'],'
from (select distinct flag1 from #临时表) a
set @sql=left(@sql,len(@sql)-1)
exec(@sql+' from #临时表 group by flag2')
go
go
drop table #表一,#临时表
(MSSQL MVP 大力)