消息 156,级别 15,状态 1,第 16 行关键字 'IF' 附近的语法不正确

Msg 156, Level 15, State 1, Line 16 Incorrect syntax near the keyword 'IF'

-- --------------------------------------------------------
-- Host:                         192.168.62.245
-- Server version:               Microsoft SQL Server 2014 - 12.0.2000.8
-- Server OS:                    Windows NT 6.1 <X64> (Build 7601: ) (WOW64) (Hypervisor)
-- HeidiSQL Version:             9.5.0.5196
-- --------------------------------------------------------

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES  */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;


-- Dumping database structure for mjDB
CREATE DATABASE IF NOT EXISTS "mjDB";
USE "mjDB";

-- Dumping structure for table mjDB.PushNotificationLog
CREATE TABLE IF NOT EXISTS "PushNotificationLog" (
    "pushNotificationLogId" INT(10,0) NOT NULL,
    "itemType" VARCHAR(20) NULL DEFAULT NULL,
    "itemId" INT(10,0) NULL DEFAULT NULL,
    "servicemanId" INT(10,0) NULL DEFAULT NULL,
    "title" VARCHAR(100) NULL DEFAULT NULL,
    "body" VARCHAR(4000) NULL DEFAULT NULL,
    "tranId" INT(10,0) NULL DEFAULT NULL,
    "createdBy" INT(10,0) NULL DEFAULT NULL,
    "createdDate" DATETIME(3) NULL DEFAULT NULL,
    PRIMARY KEY ("pushNotificationLogId")
);

我从 Heidi SQL 导出这个更新到 19/12/2017,当我尝试 运行 在 SQL Server 2014 上 运行 这个时,我收到这个错误:

Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'IF'.

Msg 102, Level 15, State 1, Line 16
Incorrect syntax near 'mjDB'.

Msg 911, Level 16, State 1, Line 17
Database 'mjDB' does not exist. Make sure that the name is entered correctly.

如果您使用 SQL 服务器,您的创建 table 语法是错误的。如果您想在创建

之前检查 table 是否存在,请像这样更改代码
IF object_id('PushNotificationLog') IS NULL
BEGIN
     CREATE TABLE [PushNotificationLog] 
     (
         pushNotificationLogId INT NOT NULL,
         itemType VARCHAR(20) NULL DEFAULT NULL,
         itemId INT NULL DEFAULT NULL,
         servicemanId INT NULL DEFAULT NULL,
         title VARCHAR(100) NULL DEFAULT NULL,
         body VARCHAR(4000) NULL DEFAULT NULL,
         tranId INT NULL DEFAULT NULL,
         createdBy INT NULL DEFAULT NULL,
         createdDate DATETIME NULL DEFAULT NULL,

         PRIMARY KEY (pushNotificationLogId)
    );
END

您也可以通过检查视图中是否存在来进行检查sys.tables

IF NOT EXISTS(SELECT 1 FROM sys.tables WHERE name = 'PushNotificationLog')
BEGIN

END

同样检查 master.sys.databases table 是否存在数据库

IF NOT EXISTS(SELECT 1 FROM master.sys.databases WHERE name = 'mjDB')
    BEGIN

    END

使用单引号 (') 代替双引号 (")
从标识符 (DatabaseName, TableName)
中删除引号 用方括号 ([])

替换列名中的引号
IF NOT EXISTS
(
    SELECT 1
    FROM [master].sys.databases
    WHERE [name] = 'mjDB'
)
    BEGIN CREATE DATABASE mjDB; END
GO

USE mjDB;
GO

-- Dumping structure for table mjDB.PushNotificationLog
IF (OBJECT_ID('dbo.PushNotificationLog', 'U') IS NULL)
    BEGIN
        CREATE TABLE dbo.PushNotificationLog
        (
            [PushNotificationLogID] INT NOT NULL
            , [ItemType] VARCHAR(20)
            , [ItemID] INT
            , [ServicemanID] INT
            , [Title] VARCHAR(100)
            , [Body] VARCHAR(4000)
            , [TranId] INT
            , [CreatedBy] INT
            , [CreatedDate] DATETIME
            , CONSTRAINT PK__PushNotificationLog PRIMARY KEY ([pushNotificationLogId])
        );
    END
GO