I am trying to return multiple records using RECORD data type, is there a way I can append to RECORD and add/append a new value with each iteration to this RECORD.
that is, I want to append to rec
so that rec
becomes a set of rows when the loop is over, which I can just RETURN at the end of my function.
Currently, I am doing this -
SELECT temp_table.col1, temp_table.col2, temp_table.col3
INTO rec
FROM temp_table
WHERE temp_table.col3 = false;
my full code is here:
CREATE OR REPLACE FUNCTION validation()
RETURNS RECORD AS $$
DECLARE
rec RECORD;
temp_row RECORD;
BEGIN
CREATE TEMPORARY TABLE temp_table (col1 TEXT, col2 INTEGER, col3 BOOLEAN) ON COMMIT DROP;
FOR temp_row IN SELECT * FROM staging.validation
LOOP
RAISE NOTICE 'sql: %', temp_row.sql;
EXECUTE format('INSERT INTO temp_table %s', temp_row.sql);
IF (SELECT DISTINCT temp_table.col3 FROM temp_table WHERE temp_table.col3 = false)=false THEN
RAISE NOTICE 'there is a false value';
SELECT temp_table.col1, temp_table.col2, temp_table.col3
INTO rec
FROM temp_table
WHERE temp_table.col3 = false;
END IF;
END LOOP;
RETURN rec;
END; $$
LANGUAGE plpgsql;
Current output after SELECT validation();
validation
(crea_ddf,8095,f)
Desired Output
validation
(crea_ddf,8095,f)
(some_source_system,some_count,f)
(some_other_source_system,some_count,f)
(.....)