简体   繁体   中英

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:

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.

Your create table syntax is wrong if you are using 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM