要从第三列起,还有每一列的字段名都不同呀:( 编号 单价 we rt u ty g j …… 9.07 178.00 0 0 1 2.0 1.0 0 …… 9.09 11.00 1 0 2 3.0 4.0 2 …… 最后要得出: 编号 单价 we rt u ty g j 和1 和2 9.07 178.00 0 0 1 2.0 1.0 0 2 2 9.09 11.00 1 0 2 3.0 4.0 2 7 5
和1 和2怎么来的? 要从第三列起 SELECT SUM(字段名) FROM #TEMP WHERE (ID MOD 2)<>0 and id>=3 SELECT SUM(字段名) FROM #TEMP WHERE (ID MOD 2)=0 and id>=3
临时表里的列会动态增加 alter table #temp add id int identity(1,1)
以下字段为字符型select identity(int,1,1) i,name into #temp from syscolumns where id=object_id('tablename')declare @a varchar(500) --可根据表大小决定长度 declare @b varchar(500) declare @str varchar(500) select @a=@a+name+'+' from #temp where i>2 and (id%2)=1 set @a=substring(@a,1,len(@a-1)) select @b=@b+name+'+' from #temp where i>2 and (id%2)=0 set @b=substring(@b,1,len(@b-1)) set @str='select field1,field2 '+@a+' '+@b+' from tablename' exec(@str)
编号 单价 we rt u ty g j ……
9.07 178.00 0 0 1 2.0 1.0 0 ……
9.09 11.00 1 0 2 3.0 4.0 2 ……
最后要得出:
编号 单价 we rt u ty g j 和1 和2
9.07 178.00 0 0 1 2.0 1.0 0 2 2
9.09 11.00 1 0 2 3.0 4.0 2 7 5
要从第三列起
SELECT SUM(字段名) FROM #TEMP WHERE (ID MOD 2)<>0 and id>=3
SELECT SUM(字段名) FROM #TEMP WHERE (ID MOD 2)=0 and id>=3
alter table #temp add id int identity(1,1)
declare @b varchar(500)
declare @str varchar(500)
select @a=@a+name+'+' from #temp where i>2 and (id%2)=1
set @a=substring(@a,1,len(@a-1))
select @b=@b+name+'+' from #temp where i>2 and (id%2)=0
set @b=substring(@b,1,len(@b-1))
set @str='select field1,field2 '+@a+' '+@b+' from tablename'
exec(@str)