如题,每天产生一张表,但是表里只有时间字段,没有日期字段。直接用日期来命名的表。比如GSM_20120201,GSM_20120202,GSM_20120203..........根据下面的代码完全能查询出符合条件的信息。就是没有日期的显示,这几天的时间都列出来了。代码如下
declare @dt1 datetime,@dt2 datetime
declare @cellname varchar(30)
declare @sql nvarchar(4000)
set @dt1='2012-01-01'
set @dt2='2012-03-07'
set @cellname='nanliangAG1-1'
select @sql=isnull(@sql+' union all ','')
+'select * from '
+[name]
+' where cell_name like '''+@cellname+'%'''
from sysobjects
where type='U'
and [name] in (select [name] from (
select 'gsm_'+right(convert(varchar(8),dateadd(d,number,@dt1),112),8) as [name]
from master..spt_values
where type='p' and dateadd(d,number,@dt1)<=@dt2)t)
insert into temp exec(@sql)
select * from temp
select @sql
查询出来的是这样的。我想把每一张表的日期标注上,比如从20120104的表,从20120105的表,从201220120105的表。就是把那个表的后缀给加到一个临时字段上显示出来。
时间 小区 代码
2.0 nanliangAG1-1 2708
3.0 nanliangAG1-1 2708
4.0 nanliangAG1-1 2708
5.0 nanliangAG1-1 2708
6.0 nanliangAG1-1 2708
7.0 nanliangAG1-1 2708
8.0 nanliangAG1-1 2708
9.0 nanliangAG1-1 2708
10.0 nanliangAG1-1 2708
11.0 nanliangAG1-1 2708
12.0 nanliangAG1-1 2708
2.0 nanliangAG1-1 2708
3.0 nanliangAG1-1 2708
4.0 nanliangAG1-1 2708
5.0 nanliangAG1-1 2708
就是想显示成这样子
日期时间 时间 小区 代码
20120102 2.0 nanliangAG1-1 2708
20120102 3.0 nanliangAG1-1 2708
20120102 4.0 nanliangAG1-1 2708
20120102 5.0 nanliangAG1-1 2708
20120102 6.0 nanliangAG1-1 2708
20120102 7.0 nanliangAG1-1 2708
20120102 8.0 nanliangAG1-1 2708
20120102 9.0 nanliangAG1-1 2708
20120102 10.0 nanliangAG1-1 2708
20120102 11.0 nanliangAG1-1 2708
20120102 12.0 nanliangAG1-1 2708
20120103 2.0 nanliangAG1-1 2708
20120103 3.0 nanliangAG1-1 2708
20120103 4.0 nanliangAG1-1 2708
20120103 5.0 nanliangAG1-1 2708
20120103 6.0 nanliangAG1-1 2708
20120103 7.0 nanliangAG1-1 2708
20120103 8.0 nanliangAG1-1 2708
20120103 9.0 nanliangAG1-1 2708
20120103 10.0 nanliangAG1-1 2708
该怎么添加临时日期字段呢,谢谢,在线等。
declare @dt1 datetime,@dt2 datetime
declare @cellname varchar(30)
declare @sql nvarchar(4000)
set @dt1='2012-01-01'
set @dt2='2012-03-07'
set @cellname='nanliangAG1-1'
select @sql=isnull(@sql+' union all ','')
+'select '''+right([name],8)+''' as data,* from '
+[name]
+' where cell_name like '''+@cellname+'%'''
from sysobjects
where type='U'
and [name] in (select [name] from (
select 'gsm_'+right(convert(varchar(8),dateadd(d,number,@dt1),112),8) as [name]
from master..spt_values
where type='p' and dateadd(d,number,@dt1)<=@dt2)t)
insert into temp exec(@sql)
select * from temp
select @sql