Thread: Using a RETURN NEXT

Using a RETURN NEXT

From
"Mr Weinbach, Larry"
Date:
Hi all,

I found this example from Postgres site

create or replace function GetRows(text) returns setof
record as
'
declare   r record;
begin   for r in EXECUTE ''select * from '' || $1 loop       return next r;   end loop;   return;
end
'
language 'plpgsql';


I am trying to use the same idea but in this way ..

CREATE OR REPLACE FUNCTION word_case() RETURNS setof
RECORD AS'
DECLARE reg record; BEGIN    FOR reg IN SELECT message FROM rtable LOOP           RETURN next reg;    END LOOP;
RETURN;END;
 
' LANGUAGE 'plpgsql';

But at execution time I am getting thi error :

WARNING:  Error occurred while executing PL/pgSQL
function word_case
WARNING:  line 5 at return next
ERROR:  Set-valued function called in context that
cannot accept a set

I also tried using my own type defined but I got the
same error.

Any hint or idea will be appreciated ...

Regards

Larry

_________________________________________________________
Do You Yahoo!?
Información de Estados Unidos y América Latina, en Yahoo! Noticias.
Visítanos en http://noticias.espanol.yahoo.com


Re: Using a RETURN NEXT

From
Joe Conway
Date:
Mr Weinbach, Larry wrote:
> But at execution time I am getting thi error :
> 
> WARNING:  Error occurred while executing PL/pgSQL
> function word_case
> WARNING:  line 5 at return next
> ERROR:  Set-valued function called in context that
> cannot accept a set
> 

You didn't show the execution time SQL statement, but the error you are 
getting usually indicates you did something like:
  SELECT word_case();

but you should have done:
  (define the function to return setof record)  SELECT * FROM word_case() AS (message text);

or
  CREATE TYPE word_case_type AS (message text);  (define the function to return setof word_case_type)  SELECT * FROM
word_case();

HTH,

Joe



Re: Using a RETURN NEXT

From
"Mendola Gaetano"
Date:
From: "Mr Weinbach, Larry" <larryweinbach@yahoo.com>

> But at execution time I am getting thi error :
> 
> WARNING:  Error occurred while executing PL/pgSQL
> function word_case
> WARNING:  line 5 at return next
> ERROR:  Set-valued function called in context that
> cannot accept a set
> 
> I also tried using my own type defined but I got the
> same error.
> 
> Any hint or idea will be appreciated ...

I guess that you are calling that function in this way:
#select word_case();

that function is a "table function" so you should use it like a table:

#select * from word_case();


Regards
Gaetano Mendola










Re: Using a RETURN NEXT

From
"Mr Weinbach, Larry"
Date:
Thanks a lot Gaetano, Joe

Now, it is working.
--- Mendola Gaetano <mendola@bigfoot.com> escribió: >
From: "Mr Weinbach, Larry" <larryweinbach@yahoo.com>
> 
> > But at execution time I am getting thi error :
> > 
> > WARNING:  Error occurred while executing PL/pgSQL
> > function word_case
> > WARNING:  line 5 at return next
> > ERROR:  Set-valued function called in context that
> > cannot accept a set
> > 
> > I also tried using my own type defined but I got
> the
> > same error.
> > 
> > Any hint or idea will be appreciated ...
> 
> I guess that you are calling that function in this
> way:
> #select word_case();
> 
> that function is a "table function" so you should
> use it like a table:
> 
> #select * from word_case();
> 
> 
> Regards
> Gaetano Mendola
> 
> 
> 
> 
> 
> 
> 
>  

_________________________________________________________
Do You Yahoo!?
Información de Estados Unidos y América Latina, en Yahoo! Noticias.
Visítanos en http://noticias.espanol.yahoo.com