关键字'with'附近的语法不正确。如果此语句是公用表表达式

时间:2022-10-25 22:46:36
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.

关键字'with'附近的语法不正确。如果此语句是公用表表达式,xmlnamespaces子句或更改跟踪上下文子句,则必须以分号结束前一个语句。

2 个解决方案

#1


7  

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

Edit: Also, you actually don't have a question. Did you install a plugin that automatically pipes your output from SQL Server Mangement Studio error logs to * questions? :)

编辑:此外,你实际上没有问题。您是否安装了一个插件,可以自动将输出从SQL Server Mangement Studio错误日志传递到*问题? :)

#2


0  

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]

#1


7  

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

Edit: Also, you actually don't have a question. Did you install a plugin that automatically pipes your output from SQL Server Mangement Studio error logs to * questions? :)

编辑:此外,你实际上没有问题。您是否安装了一个插件,可以自动将输出从SQL Server Mangement Studio错误日志传递到*问题? :)

#2


0  

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]