0

I'm trying to export a series of csv files by symbol

CREATE OR REPLACE FUNCTION exportcrypto()
  RETURNS void AS
$func$
  declare
    irec record;
BEGIN
FOR irec IN
    SELECT DISTINCT symbol FROM daily
    LOOP
   EXECUTE 
   'COPY (SELECT o as open, h as high, l as low, c as close, v as volume, 0.0 as dividend, 1.0 as split FROM daily WHERE symbol =' || irec.symbol || ')
   TO "C:/Users/d/Documents/sdata/db_exports/crypto/' || irec.symbol || '.csv" WITH DELIMITER "," CSV HEADER;'
END LOOP;
END
$func$  
LANGUAGE plpgsql;

I'm getting a variety of errors like syntax at END LOOP or at TO and have tried variations of the copy string.

1 Answer 1

1

Replace double quotes with single quotes and delete the semicolon, and unless symbol is numeric (assumed not), add quotes around that too:

EXECUTE 
'COPY (SELECT o as open, h as high, l as low, c as close, v as volume, 0.0 as dividend, 1.0 as split FROM daily WHERE symbol = ''' || irec.symbol || ''') TO ''C:/Users/d/Documents/sdata/db_exports/crypto/' || irec.symbol || '.csv'' WITH DELIMITER '','' CSV HEADER';

A literal single quote ' is coded as a doubled single quote ''.

Also rather than function that returns void, define it as a procedure, and LANGUAGE goes first too, so the whle thing should be:

CREATE OR REPLACE PROCEDURE exportcrypto()
LANGUAGE plpgsql
AS $func$
  declare irec record;
BEGIN
FOR irec IN SELECT DISTINCT symbol FROM daily LOOP
    EXECUTE 
'COPY (SELECT o as open, h as high, l as low, c as close, v as volume, 0.0 as dividend, 1.0 as split FROM daily WHERE symbol = ''' || irec.symbol || ''') TO ''C:/Users/d/Documents/sdata/db_exports/crypto/' || irec.symbol || '.csv'' WITH DELIMITER '','' CSV HEADER';
END LOOP;
END
$func$
Sign up to request clarification or add additional context in comments.

1 Comment

@Bohemain You might want to mention that procedures are valid only in Postgres v11 or higher. The language specification can go either before or after. It is basically a preference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.