I want to write a function, witch selects some values from atable and then insert them into btable.
DECLARE
loRecord record;
loRTest record;
lsQueryExecute text;
BEGIN
SELECT INTO loRTest
1 as test1,
0 as test2,
NULL::int as test_fk1,
NULL::timestamp as test_fk2,
NOW() as teststamp,
true as test_bool
FROM atable
raise notice 'result % ', loRTest;
/* result (1,0,,,"2020-01-29 11:28:33.785621+01",t) */
doing some stuff with the values and finaly inserting them into another table:
lsQueryExecute =
'insert into btable (test1, test2, test_fk1, test_fk2, teststamp, test_bool)
VALUES(
''' || loRTest.test1 || ''',
''' || loRTest.test2 || ''',
''' || loRTest.test_fk1 || ''',
''' || loRTest.test_fk2 || ''',
''' || loRTest.teststamp || ''',
''' || loRTest.test_bool ||''')';
/* EXECUTE */
raise notice 'query % ', lsQueryExecute;
/* query <NULL> */
well, concatenation with || and a null sets the whole string to null. Whats the correct (easy, nice ) way to concat the string (postgres 10)?
CONCATfunction instead of||. It ignores NULL values (>9.6). If you are on <9.6, useCOALESCE(NULL, '').quote_literalorquote_nullablefunctions, or useUSINGclause