Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Include a script within script

I'm involved is a project to migrate a project from Oracle to MySQL. In Oracle I have the ability to create a SQL script that references or inlcudes other external SQL script files when the batch is run via command line. I Have a script called CreateAllTables.sql that looks like this internally:

@tables\Site.sql @tables\Language.sql @tables\Country.sql @tables\Locale.sql @tables\Tag.sql 

I'm already aware of the MySQL command line "Source" command, but my goal is to invoke a single main .sql script file that includes other scripts via one single command line call like this:

mysql --user=root --password --database=junkdb -vv < CreateAllTables.sql 

So my question is how do I do this with MySQL?

like image 850
James Avatar asked Dec 29 '09 19:12

James


2 Answers

source works for me.

# -- foo.sql DROP TABLE foo; source bar.sql  # -- bar.sql CREATE TABLE bar (i INT NOT NULL);  $ mysql ... < foo.sql 

Now table foo is gone and bar is created.

like image 96
pilcrow Avatar answered Sep 21 '22 11:09

pilcrow


Note that the "source" option, above, only works (for me) if the script is run through a mysql client that supports it. (The mysql command-line client referenced in the OP's original question happens to be one of those clients.)

But remember "source" is not one of the many mysql-specific extensions to the sql language. It's a client-command, not a sql statement,

Why do you care?

If you're sending your sql script to the MySQL server via an alternative method (via JDBC's "execSQL", for example) the "source" command will not work for the inclusion of other scripts.

like image 26
gary.affonso Avatar answered Sep 20 '22 11:09

gary.affonso