Ver Mensaje Individual
  #2 (permalink)  
Antiguo 16/04/2013, 07:16
Avatar de jlct
jlct
 
Fecha de Ingreso: abril-2012
Ubicación: Venezuela
Mensajes: 148
Antigüedad: 12 años, 7 meses
Puntos: 19
Respuesta: Crear función que se recorra a ella misma

Hola Buenos días, ya encontré la solución a lo que buscaba y quiero compartirla por si a alguien le sirve.

Código:
CREATE OR REPLACE FUNCTION cust_fact_acct_summary(p_account_id numeric,p_ad_client_id numeric) RETURNS SETOF RECORD AS 
$BODY$
DECLARE 
rs record;
recor record;
rsl record;
sumary char(1);
BEGIN
    IF $1 is null THEN 
        FOR rsl IN Select rsu.c_elementvalue_id, rsu.pa_reportline_id from c_elementvalue ev inner join pa_reportsource rsu on ev.c_elementvalue_id = rsu.c_elementvalue_id where ev.ad_client_id = $2
        LOOP 
            FOR rs IN Select Distinct rsl.pa_reportline_id, rsl.c_elementvalue_id,DATEACCT, AmtSourceDR, AmtSourceCR, AmtSourceN from cust_fact_acct_summary(rsl.c_elementvalue_id,$2) AS (Account_ID numeric, DATEACCT timestamp, AmtSourceDR numeric, AmtSourceCR numeric, AmtSourceN numeric)
            LOOP 
                return next rs;
            END LOOP;
        END LOOP;
    ELSE 
        sumary := (Select issummary from c_elementvalue where c_elementvalue_id = $1);
        IF sumary = 'Y' THEN
            FOR recor IN Select node_id from ad_treenode tn inner join ad_tree t on tn.ad_tree_id = t.ad_tree_id where parent_id = $1 and t.treetype = 'EV' 
            LOOP
                FOR rs IN Select * from cust_fact_acct_summary(recor.node_id,$2) AS (Account_ID numeric, DATEACCT timestamp, AmtSourceDR numeric, AmtSourceCR numeric, AmtSourceN numeric)
                LOOP 
                    return next rs;
                END LOOP;
            END LOOP;
        ELSE 
            FOR rs in Select Account_ID, DATEACCT, Sum(AmtSourceDR) as AmtSourceDR,Sum(AmtSourceCR) As AmtSourceCR,Sum(AmtSourceDR-AmtSourceCR) as AmtSourceN From FACT_ACCT where Account_ID = $1 Group By Account_ID,DATEACCT
            LOOP 
                return next rs;
            END LOOP;
        END IF;
    END IF;
END;
$BODY$
LANGUAGE 'plpgsql'