Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Syntax error in MS SQL Server 2014

Author  Topic 

rococtz
Starting Member

1 Post

Posted - 2014-12-28 : 01:17:54
Hi,

I am running a script that creates a table and inserts some data. The SQL script runs perfectly fine in SQL fiddle here [url]http://sqlfiddle.com/#!2/0565e/1[/url], but when I try to run the same script in MS SQL Server 2014 I get syntax errors. Anyone can please suggest what to do?


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


This is the script that crashes in MS SQL Server 2014:

-- MySQL dump 10.13 Distrib 5.1.51, for pc-linux-gnu (i686)
--
-- Host: 127.0.0.1 Database: world
-- ------------------------------------------------------
-- Server version 5.1.51-debug-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES latin1 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!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' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `City`
--

DROP TABLE IF EXISTS `City`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `City` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` char(35) NOT NULL DEFAULT '',
`CountryCode` char(3) NOT NULL DEFAULT '',
`District` char(20) NOT NULL DEFAULT '',
`Population` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `City`
--
-- ORDER BY: `ID`

INSERT INTO `City` VALUES (1,'Kabul','AFG','Kabol',1780000);
INSERT INTO `City` VALUES (2,'Qandahar','AFG','Qandahar',237500);
INSERT INTO `City` VALUES (3,'Herat','AFG','Herat',186800);
INSERT INTO `City` VALUES (4,'Mazar-e-Sharif','AFG','Balkh',127800);
INSERT INTO `City` VALUES (5,'Amsterdam','NLD','Noord-Holland',731200);
INSERT INTO `City` VALUES (6,'Rotterdam','NLD','Zuid-Holland',593321);
INSERT INTO `City` VALUES (7,'Haag','NLD','Zuid-Holland',440900);
INSERT INTO `City` VALUES (8,'Utrecht','NLD','Utrecht',234323);
INSERT INTO `City` VALUES (9,'Eindhoven','NLD','Noord-Brabant',201843);
INSERT INTO `City` VALUES (10,'Tilburg','NLD','Noord-Brabant',193238);
INSERT INTO `City` VALUES (11,'Groningen','NLD','Groningen',172701);
INSERT INTO `City` VALUES (12,'Breda','NLD','Noord-Brabant',160398);
INSERT INTO `City` VALUES (13,'Apeldoorn','NLD','Gelderland',153491);

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-12-28 : 07:25:22
As your script is written for the mysql engine, and you are trying to implement it on mssql engine, you are bound to get syntax errors, as these two engines are very different.
You need to translate your script to a syntax that mssql understands.
Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-12-28 : 08:53:08
Translation could be:
if object_id('dbo.city','U') is not null
drop table dbo.city
;

create table dbo.city (
id int identity(1,1)
,name char(35) not null
,countrycode char(3) not null
,destrict char(20) not null
,population int not null
,primary key(id)
);

set identity_insert dbo.city on;
insert into dbo.city
(id,name,countrycode,destrict,population)
values (1,'Kabul','AFG','KABOL',1780000)
,(2,'Qandahar','AFG','Qandahar',237500)
,(3,'Herat','AFG','Herat',186800)
,(4,'Mazar-e-Sharif','NLD','Noord-Holland',127800)
,(5,'Amsterdam','NLD','Zuid-Holland',731200)
,(6,'Rotterdam','NLD','Zuid-Holland',593321)
,(7,'Haag','NLD','Zuid-Holland',440900)
,(8,'Utrecht','NLD','Utrecht',234323)
,(9,'Eindhoven','NLD','Noord-Brabant',201843)
,(10,'Tilburg','NLD','Noord-Brabant',193238)
,(11,'Groningen','NLD','Groningen',172701)
,(12,'Breda','NLD','Noord-Brabant',160398)
,(13,'Apeldoorn','NLD','Gelderland',153491)
;
set identity_insert dbo.city off;
Go to Top of Page
   

- Advertisement -