473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Returning recordsets with functions

Can someone point me to some more information or perhaps show an example
of returning a recordset from a plpgsql function. I'd like to send an
argument or arguments to the function, do some queries to return a set
of records. I've done several functions that return one value of one
type, but nothing that returns a set.

--
Robert
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
5 14279
Use the return type of SETOF and user the RETURN NEXT for each record.
If you are already returning a record that's half the battle.

http://www.postgresql.org/docs/7.4/s...ENTS-RETURNING
On Thu, 23 Sep 2004 11:26:15 -0400, Robert Fitzpatrick
<ro****@webtent.com> wrote:
Can someone point me to some more information or perhaps show an example
of returning a recordset from a plpgsql function. I'd like to send an
argument or arguments to the function, do some queries to return a set
of records. I've done several functions that return one value of one
type, but nothing that returns a set.

--
Robert

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #2
Robert Fitzpatrick wrote:
Can someone point me to some more information or perhaps show an example
of returning a recordset from a plpgsql function. I'd like to send an
argument or arguments to the function, do some queries to return a set
of records. I've done several functions that return one value of one
type, but nothing that returns a set.

Ah ha. Someone else trying to find out what I had hunted for. I could
not find an example on the web, so I started writing one.
Not fully complete yet, but here is what is there and I hope it helps.

http://www.scorefirst.com/postgresql.html

Tim
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #3
On Thu, 2004-09-23 at 18:28, Tim Penhey wrote:
Robert Fitzpatrick wrote:
Can someone point me to some more information or perhaps show an example
of returning a recordset from a plpgsql function. I'd like to send an
argument or arguments to the function, do some queries to return a set
of records. I've done several functions that return one value of one
type, but nothing that returns a set.

Ah ha. Someone else trying to find out what I had hunted for. I could
not find an example on the web, so I started writing one.
Not fully complete yet, but here is what is there and I hope it helps.
http://www.scorefirst.com/postgresql.html


Thanks to all, that is very helpful. But when I create your function or
a small test function, I get the following error. I am running
PostgreSQL 7.4.5, do you know what this means or how I can define the
list?

ERROR: a column definition list is required for functions returning
"record"

I have another question. I was getting a syntax error when trying to
create the function on the page linked above:

ERROR: syntax error at or near "INTEGER" at character 64

I made my own test function with trying to put my own variable names in
the arguments list and it created without the syntax error, but now back
to the first problem. Here is what I have so far:

CREATE OR REPLACE FUNCTION "public"."test" (varchar) RETURNS SETOF
"pg_catalog"."record" AS'
DECLARE
row_ RECORD;
cursor_ CURSOR FOR SELECT * FROM tblhudreports WHERE rems_id=$1;
BEGIN
OPEN cursor_;
LOOP
FETCH cursor_ INTO row_;
EXIT WHEN NOT FOUND;
RETURN NEXT row_;
END LOOP;

CLOSE cursor_;
RETURN;
END;
'LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

ohc=# select * from test('800000061');
ERROR: a column definition list is required for functions returning
"record"
ohc=#

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

http://archives.postgresql.org

Nov 23 '05 #4
Hello,

When using setOf functions you first create a type to call from:

|CREATE TYPE desc_results
AS (entry_id INTEGER, headline TEXT, rank REAL);
|
After creating the custom type you create a function that calls that type:

CREATE OR REPLACE FUNCTION dresults(text) RETURNS SETOF desc_results AS '

| SELECT entry_id,headline(description,q), rank(vectors,q)
FROM cmd_contact_task_entries, to_tsquery($1) AS q
WHERE vectors @@ q ORDER BY rank(vectors,q)
DESC;' LANGUAGE 'SQL';

Sincerely,

Joshua D. Drake

|

Robert Fitzpatrick wrote:
On Thu, 2004-09-23 at 18:28, Tim Penhey wrote:

