可以从SQL脚本中引用另一个SQL文件 [英] It is possible to reference another SQL file from SQL script

查看:358
本文介绍了可以从SQL脚本中引用另一个SQL文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想从Postgres的SQL文件中执行一个SQL文件。



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只是:


  1. 每行读取所有SQL文件行

  2. 剥离任何注释(以开头的行> - // / *

  3. 删除任何; 结尾

  4. 以单个语句执行结果

(请参阅 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:

  1. reading all your SQL files line per line
  2. strip any comment (lines starting with --, // or /*)
  3. removes any ; at the end
  4. 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 with hibernate.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 the hibernate.hbm2ddl.import_files property dynamically - depending on some environment property

这篇关于可以从SQL脚本中引用另一个SQL文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