Unix Technical Forum

Return data from function

This is a discussion on Return data from function within the pgsql Admins forums, part of the PostgreSQL category; --> Hello, I need to create a function that find data over 3 tables, merge and return data. With example ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Admins

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 08:25 AM
Alexander B.
 
Posts: n/a
Default Return data from function

Hello,

I need to create a function that find data over 3 tables, merge and
return data.
With example can be specified more clearly:

1.1 Find all addresses code;
1.2 For each addresses code, get address, complement and city on the
first table;
1.3 For each addresses code, get address, complement and city on the
second table;
1.4 Merge result from first and second tables and return to the function;

I would like to do not using cursor.
Do you have any ideas how to implement?

Thanks in advance.

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 08:26 AM
Jim C. Nasby
 
Posts: n/a
Default Re: Return data from function

The magic you're looking for is UNION ALL.

You could just union the two address tables together and join that to
the address code table, but that'll probably lead to an inefficient
plan. You'll likely need to join the address code table to each address
table separately, and then UNION ALL those together.

On Tue, Apr 17, 2007 at 04:17:36PM -0300, Alexander B. wrote:
> Hello,
>
> I need to create a function that find data over 3 tables, merge and
> return data.
> With example can be specified more clearly:
>
> 1.1 Find all addresses code;
> 1.2 For each addresses code, get address, complement and city on the
> first table;
> 1.3 For each addresses code, get address, complement and city on the
> second table;
> 1.4 Merge result from first and second tables and return to the function;
>
> I would like to do not using cursor.
> Do you have any ideas how to implement?
>
> Thanks in advance.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
>


--
Jim Nasby jim@nasby.net
EnterpriseDB http://enterprisedb.com 512.569.9461 (cell)

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 08:26 AM
Robert Treat
 
Posts: n/a
Default Re: Return data from function

On Tuesday 17 April 2007 15:17, Alexander B. wrote:
> Hello,
>
> I need to create a function that find data over 3 tables, merge and
> return data.
> With example can be specified more clearly:
>
> 1.1 Find all addresses code;
> 1.2 For each addresses code, get address, complement and city on the
> first table;
> 1.3 For each addresses code, get address, complement and city on the
> second table;
> 1.4 Merge result from first and second tables and return to the function;
>
> I would like to do not using cursor.
> Do you have any ideas how to implement?
>


I'm not convinced you can't do this from plain sql, but if you're sure a
function is the way to go, see the docs on set returning functions:
http://www.postgresql.org/docs/curre...ABLE-FUNCTIONS

--
Robert Treat
Database Architect
http://www.omniti.com/

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:44 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com