Incorrect syntax near the keyword 'with'.

2020-07-06 07:52发布

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

Issue is with this following code line

With DateRange(dt) As

It shows following error message

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.

3条回答
ゆ 、 Hurt°
2楼-- · 2020-07-06 07:58

Try this syntax:

CREATE TABLE [dbo].[CIQTempJointMembersLink](   
    [JointMembersID] [int] NULL,
    [fk_CIQPerson_PersonID] [int] NULL)

GO

WITH Summary AS (
    SELECT p.[JointMembersID], 
           p.[fk_CIQPerson_PersonID],            
           p.[JointMembersLinkID],
           ROW_NUMBER() OVER(PARTITION BY [fk_CIQPerson_PersonID] 
                                 ORDER BY [fk_CIQPerson_PersonID] DESC) AS rownumber
      FROM [CIQRegos].[dbo].[CIQJointMembersLink] p)

INSERT INTO [CIQTempJointMembersLink]([JointMembersID], [fk_CIQPerson_PersonID])

SELECT JointMembersLink.[JointMembersID], JointMembersLink.[fk_CIQPerson_PersonID]
FROM Summary JointMembersLink
WHERE JointMembersLink.rownumber = 1 and JointMembersLink.[fk_CIQPerson_PersonID] is NOT NULL
Order by JointMembersLink.JointMembersLinkID;



Drop Table [dbo].[CIQTempJointMembersLink]
查看更多
Deceive 欺骗
3楼-- · 2020-07-06 08:18

I had the same issue with SQL server 2017. Use semicolon before the WITH statement as follows.

;WITH
查看更多
Emotional °昔
4楼-- · 2020-07-06 08:22

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

查看更多
登录 后发表回答