Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql Postgres脚本-使用;加上;语句-要将其中一个with语句导出到文件中吗_Postgresql - Fatal编程技术网

Postgresql Postgres脚本-使用;加上;语句-要将其中一个with语句导出到文件中吗

Postgresql Postgres脚本-使用;加上;语句-要将其中一个with语句导出到文件中吗,postgresql,Postgresql,我在Linux服务器上安装了postgres 它在脚本abc.sql中使用“with”语句,如下所示 启动postgres sql脚本 with cte1 as (select …….), cte2 as (select …..) , cte3 as (select …..) Select from cte1 join cte2 …. join cte3... 脚本结束 剧本写得很好。它由shell脚本调用,输出重定向到文件,如下所示: pgsql -U tom -d db123 -f a

我在Linux服务器上安装了postgres

它在脚本abc.sql中使用“with”语句,如下所示

启动postgres sql脚本
with cte1 as (select …….), cte2 as (select …..) , cte3 as (select …..) 

Select from cte1 join cte2 …. join cte3...
脚本结束 剧本写得很好。它由shell脚本调用,输出重定向到文件,如下所示:

pgsql -U tom -d db123 -f abc.sql > /tmp/output.txt
但是,我想将“cte1”的结果单独输出到另一个输出文件。目前,我必须运行这个脚本(abc.sql,它已经计算了cte1),然后我必须运行第二个脚本(def.sql),它再次计算cte1(它自己),并将其输出发送到一个单独的输出文件

是否可以只对cte1求值一次(在脚本abc.sql中),但将结果输出到单独的文件中?我希望使用“\copy”选项(在abc.sql中),但它导致了一个错误

希望我能解释清楚


干杯

简短答案否。答案稍长,来自

WITH提供了一种编写辅助语句的方法 用于更大的查询。这些陈述经常被提及 作为通用表表达式或CTE,可以认为是定义 仅为一个查询存在的临时表。(我的重点)


基本上,您的问题与询问是否可以在另一个语句中重用subselect相同。也不能这样做。

谢谢您的回复。是的,我用临时桌子代替了,它们很有用

在sql脚本的末尾,我可以使用两个“\copy”语句输出到两个不同的文件。(显然,shell脚本不再重定向输出,而只是调用sql脚本)


祝您愉快。

您可能希望使用临时表而不是CTE。也。