How to import external sql scripts from a sql script in PostgreSQL?

戏子无情 提交于 2019-12-08 05:53:57

问题


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


回答1:


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;


来源:https://stackoverflow.com/questions/27652408/how-to-import-external-sql-scripts-from-a-sql-script-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!