CREATE OR REPLACE function f_trova_ultimo_proc_concluso( p_msisdn varchar2, p_id_richiesta out varchar2, p_data out date, p_processo out varchar2 ) return number is v_result number; begin with t_richieste as( select id_richiesta, dataricezionerichiesta as data,'D' as processo from mnp_gestione_richiesta where msisdn= p_msisdn and stato=16 union select id_richiesta, dataricezionerichiesta as data,'R' as processo from mnp_gestione_richiesta_rec where msisdn= p_msisdn and stato=12 union select id_richiesta, dataricezionerichiesta as data,'V' as processo from mnp_gest_rich_donor_virt where msisdn= p_msisdn and stato=11 union select id_richiesta, dataricezionerichiesta as data,'W' as processo from mnp_gest_rich_rec_virt where msisdn= p_msisdn and stato=12 union select id_richiesta, dataricezionerichiesta as data,'P' as processo from mnp_gestione_richiesta_porting where msisdn= p_msisdn and stato=6 union select id_richiesta, dataricezionerichiesta as data,'C' as processo from mnp_gestione_richiesta_cess where msisdn= p_msisdn and stato=3 union select id_richiesta, dataricezionerichiesta as data,'K' as processo from mnp_gestione_rich_cess_port where msisdn= p_msisdn and stato=2 ) SELECT * into p_id_richiesta, p_data, p_processo from t_richieste WHERE DATA = ( SELECT MAX(DATA) FROM T_RICHIESTE ) and rownum < 2; v_result := 0; return v_result; exception when others then v_result := 1; return v_result; end f_trova_ultimo_proc_concluso; /