【发布时间】:2023-03-21 17:20:01
【问题描述】:
我正在尝试在我的 postgres 数据库上创建一个新架构,其名称存储在现有表中,我的查询如下所示:
CREATE SCHEMA (SELECT name FROM table)
但我收到语法错误。我究竟做错了什么?这是创建新架构的有效方法吗?对于这个问题还有哪些其他解决方案?
【问题讨论】:
标签: sql postgresql select schema
我正在尝试在我的 postgres 数据库上创建一个新架构,其名称存储在现有表中,我的查询如下所示:
CREATE SCHEMA (SELECT name FROM table)
但我收到语法错误。我究竟做错了什么?这是创建新架构的有效方法吗?对于这个问题还有哪些其他解决方案?
【问题讨论】:
标签: sql postgresql select schema
你可以用动态sql来做:
do
$$
declare s_name text;
begin
-- make sure there is exactly one row in that table!
-- otherwise you need some where condition or an aggregat to ensure that.
SELECT name INTO s_name FROM some_table;
execute 'create schema '|| quote_ident(s_name);
end;
$$
【讨论】:
不可能,创建模式语法需要有效的模式名称(即有效的模式名称字符串)。在你的情况下,它会抛出异常
********** Error **********
ERROR: syntax error at or near "("
SQL state: 42601
Character: 15
【讨论】: