2

I was wondering if it is possible/how to include one .sql file in another .sql file, as is done in other programming languages like C or Java?

I am asking because I wanted to organize my sql scripts into support library files and application scripts, and so on. I searched around, and solutions seem to be about loading .sql files from the psql client (see e.g. postgreSQL - psql \i : how to execute script in a given path). What I am interested in is to load a library .sql script from inside a sql script.

P.S. I am using PL/pgSQL (PostgreSQL 9.3).

2
  • 2
    Make them stored procedures. Commented Dec 26, 2014 at 3:52
  • Provide an SSCCE to clarify your objective. Show the code you are trying the nest, even if it's not working. Commented Jan 9, 2015 at 7:41

1 Answer 1

2

Based on the answer It is possible to reference another SQL file from SQL script, on PostgreSQL, you can include another SQL's files just using the \i syntax. I just tested and is working good on PostgreSQL 9.6:

\i other_script.sql
SELECT * FROM table_1;
SELECT * FROM table_2;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.