pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
RETURN SET OF DATA WITH CURSOR
5+ messages / 3 participants
[nested] [flat]

* RETURN SET OF DATA WITH CURSOR
@ 2005-12-15 15:26 grupos <grupos@carvalhaes.net>
  2005-12-15 16:03 ` Re: RETURN SET OF DATA WITH CURSOR Richard Huxton <dev@archonet.com>
  0 siblings, 1 reply; 5+ messages in thread

From: grupos @ 2005-12-15 15:26 UTC (permalink / raw)
  To: pgsql-sql

Hi !

I am making one plpgsql function and I need to return of setof data 
using a cursor.

The problem is that my function is returning only the first row and 
column of the query. This query have more than 80 columns and 1.000 
rows. Enyone have any tip to give me?

Here the fuction...

CREATE OR REPLACE FUNCTION rel_faturamento("varchar","varchar")
   RETURNS refcursor AS
$BODY$
DECLARE
	data_inicial ALIAS FOR $1;
	data_final   ALIAS FOR $2;
	ref refcursor;
	fat_cursor CURSOR FOR
		SELECT * FROM SF2010 SF2
		INNER JOIN SD2010 SD2 ON (SD2.D2_DOC = SF2.F2_DOC)
		INNER JOIN SB1010 SB1 ON (SB1.B1_COD = SD2.D2_COD)
		INNER JOIN SA1010 SA1 ON (SF2.F2_CLIENTE = SA1.A1_COD)
		WHERE F2_EMISSAO >= data_inicial AND F2_EMISSAO <= 				data_final
		AND SF2.D_E_L_E_T_<> '*' AND sd2.d2_tes IN (SELECT 				f4_codigo FROM 
sf4010 WHERE d_e_l_e_t_ <> '*' AND f4_duplic = 'S' AND f4_codigo >= '500')
		ORDER BY SF2.F2_EMISSAO, SF2.F2_DOC, SF2.F2_HORA;

BEGIN

OPEN fat_cursor;
LOOP
FETCH fat_cursor INTO ref;

RETURN ref;
END LOOP;

CLOSE fat_cursor;
	
END;
$BODY$
   LANGUAGE 'plpgsql' VOLATILE;

When I execute it, I only receive the return below:

  SELECT rel_faturamento('20051201','20051231');
  rel_faturamento
-----------------
  010876
(1 row)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: RETURN SET OF DATA WITH CURSOR
  2005-12-15 15:26 RETURN SET OF DATA WITH CURSOR grupos <grupos@carvalhaes.net>
@ 2005-12-15 16:03 ` Richard Huxton <dev@archonet.com>
  2005-12-15 16:58   ` Re: RETURN SET OF DATA WITH CURSOR grupos <grupos@carvalhaes.net>
  0 siblings, 1 reply; 5+ messages in thread

From: Richard Huxton @ 2005-12-15 16:03 UTC (permalink / raw)
  To: grupos <grupos@carvalhaes.net>; +Cc: pgsql-sql

grupos wrote:
> Hi !
> 
> I am making one plpgsql function and I need to return of setof data 
> using a cursor.
> 
> The problem is that my function is returning only the first row and 
> column of the query. This query have more than 80 columns and 1.000 
> rows. Enyone have any tip to give me?

Yes - decide whether you are returning a set of rows or a refcursor.

Check the plpgsql docs again for how to return SETOF <rowtype> using the 
RETURN NEXT statement. Then you call your function like: SELECT * FROM 
my_func();

-- 
   Richard Huxton
   Archonet Ltd



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: RETURN SET OF DATA WITH CURSOR
  2005-12-15 15:26 RETURN SET OF DATA WITH CURSOR grupos <grupos@carvalhaes.net>
  2005-12-15 16:03 ` Re: RETURN SET OF DATA WITH CURSOR Richard Huxton <dev@archonet.com>
