?
Solved

Postgresql function returning select query

Posted on 2002-07-10
5
Medium Priority
?
1,725 Views
Last Modified: 2008-02-01
Hi,

I'm pretty novice to Postgresql, and I wonder how I could do this ...

In M$ SQL Server, you can create a procedure like this:

create procudure test () as
  select * from mytable
go

In Postgresql, you can create functions returning rows too.  You have to declare your return value as the name of a table.

In the above case, this would be:
create function test() returns setof mytable as
' select * from mytable
' language 'sql';

But, what if I wanted to join two tables, how do I return results then ?

What I want, is the equivalent to this M$ SP:

create procedure test ()
as
  select field1, field2 from table1, table2
  where table1.field = table2.field
go


I want this because I like to create procedures (or functions) that hold my queries, rather than having them executed from within some other part of my code.

In Visual Basic, I'm used to creating ADO recordsets based on simple stored procedures like the one above.  I'd like to do similar things in Postgresql.
0
Comment
Question by:vindevogel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:boolee
ID: 7145218
vindevogel  Could u give me any links to tutorials on postgres stored procedures? I am looking for any such docs..and I'm sorry I cant answer ur question at this time..
0
 
LVL 3

Expert Comment

by:rycamor
ID: 7226863
vindevogel - Why not just store your queries as views? Honestly, it seems to me that it is a serious waste of resources to create a procedure simply to process a query. That is exactly what views are for. (And also, that is your answer for how to return a joined query: make a view, and query the view in your stored procedure, if you really must do that...)

Generally, though, you should only use procedures for special data manipulation that you can't handle in a regular SQL query. If you don't want to 'see' the query in your application code, you should just create a standard include file that defines your queries. The whole point of a SQL DBMS is that it has a standardized interface (SQL), so that portability between applications and between DBMS's is fairly simple. Once you start stuffing everything away into stored procedures, you will have a headache every time you migrate. (I have one potential client that now needs to migrate 2500 stored procedures from SQL Server 7 to Oracle. This is a perfect example of someone mistakenly thinking you should use a procedure for everything. It might seem simple at first, but creates many headaches later :-(. )
0
 
LVL 54

Expert Comment

by:nico5038
ID: 7294270

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
 - PAQ'd and pts removed
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Nic;o)
0
 

Accepted Solution

by:
ComTech earned 0 total points
ID: 7314784
This question will be placed in PAQ and points refunded.

Best regards,
ComTech
Community Support
Administrator @ EE
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question