如何授予对表的触发器访问权限

时间:2019-01-18 15:50:32

标签: oracle plsql triggers grant

我收到有关成功创建的触发器的错误消息。

我尝试过...

DOM

这是触发器的代码...

DOM

user_errors中的错误消息说:“ PL / SQL:ORA-00942:表或视图不存在”

Screen shot

您知道我如何获取触发访问OTHER_TABLE的触发器吗?

我了解无法向触发器授予访问权限/授权。但是我不清楚我需要运行什么代码才能使触发器起作用。

预先感谢, 乔什

2 个答案:

答案 0 :(得分:1)

如果MYTABLEOTHER_TABLE属于同一用户,则不需要特权:

SQL> show user
USER is "SCOTT"
SQL> create table other_table (other_Table_id number);

Table created.

SQL> create table mytable (mytable_id number);

Table created.

SQL> create or replace trigger prevent_invalid_id
  2    before insert or update on mytable
  3    for each row
  4  declare
  5    row_count number;
  6  begin
  7    select count(*) into row_count
  8    from other_table where other_table_id = :new.mytable_id;
  9
 10    if row_count = 0 then
 11       raise_application_error(-20101, 'The ID provided is invalid');
 12    end if;
 13  end;
 14  /

Trigger created.

SQL>

但是,如果他们属于不同的用户,则OTHER_TABLE的所有者必须向 me 授予SELECT特权。但是,这还不够-您必须在OTHER_TABLE之前加上其所有者名称(例如other_user.other_table),或者在我自己的架构中创建一个指向其他用户OTHER_TABLE的同义词。例如:

SQL> drop table other_table;

Table dropped.

SQL> connect mike/lion@xe
Connected.
SQL> create table other_table (other_Table_id number);

Table created.

SQL> grant select on other_table to scott;

Grant succeeded.

SQL> connect scott/tiger@xe
Connected.
SQL> create or replace trigger prevent_invalid_id
  2    before insert or update on mytable
  3    for each row
  4  declare
  5    row_count number;
  6  begin
  7    select count(*) into row_count
  8    from MIKE.other_table where other_table_id = :new.mytable_id;
  9
 10    if row_count = 0 then
 11       raise_application_error(-20101, 'The ID provided is invalid');
 12    end if;
 13  end;
 14  /

Trigger created.

SQL>

注释第8行:MIKE.other_table

仅显示如果您删除/删除所有者的姓名会发生什么情况

SQL> l8
  8*   from MIKE.other_table where other_table_id = :new.mytable_id;
SQL> c/mike.//
  8*   from other_table where other_table_id = :new.mytable_id;
SQL> l
  1  create or replace trigger prevent_invalid_id
  2    before insert or update on mytable
  3    for each row
  4  declare
  5    row_count number;
  6  begin
  7    select count(*) into row_count
  8    from other_table where other_table_id = :new.mytable_id;
  9
 10    if row_count = 0 then
 11       raise_application_error(-20101, 'The ID provided is invalid');
 12    end if;
 13* end;
SQL> /

Warning: Trigger created with compilation errors.

SQL> show err
Errors for TRIGGER PREVENT_INVALID_ID:

LINE/COL ERROR
-------- -----------------------------------------------------------------
4/3      PL/SQL: SQL Statement ignored
5/8      PL/SQL: ORA-00942: table or view does not exist
SQL>

看到了吗? ORA-00942。

答案 1 :(得分:0)

弄清楚了:将OTHER_TABLE授予dbo

让我失望的是,我在寻找有关如何执行此操作的指示时使用“表所有者”一词。我以为我是餐桌老板。而是需要特权的架构(dbo)。

相关问题