2016-06-07 66 views
1

我想從存儲過程中返回存儲在臨時表中的查詢的結果。我不確定最後的陳述應該是什麼。任何幫助appriciated:如果你不使用TEMP表爲別的Postgresql:從存儲過程返回臨時表

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; 

回答

2

你的最後一句話會是這樣

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

,沒有必要創建一個臨時表所有..你可以直接做

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; 
+0

完美。順便說一下,在第一個解決方案中,列列表中的表名應該是'result'而不是'generic_info'。 – Hans