我有以下错误,这个错误只发生在第二次执行导致计划缓存
ERROR: el tipo del parámetro 11 (character varying) no coincide aquel con que fue preparado el plan (text) Where: PL/pgSQL function graficar(character varying,character varying,character varying) line 22 at asignación
character varying
这意味着以下函数中的变量或值与第 22 行中的/不匹配text
。没有明确定义文本值,我想它发生在查询字符串的串联中:
CREATE OR REPLACE FUNCTION graficar(tabla character varying, columna character varying, valor character varying) RETURNS SETOF resultado As $$
DECLARE
r resultado;
i record;
query character varying = '';
limite character varying = '';
BEGIN
IF columna <> '' THEN
limite = ' where ' || columna || ' = $1';
END IF;
IF tabla = 'edad' THEN
query = 'WITH ranges AS ( SELECT (ten*10)::text ||''-''||(ten*10+9)::text AS range, ten*10 AS r_min, ten*10+9 AS r_max FROM generate_series(0,9) AS t(ten)) SELECT r.range as nombre, count(s.*) as cuenta FROM ranges r LEFT JOIN ( select * from persona '|| limite ||' ) as s ON s.edad BETWEEN r.r_min AND r.r_max GROUP BY r.range HAVING range NOT IN (''0-9'') ORDER BY r.range;';
ELSEIF tabla = 'ingreso' THEN
query = 'WITH ranges AS ( SELECT (ten*10)::text|| ''-''||((ten*10+199))::text || '' mil'' AS range, ten*10/1000 AS r_min, (ten*10+199999)/1000 AS r_max FROM generate_series(0,(SELECT max(ingreso)/10000 FROM persona), 20) AS t(ten)) SELECT r.range as nombre, count(s.*) as cuenta FROM ranges r LEFT JOIN ( select * from persona '|| limite ||' ) as s ON s.ingreso BETWEEN r.r_min AND r.r_max GROUP BY r.range ORDER BY r.range;';
ELSE
query = 'select p.nombre, count( e.* ) as cuenta from ' || tabla::regclass ||' p left join ( select * from persona ' || limite ||' ) as e on p.nombre = e.'|| tabla::regclass ||' group by p.nombre ';
END IF;
FOR i IN EXECUTE query USING valor LOOP -- I DONT KNOW EXACTLY WHERE LINE 22 I SUPOSSE IS THIS
r = (i.nombre, i.cuenta);
RETURN NEXT r;
END LOOP;
END
$$ LANGUAGE plpgsql;