COALESCE not filtering well.

From: Mohan Raj B <brightmohan(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: COALESCE not filtering well.
Date: 2009-07-06 05:37:25
Message-ID: 19eab1aa0907052237k3884f13r344eb86f128b78df@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

G'Day!

I have issues with filtering the data based on the criteria. Please take a
look at the way I use COALESCE especially the WHERE part of my function.

The function is not returning me a filtered result.

for example, if I try to execute the function as follows:

SELECT * FROM sp_item(10,NULL); [It returns all the rows.... which is not
what I am expecting... I'm expecting only the row with itemid=10 ]

Please advise.

Thanks & Regards,
Mohan

--------------------------------------------------------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION sp_item(itemid integer, itemname character
varying)
RETURNS SETOF item AS
$BODY$

declare
ret_row record;

BEGIN

FOR ret_row in
--SELECT itemid,itemcode,itemname,itemdescription,archived from item
SELECT * FROM item
WHERE ( ( COALESCE($1,0)=0 OR itemid=$1) AND (COALESCE($2, '')='' OR
itemname LIKE '%'||$2||'%') ) LOOP
return next ret_row;

END LOOP;
return;
END;

$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100
ROWS 1000;

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Juan Pablo Cook 2009-07-06 05:55:54 LIKE problem
Previous Message Ricardo Pinho 2009-07-06 00:49:08 GISVM Server pre-release available