gpt4 book ai didi

sql - postgres 本地模式限定符

转载 作者:行者123 更新时间:2023-11-29 11:52:03 24 4
gpt4 key购买 nike

我正在使用 postgres 模式对 View 和函数进行分组,并将它们的多个版本保存在数据库中(有时需要向后兼容)因为在不同的模式中有相同函数的多个版本我不能简单地通过名称引用它但是需要写入完整的限定名称“schema.funcname”才能访问它。

当从 schemaA 中的另一个函数引用 schemaA 中的函数时,我总是必须编写 schemaA.funcname。当我稍后重命名模式时,这让我很烦恼——是否有一个限定符指示应该使用同一模式中的函数?也许像 java 中的“this”限定符?

希望它能明白我的意思

谢谢

最佳答案

您可以使用 set schema更改当前 session 的搜索路径:

create schema test1;
create schema test2;

create function test1.f() returns int as $body$ begin return 1; end; $body$ language plpgsql;
create function test2.f() returns int as $body$ begin return 2; end; $body$ language plpgsql;

set schema 'test1';
select f();

set schema 'test2';
select f();

您还可以添加 search_path函数定义:

create or replace function test1.x() returns int as $body$ begin return 1; end; $body$ language plpgsql;
create or replace function test1.f() returns int as $body$ begin return x(); end; $body$ language plpgsql set search_path = 'test1';

create or replace function test2.x() returns int as $body$ begin return 2; end; $body$ language plpgsql;
create or replace function test2.f() returns int as $body$ begin return x(); end; $body$ language plpgsql set search_path = 'test2';

select test1.f();

select test2.f();

关于sql - postgres 本地模式限定符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21991292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com