Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

342
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda