Home » SQL & PL/SQL » SQL & PL/SQL » PLSQL grant script not working correctly (12c)
PLSQL grant script not working correctly [message #653319] Fri, 01 July 2016 12:35 Go to next message
ICB555
Messages: 2
Registered: July 2016
Junior Member
I'm getting table or view doesn't exist on line 20 of this script (The grant script) and I can't figure out what's going on with it. Fairly new at this.

--part 1, disables constraints
DECLARE
TYPE cur_typ IS REF CURSOR;
c cur_typ;
select_cmd varchar2(2000);
grant_cmd varchar2(2000);
i_table_name varchar2(1000);
i_user_name varchar2(1000);
BEGIN
dbms_output.enable();
select_cmd := ' select du.username, dt.table_name
from dba_tables dt, dba_users du
where du.common=''NO''
';
open c for select_cmd;
dbms_output.put_line(select_cmd);
loop
fetch c into i_table_name, i_user_name;
exit when c%NOTFOUND;
grant_cmd := 'grant select on ' || i_user_name || '.' || i_table_name || ' to alm_ro';
execute immediate grant_cmd;
--dbms_output.put_line(grant_cmd);
end loop;
close c;
end;
/
Re: PLSQL grant script not working correctly [message #653320 is a reply to message #653319] Fri, 01 July 2016 12:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Welcome to the forum.
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

Compare:
select du.username, dt.table_name
and
fetch c into i_table_name, i_user_name;

See the error? Wink

Re: PLSQL grant script not working correctly [message #653321 is a reply to message #653320] Fri, 01 July 2016 13:24 Go to previous messageGo to next message
John Watson
Messages: 8930
Registered: January 2010
Location: Global Village
Senior Member
You mean A.B is not equal to B.A ???? I suppose that proves that the dot operator is not transitive Smile

@ICB555, usually you want your dbms_output line above the EXECUTE IMMEDIATE, that way you see the problem statement before the procedure fails trying to run it.
Re: PLSQL grant script not working correctly [message #653322 is a reply to message #653320] Fri, 01 July 2016 13:25 Go to previous messageGo to next message
ICB555
Messages: 2
Registered: July 2016
Junior Member
Thank you. I figured out that I had to insert the line "AND du.username=dt.owner.
Re: PLSQL grant script not working correctly [message #653323 is a reply to message #653322] Fri, 01 July 2016 13:30 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Sure (also).

Re: PLSQL grant script not working correctly [message #653363 is a reply to message #653323] Mon, 04 July 2016 02:56 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You don't need a dynamic select for this, can simply be:
BEGIN

  dbms_output.enable();

  FOR rec IN (SELECT 'grant select on ' || du.username || '.' || dt.table_name || ' to alm_ro' as grant_cmd
              FROM dba_tables dt
              JOIN dba_users du ON du.username=dt.owner
              WHERE du.common = 'NO') LOOP 

    dbms_output.put_line(rec.grant_cmd);
    execute immediate grant_cmd;

  END LOOP;

END;
/
Previous Topic: First business day of a given month
Next Topic: Updating Status W to A
Goto Forum:
  


Current Time: Tue Apr 23 02:27:44 CDT 2024