What you need to do is:
- Build each individual query; then
string_agg
them together with UNION ALL
as the joining string.
UNION ALL
is important, not just UNION
, as UNION
may force a sort-and-de-duplicate operation, which is slow.
I will use the format
function and its %I
and %L
format-specififers instead of quote_ident
and quote_literal
respectively, because it's massively more readable that way.
I have no idea what the stray ROM
in:
'select * from ROM ' || quote_ident("TableName") ||' UNION'
is. It doesn't make sense. I'm ignoring it. I assume it's probably an editing error, the end of an accidentally remaining FROM
.
So, first, take a query that produces a list of queries for each table:
SELECT
format('SELECT * FROM %I', "TableName")
FROM "AllTables" a
INNER JOIN INFORMATION_SCHEMA.TABLES so ON a."TableName" = so.TABLE_NAME;
then wrap it in an aggregate (string_agg
) to combine the strings, prefix the CREATE VIEW
and store the result in your query variable and execute it:
SELECT INTO que
'CREATE VIEW view1 AS ' ||
string_agg( format('SELECT * FROM %I', "TableName"), ' UNION ALL ')
FROM "AllTables" a
INNER JOIN INFORMATION_SCHEMA.TABLES so ON a."TableName" = so.TABLE_NAME;
raise notice '%',que;
execute que;
BTW, needing to do things like this often means you have a bit of a database design issue. You should not need tons of little tables with the same schema like this. It usually means you're doing this:
CREATE TABLE customer01_transactions (
trans_id serial primary key,
...
)
CREATE TABLE customer02_transactions (
trans_id serial primary key,
...
)
when you should usually be doing this:
CREATE TABLE customer_transactions (
customer_id integer,
trans_id serial,
...,
PRIMARY KEY(customer_id, trans_id)
)
union all
query that combines the data from several tables with identical columns? – Craig Ringer Jul 2 at 13:05string_agg
to aggregate query strings, using ` UNION ALL ` (not justUNION
) as the join string. This sort of thing often suggests an underlying design problem though. – Craig Ringer Jul 3 at 4:41