日期:2014-05-19  浏览次数:20855 次

数据是按月分别存人不同的表,如果指定的查询时间段参数跨两个月,这样就要在两个表中查询,有什么好办法可以动态指定查询相关的两个表

table_01----保存1月的数据
table_02----保存2月的数据
table_03----保存3月的数据
table_04----保存4月的数据
table_05----保存5月的数据
table_06----保存6月的数据
table_07----保存7月的数据
table_08----保存8月的数据
table_09----保存9月的数据
table_10----保存10月的数据
table_11----保存11月的数据
table_12----保存12月的数据
参数为:
@stime= '2007-03-28 '---开始时间
@etime= '2007-04-02 '---结束时间
有什么好办法可以动态指定要查询的两个表

------解决方案--------------------
万一是3个月呢?
------解决方案--------------------
你可以把表名当作参数 然后遍历(开始时间----带结束时间)的月份来查不同的表
------解决方案--------------------
@stime= '2007-03-28 '---开始时间
@etime= '2007-04-02 '---结束时间
根据这两个参数来判是哪些表
------解决方案--------------------
数据不多的话,直接这样:
select * from
(
select * from table_01
union
select * from table_02
...
..
.
union
select * from table_12
) as alltable
where ....

------解决方案--------------------
string SQL= "select * from( ";
for(int i=DateTime.Pase(stime).Month;i <=DateTime.Pase(etime).Month;i++)
{
if(i!=DateTime.Pase(etime).Month)
{
SQL+= " select * from table_ "+i.ToString( "0# ")+ " union ";
}
else
{
SQL+= " select * from table_ "+i.ToString( "0# ")+ " ) where ......... ";
}
}