@ 2005-12-15 16:58   ` grupos <grupos@carvalhaes.net>
  2005-12-15 17:14     ` Re: RETURN SET OF DATA WITH CURSOR Richard Huxton <dev@archonet.com>
  2005-12-16 08:59     ` Re: RETURN SET OF DATA WITH CURSOR Patrick JACQUOT <patrick.jacquot@anpe.fr>
  0 siblings, 2 replies; 5+ messages in thread

From: grupos @ 2005-12-15 16:58 UTC (permalink / raw)
  To: Richard Huxton <dev@archonet.com>; +Cc: pgsql-sql

Hi Richard,

Thanks for your promptly answer. I don't have experience returning 
refcursor but my choice would be it.
I read the documentation but I didn't find any example with my necessity...

Could you give me a small example on the same "basis" that I want?

Thanks,

Rodrigo Carvalhaes

Richard Huxton wrote:
> grupos wrote:
>> Hi !
>>
>> I am making one plpgsql function and I need to return of setof data 
>> using a cursor.
>>
>> The problem is that my function is returning only the first row and 
>> column of the query. This query have more than 80 columns and 1.000 
>> rows. Enyone have any tip to give me?
> 
> Yes - decide whether you are returning a set of rows or a refcursor.
> 
> Check the plpgsql docs again for how to return SETOF <rowtype> using the 
> RETURN NEXT statement. Then you call your function like: SELECT * FROM 
> my_func();
> 



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: RETURN SET OF DATA WITH CURSOR
  2005-12-15 15:26 RETURN SET OF DATA WITH CURSOR grupos <grupos@carvalhaes.net>
  2005-12-15 16:03 ` Re: RETURN SET OF DATA WITH CURSOR Richard Huxton <dev@archonet.com>
  2005-12-15 16:58   ` Re: RETURN SET OF DATA WITH CURSOR grupos <grupos@carvalhaes.net>
@ 2005-12-15 17:14     ` Richard Huxton <dev@archonet.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Richard Huxton @ 2005-12-15 17:14 UTC (permalink / raw)
  To: grupos <grupos@carvalhaes.net>; +Cc: pgsql-sql

grupos wrote:
> Hi Richard,
> 
> Thanks for your promptly answer. I don't have experience returning 
> refcursor but my choice would be it.
 >
> I read the documentation but I didn't find any example with my necessity...

http://www.postgresql.org/docs/8.1/static/plpgsql-cursors.html#PLPGSQL-CURSOR-USING

See the example in "returning cursors"
-- 
   Richard Huxton
   Archonet Ltd



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: RETURN SET OF DATA WITH CURSOR
  2005-12-15 15:26 RETURN SET OF DATA WITH CURSOR grupos <grupos@carvalhaes.net>
  2005-12-15 16:03 ` Re: RETURN SET OF DATA WITH CURSOR Richard Huxton <dev@archonet.com>
  2005-12-15 16:58   ` Re: RETURN SET OF DATA WITH CURSOR grupos <grupos@carvalhaes.net>
@ 2005-12-16 08:59     ` Patrick JACQUOT <patrick.jacquot@anpe.fr>
  1 sibling, 0 replies; 5+ messages in thread

From: Patrick JACQUOT @ 2005-12-16 08:59 UTC (permalink / raw)
  To: pgsql-sql

grupos wrote:

> Hi Richard,
>
> Thanks for your promptly answer. I don't have experience returning 
> refcursor but my choice would be it.
> I read the documentation but I didn't find any example with my 
> necessity...
>
> Could you give me a small example on the same "basis" that I want?
>
> Thanks,
>
> Rodrigo Carvalhaes
>
> Richard Huxton wrote:
>
>> grupos wrote:
>>
>>> Hi !
>>>
>>> I am making one plpgsql function and I need to return of setof data 
>>> using a cursor.
>>>
>>> The problem is that my function is returning only the first row and 
>>> column of the query. This query have more than 80 columns and 1.000 
>>> rows. Enyone have any tip to give me?
>>
>>
>> Yes - decide whether you are returning a set of rows or a refcursor.
>>
>> Check the plpgsql docs again for how to return SETOF <rowtype> using 
>> the RETURN NEXT statement. Then you call your function like: SELECT * 
>> FROM my_func();
>>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
>
There is a standard way :

Select * from memberstatus A where not exists
    (select * from emberstatus B where B.member_id=A.member_id and  
B.status_date >A.status_date)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2005-12-16 08:59 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-12-15 15:26 RETURN SET OF DATA WITH CURSOR grupos <grupos@carvalhaes.net>
2005-12-15 16:03 ` Richard Huxton <dev@archonet.com>
2005-12-15 16:58   ` grupos <grupos@carvalhaes.net>
2005-12-15 17:14     ` Richard Huxton <dev@archonet.com>
2005-12-16 08:59     ` Patrick JACQUOT <patrick.jacquot@anpe.fr>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox