Thread: Passing composite values to functions

Passing composite values to functions

From
"Jatinder Sangha"
Date:
Hi,
 
I'm having trouble passing a composite variable to a function.
 
I have a composite as follows:
 
    declare userRecord dnaUsers%ROWTYPE;
 
and I manipulate it like this:
 
    select into userRecord * from dnaUsers where etc;
    userRecord.userid := 'jatinder'; --etc
 
 
I would like to then insert this record into the table (kind of like this):
 
    insert into dnaUsers select userRecord;
    insert into dnaUsers select userRecord.*;
    insert into dnaUsers values (userRecord);
    insert into dnaUsers values (userRecord.*);
 
All of the above generate various errors relating to "userRecord" not being a table?
 
I can't find any examples of this in the archives or the documentation. Is this even possible?
 
I also, haven't found a way to pass my "userRecord" to a function, eg:
 
create or replace function isUserActive (dnaUsers)
returns boolean
as '
    do something with $1;
    return true;
' language plpgsql;

The problem here is that I can't pass userRecord to the new function:
 
    isActive := isUserActive (userRecord);
 
Again, the errors appear to be about "userRecord" not being a table?
 
So, is it even possible to use composite records in this manner? All of the examples I've seen, seem to be about passing a row of a direct select statement as a composite argument to a function, and not about passing a locally declared composite variable.
 
Can anyone help correct my understanding of what I can and can't do with composites?
 
Thanks,
--Jatinder

Re: Passing composite values to functions

From
Tom Lane
Date:
"Jatinder Sangha" <Jatinder@sanghaservices.co.uk> writes:
> I'm having trouble passing a composite variable to a function.

AFAIK, plpgsql doesn't support this --- it doesn't have any way to pass
a row or record variable as a parameter to the main executor.  Possibly
this will get fixed someday; I don't know of anyone working on it now
though.
        regards, tom lane