从PostgreSQL函数获取数据到Java
我已经在PostgreSQL
数据库中编写了一个简单的函数。从我的JAVA
源代码中,我像这样调用此函数
SELECT getData('active');
我的数据正确无误,但数据集的表头显示的是我的函数名(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 invINNER JOIN
(SELECT getdata('active')) as usr ON (usr.userid=inv.userid_edit)
以下查询工作正常:
SELECT usr.username FROM cust_invoice_index as invINNER JOIN
(SELECT userid, username from user_index where status='active') as usr ON (usr.userid=inv.userid_edit)
回答:
当函数返回结果集时,您应该使用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
以上是 从PostgreSQL函数获取数据到Java 的全部内容, 来源链接: utcz.com/qa/425656.html