2017-03-12 56 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

如果你想保存这些结果,您应该使用插入到查询 –