I am trying to append single quote in query in postgres function but result in error please have a look on my postgres function,
CREATE OR REPLACE function test() returns my_type as $$
declare rd varchar := '56';
declare personphone varchar := 'Philip Dannes';
declare result my_type;
declare SQL VARCHAR(300):=null;
BEGIN
-- Mandatory / Static part of the Query here
SQL = 'select pt.id from product_template pt inner join product_product pp on pt.id=pp.id where ';
IF rd IS NOT NULL
then
SQL = SQL || 'pp.radio_phone = '|| rd;
else
SQL = SQL || 'pp.radio_phone = '|| rd;
end if;
IF personphone IS NOT NULL
then
SQL = SQL || ' and pp.person_phone = '|| personphone;
else
SQL = SQL || ' and pp.person_phone = '|| personphone;
end if;
SQL = SQL || ';';
EXECUTE SQL;
return result;
END
$$ LANGUAGE plpgsql;
when i execute it it gives me error on "Philip Daves" and return query after appending as,
Select pt.id from product_template pt inner join product_product pp on pt.id=pp.id where
pp.radio_phone = 56 and pp.person_phone = Philip Dave
I know error is because 56 and Philip Dave is not in single quote when i execute function return query with single quote it works fine.
How would i append Single quote in this query ??
I tried in this way like,
SQL = SQL || ' and pp.person_phone = '|| '' || personphone;
But i function return the same query
Hopes for your suggestion
Thanks in advance