Robert Fitzpatrick wrote:
Can someone point me to some more information or perhaps show an example
of returning a recordset from a plpgsql function. I'd like to send an
argument or arguments to the function, do some queries to return a set
of records. I've done several functions that return one value of one
type, but nothing that returns a set.

Ah ha. Someone else trying to find out what I had hunted for. I could
not find an example on the web, so I started writing one.
Not fully complete yet, but here is what is there and I hope it helps.
http://www.scorefirst.com/postgresql.html


Thanks to all, that is very helpful. But when I create your function or
a small test function, I get the following error. I am running
PostgreSQL 7.4.5, do you know what this means or how I can define the
list?

ERROR: a column definition list is required for functions returning
"record"

I have another question. I was getting a syntax error when trying to
create the function on the page linked above:

ERROR: syntax error at or near "INTEGER" at character 64

I made my own test function with trying to put my own variable names in
the arguments list and it created without the syntax error, but now back
to the first problem. Here is what I have so far:

CREATE OR REPLACE FUNCTION "public"."test" (varchar) RETURNS SETOF
"pg_catalog"."record" AS'
DECLARE
row_ RECORD;
cursor_ CURSOR FOR SELECT * FROM tblhudreports WHERE rems_id=$1;
BEGIN
OPEN cursor_;
LOOP
FETCH cursor_ INTO row_;
EXIT WHEN NOT FOUND;
RETURN NEXT row_;
END LOOP;

CLOSE cursor_;
RETURN;
END;
'LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

ohc=# select * from test('800000061');
ERROR: a column definition list is required for functions returning
"record"
ohc=#

--
Command Prompt, Inc., home of Mammoth PostgreSQL - S/ODBC and S/JDBC
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-667-4564 - jd@commandprompt.com - http://www.commandprompt.com
PostgreSQL Replicator -- production quality replication for PostgreSQL
Nov 23 '05 #5

On Thu, 23 Sep 2004, Robert Fitzpatrick wrote:
Can someone point me to some more information or perhaps show an example
of returning a recordset from a plpgsql function. I'd like to send an
argument or arguments to the function, do some queries to return a set
of records. I've done several functions that return one value of one
type, but nothing that returns a set.


There's my set returning functions document on techdocs.postgresql.org,
http://techdocs.postgresql.org/guide...rningFunctions
as well as Elein's take on the above on General Bits (I think it was in
#26).

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

Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dan | last post by:
Hello, I'm getting started on my first java project. I'm likely going to use jsp/servlets connected to javabeans. The beans will connect to my backend and get/set information for me. When...
4
by: Eli Sidwell | last post by:
Trying to return a Recordset to an ASP and the Recordset is empty. The StorredProc works in the query analyzer and it even works from a quick VB app that I wrote to test it. The storedproc that...
7
by: (Pete Cresswell) | last post by:
I posted this in the MS Access group, but no luck. ------------------------------------------ I've got another stored procedure in the same app that returns multiple recordsets and the code works....
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
5
by: rhungund | last post by:
Hi All. My question is this. I have a complex stored procedure in SQL Server which works fine when I run it in Query Analyzer. However, when I call it within my ASP script, it returns nothing,...
22
by: James Cane | last post by:
Here's an interesting problem that someone might have an answer to... Some time ago, I wrote a set of utility classes which wrap up the custom row source function needed to add arbitrary items to...
16
by: Randy Harris | last post by:
I was inspired by the recent discussion of returning multiple recordsets to ADO from a stored procedure. (Amazed is probably more accurate). I asked about how to accomplish same with Oracle and...
4
by: =?Utf-8?B?cGF0cmlja2RyZA==?= | last post by:
Hi everyone! Is there any way I can write a web service that would be called from an old vb6 app and returning a dataset which would return to vb6 as recordset? Can anyone provide me with an...
4
by: stjulian | last post by:
(IIS 6.0, SQL Server 2000) I have a block of code that populates a recordset from a stored procedure. The problem is, the recordset seems to be forward only (which would be OK), but can't jump...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.