【问题标题】:CREATE SCHEMA using a name stored使用存储的名称创建模式
【发布时间】:2023-03-21 17:20:01
【问题描述】:

我正在尝试在我的 postgres 数据库上创建一个新架构,其名称存储在现有表中,我的查询如下所示:

CREATE SCHEMA (SELECT name FROM table)

但我收到语法错误。我究竟做错了什么?这是创建新架构的有效方法吗?对于这个问题还有哪些其他解决方案?

【问题讨论】:

    标签: sql postgresql select schema


    【解决方案1】:

    你可以用动态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;
    $$
    

    【讨论】:

    • @ErwinBrandstetter:感谢您修复语法错误(Oracle 最近太多...)
    【解决方案2】:

    不可能,创建模式语法需要有效的模式名称(即有效的模式名称字符串)。在你的情况下,它会抛出异常

    ********** Error **********
    
    ERROR: syntax error at or near "("
    SQL state: 42601
    Character: 15
    

    【讨论】:

    • 动态 SQL 是可能的。
    • @xiidarkevil:这个答案不是很有帮助。你应该接受 a_horse 的回答。
    猜你喜欢
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2020-07-16
    相关资源
    最近更新 更多