pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
dynamically generate path to output file
2+ messages / 2 participants
[nested] [flat]

* dynamically generate path to output file
@ 2022-03-26 09:35  robert@redo2oo.ch <robert@redo2oo.ch>
  0 siblings, 1 reply; 2+ messages in thread

From: robert@redo2oo.ch @ 2022-03-26 09:35 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org, robert rottermann <robert@redcor.ch>

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

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: dynamically generate path to output file
@ 2022-03-26 12:48  Osvaldo Kussama <osvaldo.kussama@gmail.com>
  parent: robert@redo2oo.ch <robert@redo2oo.ch>
  0 siblings, 0 replies; 2+ messages in thread

From: Osvaldo Kussama @ 2022-03-26 12:48 UTC (permalink / raw)
  To: robert@redo2oo.ch <robert@redo2oo.ch>; +Cc: pgsql-general@lists.postgresql.org, robert rottermann <robert@redcor.ch>

2022-03-26 6:35 GMT-03:00, robert@redo2oo.ch <robert@redo2oo.ch>:
> 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
>
>

Look at EXECUTE statement:
43.5.4. Executing Dynamic Commands
https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

Osvaldo





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2022-03-26 12:48 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-03-26 09:35 dynamically generate path to output file robert@redo2oo.ch <robert@redo2oo.ch>
2022-03-26 12:48 ` Osvaldo Kussama <osvaldo.kussama@gmail.com>

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