加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

可以使用PostgreSQL TYPE来定义dblink表吗?

发布时间:2020-12-13 18:10:24 所属栏目:百科 来源:网络整理
导读:在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]); 但这很冗长. 我通过使
在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]);

但这很冗长.

我通过使用dblink_connect和dblink_disconnect从我的dblink查询中抽象连接字符串来缩短它.但是,这仍然留给我手动表定义(即,[在此处插入每个列名称及其类型]).

而不是手动定义表,有没有办法我可以用TYPE或其他任何可重复使用的东西来定义它?

在我的例子中,我必须加入的远程表的数量和涉及的列数使我的查询量大.

我尝试过以下方面:

SELECT *
FROM dblink (
    'myconn','select * from table'
) AS users(postgres_pre_defined_type_here);

但我收到以下错误:

错误:返回“记录”的函数需要列定义列表

在考虑为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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读