Hello there, I have this issue in either postgreSQL 8.3 or 8.4:
I've created a script which (re)creates three temp sequences and then uses them to transpose some tables in order to get a single row.
Now I want to put it in a FUNCTION which returns a type already created (I've done a select into and then I took the fields from the created table to build the custom type). Here is the script... the thing is, pgsql says that the relation 'q' does not exist and the execution of this SQL w/out the function is successful... should I guess that the sequence creation can't be done inside a SQL function? Thanks!!
CREATE OR REPLACE FUNCTION myNewFunction(int) RETURNS SETOF myExistingType AS $$
DROP SEQUENCE IF EXISTS p;
CREATE TEMP sequence p;
DROP SEQUENCE IF EXISTS q;
CREATE TEMP sequence q;
DROP SEQUENCE IF EXISTS r;
CREATE TEMP sequence r;
SELECT
a.ad_field1,
a.ad_field2,
a.ad_field3,
...
a.consecutive
children.*,
people.*,
reffinancieras.*,
CAST(character varying 'test' as text)
--INTO test_table
FROM
pit_adquiriente AS a,
(SELECT
MAX(consulta.n1) as nombrehijo1,
MAX(consulta.f1) as fechahijo1,
MAX(consulta.n2) as nombrehijo2,
MAX(consulta.f2) as fechahijo2,
MAX(consulta.n3) as nombrehijo3,
MAX(consulta.f3) as fechahijo3,
MAX(consulta.n4) as nombrehijo4,
MAX(consulta.f4) as fechahijo4
FROM (SELECT adquirientepadre, nextval('q') as consecutivo,
CASE WHEN currval('q')=1 THEN nombrehijo END as n1,
CASE WHEN currval('q')=1 THEN fechanacimiento END as f1,
CASE WHEN currval('q')=2 THEN nombrehijo END as n2 ,
CASE WHEN currval('q')=2 THEN fechanacimiento END as f2 ,
CASE WHEN currval('q')=3 THEN nombrehijo END as n3 ,
CASE WHEN currval('q')=3 THEN fechanacimiento END as f3,
CASE WHEN currval('q')=4 THEN nombrehijo END as n4 ,
CASE WHEN currval('q')=4 THEN fechanacimiento END as f4
FROM pit_adq_hijo
WHERE adquirientepadre = $1)
AS consulta) AS children,
(SELECT
MAX(consulta.nomapeacc1) as nombreapellidoacc1,
MAX(consulta.identacc1) as idacc1,
MAX(consulta.tipoid1) as tipoidacc1,
MAX(consulta.nomapeacc2) as nombreapellidoacc2,
MAX(consulta.identacc2) as idacc2,
MAX(consulta.tipoid2) as tipoidacc2,
MAX(consulta.nomapeacc3) as nombreapellidoacc3,
MAX(consulta.identacc3) as idacc3,
MAX(consulta.tipoid3) as tipoidacc3,
MAX(consulta.nomapeacc4) as nombreapellidoacc4,
MAX(consulta.identacc4) as idacc4,
MAX(consulta.tipoid4) as tipoidacc4,
MAX(consulta.nomapeacc5) as nombreapellidoacc5,
MAX(consulta.identacc5) as idacc5,
MAX(consulta.tipoid5) as tipoidacc5
FROM (SELECT codigo_adquiriente, nextval('p') as consecutivo,
CASE WHEN currval('p')=1 THEN nombre_apellidos END as nomapeacc1,
CASE WHEN currval('p')=1 THEN identificacion END as identacc1,
CASE WHEN currval('p')=1 THEN tipo_identificacion END as tipoid1 ,
CASE WHEN currval('p')=2 THEN nombre_apellidos END as nomapeacc2,
CASE WHEN currval('p')=2 THEN identificacion END as identacc2,
CASE WHEN currval('p')=2 THEN tipo_identificacion END as tipoid2 ,
CASE WHEN currval('p')=3 THEN nombre_apellidos END as nomapeacc3,
CASE WHEN currval('p')=3 THEN identificacion END as identacc3,
CASE WHEN currval('p')=3 THEN tipo_identificacion END as tipoid3 ,
CASE WHEN currval('p')=4 THEN nombre_apellidos END as nomapeacc4,
CASE WHEN currval('p')=4 THEN identificacion END as identacc4,
CASE WHEN currval('p')=4 THEN tipo_identificacion END as tipoid4 ,
CASE WHEN currval('p')=5 THEN nombre_apellidos END as nomapeacc5,
CASE WHEN currval('p')=5 THEN identificacion END as identacc5,
CASE WHEN currval('p')=5 THEN tipo_identificacion END as tipoid5
FROM pit_people
WHERE codigo_adquiriente = $1)
AS consulta) AS people,
(SELECT
MAX(consulta.entfin1) as entidadreff1,
MAX(consulta.prod1) as productoreff1,
MAX(consulta.num1) as numeroreff1,
MAX(consulta.suc1) as sucursalreff1,
MAX(consulta.entfin2) as entidadreff2,
MAX(consulta.prod2) as productoreff2,
MAX(consulta.num2) as numeroreff2,
MAX(consulta.suc2) as sucursalreff2
FROM (SELECT codigo_adquiriente, nextval('r') as consecutive,
CASE WHEN currval('r')=1 THEN entity END as entfin1,
CASE WHEN currval('r')=1 THEN something END as prod1,
CASE WHEN currval('r')=1 THEN number_something END as num1 ,
CASE WHEN currval('r')=1 THEN place_thing END as suc1 ,
CASE WHEN currval('r')=2 THEN entity END as entfin2,
CASE WHEN currval('r')=2 THEN something END as prod2,
CASE WHEN currval('r')=2 THEN number_something END as num2 ,
CASE WHEN currval('r')=2 THEN place_thing END as suc2
FROM pit_money
WHERE codigo_adquiriente = $1)
AS consulta) AS reffinancieras
WHERE a.consecutive = $1
$$ LANGUAGE SQL;