views:

117

answers:

1

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;
+1  A: 

Generally I'll use arrays and generate_series to do stuff like this.

SELECT CASE WHEN i=1 THEN nombre_apellidos END as nomapeacc1, ...
FROM pit_people p
CROSS JOIN generate_series(1,5) s(i)

But what I think you really want is to do a crosstab. http://www.postgresql.org/docs/8.3/static/tablefunc.html

Scott Bailey
This messy code is intended to retrieve info from a single person in ONE row. This includes transposing some rows from related tables (take N rows from children and put them besides the user's info).But well, I think I'm too tired to keep on this, I'll attack the problem in other way.
Alfabravo
Yeah, I know what you are trying to do. The generate_series() will allow you to get rid of the temporary sequences so you can create a view rather than using a procedure. And the link I gave you does exactly the sort of thing you are trying to do.
Scott Bailey
Forgot to check something as accepted. And this thing works, then...
Alfabravo