crear funcion para devolver un fetch all de un cursor a partir de otra funcion

From: Marcos Luis Ortiz Valmaseda <marcosluis2186(at)googlemail(dot)com>
To: pgsql-es-ayuda(at)postgresql(dot)org
Subject: crear funcion para devolver un fetch all de un cursor a partir de otra funcion
Date: 2011-08-05 15:29:48
Message-ID: CAJs-K1uA+ZrbZ+Da_TdsUWUVD==H0458Fmk39HZctmjycHbFZg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Saludos
Estoy en creación de una función que recive como parámetro el nomre de otra
función, sus parámetros en un arreglo, y un refcursor que devuelve la misma.

La idea general es usar esta función para devolver todos los campos del
cursor de la otra función y así poder aplicarle ORDER BY, GROUP BY y
cualquier operación sobre uno de eso campos.

Acá está el código de la función:
CREATE OR REPLACE FUNCTION public.return_refcursor_fetch_all(text,
refcursor, anyarray)
RETURNS REFCURSOR AS $$
DECLARE
v_func ALIAS FOR $1; -- Funcion original
v_ref ALIAS FOR $2; -- Ref cursor de la funcion original
v_params ALIAS FOR $3; -- Parametros de la funcion
v_array_position int4; -- Posicion del arreglo para la comparacion
v_temp_text text ; -- Variable temporal para la consulta
BEGIN
-- Insertar codigo de la funcion en forma de texto y ejecutarla a una
variable
v_array_position := 0;
v_temp_text := '';

-- Recorrer todo el array para construir la candena de los parametros
WHILE v_array_position <= lenght(v_params) LOOP
v_temp_text := v_params[v_array_position] || ','
v_array_position = v_array_position + 1
END LOOP;
-- Ejecutar la consulta construida dinamicamente usando EXECUTE
EXECUTE 'SELECT * FROM '|| v_func ||'('|| v_temp_text || ')'' INTO ' ||
v_ref || '';
OPEN v_ref;
RETURN v_ref;
END;
$$ LANGUAGE plpgsql;

Entonces el error que arroja es el siguiente:
ERROR: syntax error at or near "$2"
LÍNEA 1: SELECT $1 [ $2 ] || ',' $2 = $2 + 1 END LOOP
^
CONSULTA: SELECT $1 [ $2 ] || ',' $2 = $2 + 1 END LOOP
CONTEXTO: SQL statement in PL/PgSQL function "return_refcursor_fetch_all"
near line 15

********** Error **********

ERROR: syntax error at or near "$2"
SQL state: 42601
Context: SQL statement in PL/PgSQL function "return_refcursor_fetch_all"
near line 15

--
Marcos Luis Ortíz Valmaseda
Software Engineer (UCI)
Linux User # 418229
http://marcosluis2186.posterous.com
http://www.linkedin.com/in/marcosluis2186
https://fedoraproject.org/wiki/User:Marcosluis

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Alvaro Herrera 2011-08-05 15:30:01 Re: Incluir array_length en PostgreSQL 8.1
Previous Message Marcos Luis Ortiz Valmaseda 2011-08-05 15:23:30 Incluir array_length en PostgreSQL 8.1