473,408 Members | 1,809 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,408 software developers and data experts.

Function Returning SETOF Problem

On a daily basis I place a lot of data into the empty table dailyList,
and from that data update certain fields in currentList. I thought that
using a function would be a good way to do this(?). However I get the
following error when I run updateCurrentData():
ERROR: set-valued function called in context that cannot accept a set
CONTEXT: PL/pgSQL function "updatecurrentcata" line 6 at return next
I've googled and tried variations on the function, but without success.
Can anyone help?

Here's the function:
CREATE TYPE place_finish AS (first NUMERIC, second NUMERIC, third
NUMERIC, grandttl INTEGER, lname TEXT, fname TEXT);

CREATE OR REPLACE FUNCTION updateCurrentData() RETURNS SETOF
place_finish AS '
DECLARE
rec RECORD;
updstmt TEXT;
BEGIN
FOR rec IN SELECT first, second, third, grandttl, lname, fname
FROM dailyList LOOP
RETURN NEXT rec;
updstmt := ''UPDATE currentList SET first=rec.first,
second=rec.second, third=rec.third, grandttl=rec.grandttl,
lname=rec.lname, fname=rec.fname WHERE lname=rec.lname AND
fname=rec.fname;'';
EXECUTE updstmt;
END LOOP;
RETURN 1;
END;
' LANGUAGE 'plpgsql';
Thanks
Ron

ps postgres 7.4, debian stable
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #1
9 19441

On Wed, 17 Dec 2003, Ron St-Pierre wrote:
On a daily basis I place a lot of data into the empty table dailyList,
and from that data update certain fields in currentList. I thought that
using a function would be a good way to do this(?). However I get the
following error when I run updateCurrentData():
ERROR: set-valued function called in context that cannot accept a set
CONTEXT: PL/pgSQL function "updatecurrentcata" line 6 at return next
I've googled and tried variations on the function, but without success.
Can anyone help?


This probably means that you're calling it like:
select updateCurrentData();
and you'll need to instead call it with the function in the FROM clause,
something like:
select * from updateCurrentData();

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

Nov 12 '05 #2
Stephan Szabo wrote:
On Wed, 17 Dec 2003, Ron St-Pierre wrote:
On a daily basis I place a lot of data into the empty table dailyList,
and from that data update certain fields in currentList. I thought that
using a function would be a good way to do this(?). However I get the
following error when I run updateCurrentData():
ERROR: set-valued function called in context that cannot accept a set
CONTEXT: PL/pgSQL function "updatecurrentcata" line 6 at return next
I've googled and tried variations on the function, but without success.
Can anyone help?


This probably means that you're calling it like:
select updateCurrentData();
and you'll need to instead call it with the function in the FROM clause,
something like:
select * from updateCurrentData();

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

aha, that's part of it. I now get this error:
ERROR: wrong record type supplied in RETURN NEXT
Any ideas on this one?

