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

How to grant trigger access to a table

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

我试过了...

GRANT SELECT ON OTHER_TABLE TO [me];
call SYS.DBA_ASSIST.GRANT_OBJ_PERMS('dbo.MYTABLE','SELECT','dbo.OTHER_TABLE');`

这是触发器的代码...

  CREATE OR REPLACE TRIGGER PREVENT_INVALID_ID
  BEFORE INSERT OR UPDATE ON dbo.MYTABLE
  FOR EACH ROW
  DECLARE ROW_COUNT NUMBER;
  BEGIN
  SELECT COUNT(*) INTO ROW_COUNT 
         FROM [OTHER_TABLE] WHERE OTHER_TABLE_ID = :new.MYTABLE_ID;
  IF ROW_COUNT = 0 THEN
    RAISE_APPLICATION_ERROR(-20101, 'The ID provided is invalid.');
    END IF;
  END;`

user_errors 中的错误消息说,"PL/SQL: ORA-00942: table or view does not exist"

知道如何让触发器访问 OTHER_TABLE 吗?

我了解无法将 access/authorizations 授予触发器。但我不清楚我需要 运行 什么代码才能让触发器工作。

提前致谢, 乔什

如果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 的所有者必须将 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

只是为了展示如果您 omit/remove 所有者的姓名:

会发生什么
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.

弄明白了:GRANT SELECT ON OTHER_TABLE TO dbo

让我失望的是当我在寻找有关如何执行此操作的说明时使用 'table owner' 一词。我以为我是 table 的所有者。相反,需要权限的是架构 (dbo)。