可以从SQL脚本中引用另一个SQL文件 [英] It is possible to reference another SQL file from SQL script
问题描述
mysql的类似问题:是否可以从存储过程调用sql脚本在另一个sql脚本?
为什么?
因为我在一个项目中有两个数据文件,并且我想要一行可以加载第二个文件的注释/未注释。
说明:
我想从A.SQL调用B.SQL
$ b $ Clarification2:
这是一个使用hibernate从初始SQL文件(A.SQL)创建数据库的Spring Project。
在进一步的反思中,我可能不得不从java / string / hibernate处理这个问题。
下面是配置文件:
spring.datasource.url = jdbc:postgresql:// localhost:5432 / dbname
spring.datasource.username = postgres
spring.datasource.password = root
spring.datasource.driver -class-name = org.postgresql.Driver
spring.datasource.data = classpath:db / migration / postgres / data.sql
spring.jpa.hibernate.ddl-auto = create
Hibernate只是:
- 每行读取所有SQL文件行
- 剥离任何注释(以
开头的行> -
,//
或/ *
) - 删除任何
;
结尾 - 以单个语句执行结果
(请参阅 SchemaExport.impor tScript
和 SingleLineSqlCommandExtractor
)
b
$ b 你可以做什么:
ImportSqlCommandExtractor
知道如何包含文件 - 您可以使用 hibernate.hbm2ddl.import_files_sql_extractor =(完全限定类名) code>
hibernate.hbm2ddl.import_files = prefix.sql,optional.sql,postfix.sql 定义可选文件作为附加的导入文件。 code>,您可以随意添加和删除文件引用,或者甚至可以从工件中排除该文件 - 缺少的文件只会创建一条调试消息。
Integrator
,它设置 hibernate.hbm2ddl.import_files
p动态roperty - 取决于某些环境属性
Basically I want to execute an SQL file from an SQL file in Postgres.
Similar question for mysql: is it possible to call a sql script from a stored procedure in another sql script?
Why?
Because I have 2 data files in a project and I want to have one line that can be commented/un-commented that loads the second file.
Clarification: I want to call B.SQL from A.SQL
Clarification2: This is for a Spring Project that uses hibernate to create the database from the initial SQL file (A.SQL).
On further reflection it seems I may have to handle this from java/string/hibernate.
Below is the configuration file:
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname
spring.datasource.username=postgres
spring.datasource.password=root
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.data=classpath:db/migration/postgres/data.sql
spring.jpa.hibernate.ddl-auto=create
Hibernate is just:
- reading all your SQL files line per line
- strip any comment (lines starting with
--
,//
or/*
) - removes any
;
at the end - executes the result as a single statement
(see SchemaExport.importScript
and SingleLineSqlCommandExtractor
)
There is no support for an include here.
What you can do:
- Define your own
ImportSqlCommandExtractor
which knows how to include a file - you can set that extractor withhibernate.hbm2ddl.import_files_sql_extractor=(fully qualified class name)
- Define your optional file as additional import file with
hibernate.hbm2ddl.import_files=prefix.sql,optional.sql,postfix.sql
, you can either add and remove the file reference as you like, or you can even exclude the file from your artifact - a missing file will only create a debug message. - Create an
Integrator
which sets thehibernate.hbm2ddl.import_files
property dynamically - depending on some environment property
这篇关于可以从SQL脚本中引用另一个SQL文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!