Thread: returning result set

returning result set

From
"Gohil, Hemant"
Date:

Hi,

 

I am very new to PostgerSQL and  trying to create following function and return the result set back.

For some reason I am not getting following error.

ERROR: a column definition list is required for functions returning "record"

 

This function could return multiple rows.

 

Any pointers to specific solution will also be appreciated.

 

Thanks

HG

 

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

CREATE FUNCTION function1(integer) RETURNS SETOF record AS $$

 

DECLARE

    acodes RECORD;

BEGIN

FOR acodes in

SELECT

                                case

                                                 when $1  = a.col1 then '******'

                                                 else ''

                                 end AS "firstColumn" ,

                                 a.col1,

                                 a.col2

                                 a.col3,

                                 a.col6

FROM  table1 a  INNER JOIN  table2 b  ON  a.col2  = b.col2

WHERE a.col2  in

                (

                SELECT col2

                FROM  table1

                WHERE col1 = $1

                )

ORDER BY codeId LOOP

   return next acodes;

END LOOP;

END;

$$ LANGUAGE plpgsql;

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

 

 

 

Re: returning result set

From
"Richard Broersma"
Date:
On Mon, Sep 22, 2008 at 2:40 PM, Gohil, Hemant <HGohil@dylt.com> wrote:

> For some reason I am not getting following error.
> ERROR: a column definition list is required for functions returning "record"

> CREATE FUNCTION function1(integer) RETURNS SETOF record AS $$

When you select from this function don't you have to also alias the
name of the SQL result and its columns?

SELECT * from function1( 2 ) AS myresult( colA, colB, colC, colD, colE );


Also, If I am not mistake, Functional alias also allow you to specify
datatypes correct?
http://www.postgresql.org/docs/8.3/interactive/sql-select.html


--
Regards,
Richard Broersma Jr.

Visit the Los Angeles PostgreSQL Users Group (LAPUG)
http://pugs.postgresql.org/lapug