0

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.

5
  • Any string concatenated with NULL will result in NULL, just as any number added to NULL will result in NULL, and any boolean expression such as AND NULL or OR NULL will evaluate to NULL. Commented Jul 10, 2014 at 11:09
  • @IMSoP, If I have null values to pass then what is the solution for this? Thanks Commented Jul 10, 2014 at 11:11
  • @IMSoP, In SQL Server we can do this by using SET CONCAT_NULL_YIELDS_NULL OFF. How about PostgreSQL? Commented Jul 10, 2014 at 11:14
  • what should be query if a = NULL ? Commented Jul 10, 2014 at 11:21
  • @Meem Rather than looking for a way to disable standard behaviour, just handle the nulls properly; if you want an empty string for null, COALESCE(var, '') is the simplest; but in this case that would result in invalid SQL anyway (AND C IN ()). Commented Jul 10, 2014 at 11:22

1 Answer 1

1

You can Try something like below if it satisfied your all meets for NULL as well:

create or replace function funct(a int)
returns void as
$$
declare
       wrclause varchar := '';
       sqlq varchar ;
       t varchar;
begin
    IF (a IS NOT NULL ) THEN 
        wrclause := 'where C = '|| a ||' AND C IN ('|| a || ')';
    END IF;


       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 ' || wrclause;

       raise info '%',sqlq;

       perform sqlq;

       raise info '%',t;
end;
$$
language plpgsql;

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>

select funct(null)

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
INFO:  <NULL>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. One more confusion here: what about variable t? It need to show select statement result.
where is the value of variable: table_namess ?
That is the column name which I will get from table "tablescollection2".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.