How to run a .sql file as part of a MySQL Workbenc

2020-07-22 16:37发布

问题:

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 "

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
);

回答1:

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.



回答2:

You can run scripts on MySqlWorkBench as follows:

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

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

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;")

Then execute the script.



回答3:

Just remove the quotes and semicolon. The command source is not SQL and it doesn't like quotes and semicolons. :)

source /Users/[username]/Documents/company/department.sql