Oracle - AUTHID CURRENT_USER 的过程抛出 ORA-00942:table 或视图不存在

Oracle - procedure with AUTHID CURRENT_USER throws ORA-00942: table or view does not exist

您知道为什么 AUTHID 为 CURRENT_USER 的过程抛出 ORA-00942: table 或视图不存在 即使在向调用者授予所有必需的权限之后.

我在 user1 下创建了一个过程 proc1 以插入到 logs table.日志 table 的所有者是 user1。

然后授予权限给user2

将 proc1 上的执行权限授予用户 2; 将日志插入授予用户 2;

当我从 user2 执行 proc1 时,它抛出 ORA-00942:table 或视图不存在

请问有人知道原因吗?

那是因为当前用户(即user2)没有“看到”table。您确实授予了权限,但您应该创建一个 (public) 同义词,或者 - 在创建过程时 - 指定 table 的所有者。

这是你现在拥有的:我的“user1”是 scott,而“user2”是 mike:

SQL> show user
USER is "SCOTT"
SQL> create table logs (id number, datum date);

Table created.

SQL> create sequence seq_log;

Sequence created.

SQL> create or replace procedure proc1
  2    authid current_user
  3  as
  4  begin
  5    insert into logs (id, datum)
  6      values (seq_log.nextval, sysdate);
  7  end;
  8  /

Procedure created.

SQL> grant execute on proc1 to mike;

Grant succeeded.

SQL> grant select on seq_log to mike;

Grant succeeded.

SQL> grant select, insert on logs to mike;

Grant succeeded.

SQL> exec proc1;

PL/SQL procedure successfully completed.

SQL> select * From logs order by id;

        ID DATUM
---------- --------
         1 02.01.22         --> scott successfully inserted a row (because he's the owner)

SQL> connect mike/lion
Connected.
SQL> exec proc1;
BEGIN proc1; END;             --> failed, because MIKE doesn't see SCOTT's procedure

      *
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'PROC1' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored


SQL> exec scott.proc1;           --> failed, because MIKE doesn't see SCOTT's table
BEGIN scott.proc1; END;

*
ERROR at line 1:
ORA-00942: table or view does not exist
ORA-06512: at "SCOTT.PROC1", line 5
ORA-06512: at line 1


SQL>

如果SCOTT修改程序(引用对象时使用所有者的名字):

SQL> connect scott/tiger
Connected.
SQL> create or replace procedure proc1
  2    authid current_user
  3  as
  4  begin
  5    insert into scott.logs (id, datum)           --> SCOTT here ...
  6      values (scott.seq_log.nextval, sysdate);   --> ... and here
  7  end;
  8  /

Procedure created.

SQL> connect mike/lion
Connected.
SQL> exec scott.proc1;        --> now MIKE successfully inserted a row

PL/SQL procedure successfully completed.

SQL> select * from scott.logs order by id;

        ID DATUM
---------- --------
         1 02.01.22
         2 02.01.22

SQL>

问题是:你为什么用AUTHID CURRENT_USER?使用 DEFINER(默认设置),您不会遇到此类问题。

SQL> connect scott/tiger
Connected.
SQL> create or replace procedure proc1
  2    authid definer                          --> DEFINER
  3  as
  4  begin
  5    insert into logs (id, datum)            --> no SCOTT here ...
  6      values (seq_log.nextval, sysdate);    --> ... nor here
  7  end;
  8  /

Procedure created.

SQL> connect mike/lion
Connected.
SQL> exec scott.proc1             --> no problems any more

PL/SQL procedure successfully completed.

SQL> select * From scott.logs order by id;

        ID DATUM
---------- --------
         1 02.01.22
         2 02.01.22
         3 02.01.22

SQL>