sql循环查处多张表,如何连接到一张表内

来源:百度知道 编辑:UC知道 时间:2024/06/22 01:28:11
sql循环查处多张表,表的结构相同,如何将查询出的表连接到一张表内:例如:
@beginMonth int(5),
@endMonth int(5)
as
while( @beginMonth<=@endMonth)
begin
select 。。。。。。。。。。。。。
end

declare @s varchar(2000)
...
set @s=''
while @beginmonth<=@endmonth
begin
select @s=@s+'select .... union all '
end
select @s=substring(@s,1,len(@s)-10)
execute @s

while( @beginMonth<=@endMonth)
begin
select 。。。。。。。。。。。。。

select ... from ...
union all
select ... from ...
union all
select ... from ...
......

多个表的名字如果.有没有什么相关连处?

如果没有..就只得去系统表中取表名了..复杂很多...

或者用union

如果有..那么就可以用while数据来做..

等下我来看回复..如果是我说的第二种情况..请提供表名..

select ... from ...
union all
select ... from ...
union all
select ... from ...