Re: postgresql wildcard when parameter is -1

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: ben sewell <mosherben(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: postgresql wildcard when parameter is -1
Date: 2006-08-24 11:48:38
Message-ID: 20060824114838.GA94044@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Thu, Aug 24, 2006 at 09:01:06AM +0100, ben sewell wrote:
> I have a question, when a parameter is recieved as -1 (as an integer from
> Access), I would like that parameter to be a wildcard. The logic is that I
> am sending the primary key for a table to postgres for a report but when
> there hasnt been a value chosen in the combo box -1 is sent to postgres so
> it would mean that that parameter don't apply in the SP, otherwise if it did
> have another value then the parameter would be used to do some filtering
> (joins).

You'll need to rewrite the query to handle the "wildcard". One way
would be with an expression like this:

WHERE (param = -1 OR param = column_name) AND ...

The parentheses are important if you have multiple expressions.

Another way would be to build the query string dynamically, adding
only the parts you need, then EXECUTE it (assuming PL/pgSQL; do the
equivalent in other languages). See "Executing Dynamic Commands"
in the PL/pgSQL documentation:

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

--
Michael Fuhr

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Fuhr 2006-08-24 11:58:07 Re: [GENERAL] Shared Objects (Dynamic loading)
Previous Message Bruno Wolff III 2006-08-24 11:40:12 Re: protecting a database