select 'select '+stuff(( select '+'',''+convert(varchar,'+a.name+')' from sys.columns a,sys.tables b where a.object_id=b.object_id and b.name='表名' for xml path('')),1,5,'') +' from 表名 '这样?
那你就select c1+c2+c3...+c66,或者上面的改改declare @sql varchar(max) select @sql='select '+stuff(( select '+isnull('+a.name+',0)' from sys.columns a,sys.tables b where a.object_id=b.object_id and b.name='表名' for xml path('')),1,1,'') +' from 表名 ' exec (@sql)
C1到C66数值型,数值合计还要改这句,得到的结果我要SELECT A,B,SUM(C1),SUM(C2)...SUM(66) FROM 表名 这样能合计出一行出来
这样的字段,数据有多行,
想得到的结果是一行数据,也是上面这些字段,但C1-C66字段值要合计
select 'select '+stuff((
select '+'',''+convert(varchar,'+a.name+')'
from sys.columns a,sys.tables b where a.object_id=b.object_id and
b.name='表名' for xml path('')),1,5,'') +' from 表名 '这样?
select @sql='select '+stuff((
select '+isnull('+a.name+',0)'
from sys.columns a,sys.tables b where a.object_id=b.object_id and
b.name='表名' for xml path('')),1,1,'') +' from 表名 '
exec (@sql)