Wednesday 11 January 2012

Nested source Command

In an earlier post, I showed how you can run SQL commands by storing them in a file and running it with the source command. You can run nested source commands too, as shown below. Here are 3 SQL files:
 
UNIX > cat nested_source.sql
use test;
source count_star.sql
source select_database.sql
UNIX > cat count_star.sql
select count(*) from mysql.user;
UNIX > cat select_database.sql
select database();
UNIX >
 
So if you use source on nested_source.sql, it will run the SQL contained in the files count_star.sql and select_database.sql. You can see what I mean in the example below:
 
UNIX > mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 53432
Server version: 5.0.67 Source distribution
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 
mysql> source nested_source.sql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
+----------+
| count(*) |
+----------+
|       18 |
+----------+
1 row in set (0.00 sec)
 
+------------+
| database() |
+------------+
| test       |
+------------+
1 row in set (0.00 sec)
 
mysql>

No comments: