2012-08-06 86 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

相關問題