a problem of function return setof

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ycjack
    New Member
    • May 2008
    • 4

    #1

    a problem of function return setof

    Hi, all
    Following is a simple function which returns type of setof:

    [CODE=SQL]CREATE OR REPLACE FUNCTION tesp_report_que ry()
    RETURNS SETOF rpt.tesp_report _query AS
    $BODY$
    DECLARE
    rec record;
    BEGIN
    create temp table tmpt_1 on commit drop as
    select * from test_item_info;

    FOR rec IN
    select * from tmpt_1 LOOP
    RETURN NEXT rec;
    END LOOP;

    RETURN;

    END;$BODY$
    LANGUAGE 'plpgsql';[/CODE]
    -------
    It works fine except that it only works once in a connection session. To make it work again have to restart a new connection. Is there any way to fix it? Thank you very much!

    Jack
    Last edited by eWish; May 15 '08, 04:11 AM. Reason: Please use code tags when posting code
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    Originally posted by ycjack
    Hi, all
    Following is a simple function which returns type of setof:

    CREATE OR REPLACE FUNCTION tesp_report_que ry()
    RETURNS SETOF rpt.tesp_report _query AS
    $BODY$
    DECLARE
    rec record;
    BEGIN
    create temp table tmpt_1 on commit drop as
    select * from test_item_info;

    FOR rec IN
    select * from tmpt_1 LOOP
    RETURN NEXT rec;
    END LOOP;

    RETURN;

    END;$BODY$
    LANGUAGE 'plpgsql';
    -------
    It works fine except that it only works once in a connection session. To make it work again have to restart a new connection. Is there any way to fix it? Thank you very much!

    Jack
    You have to use dynamic queries, check if this works for you
    Code:
    CREATE OR REPLACE FUNCTION tesp_report_query()
      RETURNS SETOF rpt.tesp_report_query AS
    $BODY$
    DECLARE
    	rec record;
    BEGIN
    	execute 'create temp table tmpt_1 on commit drop as 
    		select * from test_item_info';
    		
    	FOR rec IN  execute 'select * from tmpt_1' LOOP
    		RETURN NEXT rec;
    	END LOOP;
    
    RETURN;
    
    END;$BODY$
      LANGUAGE 'plpgsql';

    Comment

    • ycjack
      New Member
      • May 2008
      • 4

      #3
      Hi, rski

      Thank you so much! It does work.


      Jack

      Comment

      Working...