我正在尝试在存在于多个具有相同名称的模式中的表上创建一个 View (或在每个模式中不经修改地使用的 View )
create schema company_1;
create schema company_2;
...
CREATE TABLE company_1.orders
(
id serial NOT NULL,
amount real,
paid real,
CONSTRAINT orders_pkey PRIMARY KEY (id )
)
WITH (
OIDS=FALSE
);
CREATE TABLE company_2.orders
(
id serial NOT NULL,
amount real,
paid real,
CONSTRAINT orders_pkey PRIMARY KEY (id )
)
WITH (
OIDS=FALSE
);
....
在表订单上创建 View 而不为每个 View 指定架构或指定当前架构的正确方法是什么?
我需要但未能得到的是
CREATE OR REPLACE VIEW
public.full_orders AS
SELECT id, amount FROM orders;
或
CREATE OR REPLACE VIEW
company_1.full_orders AS
-- company_2.full_orders AS
-- company_n.full_orders AS
SELECT id, amount FROM current_schema.orders;
使用postgresql 9.2.2
编辑:我走了的路:
CREATE VIEW company_1.full_orders AS
SELECT id, amount FROM company_1.orders;
讨论架构副本 here我只是这样做
FOR src_table IN
SELECT table_name
FROM information_schema.TABLES
WHERE table_schema = source_schema AND table_type = 'VIEW'
LOOP
SELECT view_definition
FROM information_schema.views
WHERE table_name = src_table AND table_schema = source_schema INTO q;
trg_table := target_schema||'.'||src_table;
EXECUTE 'CREATE VIEW ' || trg_table || ' AS '||replace(q, source_schema, target_schema);
END LOOP;
仍在寻找更好的解决方案...
最佳答案
不可能用简单的 View 来完成。该 View 在创建时记录了基础表的标识,因此它不受稍后进行的架构设置的影响。
您可以使用动态 SQL 的集合返回函数来完成此操作,然后将其包装到 View 中。但我认为这不是一个好的解决方案。
我只会为 View 创建准重复项,就像您一直在做的那样,并增强我的部署脚本以使它们全部保持最新。
关于postgresql - 在存在于多个具有相同名称的模式中的表上创建 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14399572/