IgorAlves
IgorAlves

Reputation: 5560

How to call .sql files trough another .sql file in MySQL?

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

Upvotes: 3

Views: 4996

Answers (3)

Neethu Lalitha
Neethu Lalitha

Reputation: 3071

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!

Upvotes: 1

IgorAlves
IgorAlves

Reputation: 5560

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.

Upvotes: 3

BranLakes
BranLakes

Reputation: 358

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

Upvotes: 0

Related Questions