如何动态引用另一个数据库用户?

发布于 2024-07-30 03:02:04 字数 166 浏览 5 评论 0原文

我有一个案例,我需要引用另一个数据库用户。 我必须在引用数据库用户名时对其进行硬编码。

SELECT * FROM  eg001t3.DUAL; // example.

有没有办法从视图动态或基于数据库设置引用该数据库用户(例如001t3)?

I've a case in which I need to refer to another database user. I've to hard code database user name in view while referring to it.

SELECT * FROM  eg001t3.DUAL; // example.

Is there a way to refer to that db user (eg001t3) from view dynamically or based on a database setup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蒲公英的约定 2024-08-06 03:02:04

在 pl/sql 中,您将使用 EXECUTE IMMEDIATE 或 DBMS_SQL 来动态引用对象。

以 EXECUTE IMMEDIATE 为例:

SQL> VARIABLE dyn_user VARCHAR2(30);
SQL> EXEC :dyn_user := 'SYS';

PL/SQL procedure successfully completed
dyn_user
---------
SYS
SQL> DECLARE
  2     ln NUMBER;
  3  BEGIN
  4     EXECUTE IMMEDIATE 'SELECT 1
  5                          FROM ' || dbms_assert.schema_name(:dyn_user) 
  6                                 || '.DUAL'
  7        INTO ln;
  8     dbms_output.put_line(ln);
  9  END;
 10  /

1

PL/SQL procedure successfully completed

您还可以使用动态构建的 REF CURSOR:

SQL> DECLARE
  2     lc SYS_REFCURSOR;
  3     ln NUMBER;
  4  BEGIN
  5     OPEN lc FOR 'SELECT 1
  6                    FROM ' || dbms_assert.schema_name(:dyn_user) || '.DUAL
  7                   CONNECT BY level <= 2';
  8     LOOP
  9        FETCH lc
 10           INTO ln;
 11        EXIT WHEN lc%NOTFOUND;
 12        dbms_output.put_line(ln);
 13     END LOOP;
 14     CLOSE lc;
 15  END;
 16  /

1
1

如图所示,您可以使用 DBMS_ASSERT 来验证您的输入。

In pl/sql you would use EXECUTE IMMEDIATE or DBMS_SQL to dynamically reference objects.

Exemple with EXECUTE IMMEDIATE:

SQL> VARIABLE dyn_user VARCHAR2(30);
SQL> EXEC :dyn_user := 'SYS';

PL/SQL procedure successfully completed
dyn_user
---------
SYS
SQL> DECLARE
  2     ln NUMBER;
  3  BEGIN
  4     EXECUTE IMMEDIATE 'SELECT 1
  5                          FROM ' || dbms_assert.schema_name(:dyn_user) 
  6                                 || '.DUAL'
  7        INTO ln;
  8     dbms_output.put_line(ln);
  9  END;
 10  /

1

PL/SQL procedure successfully completed

You can also use dynamically built REF CURSOR:

SQL> DECLARE
  2     lc SYS_REFCURSOR;
  3     ln NUMBER;
  4  BEGIN
  5     OPEN lc FOR 'SELECT 1
  6                    FROM ' || dbms_assert.schema_name(:dyn_user) || '.DUAL
  7                   CONNECT BY level <= 2';
  8     LOOP
  9        FETCH lc
 10           INTO ln;
 11        EXIT WHEN lc%NOTFOUND;
 12        dbms_output.put_line(ln);
 13     END LOOP;
 14     CLOSE lc;
 15  END;
 16  /

1
1

As shown you can use DBMS_ASSERT to validate your input.

隐诗 2024-08-06 03:02:04

我添加一个新答案来演示 jva 建议的另一种方法。 所有表必须共享一个公共结构(以便 Oracle 在编译时能够知道视图列的数据类型)。

设置:

-- create 2 schemas
CREATE USER u1 IDENTIFIED BY u1;
CREATE USER u2 IDENTIFIED BY u2;
GRANT RESOURCE TO u1;
GRANT RESOURCE TO u2;

-- one table in each schema
CREATE TABLE u1.t AS 
   SELECT 2 * ROWNUM ID, 'foo' DATA FROM dual CONNECT BY LEVEL <= 5;
CREATE TABLE u2.t AS 
   SELECT 2 * ROWNUM - 1 ID, 'bar' DATA FROM dual CONNECT BY LEVEL <= 5;
GRANT SELECT ON u2.t TO u1;

-- the common structure
CREATE TYPE u1.t_row AS OBJECT (ID NUMBER, DATA VARCHAR2(3));
/
CREATE TYPE u1.t_row_list AS TABLE OF u1.t_row;
/

CREATE OR REPLACE PACKAGE u1.test_pck IS
   schema_name VARCHAR2(30) := 'U1';
   FUNCTION select_t RETURN u1.t_row_list PIPELINED;
END test_pck;
/

