0

我正在使用 postgreSQL 中的 COPY 命令将表导出到带有标题的 CSV。即使表格结果为 0,它也只会创建带有标题的 CSV 文件。仅当行数大于 0 时,我才需要带有标题的 CSV 文件。

4

1 回答 1

0

你可以 plpgsql 它:

db=# do $$
begin
if (select count(*) from t) > 0 then
copy t to '/tmp/t' with (format csv, header);
end if;
end;
$$
;
DO
db=# \! cat /tmp/t
c
2
db=# truncate table t;
TRUNCATE TABLE
db=# \! rm /tmp/t
db=# do $$
begin
if (select count(*) from t) > 0 then
copy t to '/tmp/t' with (format csv, header);
end if;
end;
$$
;
DO
db=# \! cat /tmp/t
cat: /tmp/t: No such file or directory
于 2018-07-02T15:45:21.483 回答