COALESCE not filtering well.

Поиск
Список
Период
Сортировка
От Mohan Raj B
Тема COALESCE not filtering well.
Дата
Msg-id 19eab1aa0907052237k3884f13r344eb86f128b78df@mail.gmail.com
обсуждение исходный текст
Ответы Re: COALESCE not filtering well.
Список 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;

В списке pgsql-general по дате отправления:

Предыдущее
От: sqlguru
Дата:
Сообщение: Passing a table to function
Следующее
От: Abbas
Дата:
Сообщение: Re: Postgres Plus Advanced Server and general Postgres compatibility?