--Definition of the pipelined function and the view:
CREATE OR REPLACE PACKAGE BODY u1.test_pck IS

   FUNCTION select_t RETURN u1.t_row_list PIPELINED IS
      l_rc     SYS_REFCURSOR;
      l_id     NUMBER;
      l_data   VARCHAR2(3);
   BEGIN
      OPEN l_rc FOR 'SELECT id, data 
                       FROM ' || dbms_assert.schema_name(schema_name) || '.t';
      LOOP
         FETCH l_rc
            INTO l_id, l_data;
         EXIT WHEN l_rc%NOTFOUND;
         PIPE ROW (u1.t_row(l_id, l_data));
      END LOOP;
      CLOSE l_rc;
   END select_t;

END test_pck;
/

CREATE OR REPLACE VIEW u1.v AS 
SELECT ID, DATA 
  FROM TABLE(u1.test_pck.select_t);

然后,您将在包含架构名称的包中定义全局变量,然后查询视图:

SQL> EXEC u1.test_pck.schema_name := 'U1';

PL/SQL procedure successfully completed
SQL> SELECT * FROM u1.v;

        ID DATA
---------- ----
         2 foo
         4 foo
         6 foo
         8 foo
        10 foo
SQL> EXEC u1.test_pck.schema_name := 'U2';

PL/SQL procedure successfully completed
SQL> SELECT * FROM u1.v;

        ID DATA
---------- ----
         1 bar
         3 bar
         5 bar
         7 bar
         9 bar

I add a new answer to demonstrate another method suggested by jva. All the tables must share a common structure (so that Oracle will be able to know the datatype of the columns of the view at compile time).

Setup:

-- create 2 schemas
CREATE USER u1 IDENTIFIED BY u1;
CREATE USER u2 IDENTIFIED BY u2;
GRANT RESOURCE TO u1;
GRANT RESOURCE TO u2;

-- one table in each schema
CREATE TABLE u1.t AS 
   SELECT 2 * ROWNUM ID, 'foo' DATA FROM dual CONNECT BY LEVEL <= 5;
CREATE TABLE u2.t AS 
   SELECT 2 * ROWNUM - 1 ID, 'bar' DATA FROM dual CONNECT BY LEVEL <= 5;
GRANT SELECT ON u2.t TO u1;

-- the common structure
CREATE TYPE u1.t_row AS OBJECT (ID NUMBER, DATA VARCHAR2(3));
/
CREATE TYPE u1.t_row_list AS TABLE OF u1.t_row;
/

CREATE OR REPLACE PACKAGE u1.test_pck IS
   schema_name VARCHAR2(30) := 'U1';
   FUNCTION select_t RETURN u1.t_row_list PIPELINED;
END test_pck;
/

--Definition of the pipelined function and the view:
CREATE OR REPLACE PACKAGE BODY u1.test_pck IS

   FUNCTION select_t RETURN u1.t_row_list PIPELINED IS
      l_rc     SYS_REFCURSOR;
      l_id     NUMBER;
      l_data   VARCHAR2(3);
   BEGIN
      OPEN l_rc FOR 'SELECT id, data 
                       FROM ' || dbms_assert.schema_name(schema_name) || '.t';
      LOOP
         FETCH l_rc
            INTO l_id, l_data;
         EXIT WHEN l_rc%NOTFOUND;
         PIPE ROW (u1.t_row(l_id, l_data));
      END LOOP;
      CLOSE l_rc;
   END select_t;

END test_pck;
/

CREATE OR REPLACE VIEW u1.v AS 
SELECT ID, DATA 
  FROM TABLE(u1.test_pck.select_t);

You would then define the global variable in the package containing the schema name and then query the view:

SQL> EXEC u1.test_pck.schema_name := 'U1';

PL/SQL procedure successfully completed
SQL> SELECT * FROM u1.v;

        ID DATA
---------- ----
         2 foo
         4 foo
         6 foo
         8 foo
        10 foo
SQL> EXEC u1.test_pck.schema_name := 'U2';

PL/SQL procedure successfully completed
SQL> SELECT * FROM u1.v;

        ID DATA
---------- ----
         1 bar
         3 bar
         5 bar
         7 bar
         9 bar
痴骨ら 2024-08-06 03:02:04

可能有更优雅的选项,但您可以动态创建同义词或使用动态 SQL / EXECUTE IMMEDIATE。

There may be more elegant options, but you could either create synonyms on the fly or use Dynamic SQL / EXECUTE IMMEDIATE.

吻泪 2024-08-06 03:02:04

另一个可能适合您的选项(取决于您需要执行此操作的应用程序环境)是暂时将命名空间更改为感兴趣的模式:

  1. alter session set current_schema = eg001t3;
  2. 从whateverTableBelongsToEG001T3中选择*; -- 这里不需要模式限定符
  3. alter session set current_schema = ... -- 回到您的连接模式名称

set current_schema 不会以任何方式绕过 Oracle 权限模型 - 您至少仍然需要在其他模式的表上进行 SELECT出于兴趣。

Another option that might work for you (depending on the application environment from which you need to do this) is to temporarily change your namespace to the schema of interest:

  1. alter session set current_schema = eg001t3;
  2. select * from whateverTableBelongsToEG001T3; -- no schema qualifier needed here
  3. alter session set current_schema = ... -- back to your connecting schema name

set current_schema doesn't bypass the Oracle privilege model in any way - you'll still need at least SELECT on the other schema's tables of interest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文