I have several views on the same table containing overlapping data. By dynamically retrieving the views from the information_schema and unioning them I can create a text query result. However I still need to manually run that query to get the desired result.
This is the query I use to build the union query:
SELECT string_agg('SELECT * FROM '||TABLE_NAME, ' UNION ')::TEXT
FROM INFORMATION_SCHEMA.VIEWS
where TABLE_NAME like '%value%'
I did an initial attempt with executing the query and storing it in a temporary table, however no success yet:
do $$
declare
q text;
begin
q := format('SELECT string_agg(''SELECT * FROM ''||TABLE_NAME, '' UNION '')::TEXT FROM INFORMATION_SCHEMA.VIEWS where TABLE_NAME like ''%value%''');
execute format('create or replace temp view t ', q);
end $$;
select * from t;
How can I execute the created query and get the actual result?