Unix Technical Forum

Function parameters

This is a discussion on Function parameters within the pgsql Sql forums, part of the PostgreSQL category; --> Hi, I am unable to understand the handling of record types in Postgres. Specifically I wish to understand how ...


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

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 05:55 PM
Thomas Crawley
 
Posts: n/a
Default Function parameters

Hi,

I am unable to understand the handling of record types in Postgres.

Specifically I wish to understand how to process a returned record
type in a calling function.

I have the following code:

One function update_version returns a record containing an updated
flag and a version number
The other function call_update_version tries to get the version number
from the record returned
from update_version.

================================================== =========================
DROP FUNCTION update_version( test_param INTEGER, OUT updated BOOL,
OUT version INTEGER);

CREATE OR REPLACE FUNCTION update_version( test_param INTEGER) RETURNS
record AS
$$
declare
updated BOOL;
new_version INTEGER;
ret RECORD;
begin
IF test_param > 5 THEN
updated := true;
new_version = test_param + 5;
ELSE
updated := False;
new_version := test_param;
END IF;

ret := ( updated, new_version );

RETURN ret;
end;
$$
language plpgsql;


SELECT update_version(0);

SELECT update_version(10);


CREATE OR REPLACE FUNCTION call_update_version( test_param INTEGER,
OUT new_version INTEGER) RETURNS INTEGER AS
$$
declare
nv_rec RECORD;
updtd BOOL;
begin
SELECT update_version(0) INTO nv_rec;
new_version = nv_rec.new_version;

RETURN;
end;
$$
language plpgsql;

SELECT call_update_version(0);

=================================================

Executing this code produces the following output:

=================================================
DROP FUNCTION
CREATE FUNCTION
update_version
----------------
(f,0)
(1 row)

update_version
----------------
(t,15)
(1 row)

CREATE FUNCTION
psqlg_test.sql:47: ERROR: record "nv_rec" has no field
"new_version"
CONTEXT: PL/pgSQL function "call_update_version" line 7 at assignment

==================================================

Can someone explain how to extract the fields from the returned record
in the function call_update_version ?

This seems to be impossible even using types etc.

The original problem was accessing out parameters from the caller as
these
are returned from a record. There does not seem to be any way to
access
the fields of the record. Can anyone demonstrate how to access the
fields
of the record.

The documentation is not very clear on all this and examples are
lacking.

Thanks in advance...

Tom

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 10:37 AM.


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