User981636
User981636

Reputation: 3621

How to run a .sql file as part of a MySQL Workbench 6.2 query?

I'm using MySQL Workbench 6.2 [Windows7] and I want to create a script with all my steps. Among the steps, I have a series of .sql files stored from on my computer that create and populate tables. I want to run these files from the query tab but every time I use this command:

source C:/Users/[username]/Desktop/sampdb/create_president.sql;

I get an Error 1064, which says

"Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql> source C:/Users/[username]/Desktop/sampdb/create_president.sql at line 1 "

MySQL nested script nightmare

Can anyone tell me what I'm doing wrong? How can I refer to a .sql file within a MySQL Workbench script?

What code should I use in the MySQL Workbench instead of source? I have tried LOAD DATA LOCAL INFILE 'C:/Users/[username]/Desktop/sampdb/create_president.sql'; but it didn't work either. Any idea what can be wrong?

*Just for additional information, create_president.sql contains the following code:

DROP TABLE IF EXISTS president;
#@ _CREATE_TABLE_
CREATE TABLE president
(
  last_name  VARCHAR(15) NOT NULL,
  first_name VARCHAR(15) NOT NULL,
  suffix     VARCHAR(5) NULL,
  city       VARCHAR(20) NOT NULL,
  state      VARCHAR(2) NOT NULL,
  birth      DATE NOT NULL,
  death      DATE NULL
);

Upvotes: 4

Views: 19960

Answers (2)

Mike Lischke
Mike Lischke

Reputation: 53317

The source command is a pure (command line) client specific command which MySQL Workbench does not support. Just load the sql file to run it.

Update (after you edited your question)

Your SQL file contains DDL. LOAD DATA INFILE however only imports DML. There's no replacement for the MySQL commandline client's SOURCE command. Support for that in MySQL Workbench is however on our todo list.

Upvotes: 5

Shammi Jayasinghe
Shammi Jayasinghe

Reputation: 69

You can run scripts on MySqlWorkBench as follows:

From the file menu, Select "Run SQL Script".

enter image description here

Then, In the popup window, Open the script from your machine.

enter image description here

Then Select the schema name which you are going to execute this script. (Previously i created the schema using the command "create database REGISTRY_LOCAL1;")

enter image description here

Then execute the script.

enter image description here

Upvotes: 5

Related Questions