Re: Using null or not null in function arguments

From: Raymond O'Donnell <rod(at)iol(dot)ie>
To: Igor Katson <descentspb(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Using null or not null in function arguments
Date: 2009-01-23 12:14:42
Message-ID: 4979B4B2.9060302@iol.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 23/01/2009 11:16, Igor Katson wrote:

> How do I write a function without complex logic, which will do:
> a) If the input argument is NULL, then the corresponding select
> statement will change from
>
> column = arg
> to
> column IS NULL

You could build your statement dynamically as a string, then execute it
using EXECUTE:

http://www.postgresql.org/docs/8.3/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

> b) If the input argument is NULL, then the corresponding select
> statement will be removed, so if it was not written.

Likewise - something like

if i_city_id is null then
... build statement...
... execute statement ...
end if;

HTH,

Ray.

------------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod(at)iol(dot)ie
Galway Cathedral Recitals: http://www.galwaycathedral.org/recitals
------------------------------------------------------------------

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Igor Katson 2009-01-23 12:33:03 Re: Using null or not null in function arguments
Previous Message Howard Cole 2009-01-23 12:08:02 How to convert ByteA to Large Objects