Re: function to return both table row and varchar

From: "Kenneth Lundin" <kenneth(dot)lundin(at)dacom(dot)se>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: function to return both table row and varchar
Date: 2009-01-15 11:23:03
Message-ID: 93d3a1c90901150323g57dd5aedy2104bf0e202fe947@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Sorry, I should have RTFM(!!!). I found it under 4.2.4 Field selection.
Apparently it works just as I want, but I should have put parenthesis around
the row-name like this:

> select result,(resulting_row).name from verify_record(1234);
name | result
-------|--------
"Test" | "OK"

I also discovered you can do a
> select result,(resulting_row).* from verify_record(1234);
to combine the both results to a single returning row if needed... sweet!

//Kenneth

On Thu, Jan 15, 2009 at 11:10 AM, Kenneth Lundin <kenneth(dot)lundin(at)dacom(dot)se>wrote:

> Hi,
>
> i'm defining a function in plpqsql and would like it to return one varchar
> and one row from another table. I have defined it like this (this is only a
> test and does not really make sense yet, but it's the principle i'm after):
>
> CREATE OR REPLACE FUNCTION verify_record(IN number_to_verify bigint, OUT
> resulting_row logbook, OUT result character varying)
> RETURNS record AS
> $BODY$
> BEGIN
> SELECT * INTO resulting_row FROM logbook WHERE
> id_number=number_to_verify::varchar;
> SELECT 'OK' INTO result;
> END
> $BODY$
> LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
> COST 100;
>
> It works fine and i can do a select like this:
>
> > select * from verify_record(1234);
> resulting_row | result
> ------------------------------|--------
> (1,"Test","Registered",.....) | "OK"
>
> So far so good, but how do I use the the resulting_row further, say if i
> would like to select only a few columns or perhaps define a view that
> returns the 'result' column and only column 2 "Test" from the resulting_row?
> What I'd like to do is a select and sub-address the individual columns of
> the resulting_row, like writing (given 'name' is the name of some column in
> resulting_row):
>
> > select returned_row.name, result from verify_record(1234);
>
> or perhaps
>
> > select returned_row['name'], result from verify_record(1234);
>
> and have it return something like:
> name | result
> -------|--------
> "Test" | "OK"
>
> Is this possible or am I on the wrong track here?
>
> //Kenneth
>
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sam Mason 2009-01-15 11:33:09 Re: Query question
Previous Message Grzegorz Jaśkiewicz 2009-01-15 10:23:34 Re: inconsistency in aliasing