Postgresql:从存储过程返回临时表

时间:2016-06-07 02:15:22

标签: sql postgresql plpgsql postgresql-9.1

我想从存储过程返回存储在临时表中的查询结果。我不确定最后的陈述应该是什么。任何帮助都是适当的:

CREATE OR REPLACE FUNCTION public.get_generic_info(
    IN in_app_id character varying,
    IN key_id uuid)
RETURNS TABLE(info text, last_update timestamp with time zone) AS
$BODY$  
BEGIN
    CREATE TEMP TABLE result AS
        SELECT  generic_info.info::text, generic_info.last_update FROM 
                generic_info 
        WHERE generic_info.app_id = in_app_id AND generic_info.id = key_id;

    -- some data manipulations based on 'result' rows       
    RETURN ???????;
END;

1 个答案:

答案 0 :(得分:2)

你最后的陈述就像这样

RETURN QUERY 
     SELECT  result.info::text, result.last_update FROM 
      result;

如果您没有使用TEMP表进行其他任何操作,则根本不需要创建临时表。您可以直接执行

RETURN QUERY
SELECT  generic_info.info::text, generic_info.last_update FROM 
                generic_info 
        WHERE generic_info.app_id = in_app_id AND generic_info.id = key_id;