Home » SQL & PL/SQL » SQL & PL/SQL » "No data found" stops the loop action inside PL/SQL block (Oracle 11.2.0.1.0 on OEL 5.7 (VM))
"No data found" stops the loop action inside PL/SQL block [message #588994] |
Mon, 01 July 2013 04:20  |
Andrey_R
Messages: 441 Registered: January 2012 Location: Israel
|
Senior Member |
|
|
Hi all,
I am trying to create an anonymous PL/SQL block to output privilege information for each of the users listed in DBA_USERS
In a loop. This is my block so far (not finished):
declare
v_usr varchar2(30);
v_out_header varchar2(100);
v_output varchar2(100);
v_qry varchar2(150);
--
cursor uc is select username from dba_users order by 1;
begin
v_out_header := '***User-Role Privilege report***'||chr(10)||'-----------------------------------';
dbms_output.put_line(v_out_header);
open uc;
loop
fetch uc into v_usr;
exit when uc%notfound;
select 'username: '|| username||' , '||'profile: '||profile into v_output
from dba_users where username=v_usr;
dbms_output.put_line(v_output);
dbms_output.put_line('SYSTEM privileges granted directly to the user(not through ROLE) : '
--||chr(10)
);
v_output := '';
v_qry := 'select grantee ||'',''|| privilege from DBA_SYS_PRIVS where grantee not in
(select role from dba_roles) and grantee ='||'''' ||v_usr||'''' ;
execute immediate v_qry into v_output;
--
dbms_output.put_line(v_output);
dbms_output.put_line('------------------------------'||chr(10));
end loop;
close uc;
exception
when no_data_found
then dbms_output.put_line('no_data_found');
end;
The output is as follows:
***User-Role Privilege report***
-----------------------------------
username: ANDREY , profile: DEFAULT
SYSTEM privileges granted directly to the user(not through ROLE) :
no_data_found
A problem I am encountering is that for some users I have no direct privileges that are not granted through roles,
And when I have the expression v_qry (which is basically "'select grantee ||'',''|| privilege from DBA_SYS_PRIVS where grantee not in
(select role from dba_roles) and grantee ='||'''' ||v_usr||''''") not initialized with values because the select statement retrieved 0 results,
I have the process interfered by the no_data_found error/exception.
Questions:
Any ideas on how I can preferrably simply, avoid/overcome my problem?
Some way to make the loop go on in spite of no data found? maybe something similar to NVL?
Thanks in advance.
Regards,
Andrey
Edit: added brackets to the topic ("No data found"....)
[Updated on: Mon, 01 July 2013 04:23] Report message to a moderator
|
|
|
|
|
|
|
Re: No data found stops the loop action inside PL/SQL block [message #589014 is a reply to message #589009] |
Mon, 01 July 2013 07:28   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Andrey_R wrote on Mon, 01 July 2013 13:23
How can I use a for loop and avoid dynamic SQL for displaying the multiple privileges for each user?
Take the query you have - remove execute immediate and extraneous quotes. There's nothing remotely dynamic about that query.
Andrey_R wrote on Mon, 01 July 2013 13:23
What do I use as the range for the "for" loop?
Range? You realise that you can use for loops with select statments? If not I suggest a read of the documentation.
FOR rec IN (select grantee ||','|| privilege as output
from DBA_SYS_PRIVS
where grantee not in (select role from dba_roles)
and grantee = v_usr) LOOP
It really is that simple.
[EDIT: removed some quotes]
[Updated on: Mon, 01 July 2013 07:29] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
Re: No data found stops the loop action inside PL/SQL block [message #589119 is a reply to message #589109] |
Tue, 02 July 2013 07:44  |
Andrey_R
Messages: 441 Registered: January 2012 Location: Israel
|
Senior Member |
|
|
cookiemonster wrote on Tue, 02 July 2013 13:53You can shorten your code quite a bit - the cursor loop at the start should be a for loop.
The subsequent select does not need to exist - the cursor can select profile.
You don't need any of the variables.
You don't need the exception handler.
You don't need to wrap each for loop in a begin end - it's pointless unless you're going to add an exception handler.
Thanks for the tip!
This is my code so far:
--
begin
dbms_output.put_line( ' -----------------------------------'||chr(10)||'/****User-Role Privilege report****/'||chr(10)||'-----------------------------------'||chr(10));
for item_uname in (select username,chr(10)||'username: '|| username||' , '||'profile: '||profile user_prof_info from dba_users where username not in ('SYS','SYSTEM') order by 1)
loop --outer loop start
dbms_output.put_line(item_uname.user_prof_info);
--
--
--direct system privs
dbms_output.put_line('SYSTEM privileges granted directly to the user(not through ROLE) : '||chr(10)||'-------------------------------------------------------------------');
--
for item_drct_sys_priv in (select privilege direct_sys_priv from DBA_SYS_PRIVS where grantee =item_uname.username)
loop --inner loop start1
--
--
dbms_output.put_line(item_drct_sys_priv.direct_sys_priv);
end loop;--inner loop end
--
--role system privs
dbms_output.put_line(chr(10)||'SYSTEM privileges granted through ROLES : '||chr(10)||'-------------------------------------------------------------------');
--
for item_role_sys_priv in (select 'Role: "'||grantee||'", Privilege: ' ||privilege role_sys_priv from DBA_SYS_PRIVS where grantee in
(select granted_role from DBA_ROLE_PRIVS where grantee = item_uname.username) order by role_sys_priv)
loop --inner loop start2
dbms_output.put_line(item_role_sys_priv.role_sys_priv);
end loop;--inner loop end2
dbms_output.put_line('----------------------------**END OF INFO**-------------------------------'||chr(10));
end loop;--outer loop end
--
end;
Regards,
Andrey
[Updated on: Tue, 02 July 2013 07:44] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Wed Jun 04 17:23:12 CDT 2025
|