473,396 Members | 2,024 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,396 software developers and data experts.

Passing RECORD variable from func1() to func2()

Hello

Hopefully someone can shed some light on the following issue. After
chatting at irc.freenode.net/#postgresql, without success, this is my last
effort before giving up and using a temp table.

Essentially, I would like to pass a RECORD variable from one function to
another using plpgsql:

func2(record)
rec1 alias for $1
begin
-- do work on rec1.*
raise notice ''val1=% val2=%'', rec1.col1, rec1.col2;
end;

func1()
declare
temprec record;
begin
for temprec in select * from table1, table2...
loop
...
select func2(temprec); /* pass temprec row to func2() */
...
end loop;
end;

Then call with:
SELECT FUNC1();

Is this possible? The docs only speak about RECORD type being used to
*return* rows, but not to pass it.

Any pointers would be appreciated.

Regards
Henry
--------------------------------------------------------
This message was sent using MetroWEB's AirMail service.
http://www.metroweb.co.za/ - full access for only R73.
Free Web Accelerator, WebMail, Calendar, Anti-Virus,
Anti-Spam, 10 emails, 100MB personal webspace, and more!
Phone Now! 086 11 11 440

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

Nov 23 '05 #1
7 6051
"Henry Combrinck" <he***@metroweb.co.za> writes:
Essentially, I would like to pass a RECORD variable from one function to
another using plpgsql: func2(record)


You can't declare a plpgsql function that accepts RECORD; this is simply
not supportable. (For one thing, which actual record types should such
a function be considered to match? It's a nonstarter even at the level
of function argument resolution, let alone the implementation issues.)
It has to take some named rowtype, instead.

There are implementation restrictions in 7.4 and before that prevent
plpgsql functions from passing row or record variables to other
functions, so you'd have problems at the calling end as well.
FWIW these restrictions are fixed for 8.0.

regards, tom lane

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

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

Nov 23 '05 #2
> "Henry Combrinck" <he***@metroweb.co.za> writes:
Essentially, I would like to pass a RECORD variable from one function to
another using plpgsql:

func2(record)


You can't declare a plpgsql function that accepts RECORD; this is simply
not supportable. (For one thing, which actual record types should such
a function be considered to match? It's a nonstarter even at the level
of function argument resolution, let alone the implementation issues.)
It has to take some named rowtype, instead.


Thanks for the response.

Can you give an example of what a "named rowtype" is? Are you refering to
creating some kind of custom TYPE, and then passing that to
func2(custom_type_row)?

Thanks
Henry
--------------------------------------------------------
This message was sent using MetroWEB's AirMail service.
http://www.metroweb.co.za/ - full access for only R73.
Free Web Accelerator, WebMail, Calendar, Anti-Virus,
Anti-Spam, 10 emails, 100MB personal webspace, and more!
Phone Now! 086 11 11 440

