2013-04-09 116 views
0

首先我會解釋你的情況。CTE循環查詢

我想將數據從一個表(View_Solidnet_Training)傳輸到另一個表(OBJ_Availability)。 有一個問題:在視圖中有一個開始日期和結束日期!在OBJ_Availability每個日期有一個記錄。所以視圖中的一行在另一個表中有多行。

我必須與CTE合作。所以光標不適合我。 中間的WITH運行完美,但是當我想添加額外的WITH來檢查ID是否不爲零時,它必須將變量@Start@End更改爲視圖中的新記錄。

對不起我的英文,這不是很好,但我希望你瞭解情況。

這裏是我的代碼:

DECLARE @Start AS DATETIME; 
DECLARE @End AS DATETIME; 
SET @Start = '2013-04-09'; 
SET @End = '2013-04-11'; 
with cte1 as 
(

with cte2 as 
(
select @Start as DateValue 
union all 

select DateValue + 1 
from cte2 
where DateValue + 1 <= @End 
) 
into OBJ_Availability 
select 34, DateValue, 'AM', 2, 'Test' from cte2 
) 
select * from cte1 where PK_Training_ID is not null; 

這樣的事情,但我不明白的地方變得視圖的信息。我從未在任何地方提過這個名字

回答

0

我還不能發表評論,所以在回答之前我需要問幾個問題。

1,CTE的名字怎麼和視圖的名字一樣?它不應該工作。

2,你是什麼意思'檢查ID是不是零,它必須改變變量@Start和@End到視圖中的新記錄',我想不通你爲什麼需要做空檢查

3,您確定可以使用DateValue + 1獲取下一個日期嗎?你應該使用DATEADD嗎?

最後,CTE內部不能有CTE,這是行不通的。在CTE中聲明變量也是不可能的。

這裏是我最好的客人:

首先,正如你所說,你的觀點有一個開始日期列和結束日期列,

所以我假設有視圖中的起始日期和結束日期列,這裏是我的SQL:

DECLARE @start AS DATETIME 
DECLARE @end AS DATETIME 

SELECT @start = min(StartDate) 
from View_Solidnet_Training 
where PK_Training_ID is not null 

SELECT @end = max(EndDate) 
from View_Solidnet_Training 
where PK_Training_ID is not null 

;with cte_dates as 
(
    select @start DateValue 
    union all 
    select DateValue + 1 
    from cte_dates 
    where DateValue + 1 <= cast(@end as datetime) 
) 
into OBJ_Availability 
select v.PK_Training_ID, DateValue, 'AM', 2, 'Test' --columns from the view 
from cte_dates cte 
join View_Solidnet_Training v on v.StartDate < cte.DateValue and cte.DateValue < v.EndDate 
where v.PK_Training_ID is not null 

最大()和min函數圖的是最新的和最古老的日期視圖

那麼CTE cte_dates從@啓動創建日期的列表,以@end

則連接到CTE將反覆從起始日期的範圍內,以結束日期

希望這有助於

記錄順便說一下,我沒有在我的家用電腦的sql,所以我可以檢查是否

SELECT @start = min(StartDate) 
from View_Solidnet_Training 
where PK_Training_ID is not null 

運行與否,但你應該明白我的意思

使用時間,而不是CTE:

DECLARE @start AS DATETIME 
DECLARE @end AS DATETIME 

SELECT @start = min(StartDate) 
from View_Solidnet_Training 
where PK_Training_ID is not null 

SELECT @end = max(EndDate) 
from View_Solidnet_Training 
where PK_Training_ID is not null 

DECLARE @AllDates table 
     (DateValue datetime) 

DECLARE @dCounter datetime 
SELECT @dCounter = @start 

WHILE @dCounter <= @end 
BEGIN 
INSERT INTO @AllDates VALUES (@dCounter) 
SELECT @[email protected]+1 
END 

insert into OBJ_Availability 
select v.PK_Training_ID, DateValue, 'AM', 2, 'Test' --columns from the view 
from @AllDates d 
join View_Solidnet_Training v on v.StartDate < d.DateValue and d.DateValue < v.EndDate 
where v.PK_Training_ID is not null 

或者你甚至可以做

DECLARE @start AS DATETIME 
DECLARE @end AS DATETIME 

SELECT @start = min(StartDate) 
from View_Solidnet_Training 
where PK_Training_ID is not null 

SELECT @end = max(EndDate) 
from View_Solidnet_Training 
where PK_Training_ID is not null 


DECLARE @dCounter datetime 
SELECT @dCounter = @start 

WHILE @dCounter <= @end 
BEGIN 

    insert into OBJ_Availability 
    select v.PK_Training_ID, DateValue, 'AM', 2, 'Test' --columns from the view 
    from View_Solidnet_Training v 
    where v on v.StartDate < @dCounter and @dCounter < v.EndDate and v.PK_Training_ID is not null 

    SELECT @[email protected]+1 
END 
+0

1,不知道插入視圖的一個行後 2,這一刻它停止。所以我必須做一個新的循環來檢查是否有另外一行,他必須用新值設置變量並將它們改爲插入下一行。 3,這是事實,它的工作原理。 4,但我需要在循環中進行循環,因爲在第一次插入之後,他必須更改日期並重新開始,在視圖中插入下一個。 – user2206834 2013-04-09 09:38:07

+0

還有一個問題,PK_Traing_ID從哪裏來?不屬於OBJ_Availability? – 2013-04-09 09:44:54

+0

nop,它是來自視圖的ID:View_Solidnet_Training – user2206834 2013-04-09 09:46:23