I need to run the following statement in PL/pgSQL.
if file_record.has_recieved_time_column then
EXECUTE FORMAT('COPY mytemp (columnA, columnB, columnC)
FROM %L
DELIMITER '',''
CSV HEADER;', file_record.file_path);
elseif file_record.has_header then
-- dont load columnC
EXECUTE FORMAT('COPY mytemp (columnA, columnB)
FROM %L
DELIMITER '',''
CSV HEADER;', file_record.file_path);
else
-- dont load columnC and the file has no header
EXECUTE FORMAT('COPY mytemp (columnA, columnB)
FROM %L
DELIMITER '',''
CSV;', file_record.file_path);
end if;
How can I avoid repeating repeating myself in this code?
COPYstatements. What do you mean by repeating? Does the function goes on with multiple IF / ELSE ?cols = (colA, colB)then doif file_record.has_recieved_time_column: cols += colCand then passcolsas a parameter to theCOPYstatement and do something similar for theHEADER