Re: Procedures

Поиск
Список
Период
Сортировка
От Raymond O'Donnell
Тема Re: Procedures
Дата
Msg-id 4B7FE6C3.4030702@iol.ie
обсуждение исходный текст
Ответ на Re: Procedures  (Nilesh Govindarajan <lists@itech7.com>)
Ответы Re: Procedures  (Nilesh Govindarajan <lists@itech7.com>)
Список pgsql-general
On 20/02/2010 13:28, Nilesh Govindarajan wrote:
> Okay here's my query -
>
> select c.cid, c.subject, n.title from comments c, node n where c.nid =
> n.nid and c.status != 0;
>
> This is the query to check list of comments requiring admin approval and
> also the article titles on which this is posted.
>
> I want to see this result on the screen at psql prompt. Since it may
> return multiple rows, a cursor has to be employed here.
>
> Now if I employ a cursor here in the function/procedure, how to see the
> results ?

Have you declared your function to return SETOF the row type returned?
if so, you don't have to use a cursor, and the function will simply
return all the rows.

For example, using SQL (not tested):

  create or replace function comments_for_approval()
  returns setof record
  as
  $$
    select c.cid, c.subject, n.title
    from comments c, node n
    where c.nid = n.nid
    and c.status != 0;
  $$
  language sql;

....or something like that. If you use pl/pgsql, then you'll need to use
a different idiom:

  create or replace function comments_for_approval()
  returns setof record
  as
  $$
  declare
    m_rec record;
  begin
    for m_rec in
      select c.cid, c.subject, n.title
      from comments c, node n
      where c.nid = n.nid
      and c.status != 0
    loop
      return next m_rec;
    end loop;
    return;
  end;
  $$
  language plpgsql;

Either way, simply execute the query in psql:

  select * from comments_for_approval();

HTH,

Ray.


--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie

В списке pgsql-general по дате отправления:

Предыдущее
От: Nilesh Govindarajan
Дата:
Сообщение: Re: Procedures
Следующее
От: Thomas Kellerer
Дата:
Сообщение: Re: Procedures