3

I have 25 sql files that comes from a mysqldump each one. Lets says they call db1.sql, db2.sql ... and so on.

I want to create a sql file to call all the others. Lets says this file will cal rumTest.sql .

Inside runTest.sql I wrote:

\. C:\pathToFIle\db1.sql;
\. C:\pathToFIle\db2.sql;
\. C:\pathToFIle\db3.sql;
...

So, inside my command window I call the file as

\. C:\pathToFIle\rumTest.sql;

This is not working. I've tried in different ways to put double cotes when calling "\. C:\pathToFIle\db1.sql;" inside rumTest.sql.

I see that Mysql reads what is inside rumTest.sql but not execute the command to read db1.sql, for example. So, what could be the best approach to perform this task? I fond these 2 posts but they are related to oracle I need to call another sql file within an sql file using sql plus

3
  • Have you tried quoting just the file name, not the whole command? \. "C:\pathToFIle\db1.sql"; Commented Apr 16, 2015 at 22:57
  • Hi Eresov. I've tried all possibilities of quoting. Commented Apr 17, 2015 at 1:01
  • Try escaping the backslashes: source c:\\pathtofile\\db1.sql; you can also try '/' as a path separator source c:/pathtofile/db1.sql; Commented Apr 17, 2015 at 14:50

3 Answers 3

3

I found a solution - that works (tested).

We do not need to use ";" at the end. So the file should look like this:

//File rumTest.sql 

\. C:\pathToFIle\db1.sql
\. C:\pathToFIle\db2.sql
\. C:\pathToFIle\db3.sql

But if we want to insert some sql command we should use ";" for each line. The code should looks like this:

//File rumTest.sql    

\. C:\pathToFIle\db1.sql
USE db1;
SHOW TABLES;
\. C:\pathToFIle\db2.sql
SHOW TABLES; //(Display db2 tables. db2 is already selected)
\. C:\pathToFIle\db3.sql

So in your cmd window You only call

\. C:\pathToFIle\rumTest.sql

then the file will call all others.

Sign up to request clarification or add additional context in comments.

1 Comment

You have to note the source command (alias \.) is not a SQL command but a mySQL command. The naming of the main script should not have the extension .sql as it is more a batch commands for mySQL and this lead to confusion as such sql file would not be executable from a classic SQL editor such as MySQL-Workbench.
1

Inside your main sql file, you can place the other sql file like below:

Inside Main file (C:\main.sql)

SET @NAME = 'XYZ';

SOURCE C:\abc.sql

To execute this C:\main.sql:

mysql -h abc -u xyz -p mno < C:\main.sql

inside C:\abc.sql

use table1;

insert into table1.school(name)values(concat('RAM_',@NAME));

All occurences of NAME in file C:\abc.sql would be replaced by XYZ.

output: insert into table1.school(name)values(RAM_XYZ);

This would work.You may try!

Comments

0

You should be able to use the source command in your script:

source C:\pathToFIle\db1.sql;
source C:\pathToFIle\db2.sql;
source C:\pathToFIle\db3.sql;
...

and call the script like:

mysql -u YourUsername -p < C:\pathToFIle\rumTest.sql

3 Comments

Hi Bran, It will not change. The script is to create a db.
Edited based on your comment. What error messages are displayed when you run your scripts?
You have to note the source command is not a SQL command but a mySQL command. The naming of the main script should not have the extension .sql as it is more a batch commands for mySQL and this lead to confusion as such sql file would not be executable from a classic SQL editor such as MySQL-Workbench.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.