TIA
Ron
---------------------------(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 12 '05 #3
Ron St-Pierre <rs*******@syscor.com> writes:
On a daily basis I place a lot of data into the empty table dailyList,
and from that data update certain fields in currentList. I thought that
using a function would be a good way to do this(?). However I get the
following error when I run updateCurrentData():
ERROR: set-valued function called in context that cannot accept a set


You're probably doing
SELECT updateCurrentData();
where you should be doing
SELECT * FROM updateCurrentData();

There are some cases where you can invoke set-valued functions in the
target list rather than in the FROM list, but this isn't one of 'em.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #4
On Wed, 17 Dec 2003, Ron St-Pierre wrote:
Stephan Szabo wrote:
On Wed, 17 Dec 2003, Ron St-Pierre wrote:
On a daily basis I place a lot of data into the empty table dailyList,
and from that data update certain fields in currentList. I thought that
using a function would be a good way to do this(?). However I get the
following error when I run updateCurrentData():
ERROR: set-valued function called in context that cannot accept a set
CONTEXT: PL/pgSQL function "updatecurrentcata" line 6 at return next
I've googled and tried variations on the function, but without success.
Can anyone help?


This probably means that you're calling it like:
select updateCurrentData();
and you'll need to instead call it with the function in the FROM clause,
something like:
select * from updateCurrentData();

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

aha, that's part of it. I now get this error:
ERROR: wrong record type supplied in RETURN NEXT
Any ideas on this one?


That sounds like a mismatch between the record in rec and your declared
output type, but I couldn't say for sure without a complete example
including the table declarations really.

---------------------------(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 12 '05 #5
Stephan Szabo wrote:
<snip>
and you'll need to instead call it with the function in the FROM clause,
>something like:
> select * from updateCurrentData();
>

aha, that's part of it. I now get this error:
ERROR: wrong record type supplied in RETURN NEXT
Any ideas on this one?
That sounds like a mismatch between the record in rec and your declared
output type, but I couldn't say for sure without a complete example
including the table declarations really.

</snip>
You were right again. The order of columns in my record_type was
different than my select. Now when I run the script I get the following
error:
ERROR: relation "rec" does not exist

Here are my record type and function:
CREATE TYPE place_finish AS (first NUMERIC, second NUMERIC, third
NUMERIC, grandttl INTEGER, lname TEXT, fname TEXT);

CREATE OR REPLACE FUNCTION updateSecondaryData () RETURNS SETOF
place_finish AS '
DECLARE
rec RECORD;
updstmt TEXT;
BEGIN
FOR rec IN SELECT first, second, third, grandttl, lname,
fname FROM dailyList LOOP
RETURN NEXT rec;
updstmt := ''UPDATE currentList SET first=rec.first,
second=rec.second, third=rec.third, grandttl=rec.grandttl,
lname=rec.lname, fname=rec.fname WHERE lname=rec.lname AND
fname=rec.fname;'';
EXECUTE updstmt;
END LOOP;
RETURN 1;
END;
' LANGUAGE 'plpgsql';

If I modify the function and try to run the update statement directly
<snip>
FOR rec IN SELECT first, second, third, grandttl, lname,
fname FROM dailyList LOOP
RETURN NEXT rec;
UPDATE currentList SET first=rec.first,
second=rec.second, third=rec.third, grandttl=rec.grandttl,
lname=rec.lname, fname=rec.fname WHERE lname=rec.lname AND fname=rec.fname;
END LOOP;
</snip>

: I get this error:
ERROR: infinite recursion detected in rules for relation "currentlist"
CONTEXT: PL/pgSQL function "updatesecondarydata " line 7 at SQL
statement

Any ideas on what I'm doing wrong this time?

TIA
Ron

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #6
Ron St-Pierre <rs*******@syscor.com> writes:
: I get this error:
ERROR: infinite recursion detected in rules for relation "currentlist"


So what kind of rules have you got on "currentlist"? I don't believe
that complaint has anything to do with your plpgsql function.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #7

On Thu, 18 Dec 2003, Ron St-Pierre wrote:
Stephan Szabo wrote:
<snip>
and you'll need to instead call it with the function in the FROM clause,
>something like:
> select * from updateCurrentData();
>
aha, that's part of it. I now get this error:
ERROR: wrong record type supplied in RETURN NEXT
Any ideas on this one?
That sounds like a mismatch between the record in rec and your declared
output type, but I couldn't say for sure without a complete example
including the table declarations really.
</snip>
You were right again. The order of columns in my record_type was
different than my select. Now when I run the script I get the following
error:
ERROR: relation "rec" does not exist


For the first one, you're making a query string that has lines like
foo = rec.bar
where you really want
foo = <value of rec.bar>

So for execute you want something like
'' ... foo = '' || rec.bar || '' ... ''
(possibly requiring casts)
<snip>
FOR rec IN SELECT first, second, third, grandttl, lname,
fname FROM dailyList LOOP
RETURN NEXT rec;
UPDATE currentList SET first=rec.first,
second=rec.second, third=rec.third, grandttl=rec.grandttl,
lname=rec.lname, fname=rec.fname WHERE lname=rec.lname AND fname=rec.fname;
END LOOP;
</snip>

: I get this error:
ERROR: infinite recursion detected in rules for relation "currentlist"
CONTEXT: PL/pgSQL function "updatesecondarydata " line 7 at SQL
statement


As Tom said, this looks like something else. Do you have a rule on
currentlist that also does an update on currentlist, perhaps forcing
certain values or something?

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

http://archives.postgresql.org

Nov 12 '05 #8
Ron St-Pierre wrote:
Here are my record type and function:
Note -- you could make it easier for people to help, and hence increase
your chances of getting help, if your sample code is complete. I.e.
provide the needed table definition(s) and even some sample data (INSERT
statements) so we don't have to reverse engineer those things.
END LOOP;
RETURN 1;
END;
' LANGUAGE 'plpgsql';


The line "RETURN 1;" ought to be just "RETURN;"

HTH,

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

Nov 12 '05 #9
Stephan Szabo wrote:
<snip>
For the first one, you're making a query string that has lines like
foo = rec.bar
where you really want
foo = <value of rec.bar>

So for execute you want something like
'' ... foo = '' || rec.bar || '' ... ''
(possibly requiring casts)

</snip>
Okay, fixed that ...

<snip>
As Tom said, this looks like something else. Do you have a rule on
currentlist that also does an update on currentlist, perhaps forcing
certain values or something?

</snip>

Yes, I did have a rule on the table which I had completely forgotten about. I removed it and the function works properly now.
Thanks Stephan and Tom!

Ron

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #10

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

Similar topics

1
by: Julie May | last post by:
I have 90% of my function working and I know to get the next 10% it is justa matter of getting the quotations and the escaping quotations correct. Here is the portion that does work: <working...
3
by: ezra epstein | last post by:
I'm been banging my head over this for a little while now. Here's a simple function to return a record: <code> CREATE OR REPLACE FUNCTION create_item_record_for_override(INTEGER, VARCHAR )...
10
by: Terry Lee Tucker | last post by:
I need to return a row of data from a function. I've been looking the the HTML docs and have found nothing of value. If I try to return a variable of type RECORD, I get the following error:...
4
by: Sky | last post by:
HI everybody ! I have a problem, but I don't know the solution: CREATE TABLE person( user_id SERIAL NOT NULL, uid CHARACTER(20) NOT NULL, pwd CHARACTER(20) NOT NULL, PRIMARY KEY (user_id)...
1
by: Chris Ochs | last post by:
I read the docs but I'm still a little lost as to how to do this. I have this function which works fine. CREATE OR REPLACE FUNCTION lookup_customer_byemail(varchar) RETURNS SETOF customer_rec...
2
by: Vladimir M | last post by:
Hi I am writing a function with PL/pgSQL, which returns result of some complex query with several tables. In manual i found such example: CREATE OR REPLACE FUNCTION Test() RETURNS SETOF...
0
by: thefreecat | last post by:
I wrote a function to list the content of a group : create or replace function members(int,char) returns setof int as... To list the content of group 2 I execute : select * from...
0
by: lazybee26 | last post by:
Hello – I’m trying to findout a better solution to this approach. Currently if I have to return columns from multiple tables, I have to define my own TYPE and then return SETOF that type in...
2
by: ycjack | last post by:
Hi, all Following is a simple function which returns type of setof: CREATE OR REPLACE FUNCTION tesp_report_query() RETURNS SETOF rpt.tesp_report_query AS $BODY$ DECLARE rec record; BEGIN...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.