Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old May 8th, 2008, 07:53 AM
Newbie
 
Join Date: May 2008
Posts: 4
Default a problem of function return setof

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

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION tesp_report_query()
  2.   RETURNS SETOF rpt.tesp_report_query AS
  3. $BODY$
  4. DECLARE
  5.     rec record;
  6. BEGIN
  7.     create temp table tmpt_1 on commit drop as 
  8.         select * from test_item_info;
  9.  
  10.     FOR rec IN 
  11.     select * from tmpt_1 LOOP
  12.         RETURN NEXT rec;
  13.     END LOOP;
  14.  
  15. RETURN;
  16.  
  17. END;$BODY$
  18.   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

Last edited by eWish; May 15th, 2008 at 05:11 AM. Reason: Please use code tags when posting code
Reply
  #2  
Old May 8th, 2008, 09:53 PM
Moderator
 
Join Date: Dec 2006
Posts: 250
Default

Quote:
Originally Posted by ycjack
Hi, all
Following is a simple function which returns type of setof:

CREATE OR REPLACE FUNCTION tesp_report_query()
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
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION tesp_report_query()
  2.   RETURNS SETOF rpt.tesp_report_query AS
  3. $BODY$
  4. DECLARE
  5.     rec record;
  6. BEGIN
  7.     execute 'create temp table tmpt_1 on commit drop as 
  8.         select * from test_item_info';
  9.  
  10.     FOR rec IN  execute 'select * from tmpt_1' LOOP
  11.         RETURN NEXT rec;
  12.     END LOOP;
  13.  
  14. RETURN;
  15.  
  16. END;$BODY$
  17.   LANGUAGE 'plpgsql';
  18.  
Reply
  #3  
Old May 14th, 2008, 01:15 AM
Newbie
 
Join Date: May 2008
Posts: 4
Default

Hi, rski

Thank you so much! It does work.


Jack
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles