plpgsql Select statement in For loop to create multiple CSV files

Viewed 2439

I would like to repeat the following query 8760 times, replacing ‘2’ with 1 to 8760 for every hour in the year. The idea is to create a separate CSV file for each hour for further processing.

COPY 
(SELECT *
FROM 
  public.completedsolarirad2012
WHERE 
  completedsolarirad2012."UniquetmstmpID" = 2)
TO 'C:\temp\2012hour2.csv' WITH DELIMITER ','
CSV HEADER

I have put together the following function (testing with only a few hours):

CREATE OR REPLACE FUNCTION everyhour()
  RETURNS void AS
$BODY$BEGIN
FOR i IN 0..5 LOOP
  EXECUTE $x$ 
  COPY (
    SELECT *
    FROM 
    public.completedsolarirad2012
    WHERE 
    completedsolarirad2012."UniquetmstmpID" = i
    )
   TO $concat$ 'C:\temp.' || i::text 
         || '.out.csv' WITH DELIMITER ',' CSV HEADER $concat$
    $x$;
END LOOP;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION everyhour()
  OWNER TO postgres;

I seem to be having two separate problems:

Firstly, I’m getting:

Error: column "i" does not exist.

Secondly, when I test the concatenation statement only by replacing “i” with e.g. “2”, I get:

Error: relative path not allowed for COPY to file

I am the postgres superuser, so I do not understand why I am having this problem.

Note: Removing the $concat$ double-quoting around the concatenation statement gives the following error:

ERROR:  syntax error at or near "||"
LINE 9:    TO 'C:\temp.' || i::text

I would be very grateful for any help.

1 Answers
Related