Postgresql 如何组合custiom定义的变量并将其显示为postgres中表的记录

Postgresql 如何组合custiom定义的变量并将其显示为postgres中表的记录,postgresql,plpgsql,Postgresql,Plpgsql,我是plpgsql的初学者,正在从事一个项目,该项目要求我编写一个函数,以2列(res,Result)的形式返回两个变量。我已经做了相当多的搜索,但没有找到相同的答案。下面是对我的代码的引用 CREATE OR REPLACE FUNCTION propID(character varying) RETURNS SETOF RECORD AS $val$ DECLARE t_row record; res BOOLEAN; result character varyin

我是plpgsql的初学者,正在从事一个项目,该项目要求我编写一个函数,以2列(res,Result)的形式返回两个变量。我已经做了相当多的搜索,但没有找到相同的答案。下面是对我的代码的引用

CREATE OR REPLACE FUNCTION propID(character varying) 
RETURNS SETOF RECORD AS $val$
DECLARE
    t_row record;
    res BOOLEAN;
    result character varying;
    value record;
BEGIN
   FOR t_row IN SELECT property_id FROM property_table WHERE ward_id::TEXT = $1  LOOP

    RAISE NOTICE 'Analyzing %', t_row;

    res := false; -- here i'm going to replace this value with a function whos return type is boolean in future
    result := t_row.property_id;

    return next result;  --here i want to return 2 variables (res,result) in the form of two columns (id,value) 

  END LOOP;

END;
$val$
language plpgsql;

非常感谢您对上述查询的任何帮助。

假设
property\u id
ward\u id
是整数,您可以通过以下简单查询实现您的目标:

select some_function_returning_boolean(property_id), property_id
from property_table
where ward_id = 1; -- input parameter
如果您绝对需要一个函数,它可以是一个SQL函数,如

create or replace function prop_id(integer) 
returns table (res boolean, id int) language sql
as $$
    select some_function_returning_boolean(property_id), property_id
    from property_table
    where ward_id = $1
$$;
在plpgsql函数中,应使用
返回查询

create or replace function prop_id(integer) 
returns table (res boolean, id int) language plpgsql
as $$
begin
    return query
    select some_function_returning_boolean(property_id), property_id
    from property_table
    where ward_id = $1;
end
$$;

假设
property\u id
ward\u id
是整数,您可以通过以下简单查询实现您的目标:

select some_function_returning_boolean(property_id), property_id
from property_table
where ward_id = 1; -- input parameter
如果您绝对需要一个函数,它可以是一个SQL函数,如

create or replace function prop_id(integer) 
returns table (res boolean, id int) language sql
as $$
    select some_function_returning_boolean(property_id), property_id
    from property_table
    where ward_id = $1
$$;
在plpgsql函数中,应使用
返回查询

create or replace function prop_id(integer) 
returns table (res boolean, id int) language plpgsql
as $$
begin
    return query
    select some_function_returning_boolean(property_id), property_id
    from property_table
    where ward_id = $1;
end
$$;

@S.Sho-注意,
SELECT
是SQL中执行循环的一种自然方式,通常比带变量的显式循环更简单、性能更好。@S.Sho-注意,
SELECT
是SQL中执行循环的一种自然方式,通常比带变量的显式循环更简单、性能更好。