如何从PostgreSQL中的sql脚本导入外部sql脚本? [英] How to import external sql scripts from a sql script in PostgreSQL?

查看:126
本文介绍了如何从PostgreSQL中的sql脚本导入外部sql脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能/如何在另一个.sql文件中包含一个.sql文件,就像在其他编程语言(如C或Java)中一样?

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?

我在问,因为我想将我的sql脚本组织成支持库文件和应用程序脚本,等等。我四处搜索,解决方案似乎是关于从psql客户端加载.sql文件(参见例如 postgreSQL - psql \ i:如何执行给定路径中的脚本)。我感兴趣的是从sql脚本中加载库.sql脚本。

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。我正在使用PL / pgSQL(PostgreSQL 9.3)。

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

推荐答案

基于答案可以从SQL脚本中引用另一个SQL文件,在PostgreSQL上,只需使用 \ i 语法。我刚刚测试过并且在PostgreSQL 9.6上运行良好:

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;

这篇关于如何从PostgreSQL中的sql脚本导入外部sql脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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