Postgres自定義函式返回記錄集(虛擬表結構)
阿新 • • 發佈:2019-01-09
CREATE OR REPLACE FUNCTION fun_get_real_inv_qty(pvOrderId varchar) RETURNS SETOF record AS $BODY$begin --drop table if exists tmp_1 ; --create temp table tmp_1 as return query select fp_prod_id,fq_part_no,fq_name, sum(case when fo_type='P' then -fp_qty else 0 end ) as purchase_qty, sum(case when fo_type='S' then -fp_qty else 0 end ) as saleqty, sum(case when fo_type='S' then -fp_qty when fo_type='P' then fp_qty else 0 end ) as surplus_qty from tp_send_det,to_send_note,tq_prod_mstr where fp_order_id=pvOrderId and fo_note_id=fp_note_id and fq_prod_id=fp_prod_id and fq_type='I' group by fp_prod_id,fq_part_no,fq_name ; end;$BODY$ LANGUAGE plpgsql VOLATILE COST 100 ROWS 10; ALTER FUNCTION fun_get_real_inv_qty() OWNER TO postgres;
上面是例子,呼叫這個函式:
select * from fun_get_real_inv_qty('D302') f(fp_prod_id bigint,fq_part_no varchar ,fq_name varchar ,purchase_qty numeric ,saleqty numeric ,surplus_qty numeric ) ;
f...後面帶的是記錄的column定義 必須與函式輸出的列數量及每列資料型別一一對應.