I am trying to create this statement into a function:
\copy aux("nombre") TO '/home/david/lugares.csv' delimiters ';';
So I do the next:
CREATE OR REPLACE FUNCTION crearcsv()
RETURNS void AS
$BODY$
DECLARE STATEMENT TEXT;
BEGIN
RAISE NOTICE 'CREAR CSV';
STATEMENT:= '\copy aux ("nombre") TO ''/home/david/lugares.csv'' delimiters '';'';';
RAISE NOTICE '%',STATEMENT;
EXECUTE STATEMENT;
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
But I get the next when I call to the function:
NOTICE: \copy aux ("nombre") TO '/home/david/lugares.csv' delimiters ';'; ERROR: syntax error at or near "\"
LINE 1: \copy aux ("nombre") TO '/home/david/lugares.csv' delimiters... ^
QUERY: \copy aux ("nombre") TO '/home/david/lugares.csv' delimiters ';';
CONTEXT: PL/pgSQL function crearcsv() line 7 at EXECUTE statement**
This statement works fine in PSQL console
Any help?
\copy
is a psql command that does more or less what the sql command copy does - with the most important difference that\copy
uses files on the filesystem of the computer running psql, andcopy
uses files on the server's filesystem. So, they are highly similar but not identical, and you cannot use \copy in a function as it's not an sql command. – Alginate\copy
is apsql
command and can only be executed from withinpsql
. It is not a general SQL statement. – Remorse