POSTGRES如何创建返回查询结果的函数或过程?

时间:2019-04-27 15:21:02

标签: sql postgresql stored-functions

我有此查询返回了一条记录, 我想创建一个返回查询的每一列的函数或过程

CREATE FUNCTION sp_infr(
ano_inicial int, 
ano_final int)  RETURNS SETOF record AS $$
SELECT "I"."PTCodPapeleta",
"I"."PTFechaHora"::timestamp::date,
RTRIM("I"."PTCodInfraccion"),
"I"."PTCodPlaca",
"I"."PTNombApelConductor",
"I"."PTDniConductor","I"."PTCodiConductor","I"."PTDomiConductor",'',"I"."PTImporte"
FROM "t700infractrans" as "I","t700deudas" as "D"
WHERE EXTRACT(YEAR from "PTFechaHora") >= ano_inicial and EXTRACT(YEAR from "PTFechaHora") <= ano_final
and "I"."PTCodPapeleta" = "D"."CodPapel" and "D"."Estado" = 'P'
ORDER BY 2;
$$ LANGUAGE SQL;

选择功能

select sp_infr(2014,2018);

返回

 sp_infr 
 -------------------------------------------------------
 (A1401008167,2014-01-01,M-3,S20874,"CASTRO CORDOVA JOSE RUSBER",#,00000521539,"TARAPOTO - JR JR. PROGRESO, 664","",1900.000)
 -------------------------------------------------------
 (A1401008216,2014-01-01,G-58,S13614,"ISUIZA  ISUIZA SEGUNDO JUSTO",45829634,00000547926,"TARAPOTO -  ,  - DIR.REFER.:JR. RAMON CASTILLA S/N - BELLAVISTA","",304.000)

(2 row)

我想分别返回每一列

PTCodPapeleta | PDTFechaHora | rtrim | PTCodPlaca | PTNombApelConductor | 
---------------+--------------+-------+------------+---------------------+ ...->>>>>
 A2015032603   | 2014-01-01   | G-59  |   S2332    |      Jet Lee
---------------|--------------|-------|------------|---------------------|
 ...

0 个答案:

没有答案