Re: Newbie: help with FUNCTION - Mailing list pgsql-general

From Rick Apichairuk
Subject Re: Newbie: help with FUNCTION
Date
Msg-id 9776bb660502251824587de62d@mail.gmail.com
Whole thread Raw
In response to Re: Newbie: help with FUNCTION  (elein@varlena.com (elein))
List pgsql-general
> > On Thu, Feb 24, 2005 at 09:00:46PM +0000, Charl Gerber wrote:
> > I'm trying to create  a function that takes 1
> > paramater (eg an integer) as input, then does 5
> > database updates or deletes in 5 different SQL
> > statements and returns 5 integers (in one resultset)
> > indicating how many rows were affected by the various
> > updates/deletes.
> >
> > How do I do this?

You could return an array like elein recommended....

> > How can I specify the names of the 5 output colums?

but you won't have "names" associated with the output columns. To have
something other than integers as the index (such as a string), you
need to use an associative array.

sub foo
{
    my $integer = shift;

    my $ret1 = $dbh->do('some sql here');

    my $ret2 = $dbh->do('some sql here');

    # etc.... for 5 statements.

    my %hash = (
        'Column Label 1' => $ret1,
        'Column Label 2' => $ret2,
        #..... etc...
    );

     return \%hash;
}

So, you should read about associative arrays (aka hashes) and references.

Rick

pgsql-general by date:

Previous
From: Tzahi Fadida
Date:
Subject: SPI vs low level functions.
Next
From: Greg Stark
Date:
Subject: Re: row numbering