15
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.

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Nilish
  • 1,066
  • 3
  • 12
  • 26

4 Answers4

15

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

aquinas
  • 23,318
  • 5
  • 58
  • 81
  • Is it required before with statement? – Nilish May 29 '12 at 03:26
  • Well, yes, "If this statement is a common table expression...the previous statement must be terminated with a semicolon." You have a CTE right? – aquinas May 29 '12 at 03:30
  • 1
    @Nilish: If I am not much mistaken, SQL Server 2005 tolerated the absence of a semicolon before `WITH ...` (CTE) in certain situations, but starting with SQL Server 2008 you must *always* put a semicolon before `WITH`. – Andriy M May 30 '12 at 06:36
  • 1
    It should be stressed that, sometimes, all that is needed is a semi-colon just before the WITH statement... even if there is no code before the statement. Just place a semi-colon on a blank line above your statement. – WEFX Jun 12 '14 at 17:42
  • @aquinas I have a similar issue posted [here](https://stackoverflow.com/q/64361973/1232087) but the context is different so I don't know if you would have any suggestions. – nam Oct 14 '20 at 21:58
10

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

;WITH
Chamila Maddumage
  • 3,304
  • 2
  • 32
  • 43
  • 1
    I tried your suggestion but now get a different error posted [here](https://stackoverflow.com/q/64361973/1232087). Any suggestions? – nam Oct 14 '20 at 22:01
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]
Nalan Madheswaran
  • 10,136
  • 1
  • 57
  • 42
0

I faced this issue when working on an MS SQL Server 2012.

I was trying to restore a database using the script below

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

And then this was throwing the error below when I tried executing the restore task:

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 '.'.

Here's how I fixed it:

The issue was that the database name contains some special characters like the '.' character.

All I had to do was to add square brackets '[ ]' around the database name, like this:

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

And this time, the database restore task ran successfully.

That's all.

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143