Oracle存储过程(包)中的执行没有权限?Authid Current_User使用

文章讲述了在Oracle中,用户在存储过程中默认无法使用其角色权限。通过在存储过程中添加`AuthidCurrent_User`选项,可以使得存储过程能利用当前用户的角色权限,避免单独授予每个权限的繁琐。示例中,通过修改存储过程,成功创建了emp2表,即使用户拥有DBA角色但未直接授予创建表的权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Oracle存储过程(包)中的执行没有权限?Authid Current_User使用

1.概述
由于用户拥有的role权限在存储过程是不可用的。遇到这种情况,我们一般需要显示授权,如grant create table to user;但这种方法太麻烦,有时候可能需要进行非常多的授权才能执行存储过程,实际上,oracle给我们提供了在存储过程中使用role权限的方法:修改存储过程,加入Authid Current_User时存储过程可以使用role权限。

下面以例子说明:

----执行存储过程p_create_emp2报下面的错误:
SQL> exec p_create_emp2;
 
begin p_create_emp2; end;
 
ORA-01031: 权限不足
ORA-06512:"ECHO.P_CREATE_EMP2", line 12
ORA-06512: 在 line 2

----查看p_create_emp2,该存储过程是要创建emp2表
create or replace procedure P_create_emp2 as
  cursor t_cursor is
  select * from user_tables where table_name = 'EMP2';
  t_cur t_cursor%rowtype;
  num int:=0;
begin
  for t_cur in t_cursor loop
   num:=num+1;
  end loop;
  if num=1 then
    execute immediate 'drop table emp2 purge';
    execute immediate 'create table emp2 as select * from emp';
  else
    execute immediate 'create table emp2 as select * from emp';
  end if;
end P_create_emp2;

----查看echo用户的角色或权限
SQL> select * from dba_role_privs where grantee='ECHO';
 
GRANTEE                        GRANTED_ROLE                   ADMIN_OPTION DEFAULT_ROLE
------------------------------ ------------------------------ ------------ ------------
ECHO                           DBA                            NO           YES
----有dba的角色,说明建表的权限

----修改存储过程p_create_emp2:
create or replace procedure P_create_emp2 authid current_user as
  cursor t_cursor is
  select * from user_tables where table_name = 'EMP2';
  t_cur t_cursor%rowtype;
  num int:=0;
begin
  for t_cur in t_cursor loop
   num:=num+1;
  end loop;
  if num=1 then
    execute immediate 'drop table emp2 purge';
    execute immediate 'create table emp2 as select * from emp';
  else
    execute immediate 'create table emp2 as select * from emp';
  end if;
end P_create_emp2;

----再次执行:
SQL> exec p_create_emp2;
 
PL/SQL procedure successfully completed
 
SQL> desc emp2;
Name     Type         Nullable Default Comments 
-------- ------------ -------- ------- -------- 
EMPNO    NUMBER(4)    Y                         
ENAME    VARCHAR2(10) Y                         
JOB      VARCHAR2(9)  Y                         
MGR      NUMBER(4)    Y                         
HIREDATE DATE         Y                         
SAL      NUMBER(7,2)  Y                         
COMM     NUMBER(7,2)  Y                         
DEPTNO   NUMBER(2)    Y                

----在存储过程加了Authid Current_User选项,表创建成功。;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值