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
Import of other files is not supported in Sql, but if you execute the script with psql can you use the \i
syntax:
SELECT * FROM table_1;
\i other_script.sql
SELECT * FROM table_2;
This will probably not work if you execute the sql with other clients than psql
.
Hibernate is just:
--
, //
or /*
);
at the end(see SchemaExport.importScript
and SingleLineSqlCommandExtractor
)
There is no support for an include here.
What you can do:
ImportSqlCommandExtractor
which knows how to include a file - you can set that extractor with hibernate.hbm2ddl.import_files_sql_extractor=(fully qualified class name)
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.Integrator
which sets the hibernate.hbm2ddl.import_files
property dynamically - depending on some environment propertyIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With