存储过程执行权限

最后更新时间: 2014年4月13日,星期日


存储过程分为两种,即DR(Definer's Rights ) Procedure和IR(Invoker's Rights ) Procedure。为什么会有两种存储过程呢?比如说用户user02创建了修改表t1的存储过程,当用户user01调用时,是修改的user01自己的t1表还是user02的t1表?  

示例:
用户user02将存过赋执行权限给user01:
grant execute on user02.prc1 to user01;
grant execute on user02.prc2 to user01;
grant execute on user02.prc3 to user01;


用户user01执行user02下的存过 prc1,prc2,prc3;
用户user01只有执行user02下存过的权限,没有查询/修改user02.t1的任何权限;
--默认是 authid DEFINER
create or replace procedure prc1 is
v_sql varchar2(200);
begin 
begin 
    v_sql :='drop table t1 purge';
    EXECUTE IMMEDIATE v_sql;
    v_sql:='create table t1(id int)';
    EXECUTE IMMEDIATE v_sql;
exception when others then
    dbms_output.put_line('表不存在!');
    v_sql:='create table t1(id int)';
    EXECUTE IMMEDIATE v_sql;
end;
for i in 1 .. 10
loop
insert into t1 values(0);
end loop;
commit;
end;
/
-- DEFINER 表示使用的是定义者的权限,即user02的权限,修改的是 user02.t1表,尽管user01没有直接修改user02.t1表的任何权限
create or replace procedure prc2 authid DEFINER is
v_sql varchar2(200);
begin 
  begin 
    v_sql :='drop table t1 purge';
    EXECUTE IMMEDIATE v_sql;
    v_sql:='create table t1(id int)';
    EXECUTE IMMEDIATE v_sql;
    exception when others then
    dbms_output.put_line('表不存在!');
    v_sql:='create table t1(id int)';
    EXECUTE IMMEDIATE v_sql;
  end;
  for i in 1 .. 10
  loop
    insert into t1 values(2);
  end loop;
  commit;
end;
/

-- current_user 表示调用者权限,修改的是user01.t1表
create or replace procedure prc3 authid current_user is
v_sql varchar2(200);
begin 
  begin 
    v_sql :='drop table t1 purge';
    EXECUTE IMMEDIATE v_sql;
    v_sql:='create table t1(id int)';
    EXECUTE IMMEDIATE v_sql;
    exception when others then
    dbms_output.put_line('表不存在!');
    v_sql:='create table t1(id int)';
    EXECUTE IMMEDIATE v_sql;
  end;
  for i in 1 .. 10
  loop
    insert into t1 values(3);
    
  end loop;
  commit;
end;
/


参考: 



1. 匿名块(DECLARE BEGIN END;)总是IR Procedure,触发器和视图总是DR Procedure。我们可以通过视图*_PROCEDURES来查看存储过程的AUTHID属性值。
2. 存储过程、包、都适用。




原文地址:https://www.cnblogs.com/bowshy/p/3661867.html