pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dawid Kuroczko <qnex42@gmail.com>
To: Hengky Lie <hengkyliwandouw@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Need help with CASE statement in Function
Date: Wed, 3 Oct 2007 19:04:22 +0200
Message-ID: <758d5e7f0710031004q3c71b07bqc04158750527621e@mail.gmail.com> (raw)
In-Reply-To: <000d01c805dc$9e8db450$1d00a8c0@hengky>
References: <000d01c805dc$9e8db450$1d00a8c0@hengky>

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



view thread (3+ messages)  latest in thread

Message-ID: <758d5e7f0710031004q3c71b07bqc04158750527621e@mail.gmail.com>
Permalink:  ../758d5e7f0710031004q3c71b07bqc04158750527621e@mail.gmail.com/
Also on:    postgresql.org/message-id/758d5e7f0710031004q3c71b07bqc04158750527621e@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: qnex42@gmail.com, hengkyliwandouw@gmail.com
  Subject: Re: Need help with CASE statement in Function
  In-Reply-To: <758d5e7f0710031004q3c71b07bqc04158750527621e@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox