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

前端 未结 4 1380
一向
一向 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:12

    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

提交回复
热议问题