Ell*_* B. 6 postgresql plpgsql postgresql-9.3
在Postgres中,您可以使用dblink
如下链接到您的其他数据库:
SELECT *
FROM dblink (
'dbname=name port=1234 host=host user=user password=password',
'select * from table'
) AS users([insert each column name and its type here]);
Run Code Online (Sandbox Code Playgroud)
但这很冗长.
我通过使用dblink_connect
和dblink_disconnect
从我的dblink
查询中抽象连接字符串来缩短它.但是,这仍然留给我手动表定义(即[insert each column name and its type here]
).
而不是手动定义表,有没有一种方法可以用一个TYPE
或其他可重复使用的东西来定义它?
在我的例子中,我必须加入的远程表的数量和涉及的列数使我的查询量大.
我尝试过以下方面:
SELECT *
FROM dblink (
'myconn',
'select * from table'
) AS users(postgres_pre_defined_type_here);
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
ERROR: a column definition list is required for functions returning "record"
当您考虑为 dblink 创建多种类型时,您也可以接受创建多种函数。这些功能将被明确定义并且非常易于使用。
例子:
create or replace function dblink_tables()
returns table (table_schema text, table_name text)
language plpgsql
as $$
begin
return query select * from dblink (
'dbname=test password=mypassword',
'select table_schema, table_name from information_schema.tables')
as tables (table_schema text, table_name text);
end $$;
select table_name
from dblink_tables()
where table_schema = 'public'
order by 1
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1018 次 |
最近记录: |