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 ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 psql "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 |