简体   繁体   English

消息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")
);

I exported this one from HeidiSQL updated to 19/12/2017, when I try to run this on SQL Server 2014 I get this error: 我从更新到19/12/2017的HeidiSQL导出了此错误,当我尝试在SQL Server 2014上运行此错误时出现此错误:

Msg 156, Level 15, State 1, Line 16 消息156,第15级,状态1,第16行
Incorrect syntax near the keyword 'IF'. 关键字“ IF”附近的语法不正确。

Msg 102, Level 15, State 1, Line 16 Msg 102,第15级,状态1,第16行
Incorrect syntax near 'mjDB'. 'mjDB'附近的语法不正确。

Msg 911, Level 16, State 1, Line 17 消息911,第16级,州1,第17行
Database 'mjDB' does not exist. 数据库'mjDB'不存在。 Make sure that the name is entered correctly. 确保正确输入名称。

Your create table syntax is wrong if you are using SQL Server. 如果使用SQL Server,则创建表语法错误。 Change the code like this if you wish to check the table existence before creating 如果要在创建之前检查表是否存在,请像这样更改代码

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

You can also do the check by checking the existence in the view sys.tables 您也可以通过检查sys.tables视图中是否存在来进行检查

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

END

Similarly Check in the master.sys.databases table for the existence of Database 同样,在master.sys.databases表中检查是否存在数据库

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

    END

Use single quotes (') instead of double quotes (") 使用单引号(')代替双引号(“)
Remove the quotes from the identifiers (DatabaseName, TableName) 从标识符(数据库名称,表名称)中删除引号
Replace the quotes in the column names with square brackets ([]) 用方括号([])替换列名中的引号

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 消息 156,级别 15,State 1,第 5 行关键字“LEFT”附近的语法不正确。 消息 156,级别 15,State 1,第 6 行 'b' 附近的语法不正确 - Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'LEFT'. Msg 156, Level 15, State 1, Line 6 Incorrect syntax near 'b' Msg 156,Level 15,State 1,Procedure inventory,Line 6 [Batch Start Line 2]关键字&#39;Where&#39;附近的语法不正确 - Msg 156, Level 15, State 1, Procedure inventory, Line 6 [Batch Start Line 2] Incorrect syntax near the keyword 'Where' 消息156,级别15,状态1,第50行关键字“ GROUP”附近的语法错误 - Msg 156, Level 15, State 1, Line 50 Incorrect syntax near the keyword 'GROUP' 消息156,级别15,状态1,过程ShowDirectorateList,第739行关键字&#39;END&#39;附近的语法不正确 - Msg 156, Level 15, State 1, Procedure ShowDirectorateList, Line 739 Incorrect syntax near the keyword 'END' 消息156,级别15,状态1,行2493关键字“ ON”附近的语法错误 - Msg 156, Level 15, State 1, Line 2493 Incorrect syntax near the keyword 'ON' 消息156,级别15,状态1,第22行在关键字“具有”附近的语法错误 - Msg 156, Level 15, State 1, Line 22 Incorrect syntax near the keyword 'having' 消息156,级别15,状态1,过程CLR_TRIGGER_NAUDOJASI,第1行关键字“插入”附近的语法错误 - Msg 156, Level 15, State 1, Procedure CLR_TRIGGER_NAUDOJASI, Line 1 Incorrect syntax near the keyword 'insert' 消息 156,级别 15,状态 1,第 7 行关键字“BETWEEN”附近的语法不正确 - Msg 156, Level 15, State 1, Line 7 Incorrect syntax near the keyword 'BETWEEN' 消息 156,级别 15,状态 1,第 2 行 关键字“case”附近的语法不正确。 我的 SQL 有什么问题 - Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'case'. Whats wrong in my SQL SQL Server:错误消息156,级别15,状态1,第12行关键字“ AS”附近的语法不正确 - SQL Server: error Msg 156, Level 15, State 1, Line 12 Incorrect syntax near the keyword 'AS'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM