【问题标题】:Oracle select schema depending on select-statementOracle 选择模式取决于选择语句
【发布时间】:2014-10-05 08:09:13
【问题描述】:

我有两个带有同一张表的模式。例如 schema1.adress 和 schema2.adress。 两张表完全相同。

表客户的布局: 客户编号:整数 名称:varchar2(50)

现在我想使用类似
的方式来获取模式 1 的客户 "select * from customer where schemaname = 1" 或
"select * from customer where schemaname = 2"

Oracle 中是否有一种机制可以根据选择语句中的条件切换架构?

在你问之前:对于一个新项目,我必须查询旧架构。我无法更改架构,但我可以设置架构/用户的任何权限。

有什么想法吗?

感谢您的回复,

斯文

【问题讨论】:

    标签: oracle select schema


    【解决方案1】:

    所以当您想从 schema1.customer 中选择和从 schema2.customer 中选择时,您都以同一用户身份登录

    一种可能的方式可以是:

    select *
      from (
       select 'schema1' schema$name, c.* from schema1.customer c
       union all
       select 'schema2' schema$name, c.* from schema2.customer c
      )
     where schema$name = 'schema1'
    

    如果您可以创建视图,则可以创建如下视图:

    create or replace view customer_view as
       select 'schema1' schema$name, c.* from schema1.customer c
       union all
       select 'schema2' schema$name, c.* from schema2.customer c
    

    这样就可以做到:

    select *
      from customer_view
     where schema$name = 'schema1'
    

    无论您是否使用视图,Oracle 优化器在大多数情况下都可以像这样将谓词“where schema$name = 'schema1'”推送到 UNION ALL 中,然后它会识别出它不需要访问 schema2.customer全部。

    【讨论】:

      【解决方案2】:

      您可以为您希望他们访问的 schema.table 的用户创建一个私有同义词,

      create synonym user1.customer for schemaname1.customer;
      create synonym user2.customer for schemaname2.customer;
      

      那么您的查询将始终只是 select * from customer;

      【讨论】:

        猜你喜欢
        • 2015-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-14
        • 2015-01-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多