Re: Need help with CASE statement in Function - Mailing list pgsql-sql

From Dawid Kuroczko
Subject Re: Need help with CASE statement in Function
Date
Msg-id 758d5e7f0710031004q3c71b07bqc04158750527621e@mail.gmail.com
Whole thread Raw
In response to Need help with CASE statement in Function  ("Hengky Lie" <hengkyliwandouw@gmail.com>)
List pgsql-sql
On 10/3/07, Hengky Lie <hengkyliwandouw@gmail.com> wrote:
> Dear friends,
> I am a new user to postgreSQL and really need help to solve my "stupid ?"
> problem.
>
> I have created  function with 4 arguments like this :
>
> CREATE OR REPLACE FUNCTION "public"."fHistoryCard" (begdate date, enddate
> date, ProductID varchar, storeID varchar) RETURNS SETOF "public"."tbltrans"
> AS
>
> $body$
> /* New function body */
> select * from tbltrans
> where tbltrans."Date" between $1 and $2
> and upper(tbltrans."ProductID")=upper($3)
> and tbltrans."StoreID"=$4
> order by tbltrans."Tanggal";
> $body$
>
> LANGUAGE 'sql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
> ---------------------
> My Question is : How to make argument 4 optional ? When IS NULL the function
> will show all transaction between date $1 and $2 and product ID=$3
> But when $4 is not null then the fucntion will show all transaction between
> date $1 and $2 and product ID=$3 AND STOREID=$4.

Try something like:

CREATE OR REPLACE FUNCTION "public"."fHistoryCard" (begdate date, enddate
date, ProductID varchar, storeID varchar) RETURNS SETOF "public"."tbltrans"
AS

$body$
/* New function body */   SELECT * FROM tbltrans    WHERE tbltrans."Date" between $1 and $2    AND
upper(tbltrans."ProductID")=upper($3)   AND ($4 IS NULL OR tbltrans."StoreID"=$4)  ORDER BY tbltrans."Tanggal";
 
$body$
LANGUAGE 'sql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

In other words, change:tbltrans."StoreID"=$4
into($4 IS NULL OR tbltrans."StoreID"=$4)

Haven't tested it, but should work fine.
  Regards,     Dawid


pgsql-sql by date:

Previous
From: "Hengky Lie"
Date:
Subject: Need help with CASE statement in Function
Next
From: Richard Broersma Jr
Date:
Subject: Re: Need help with CASE statement in Function