2017-03-12 93 views
0

這是我的前一個問題的延伸: Auto generating dates based on a table保存數據到表

我得到了它完美的作品

with n as (
     select row_number() over (order by (select null)) - 1 as n 
     from master..spt_values 
    ) 
select t.*, dateadd(day, n.n, t.startDate) as thedate 
from t join 
    n 
    on dateadd(day, n.n, t.startDate) <= t.endDate; 

但是該決議,我想提出的通過將結果保存到表中來保持結果。可以持續數據嗎?我試過select into語句,但它沒有工作

整個語句是:收到

select into ABC 
(
    with n as (
      select row_number() over (order by (select null)) - 1 as n 
      from master..spt_values 
     ) 
    select t.*, dateadd(day, n.n, t.startDate) as thedate 
    from t join 
     n 
     on dateadd(day, n.n, t.startDate) <= t.endDate 
) 

錯誤是:

Msg 156, Level 15, State 1, Line 146 
Incorrect syntax near the keyword 'into'. 
Msg 319, Level 15, State 1, Line 148 
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon. 
Msg 102, Level 15, State 1, Line 168 
Incorrect syntax near ')'. 

如何將結果保存到一個表?

+0

請發表您的完整的查詢插入查詢是不存在這個問題 –

回答

1

通知,其中into ABC

;with n as (
    select row_number() over (order by (select null)) - 1 as n 
    from master..spt_values 
) 
select t.* 
    , dateadd(day, n.n, t.startDate) as thedate 
into ABC 
from t join n  
    on dateadd(day, n.n, t.startDate) <= t.endDate 
+0

謝謝約翰!有用!!非常感謝。一個好奇的問題是,以前的分號(; with)是什麼?沒有它我就可以逃脫。 – user1205746

+0

@ user1205746我包括了;以防萬一您在cte –

+0

@ user1205746之前沒有提及...快樂它幫助:) –

0
with n as (
      select row_number() over (order by (select null)) - 1 as n 
      from master..spt_values 
     ) 
with ABC as(
    select t.*, dateadd(day, n.n, t.startDate) as thedate 
    from t join 
     n 
     on dateadd(day, n.n, t.startDate) <= t.endDate 
) 

現在您可以在另一個查詢中使用Abc。

+0

如果你想保存這些結果,您應該使用插入到查詢 –