重新格式化PostgreSQL函数数据的输出

时间:2013-12-18 15:24:38

标签: sql function postgresql

我在PostgreSQL数据库中创建了以下函数

CREATE or REPLACE FUNCTION getSR15ReportData(value text) 
RETURNS  TABLE(
    userid integer,
    username varchar(50)
) AS -- text AS --
$body$
DECLARE
    fullsql TEXT;
    records RECORD;
    exeQuery TEXT;
BEGIN

fullsql:= 'SELECT userid, username from user_index where status='''value'''';

exeQuery := 'SELECT * FROM (' || fullsql || ') AS records';

RETURN QUERY EXECUTE exeQuery;

END
$body$
LANGUAGE plpgsql;

它给我输出如下:

getdate
--------------
501,alexanda
502,cathie

但我需要输出:

userid  username
------|---------
501,alexanda
502,cathie

1 个答案:

答案 0 :(得分:0)

我对这个问题很傻。这很简单。谢谢a_horse_with_no_name。他在我的另一个问题Get data from PostgreSQL function to java

上解决了这个问题

只需要调用函数:

SELECT * FROM getdata('active')

完整查询将如下所示:

SELECT usr.username 
FROM cust_invoice_index as inv
 JOIN (SELECT * FROM getdata('active')) as usr ON usr.userid=inv.userid_edit