Home > OS >  Trying to Run a SQL script file, Getting Syntax error : Incorrect syntax near '`'
Trying to Run a SQL script file, Getting Syntax error : Incorrect syntax near '`'

Time:06-23

I'm trying to execute a SQL script dump file which have multiple tables. I want to read data from this dump file and make a sqlite(offline) database. I'm using following SMO procedures and code is following

string sqlConnectionString = @"Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;";
            FileInfo file = new FileInfo("podcat-22-06-2022.sql");
            string script = file.OpenText().ReadToEnd();
            SqlConnection conn = new SqlConnection(sqlConnectionString);
            SqlCommand cmd = new SqlCommand(script, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

Following is first table script of sql file

-- MySQL dump 10.16  Distrib 10.1.21-MariaDB, for Win32 (AMD64)
--
-- Host: localhost    Database: localhost
-- ------------------------------------------------------
-- Server version   10.1.21-MariaDB

/*!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 utf8 */;
/*!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 */;

--
-- Current Database: `podcat`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `podcat` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `podcat`;

--
-- Table structure for table `label_fields`
--

DROP TABLE IF EXISTS `label_fields`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `label_fields` (
  `lable_id` int(11) NOT NULL AUTO_INCREMENT,
  `label_name` varchar(200) NOT NULL,
  `p-t-id` int(11) NOT NULL,
  `status` enum('Y','N') NOT NULL,
  `sort_order` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`lable_id`),
  UNIQUE KEY `lable_id` (`lable_id`)
) ENGINE=InnoDB AUTO_INCREMENT=485 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

database is big so i suppose i dont need to post all of it.

im getting following exception

Exception:Thrown: "Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
The label 'parts' has already been declared. Label names must be unique within a query batch or stored procedure.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Incorrect syntax near the keyword 'with'. If this " (System.Data.SqlClient.SqlException)
A System.Data.SqlClient.SqlException was thrown: "Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
Incorrect syntax near '`'.
The label 'parts' has already been declared. Label names must be unique within a query batch or stored procedure.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Incorrect syntax near the keyword 'with'. If this "
Time: 23-Jun-22 12:03:36 PM
Thread:<No Name>[9336]

What I'm gathering from this information is SQL script is different version maybe older than execution method I'm following. cause I don't think SQL script could be wrong because its been used here as a backup... but I may be wrong as said earlier im new to sql stuff. So Please tell me what I'm doing wrong. Thanks

CodePudding user response:

You are using ` instead of ' Try replacing them and it should work

  • Related