Re: select where true, or select where input = '$var' - Mailing list pgsql-general

From David G Johnston
Subject Re: select where true, or select where input = '$var'
Date
Msg-id 1424385211867-5838638.post@n5.nabble.com
Whole thread Raw
In response to Re: select where true, or select where input = '$var'  (zach cruise <zachc1980@gmail.com>)
List pgsql-general
zach cruise wrote
> sorry, corrected below:
>
> 1 select *
> 2 from table
> 3 if '$sanitized_variable' = '' then -- $variable not provided or
> undefined or empty,
> 4  where true -- so select every row/record
> 5 else
> 6  where input = '$sanitized_variable' -- variable provided or defined
> or not-empty, so select only matching rows/records where input is a
> column/field
> 7 end if;

You cannot directly put that kind of logic in SQL.  You can normalize the
input so that if it is "not provided or undefined" you convert it into
"empty" and then write a single query that recognizes the "empty" input as
being the "select-all" form and anything non-empty input as being a limited
form.

SELECT ... FROM ... WHERE ($1::text = '') OR ($1::text = input_col)

COALESCE(...) could also possibly be useful...

David J.




--
View this message in context:
http://postgresql.nabble.com/select-where-true-or-select-where-input-var-tp5838612p5838638.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


pgsql-general by date:

Previous
From: Juan Pablo L
Date:
Subject: Re: rollback in C functions
Next
From: Juan Pablo L
Date:
Subject: Re: rollback in C functions