用union all
select sum(aa.s1) from (
select * from 表1
union all
select * form 表2
...
) aa
where ypdm='52' and ysdm='4902'
select sum(aa.s1) from (
select * from 表1
union all
select * form 表2
...
) aa
where ypdm='52' and ysdm='4902'
from MZ_F040101
where ypdm='52' and ysdm='4902'把表名称做变量传进去啊
as
--declare @begindate varchar(6), @enddate varchar(6)
declare @now varchar(6), @str varchar(8000)
set @begindate='040101'
set @enddate='040131'
set @now=@begindate
set @str=''
while @now<=@enddate
begin
set @str=@str+'union select sum(sl) as sumall from MZ_F'+@now+' where ypdm=''52'' and ysdm=''4902'''
set @now=right(convert(varchar(8),dateadd(d,1,convert(datetime, @now)),112),6)
end
set @str='select sum(sumall) from ('+stuff(@str,1,6,'')+') a'
exec(@str)
--未测试,语句应该没有问题,如果还要判断where语句,可以考虑再添加两个变量
set @enddate='040131'
这两个是我测试的时候用的,忘了注释了,你注释掉吧,否则存储过程参数传不进去