【发布时间】:2023-03-05 06:16:01
【问题描述】:
我有一个用于更改数据库的 SQL 脚本。出于审计和可追溯性的原因,我希望脚本根据 DBNAME 和时间戳生成输出文件名。
db_test=> select 'script_output-' || :'DBNAME' || '-' ||
to_char(now(),'yyyymmdd-HH24MISS') || '.txt' as spoolfile;
spoolfile
--------------------------------------------
script_output-db_test-20190718-163936.txt
但是,当我尝试将查询结果分配给 \out 运算符时,我得到以下
db_test=> \out select 'script_output-' || :'DBNAME' || '-' || to_char(now(),'yyyymmdd-HH24MISS') || '.txt'
\out: extra argument "script_output-" ignored
\out: extra argument "||" ignored
\out: extra argument ":'DBNAME'" ignored
\out: extra argument "||" ignored
\out: extra argument "-" ignored
\out: extra argument "||" ignored
\out: extra argument "to_char(now(),yyyymmdd-HH24MISS)" ignored
\out: extra argument "||" ignored
\out: extra argument ".txt" ignored
实际创建的输出文件名为select
SDV184022L:~ myuser$ ls -la Downloads/DB-Install/sele*
-rw-r--r-- 1 myuser myuser 0B Jul 18 11:42 Downloads/DB-Install/select
基于这些结果和大量搜索,我将假设这是不可能的,那么有没有人有另一种方法来完成为输出指令生成动态文件名?
【问题讨论】:
标签: postgresql psql