---------------------------(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 #3
"Henry Combrinck" <he***@metroweb.co.za> writes:
Can you give an example of what a "named rowtype" is?


The result of CREATE TYPE AS, or the row type implicitly created for a
table. For instance

CREATE TYPE complex AS (r float, i float);

CREATE FUNCTION abs(complex) RETURNS float AS ...

or

CREATE TABLE users (name text, ...);

CREATE FUNCTION foobar(users) RETURNS ...

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 23 '05 #4
On Mon, Sep 06, 2004 at 10:39:54PM +0200, Henry Combrinck wrote:
Can you give an example of what a "named rowtype" is? Are you refering to
creating some kind of custom TYPE, and then passing that to
func2(custom_type_row)?


You can use a table's rowtype, that is, a type that has the name of the
table and the same column types. Or you can create a "standalone type" with

CREATE TYPE foo AS (f1 int, f2 text, ...)

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"Find a bug in a program, and fix it, and the program will work today.
Show the program how to find and fix a bug, and the program
will work forever" (Oliver Silfridge)
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #5
Thanks for the CREATE TYPE samples. *Using* the custom types seems to be
a problem (or rather, I'm using it incorrectly). The following code fails
with the error message

"WARNING: Error occurred while executing PL/pgSQL function f_test00
WARNING: line 8 at SQL statement
ERROR: Attribute "rec1" not found"

/* create same columns as table1 */
create type TYPE_T1 as (col1 int, col2 int,... etc);
CREATE or replace FUNCTION f_test01 (TYPE_T1)
RETURNS int AS '
declare
temprec alias for $1
begin
raise notice ''eid: % pcyc: %'', temprec.col1, temprec.col2;
return 0;
end;
' LANGUAGE 'plpgsql';

CREATE or replace FUNCTION f_test00 () RETURNS int AS '
declare
rec1 TYPE_T1%ROWTYPE;
begin
for rec1 in select * from table1
loop
select f_test01(rec1); /* this is where it fails. */
end loop;
return 0;
end;
' LANGUAGE 'plpgsql';

--------------------------------------------------------
This message was sent using MetroWEB's AirMail service.
http://www.metroweb.co.za/ - full access for only R73.
Free Web Accelerator, WebMail, Calendar, Anti-Virus,
Anti-Spam, 10 emails, 100MB personal webspace, and more!
Phone Now! 086 11 11 440

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

Nov 23 '05 #6
howzit Henry

I've pasted in the head of one of my functions. Hope it helps. It is
called through the following:

SELECT * from fn_usr32_show_sections ( <integer> );

Rory

On 07/09/04, Henry Combrinck (he***@metroweb.co.za) wrote:
Thanks for the CREATE TYPE samples. *Using* the custom types seems to be
a problem (or rather, I'm using it incorrectly). The following code fails
with the error message

"WARNING: Error occurred while executing PL/pgSQL function f_test00
WARNING: line 8 at SQL statement
ERROR: Attribute "rec1" not found"

/* create same columns as table1 */
create type TYPE_T1 as (col1 int, col2 int,... etc); .... declare
rec1 TYPE_T1%ROWTYPE;
begin
for rec1 in select * from table1
loop
select f_test01(rec1); /* this is where it fails. */
end loop;
return 0;
end;
' LANGUAGE 'plpgsql';

CREATE TYPE sec_sections as (
sectionid INTEGER,
sectionname VARCHAR,
secupdated VARCHAR,
secreports INTEGER,
secusers INTEGER
);

CREATE OR REPLACE FUNCTION
fn_usr32_show_sections ( integer ) RETURNS setof sec_sections
AS '
DECLARE
userid ALIAS for $1;
resulter sec_sections%rowtype;
BEGIN

IF userid is NULL THEN
RAISE EXCEPTION ''No user provided : ref usr32'';
RETURN 0;
END IF;

PERFORM fn_util07_isadmin(userid);

FOR resulter IN
SELECT
s.n_id as sectionid,
s.t_name as sectionname,
COALESCE(to_char(lupd.updated, ''DD Mon''), ''None'') as secupdated,
COALESCE(rept.num,0) as secreports,
COALESCE(usr.num,0) as secusers
...etc..
--
Rory Campbell-Lange
<ro**@campbell-lange.net>
<www.campbell-lange.net>

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

http://archives.postgresql.org

Nov 23 '05 #7
Howzit Henry

On 06/09/04, Henry Combrinck (he***@metroweb.co.za) wrote:
Essentially, I would like to pass a RECORD variable from one function to
another using plpgsql:


You may want to have a look at using cursor references.

For instance:

CREATE FUNCTION use_cursors ( INTEGER ) RETURNS INTEGER AS '
DECLARE
ref_cursors REFCURSOR;
total INTEGER := 0;
BEGIN
curs := get_ref_cursor_from_other_function ( $1 );
total := use_curs_to_do_totaling_function ( ref_cursors );
RETURN total;
END;
' LANGUAGE 'plpgsql';
CREATE FUNCTION get_ref_cursor_from_other_function ( INTEGER ) RETURNS REFCURSOR AS '
DECLARE
next_val REFCURSOR;
BEGIN
OPEN next_val FOR
SELECT * FROM mytable WHERE intcol = $1;
RETURN ( next_val);
END;
' LANGUAGE 'plpgsql';

CREATE FUNCTION use_curs_to_do_totaling_function ( REFCURSOR ) RETURNS INTEGER AS '
DECLARE
myrow mytable%rowtype;
total INTEGER := 0;
next_val ALIAS for $1;
BEGIN
LOOP
FETCH next_val INTO myrow;
EXIT WHEN NOT FOUND;
total := total + myrow.<somecolval>;
END LOOP;
RETURN (total);
END;
' LANGUAGE 'plpgsql';

--
Rory Campbell-Lange
<ro**@campbell-lange.net>
<www.campbell-lange.net>

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

Nov 23 '05 #8

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

Similar topics

8
by: Mike | last post by:
mylist = I want to print out the names of the variables in mylist (not the values of a, b, and c). How do I go about doing this. Thanks. Mike
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
4
by: Andy | last post by:
Hi, I'm doing a nested pass by reference of a vector. I'm in Vis Studio 2003. Example: vector <unsigned long long> vect; func1(vect); where func1 calls func2(vect);
1
by: george doubleu | last post by:
hi, i'm using "gcc version 3.3.3 (Debian)" to compile the program pasted below. I get the two warnings you can see in the remarks. The second warning is perfectly OK for me, but the first one I...
6
by: brian_harris | last post by:
I have a function that passes a string class pointer to a function, this function is then suppose to fill it and the outer function uses it. But I believe that I am running into problem due to...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
9
by: tai | last post by:
Hi. I'm looking for a way to define a function that's only effective inside specified function. Featurewise, here's what I want to do: bar_plugin_func = function() { ...; setTimeout(...);...
6
by: CptDondo | last post by:
How do you declare a function with 0 or mroe arguments? I have a bunch of functions like this: void tc_cm(int row, int col); void tc_do(void); void tc_DO(int ln); and I am trying to ...
5
by: Guillermo | last post by:
Hi, This must be very basic, but how'd you pass the same *args several levels deep? def func2(*args) print args # ((1, 2, 3),) # i want this to output (1, 2, 3) as func1!
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
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
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,...

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.