select * from 地区_200801 union all select * from 地区_200802 union all select * from 地区_200803 ... union all select * from 地区_200809
use kq; declare @begin datetime,@end datetime declare @i int,@j int,@sql varchar(8000) set @begin='2008-01-01' set @end='2008-09-01' set @sql='' set @i=year(@begin) while @i<=year(@end) begin if @i=year(@begin) set @j=month(@begin) else set @j=1 while @j<=12 begin set @sql=@sql+' select * from 地区_'+rtrim(@i)+right(rtrim(100+@j),2)+' union all' if @i=year(@end) and @j=month(@end) break set @j=@j+1 end set @i=@i+1 end set @sql=left(@sql,len(@sql)-10) exec(@sql)
declare @begin datetime,@end datetime declare @i int,@j int,@sql varchar(8000) set @begin='2008-01-01' set @end='2008-09-01' set @sql='' set @i=year(@begin) while @i<=year(@end) begin if @i=year(@begin) set @j=month(@begin) else set @j=1 while @j<=12 begin set @sql=@sql+' select * from 地区_'+rtrim(@i)+right(rtrim(100+@j),2)+' union all' if @i=year(@end) and @j=month(@end) break set @j=@j+1 end set @i=@i+1 end set @sql=left(@sql,len(@sql)-10) exec(@sql)
Declare @i int,@SQL varchar(8000) SET @i=1 SET @SQL='' WHILE @i<9 BEGIN SET @SQL=@SQL+' SELECT * FROM 地区_20080'+cast(@i as varchar(2))+' UNION ALL' SET @i=@i+1 ENDSET @SQL=@SQL+' SELECT * FROM 地区_20080'+cast(@i as varchar(2))EXEC(@SQL)
use kq; declare @begin datetime,@end datetime declare @i int,@j int,@sql varchar(8000) set @begin='2008-01-01' set @end='2008-09-01' set @sql='' set @i=year(@begin) while @i<=year(@end) begin if @i=year(@begin) set @j=month(@begin) else set @j=1 while @j<=12 begin set @sql=@sql+' select * from 地区_'+rtrim(@i)+right(rtrim(100+@j),2)+' union all' if @i=year(@end) and @j=month(@end) break set @j=@j+1 end set @i=@i+1 end set @sql=left(@sql,len(@sql)-10) --print @sql exec(@sql)--如下是执行print后打印出的结果,其实用exec执行就可以 select * from 地区_200801 union all select * from 地区_200802 union all select * from 地区_200803 union all select * from 地区_200804 union all select * from 地区_200805 union all select * from 地区_200806 union all select * from 地区_200807 union all select * from 地区_200808 union all select * from 地区_200809
SELECT * FROM 地区_200801 UNION ALL SELECT * FROM 地区_200802 UNION ALL SELECT * FROM 地区_200803 UNION ALL SELECT * FROM 地区_200804 UNION ALL SELECT * FROM 地区_200805 UNION ALL SELECT * FROM 地区_200806 UNION ALL SELECT * FROM 地区_200807 UNION ALL SELECT * FROM 地区_200808 UNION ALL SELECT * FROM 地区_200809
union all
select * from 地区_200802
union all
select * from 地区_200803
...
union all
select * from 地区_200809
use kq;
declare @begin datetime,@end datetime
declare @i int,@j int,@sql varchar(8000)
set @begin='2008-01-01'
set @end='2008-09-01'
set @sql=''
set @i=year(@begin)
while @i<=year(@end)
begin
if @i=year(@begin) set @j=month(@begin)
else set @j=1
while @j<=12
begin
set @sql=@sql+' select * from 地区_'+rtrim(@i)+right(rtrim(100+@j),2)+' union all'
if @i=year(@end) and @j=month(@end) break
set @j=@j+1
end
set @i=@i+1
end
set @sql=left(@sql,len(@sql)-10)
exec(@sql)
declare @i int,@j int,@sql varchar(8000)
set @begin='2008-01-01'
set @end='2008-09-01'
set @sql=''
set @i=year(@begin)
while @i<=year(@end)
begin
if @i=year(@begin) set @j=month(@begin)
else set @j=1
while @j<=12
begin
set @sql=@sql+' select * from 地区_'+rtrim(@i)+right(rtrim(100+@j),2)+' union all'
if @i=year(@end) and @j=month(@end) break
set @j=@j+1
end
set @i=@i+1
end
set @sql=left(@sql,len(@sql)-10)
exec(@sql)
SET @i=1
SET @SQL=''
WHILE @i<9
BEGIN
SET @SQL=@SQL+' SELECT * FROM 地区_20080'+cast(@i as varchar(2))+' UNION ALL'
SET @i=@i+1
ENDSET @SQL=@SQL+' SELECT * FROM 地区_20080'+cast(@i as varchar(2))EXEC(@SQL)
declare @begin datetime,@end datetime
declare @i int,@j int,@sql varchar(8000)
set @begin='2008-01-01'
set @end='2008-09-01'
set @sql=''
set @i=year(@begin)
while @i<=year(@end)
begin
if @i=year(@begin) set @j=month(@begin)
else set @j=1
while @j<=12
begin
set @sql=@sql+' select * from 地区_'+rtrim(@i)+right(rtrim(100+@j),2)+' union all'
if @i=year(@end) and @j=month(@end) break
set @j=@j+1
end
set @i=@i+1
end
set @sql=left(@sql,len(@sql)-10)
--print @sql
exec(@sql)--如下是执行print后打印出的结果,其实用exec执行就可以
select * from 地区_200801 union all select * from 地区_200802 union all select * from 地区_200803 union all select * from 地区_200804 union all select * from 地区_200805 union all select * from 地区_200806 union all select * from 地区_200807 union all select * from 地区_200808 union all select * from 地区_200809
SELECT * FROM 地区_200801 UNION ALL SELECT * FROM 地区_200802 UNION ALL
SELECT * FROM 地区_200803 UNION ALL SELECT * FROM 地区_200804 UNION ALL
SELECT * FROM 地区_200805 UNION ALL SELECT * FROM 地区_200806 UNION ALL SELECT * FROM 地区_200807
UNION ALL SELECT * FROM 地区_200808 UNION ALL SELECT * FROM 地区_200809