2016-10-13 2 views

ответ

3

На подходе является создание динамического сценария:

SELECT 'SELECT something FROM ' || table_name ||';' 
    FROM information_schema.columns 
    WHERE column_name = 'something' 

затем запустить вывод скрипта генерируемой. Если вас беспокоят имена схем, типы столбцов, союзы и т. Д., Измените динамическое поколение sql соответственно.

0
begin; 
do $$ 
declare 
    stmt text; 
    tbls text[]; 
    col text := 'x'; 
begin 
    select 
    array_agg(format(
     'select ''%2$s.%3$s'' as tbl, %1$I::text from %2$I.%3$I', 
     col, table_schema, table_name)) 
    into tbls 
    from information_schema.columns 
    where column_name = col; 
    raise info '%', tbls; 
    stmt := array_to_string(tbls, ' union all '); 
    raise info '%', stmt; 
    execute 'declare foo cursor for ' || stmt; 
end $$; 
fetch all from foo; 
rollback; 

и результат (для моей тестовой БД):

INFO: {"select 'public.dummy' as tbl, x::text from public.dummy","select 'nd.t' as tbl, x::text from nd.t"} 
INFO: select 'public.dummy' as tbl, x::text from public.dummy union all select 'nd.t' as tbl, x::text from nd.t 
╔══════════════╤═══╗ 
║  tbl  │ x ║ 
╠══════════════╪═══╣ 
║ public.dummy │ X ║ 
║ nd.t   │ 3 ║ 
╚══════════════╧═══╝ 
(2 rows) 

Он должен быть выполнен в одной транзакции (begin; ... rollback;), потому что курсор создан declare ... заявление существует только в сделке.

Смежные вопросы