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;
및 결과 (내 테스트 DB에 대한)입니다 :
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)
declare ...
문에 의해 생성 된 커서는 트랜잭션에 존재하기 때문에이 단일 트랜잭션 (begin; ... rollback;
)에서 실행해야합니다.