This PostgreSQL COPY command works:
copy a from '/tmp/main.csv' WITH (FORMAT csv)
but I want the tablename and filepath to be dynamically generated. How can I do this?
I did try with following by calling the select import_csv('/tmp/main.csv','a');
CREATE OR REPLACE FUNCTION import_csv(
csv_path text,
target_table text)
RETURNS void AS
$BODY$
begin
set schema 'public';
raise notice 'CSV PATH: %,TABLE NAME: %',csv_path,target_table;
execute format('copy %I from %I WITH (FORMAT csv)',target_table, csv_path);
return;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION import_csv(text, text)
OWNER TO postgres;
I got Error:
NOTICE: CSV PATH: /tmp/main.csv,TABLE NAME: a
ERROR: syntax error at or near ""/tmp/main.csv""
LINE 1:
copy a from "/tmp/main.csv" WITH (FORMAT csv)