试验:
--sys用户
drop user test1 ;
drop user test2 ;
create user test1 identified by test;
grant connect to test1;
grant create table to test1;
create user test2 identified by test;
grant connect to test2;
grant create table to test2;
conn test1/test
create table departments(deptno number, dname varchar2(10),mgr number,loc number);
create or replace procedure create_dept(v_deptno NUMBER,
v_name VARCHAR2,
v_mgr NUMBER,
v_loc NUMBER
) is
begin
INSERT INTO departments
VALUES (v_deptno,v_name,v_mgr,v_loc);
end create_dept;
以上test1用户无法创建
--sys执行如下操作
grant create any procedure to test1;
以上存储过程test1用户可以创建,但无法执行如下:
declare
begin
create_dept(1,'a',1,1);
end;
--sys执行如下操作
alter user test1 quota 10M on USERS;
则上面的可以执行
--test2用户执行
declare
begin
test1.create_dept(2,'a',2,2);
end;
无法执行以上
grant execute any procedure to test2;
则test2用户可以执行,数据插入到test1.departments中。
事实上,存储过程包括2种,DR(Definer's Rights 定义者权限 ) Procedure和IR(Invoker's Rights 调用者权限) Procedure。
前文默认为定义者权限存储过程。
create or replace procedure create_dept(v_deptno NUMBER,
v_name VARCHAR2,
v_mgr NUMBER,
v_loc NUMBER
) is
……
完全等价于
create or replace procedure create_dept(v_deptno NUMBER,
v_name VARCHAR2,
v_mgr NUMBER,
v_loc NUMBER
) AUTHID DEFINER is
如果通过test1用户将存储过程修改为:
create or replace procedure create_dept(v_deptno NUMBER,
v_name VARCHAR2,
v_mgr NUMBER,
v_loc NUMBER
) AUTHID CURRENT_USER is
begin
INSERT INTO departments
VALUES (v_deptno,v_name,v_mgr,v_loc);
end create_dept;
则此时,存储过程变成了调用者模式后,test1用户可以执行此存储过程,而test2无法执行(错误提醒:表和视图不存在)。
--sys用户
alter user test2 quota 10M on USERS;
--test2用户
create table departments(deptno number, dname varchar2(10),mgr number,loc number);
此后test2用户再执行
declare
begin
test1.create_dept(2,'a',2,2);
end;
结果提醒可以执行成功,数据插入到test2.departments中。
再次通过test1对存储过程进行修改为定义者权限:
create or replace procedure create_dept(v_deptno NUMBER,
v_name VARCHAR2,
v_mgr NUMBER,
v_loc NUMBER
) AUTHID DEFINER is
begin
INSERT INTO departments
VALUES (v_deptno,v_name,v_mgr,v_loc);
end create_dept;
再次通过test2执行如下:
declare
begin
test1.create_dept(3,'c',3,3);
end;
数据插入到test1.departments中。