[英]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.