Search code examples
postgresqlinsertplpgsqlexecute

Execute INTO to accept NULL values


I have searched the internet and cannot find any information or examples of how to use EXECUTE INTO including when the stamement may return nothing.

I am getting the error

query string argument of EXECUTE is null

I have tried using IF EXISTS() Creating a statement that includes a count and a whole raft of things.At this point I feel like im going around in circles. FRUSTRATING!

Is there a default method for doing this?


Solution

  • here execute format('select 1 where false') into t will put nothing to t, so later execute t would give

    ERROR: query string argument of EXECUTE is null

    so simple coalesce will help:

    t=# do $$
    declare t text;
    begin
    execute format('select 1 where %L',false) into t;
    execute coalesce(t,'select now()') into t;
    raise '%',t;
    end;
    $$
    ;
    ERROR:  2017-07-11 16:11:03.149521+00
    Time: 0.349 ms