这样如何进行查询呢?
假设数据库为每天纪录一个新表
我想查询连续两三天的数据,如何写查询语句呢?
------解决方案----------------------今天
declare @tablename1 as varchar(11)
set @tablename1 = '[f ' + convert(varchar(10),getdate(),120)
--昨天
declare @tablename2 as varchar(11)
set @tablename2 = '[f ' + convert(varchar(10),getdate()-1,120)
--查询
declare @sql varchar(4000)
set @sql = 'select * from ' + @tablename1
set @sql = ' union all '
set @sql = @sql + 'select * from ' + @tablename2
EXEC(@sql)
------解决方案--------------------create table tb20070325 (id int)
insert into tb20070325(id)
select 1
create table tb20070326 (id int)
insert into tb20070325(id)
select 2
create table tb20070327 (id int)
insert into tb20070325(id)
select 3
declare @sql nvarchar(1000)
set @sql = 'select id from tb ' + convert(char(8),getdate(),112) + ' union '
+ 'select id from tb ' + convert(char(8),dateadd(dd,-1,getdate()),112) + ' union '
+ 'select id from tb ' + convert(char(8),dateadd(dd,-2,getdate()),112)
exec sp_executesql @sql
drop table tb20070325
drop table tb20070326
drop table tb20070327