0
讓我說我有3個模式:foo,bar,baz。所以,我可以這樣做:postgres select all schemas
select * from foo.table, bar.table, baz.table;
如果我不想指定模式的名字,我想整個被一些用戶所擁有的所有模式或全部選擇?
讓我說我有3個模式:foo,bar,baz。所以,我可以這樣做:postgres select all schemas
select * from foo.table, bar.table, baz.table;
如果我不想指定模式的名字,我想整個被一些用戶所擁有的所有模式或全部選擇?
如果你的表有不同的名稱(應該是什麼在大多數情況下的規則)設置search path:
select * from foo.table1, bar.table2, baz.table3;
set search_path = foo, bar, baz;
select * from table1, table2, table3;
重要提示:使用明確連接的,而不是從條款清單表。
select *
from table1
join table2 on ...
join table3 on ...
如果你有幾種模式可以參考他們在一個函數的名稱相同的表。你需要從系統目錄信息:
此查詢列出了一個數據庫given_table_name所有表:
select
n.nspname, c.relname
from
pg_class c
join
pg_namespace n on n.oid = c.relnamespace
where
c.relkind = 'r'
and c.relname = 'given_table_name';
以下功能:
假設你已經定義的函數do_something_with_this_table(表名regclass的)早些時候:
create function do_something_with_all_these_tables(tablename text)
returns void language plpgsql
as $$
declare
schemaname text;
begin
for schemaname in
select n.nspname
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where c.relkind = 'r' and c.relname = tablename
loop
execute format(
'select do_something_with_this_table(''%s.%s'')',
schemaname, tablename);
end loop;
end $$;
select do_something_with_all_these_tables('given_table_name');
瞭解更多:
您示例中的查詢將給出三個表無約束聯接。我猜這不是你想要的。在任何情況下,答案都是「否」,除非您編寫一個函數爲您動態創建查詢。 –
這一次它正是我想要的 - 我需要它只是列出我的開發箱中的所有表,以檢查哪些包含數據:) – pawel7318
我發現模式名稱可以用'select schema_name from information_schema.schemata;'但我不知道如何構建將使用它的查詢。 – pawel7318