日期:2014-05-18 浏览次数:20406 次
--1:找到所有天 select CONVERT(varchar(12),DATEADD(DAY,number,'2012-05-31'),112) from master..spt_values where type='p' and number between 0 and 4 /* ------------ 20120531 20120601 20120602 20120603 20120604 (5 row(s) affected) */ --2:找到所有表 /*这块自己写,下面是个例子*/ select name from sysobjects where xtype='u' --截图表名称 和 天数匹配 and SUBSTRING(name,1,6) in ( --把之前找到得天数拿过来... select CONVERT(varchar(12),DATEADD(DAY,number,'2012-05-31'),112) from master..spt_values where type='p' and number between 0 and 4 ) --3: 根据找到的表 拼动态语句(下面的语句 要换成 第二块 找到的所有批次表) declare @sql varchar(max) select @sql=isnull(@sql,'') +' union all select * from '+CONVERT(varchar(12),DATEADD(DAY,number,'2012-05-31'),112) from master..spt_values where type='p' and number between 0 and 4 set @sql=STUFF(@sql,1,11,'') print @sql --exec(@sql) /* -------------------------------- select * from 20120531 union all select * from 20120601 union all select * from 20120602 union all select * from 20120603 union all select * from 20120604 */