如何从postgresql函数返回单行?我应该在returns
之后使用什么类型的广告?
这些是我的代码:
create or replace function get_perf()
returns ???? as $$
select task_ext_perf.performance, task_ext_perf.unit from task_ext_perf order by task_ext_perf.tm limit 1;
$$
language sql;
答案 0 :(得分:2)
考虑这样的表User
:
User
------
id
name
您可以使用以下过程返回表格的单行:
CREATE OR REPLACE function get_user()
RETURNS User AS $$
SELECT id, name
FROM User
WHERE id = 1
$$
language sql;
如果您想要返回更详细的行,则必须返回record
,其中包含OUT
个参数。
CREATE FUNCTION get_user(OUT id integer, OUT name character varying, OUT linkedString character varying)
RETURNS record LANGUAGE sql AS $$
SELECT
u.id, u.name, ot.string
FROM Users u
INNER JOIN OtherTable ot ON ot.user_id = u.id
WHERE id = 1
$$;