Re: Function returning subset of columns from table (return type) - Mailing list pgsql-general

From Gurjeet Singh
Subject Re: Function returning subset of columns from table (return type)
Date
Msg-id 65937bea0802031637n2887319jc1e4ca159e61ad0@mail.gmail.com
Whole thread Raw
In response to Re: Function returning subset of columns from table (return type)  (brian <brian@zijn-digital.com>)
Responses Re: Function returning subset of columns from table (return type)
List pgsql-general
On Feb 3, 2008 3:23 PM, brian <brian@zijn-digital.com> wrote:
CREATE TYPE your_type
AS (
  added CHAR(11) NOT NULL,
  updated CHAR(11) NOT NULL,
  text_col TEXT
);

CREATE FUNCTION get_note(id INT)
RETURNS SETOF your_type IMMUTABLE
AS $$

  DECLARE
    your_row your_type%rowtype;

  BEGIN
    SELECT INTO your_row
      to_char(added, 'Mon D YYYY'),
      to_char(updated, 'Mon D YYYY'),
      text_col
    FROM note
    WHERE id = $1;

    RETURN your_row;
  END;
$$ LANGUAGE sql;


All's okay, except you should not have declared it IMMUTABLE, because the results depend on a database query.

From the docs:
IMMUTABLE indicates that the function cannot modify the database and always returns the same result when given the same argument values; that is, it does not do database lookups or otherwise use information not directly present in its argument list

Best regards,
--
gurjeet[.singh]@EnterpriseDB.com
singh.gurjeet@{ gmail | hotmail | indiatimes | yahoo }.com

EnterpriseDB      http://www.enterprisedb.com

17° 29' 34.37"N,   78° 30' 59.76"E - Hyderabad
18° 32' 57.25"N,   73° 56' 25.42"E - Pune
37° 47' 19.72"N, 122° 24' 1.69" W - San Francisco *

http://gurjeet.frihost.net

Mail sent from my BlackLaptop device

pgsql-general by date:

Previous
From: brian
Date:
Subject: Re: Function returning subset of columns from table (return type)
Next
From: "Christopher Browne"
Date:
Subject: Re: postgresql book - practical or something newer?