I have the following function with one parameter. I need to display a string which consist select statement and where condition.
Example:
create or replace function funct(a int)
returns void as
$$
declare
wrclause varchar := '';
sqlq varchar ;
t varchar;
begin
wrclause := ' AND C IN ('|| a || ')';
sqlq := ' t :=select string_agg(''select *, abcd as "D" from '' || table_namess ||, '' Union all '') as namess
from tablescollection2 ud
inner join INFORMATION_SCHEMA.Tables so on ud.table_namess = so.Table_name where C = '|| a ||'' || wrclause;
raise info '%',sqlq;
perform sqlq;
raise info '%',t;
end;
$$
language plpgsql;
Calling Function: With value 1
select funct(1);
Output:
INFO: t :=select string_agg('select *, abcd as "D" from ' || table_namess ||, ' Union all ') as namess
from tablescollection2 ud
inner join INFORMATION_SCHEMA.Tables so on ud.table_namess = so.Table_name where C = 1 AND C IN (1)
INFO: <NULL>
Calling Function: With null value
select funct(null);
Output:
INFO: <NULL>
INFO: <NULL>
Note: I am not getting after assigning empty string to wrclause variable why I am getting <NULL> string and also <NULL> to variable t.
NULLwill result inNULL, just as any number added toNULLwill result inNULL, and any boolean expression such asAND NULLorOR NULLwill evaluate toNULL.nullvalues to pass then what is the solution for this? ThanksSET CONCAT_NULL_YIELDS_NULL OFF. How about PostgreSQL?COALESCE(var, '')is the simplest; but in this case that would result in invalid SQL anyway (AND C IN ()).