pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: robert@redo2oo.ch <robert@redo2oo.ch>
To: pgsql-general@lists.postgresql.org, robert rottermann <robert@redcor.ch>
Subject: dynamically generate path to output file
Date: Sat, 26 Mar 2022 10:35:23 +0100
Message-ID: <bd57256b-162c-ac18-ecc0-968ee582b744@redo2oo.ch> (raw)

Hi Friends

I would like to generate the path of a CSV file to which I output a query.

This is what I try:

CREATEORREPLACEPROCEDUREexport_cvs(
home_dir varchar
)
AS
$BODY$
DECLARE
OUTFILE varchar;
BEGIN
OUTFILE = (home_dir || '/tmp/company.csv');
copy(
select'company_'||id as"External ID",
nameas"Name",'True'as"Is a Company",
email,
phone ,
company_registry
fromres_company
) TOOUTFILE withCSV HEADER;
END;
$BODY$
LANGUAGEplpgsql;

this produces a syntax error:

psql:export_contacts_short.sql:21: ERROR:  syntax error at or near "OUTFILE"
LINE 17:     ) TO OUTFILE  with CSV HEADER;


It works fine when I replace "TO OUTFILE" with a hard coded string.

Can anybody of you give me a hint how to do that?

Thanks a lot.

Robert

view thread (2+ messages)  latest in thread

Message-ID: <bd57256b-162c-ac18-ecc0-968ee582b744@redo2oo.ch>
Permalink:  ../bd57256b-162c-ac18-ecc0-968ee582b744@redo2oo.ch/
Also on:    postgresql.org/message-id/bd57256b-162c-ac18-ecc0-968ee582b744@redo2oo.ch

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: robert@redo2oo.ch, robert@redcor.ch
  Subject: Re: dynamically generate path to output file
  In-Reply-To: <bd57256b-162c-ac18-ecc0-968ee582b744@redo2oo.ch>
  References: <bd57256b-162c-ac18-ecc0-968ee582b744@redo2oo.ch>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox