Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

339
Views
Does not exist column when creating postgresql function

I try to create a dynamic function that exports data specific ids controlled by a for, but when I run the function it shows me the error that there is no column i , I would like to concatenate the variable i with the name of the output file

CREATE OR REPLACE FUNCTION mifuncion() RETURNS void AS $$
BEGIN
  FOR i IN 1..5 LOOP
    copy (select nombre,dni,edad from test where id=i) TO 'C:\Users\Usuario\Documents\user'+i+'.csv' WITH  CSV HEADER;
  END LOOP;
END;
$$ LANGUAGE plpgsql;
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

COPY doesn't support query parameters, so you can't use PL/PGSQL variables with COPY.
You should use dynamic SQL with EXECUTE instead. You could declare variables to store query statement and file path, and then execute it like this

EXECUTE FORMAT('COPY (%s) TO %L WITH CSV HEADER', your_query, file_path);

OR

EXECUTE FORMAT('COPY (%s) TO %L WITH CSV HEADER', 'SELECT nombre, dni, edad FROM test WHERE id = ' || i, 'C:\Users\Usuario\Documents\user' || i || '.csv');
over 4 years ago · Santiago Trujillo Report

0

You could use dynamic SQL with EXECUTE instead,like this

statement :='COPY (select nombre,dni,edad from test where id=' || i || ') to ''C:\Users\Usuario\Documents\user' || i ||'.csv''';
EXECUTE statement;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!