Oracle:Authid Current_User使用
由于用户拥有的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选项,表创建成功。