I have to table with four columns as shown below:
Table:
create table fortesting
(
cola int not null,
colb timestamp null,
colc text null,
cold int null
)
Now I want to create a function to insert the records into the table with the dynamic sql so I used language plpgsql function as shown below.
Function:
create or replace function functionTest
(
p_cola int,
p_colb timestamp = null,
p_colc text = '',
p_cold int = null
)
returns void as
$body$
Declare v_query varchar;
begin
v_query := 'insert into fortesting(cola,colb,colc,cold) values('||p_cola||','''||p_colb||''','''||p_colc||''','||p_cold||')';
RAISE INFO '%',v_query;
EXECUTE v_query;
end;
$body$
language plpgsql;
Calling FUNCTION:
I only pass two values to two parameter rest should go null into table.
SELECT * FROM functionTest( p_cola := 1, p_colc:='abc');
Error Details:
INFO: <NULL>
ERROR: query string argument of EXECUTE is null
CONTEXT: PL/pgSQL function functiontest(integer,timestamp without time zone,text,integer) line 11 at EXECUTE statement
quote_identorquote_literalor useUSINGclause. Without it the code is SQL injection vulnerable!