Postgres函数返回一条记录,而我有很多记录?

时间:2014-05-27 11:50:16

标签: sql postgresql postgresql-9.1 plpgsql set-returning-functions

我有很多记录,我的简单查询返回但是当我使用函数时它只给我第一条记录,

首先我使用

创建自己的数据类型
CREATE TYPE my_type (usr_id integer , name varchar(30));

我的功能是,

CREATE OR REPLACE function test() returns my_type as $$
    declare rd varchar := '21';
    declare personphone varchar := NULL;
    declare result my_type;
    declare SQL VARCHAR(300):=null; 
DECLARE
    radiophone_clause text = '';

BEGIN        
    IF rd IS NOT NULL then
        radiophone_clause = 'and pp.radio_phone = '|| quote_literal(rd);
    END IF;

    IF personphone IS NOT NULL then      
        radiophone_clause = radiophone_clause|| 'and pp.person_phone = '|| quote_literal(personphone);
    END IF;
    radiophone_clause = substr(radiophone_clause, 5, length(radiophone_clause)- 4);

     EXECUTE format('select pt.id,pt.name from product_template pt inner join product_product pp on pt.id=pp.id where %s ;', radiophone_clause) into result.id,result.name ;
    return result;
END;
$$ LANGUAGE plpgsql;

在这个函数中我返回my_type,它只返回第一行如何返回多行,

2 个答案:

答案 0 :(得分:2)

要从plpgsql函数返回复合类型集,您应该:

  • 将函数的返回类型声明为setof composite_type
  • 使用return query(或return next)指令(documentation)

我仅在更改返回类型的上下文中编辑了代码(仅作为示例):

DROP function test();   -- to change the return type one must drop the function

CREATE OR REPLACE function test() 
-- returns my_type as $$
returns setof my_type as $$                    -- (+)
    declare rd varchar := '21';
    declare personphone varchar := NULL;
--    declare result my_type;
--    declare SQL VARCHAR(300):=null; 
DECLARE
    radiophone_clause text = '';

BEGIN        
    IF rd IS NOT NULL then
        radiophone_clause = 'and pp.radio_phone = '|| quote_literal(rd);
    END IF;

    IF personphone IS NOT NULL then      
        radiophone_clause = radiophone_clause|| 'and pp.person_phone = '|| quote_literal(personphone);
    END IF;
    radiophone_clause = substr(radiophone_clause, 5, length(radiophone_clause)- 4);

    RETURN QUERY                               -- (+)
    EXECUTE format('select pt.id,pt.name from product_template pt inner join product_product pp on pt.id=pp.id where %s ;', radiophone_clause)
    ;                                          -- (+)
--    into result.id,result.name;
--    return result;
END;
$$ LANGUAGE plpgsql;

答案 1 :(得分:2)

您需要返回setof my_type,如果我了解您的需求,则不需要动态SQL

create or replace function test() returns setof my_type as $$
declare
    rd varchar := '21';
    personphone varchar := NULL;
begin
    return query
    select pt.id, pt.name
    from
        product_template pt
        inner join
        product_product pp using(id)
    where
        (pp.radio_phone = rd or rd is null)
        and
        (pp.person_phone = personphone or personphone is null)
    ;
end;
$$ language plpgsql;

如果你传递参数,它可以是普通的sql

create or replace function test(rd varchar, personphone varchar)
returns setof my_type as $$
    select pt.id, pt.name
    from
        product_template pt
        inner join
        product_product pp using(id)
    where
        (pp.radio_phone = rd or rd is null)
        and
        (pp.person_phone = personphone or personphone is null)
    ;
$$ language sql;