MySQL 在脚本中包含一个脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1976396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:53:30  来源:igfitidea点击:

MySQL Include a script within script

sqlmysqlscripting

提问by James

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:

我参与了一个将项目从 Oracle 迁移到 MySQL 的项目。在 Oracle 中,当批处理通过命令行运行时,我能够创建引用或包含其他外部 SQL 脚本文件的 SQL 脚本。我有一个名为 CreateAllTables.sql 的脚本,内部看起来像这样:

@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 命令行“Source”命令,但我的目标是通过一个命令行调用调用一个包含其他脚本的主 .sql 脚本文件,如下所示:

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

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

所以我的问题是如何使用 MySQL 做到这一点?

回答by pilcrow

sourceworks for me.

source为我工作。

# -- foo.sql
DROP TABLE foo;
source bar.sql

# -- bar.sql
CREATE TABLE bar (i INT NOT NULL);

$ mysql ... < foo.sql

Now table foois gone and baris created.

现在表foo消失了,并且创建了bar

回答by gary.affonso

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

请注意,上面的“源”选项仅在脚本通过支持它的 mysql 客户端运行时才有效(对我而言)。(在 OP 的原始问题中引用的 mysql 命令行客户端恰好是这些客户端之一。)

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

但请记住,“源代码”不是sql 语言的许多特定于 mysql 的扩展之一。这是一个客户端命令,而不是一个 sql 语句,

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.

如果您通过替代方法(例如,通过 JDBC 的“execSQL”)将 sql 脚本发送到 MySQL 服务器,则“source”命令将不适用于包含其他脚本。

回答by Roland Bouman

You can do a similar thing with source in mysql.

你可以用 mysql 中的 source 做类似的事情。

I have inc1.sql with these contents:

我有包含以下内容的 inc1.sql:

use test;
create table testinc(
   id int  
);

And inc2.sql like this:

和 inc2.sql 像这样:

insert into testinc values (1);

and main.sql like this:

和 main.sql 像这样:

source inc1.sql
source inc2.sql

And i can run main.sql like this:

我可以像这样运行 main.sql:

mysql -uroot -pmysql -P3351 -e"Source main.sql"

After that I can verify that it worked by doing this:

之后,我可以通过执行以下操作来验证它是否有效:

mysql> use test;
Database changed
mysql> select * from testinc;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)