oracle使用存储过程创建用户并授权(版本2):20181104
增加判断用户是否存在
create or replace procedure p_create_uesr(newUser in varchar2,
newPasswd in varchar2,
tbName in varchar2) Authid Current_User as
/*因role在存储过程中不可用,使用Authid Current_User参数可解决*/
v_userYorN varchar(30);
begin
--判断用户是否存在
select username
into v_userYorN
from dba_users
where username = upper(newUser);
--用户已存在
dbms_output.put_line('创建失败!!!用户:' || newUser || '已存在');
exception
when no_data_found then
v_userYorN := '';
--因查询不到有这个用户时会抛出异常,此时用户不存在则创建用户
if v_userYorN is null then
execute immediate 'create user ' || newUser || ' identified by ' ||
newPasswd || ' account unlock default tablespace ' ||
tbName || ' temporary tablespace temp';
--授权connect,resource
execute immediate 'grant connect,resource to ' || newUser;
dbms_output.put_line('创建成功。新建的用户为:' || newUser);
end if;
end;