我已经在PostgreSQL数据库中编写了一个简单的函数。从我的JAVA源代码中,我像这样调用此函数
PostgreSQL
JAVA
SELECT getData('active');
我的数据正确无误,但数据集的表头显示的是我的函数名(getdata),而不是userid和username。在这种情况下如何获取数据?
getdata
userid
username
CREATE or REPLACE FUNCTION getData(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
我正在努力实现以下目标:
SELECT usr.username FROM cust_invoice_index as inv INNER JOIN (SELECT getdata('active')) as usr ON (usr.userid=inv.userid_edit)
以下查询工作正常:
SELECT usr.username FROM cust_invoice_index as inv INNER JOIN (SELECT userid, username from user_index where status='active') as usr ON (usr.userid=inv.userid_edit)
当函数返回结果集时,您应该使用select * from getdata('active')。
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