请教大家一下,我的数据表有一百列,要对所有列求累加有什么简单的SQL语句吗?我现在是
select sum(列1) as 列1,sum(列2) as 列2,.......,sum(列100) as 列100 form 数据表 where......
太痛苦了,请问有简单的SQL语句可以完成所有列求累加的功能吗?

解决方案 »

  1.   

    create table tb(col1 int,col2 int)
    insert into tb values(1 , 2)
    insert into tb values(3 , 4)
    godeclare @output varchar(8000)
    select @output = coalesce(@output + ',' , '') + 'sum(' + name + ') sum_' + name from (select name from syscolumns where id = object_id('tb')) t
    exec ('select ' + @output + ' from tb' )drop table tb/*
    sum_col1    sum_col2    
    ----------- ----------- 
    4           6
    */