12
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

问题在于以下代码行

With DateRange(dt) As

它显示以下错误消息

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

4

4 回答 4

14

添加一些分号:

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

于 2012-05-29T03:16:05.663 回答
7

我在 SQL Server 2017 中遇到了同样的问题。在 WITH 语句之前使用分号,如下所示。

;WITH
于 2019-05-21T10:46:19.437 回答
0

试试这个语法:

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]
于 2017-01-25T19:04:15.920 回答
0

我在使用 MS SQL Server 2012 时遇到了这个问题。

我试图使用下面的脚本恢复数据库

USE master;
GO

ALTER DATABASE com.mydb.dev SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO

    
RESTORE DATABASE com.mydb.dev
    FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\com.mydb.dev_21-08-2020.bak'
    WITH REPLACE,
         STATS = 10,
         RESTART,
    MOVE 'com.mydb.dev' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev.mdf',
    MOVE 'com.mydb.dev_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev_log.ldf'
GO
    
ALTER DATABASE com.mydb.dev SET MULTI_USER;
GO

然后当我尝试执行还原任务时,这引发了以下错误:

Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '.'.
Msg 319, Level 15, State 1, Line 6
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 6
Incorrect syntax near 'IMMEDIATE'.
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near '.'.
Msg 319, Level 15, State 1, Line 11
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 17
Incorrect syntax near '.'.

这是我修复它的方法

问题是数据库名称包含一些特殊字符,例如 ' ' 特点。

我所要做的就是在数据库名称周围添加方括号“[]” ,如下所示:

USE master;
GO

ALTER DATABASE [com.mydb.dev] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO

    
RESTORE DATABASE [com.mydb.dev]
    FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\com.mydb.dev_21-08-2020.bak'
    WITH REPLACE,
         STATS = 10,
         RESTART,
    MOVE 'com.my_db.dev' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev.mdf',
    MOVE 'com.mydb.dev_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev_log.ldf'
GO
    
ALTER DATABASE [com.mydb.dev] SET MULTI_USER;
GO

而这一次,数据库恢复任务运行成功。

就这样。

我希望这有帮助

于 2020-08-26T22:17:15.827 回答