2012-08-06 60 views
0

我正在声明一个表变量,如下所示。在SQL Server 2008中将值插入表变量

declare @TableVar table (interval Time not null) 

我需要插入从“上午08点”到“下午8点”与间隔的初始值,随着时间,的说30分钟,到这个表中的变量。

我需要数据的表的变量,如下

8:00 am 
8:30 am 
9:00 am 
. 
. 
. 
12:00 pm 
12.30 pm 
. 
. 
7:00 pm 
7:30 pm 
8:00 pm 

30分钟该时间间隔是从一个字段DMCDur(int)衍生自表。

DECLARE @intFlag INT 
SET @intFlag = (select D.DMCDur from doctor_master D where D.doc_id=3) 

基本上,我需要查询表以得到列DMCDur可以是:30,20,15等代表:30分钟,20分钟,15分钟,分别。

我需要设置的开始时间作为8:00 am和需要添加DMCDur与该开始时间和产生具有间隔为DMCDur一组时间和插入这些值到我上面提到的表变量。

我的最终目标是将这组时间与另一个表数据连接起来并填充网格。所以想到这样做。

请在这个示例存储过程建议你的想法相同将不胜感激。 注意:另一个表中的连接变量是一个DATETIME变量(eg 2012-08-06 08:00:00.000)。因此表变量也应该是DATETIME作为数据类型,这样我可以在这次连接两个表。

+0

如果DMCDur没有完全划分总时间段 - 如果最后一次早于或晚于8pm? – 2012-08-06 08:48:39

+0

我只需要把DMCDur的时间段作为时间间隔,上次应该早于8点。 – Soumya 2012-08-06 08:58:21

+0

最后解释请注意。 – danihp 2012-08-06 09:17:14

回答

0

试试这个:

DECLARE @intFlag INT 
SET @intFlag =30 

declare @start_time time='08:00:00' 
declare @end_time time='20:00:00' 
declare @t table (date_time datetime) 

insert into @t 
select DATEADD(mi,number*@intFlag,@start_time) as [time] 
from master..spt_values where type='p' 
and number<=12*(60/@intFlag) 

select * from @t 

EDIT1:按你的最后请注意:

select * from @t join <other table> 
on convert(time,date_time)=convert(time,<othertableColumn >) 
+0

感谢您的回答 – Soumya 2012-08-06 11:06:54

2

你应该使用一个排发电机。我用Itzik Ben Gan's Row Generator

create table #TableVar (interval Time not null) 

declare @elapsed int 
declare @from_time time, 
     @to_time time 
select @elapsed = 30, 
     @from_time = '08:00:00', 
     @to_time = '20:00:00' 

;WITH 
Nbrs_3(n) AS (SELECT 1 UNION SELECT 0), 
Nbrs_2(n) AS (SELECT 1 FROM Nbrs_3 n1 CROSS JOIN Nbrs_3 n2), 
Nbrs_1(n) AS (SELECT 1 FROM Nbrs_2 n1 CROSS JOIN Nbrs_2 n2), 
Nbrs_0(n) AS (SELECT 1 FROM Nbrs_1 n1 CROSS JOIN Nbrs_1 n2), 
Nbrs (n) AS (SELECT 1 FROM Nbrs_0 n1 CROSS JOIN Nbrs_0 n2), 
D (n) as (SELECT ROW_NUMBER() OVER (ORDER BY n) FROM Nbrs ), 
all_times as (
    SELECT 
    dateadd(minute, (n - 1) * @elapsed, @from_time) as [a_time] 
    FROM 
    D 
    where 
    n <= (12 * 60.0/@elapsed) + 1 
) 
insert 
into #TableVar 
select * from all_times 

结果:

;select * from #TableVar 


interval 
-------- 
08:00:00 
08:30:00 
09:00:00 
09:30:00 
... 
19:00:00 
19:30:00 
20:00:00 

*编辑*由于OP变化要求:

你可以施放日期时间,以时间来获得部分时间:

create table #dates (some_date dateTime not null) 

insert into #dates values 
('2012-01-01 07:30:00'), 
('2012-01-01 08:00:00'), 
('2012-01-01 08:30:00'), 
('2012-01-01 09:00:00'); 


select   d.*, t.* 
from    #dates d 
left outer join #TableVar t 
      on cast(d.some_date as time) = t.interval; 

结果:

some_date    interval 
-------------   -------- 
2012-01-01 07:30:000 _NULL_   
2012-01-01 08:00:000 8:00:00 
2012-01-01 08:30:000 8:30:00 
2012-01-01 09:00:000 9:00:00 
+0

感谢您的回答。我需要将这些数据与另一个表中的日期时间字段结合起来,就像'2012-08-06 08:00 :00.000'其中日期是当前日期。请在第一篇文章中遗漏此部分。我怎样才能将表变量作为日期时间与当前日期,以便我可以与另一个表连接以产生组合结果? – Soumya 2012-08-06 09:28:10

+0

@Soumya,我编辑了我的答案以符合您的新要求。 – danihp 2012-08-06 11:35:42

相关问题