Wednesday, May 26, 2021

How to trim output from a psql file - equivalent to SET commands in sqlplus

I have the following query, which will generate DML:
SELECT 'drop table ' || table_schema || '.' || table_name ||';'
FROM information_schema.tables
WHERE table_schema = 'myschema'
AND table_name like '%2020%'
AND table_type = 'BASE TABLE';
I put it in a file called gen_drop.sql

When executed like this:
 psql  testdb01 -f gen_drop_tab.sql -L gen_drop_tab.log
The file output will show header and row count, like this:
                   ?column?
-----------------------------------------------
 drop table myschema.tabA_2020_09;
 drop table myschema.tabB_2020_09;
 drop table myschema.tabC_2021_05;

(27 rows)

To avoid this, add the -t flag to your command line. -t is "print tuples only"
psql  testdb01 -f gen_drop_tab.sql -o drop_tab.sql -t

To translate into the world of Oracle, I consider the above to be the equivalent of spooling a file using sqlplus and adding the directives:
set trimspool on
set pages 0
set heading off
set feedback off
set verify off
set echo off

No comments:

Post a Comment