SQL-Server: Incorrect syntax near the keyword 'with'. If this statement is a common table expression

前端 未结 4 1484
一向
一向 2021-02-20 17:39
create table #temp
(
  pName Varchar(20),
  DateBegin DateTime,
  DateEnd DateTime
)

Insert Into #temp(pName, DateBegin, DateEnd)
Values(\'Player1\', \'01/04/2012\', \'         


        
4条回答
  •  被撕碎了的回忆
    2021-02-20 18:24

    Add some semicolons:

    create table #temp
    (
      pName Varchar(20),
      DateBegin DateTime,
      DateEnd DateTime
    )
    
    Insert Into #temp(pName, DateBegin, DateEnd)
    Values('Player1', '01/04/2012', '01/05/2012')
    
    Insert Into #temp(pName, DateBegin, DateEnd)
    Values('Player2', '02/01/2012', '02/05/2012');
    
    With DateRange(dt) As
    (
        Select Convert(Datetime, '01/01/2012')
        UNion All
        Select DateAdd(dd, 1, Dat.dt) From DateRange Dat Where Dat.dt < CONVERT(Datetime, '01/31/2012')
    )
    
    Select T.pName, Dt.dt from #temp T
    Inner Join DateRange Dt on Dt.dt BETWEEN T.DateBegin and T.DateEnd;
    
    Drop Table #temp
    

    http://sqlfiddle.com/#!6/06e89

提交回复
热议问题