Skip to Main Content
  • Questions
  • ORA-1031 in a stored procedure doing dynamic sql.

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Aleksey.

Asked: June 18, 2001 - 1:55 pm UTC

Last updated: November 23, 2009 - 12:33 pm UTC

Version: 8.0.5

Viewed 1000+ times

You Asked

Access administration problem:
I created a package
..
Package Body PKG_AXT_FILES
Is
…..
Procedure create_file_info_table is
Begin
begin
dbms_utility.exec_ddl_statement ('truncate table server_file_info');
exception
when others then
dbms_utility.exec_ddl_statement
('create table server_file_info (file_name varchar2(50),time_stamp date)');
end;
Exception
When OTHERS then
dbms_output.put_line(sqlerrm);
End; -- Procedure
…
.. and ran it..

10:52:03 HUMQA>exec pkg_axt_files.create_file_info_table;
ORA-01031: insufficient privileges
ORA-00942: table or view does not exist

PL/SQL procedure successfully completed.

10:54:00 HUMQA>

Then - in the same session:

10:54:00 HUMQA>exec dbms_utility.exec_ddl_statement('create table server_file_info (file_name varchar2(50),time_stamp date)');

PL/SQL procedure successfully completed.

11:00:16 HUMQA>
…………..
11:01:50 HUMQA>exec dbms_utility.exec_ddl_statement('truncate table server_file_info');

PL/SQL procedure successfully completed.

11:02:25 HUMQA>

What privileges do I need to make my package work?

Thanks.
With sincere admiration, Alex Titiyevskiy


and Tom said...

see
</code> http://asktom.oracle.com/Misc/RolesAndProcedures.html <code>

I cannot tell which line is causing the error, if it is the create table, you need to be granted the CREATE TABLE priv directly, not via a role.



Rating

  (5 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

RESOURCE role

Jim B, June 20, 2001 - 12:32 pm UTC

Good reply, even tho this subject get beaten to death almost every day.
Many times a user can create a table in sqlplus becausethey have been granted the RESOURCE role. As the link identifies, privs thru a role cannot be used in PL/SQL

Even select access..??

A reader, May 31, 2003 - 1:15 am UTC

Hi Tom,
I am using dynamic sql to access a view form some other users schema, i am unable to do so unless i have select access on the table,
for eg user bis owner of table a & view a1

user A has select access on view A1 of user B schema, 'not' given via role.

Dynamic sql in a procedure invoked by users A gives insufficient privilige.unless user a is given select access on table a

Is this expected behavaviour or something wrong.


cheers

Tom Kyte
May 31, 2003 - 11:49 am UTC

expected, documented behavior

</code> http://asktom.oracle.com/Misc/RolesAndProcedures.html <code>

A reader, June 01, 2003 - 10:48 pm UTC

Hi Tom,
I did read that, but as mentioned i hv given the select access directly to the user and not via a role,
why does the user need a select access on the table too when accessing a view as dynamic sql in a procedure,
dont u think it beats one of the very basic purposes of view.

cheers


Tom Kyte
June 02, 2003 - 7:21 am UTC

Here is my test case that shows if A had been granted select on B.V by B and V is a view, A can dynamically access it.  Where is yours that shows otherwise?

(try 

SQL> set role none

and see if that view is still accessible, if it isn't - its a role)

ops$tkyte@ORA817DEV> select * from dba_sys_privs where grantee='PUBLIC';

no rows selected

ops$tkyte@ORA817DEV>
ops$tkyte@ORA817DEV> drop user a cascade;

User dropped.

ops$tkyte@ORA817DEV> drop user b cascade;

User dropped.

ops$tkyte@ORA817DEV>
ops$tkyte@ORA817DEV> create user a identified by a;

User created.

ops$tkyte@ORA817DEV> create user b identified by b;

User created.

ops$tkyte@ORA817DEV>
ops$tkyte@ORA817DEV> grant create session, create procedure to a;

Grant succeeded.

ops$tkyte@ORA817DEV> grant create session, create table, create view to b;

Grant succeeded.

ops$tkyte@ORA817DEV>
ops$tkyte@ORA817DEV> alter user b default tablespace users quota unlimited on users;

User altered.

ops$tkyte@ORA817DEV>
ops$tkyte@ORA817DEV> @connect b/b
ops$tkyte@ORA817DEV> set termout off
b@ORA817DEV> REM GET afiedt.buf NOLIST
b@ORA817DEV> set termout on
b@ORA817DEV>
b@ORA817DEV> create table t ( x int );

Table created.

b@ORA817DEV> insert into t values ( 1 );

1 row created.

b@ORA817DEV> create view v as select * from t;

View created.

b@ORA817DEV>
b@ORA817DEV> grant select on v to a;

Grant succeeded.

b@ORA817DEV>
b@ORA817DEV> @connect a/a
b@ORA817DEV> set termout off
a@ORA817DEV> REM GET afiedt.buf NOLIST
a@ORA817DEV> set termout on
a@ORA817DEV>
a@ORA817DEV> create procedure p
  2  as
  3          type rc is ref cursor;
  4          l_cursor rc;
  5          l_x      number;
  6  begin
  7          open l_cursor for 'select * from b.v';
  8          loop
  9                  fetch l_cursor into l_x;
 10                  exit when l_cursor%notfound;
 11                  dbms_output.put_line( l_x );
 12          end loop;
 13          close l_cursor;
 14  end;
 15  /

Procedure created.

a@ORA817DEV>
a@ORA817DEV> exec p
1

PL/SQL procedure successfully completed.

 

Lost link

Galen Boyer, September 18, 2009 - 2:56 pm UTC

Hi Tom,

Was trying to send a link to a co-worker and found it no longer existed, http://asktom.oracle.com/~tkyte/Misc/RolesAndProcedures.html
Tom Kyte
September 28, 2009 - 8:14 am UTC

I need to upload my "public html" stuff into my files and recreate that stuff. I'll do that this week.

Reference link is not working

David, November 16, 2009 - 6:35 pm UTC

The link is not working:
http://asktom.oracle.com/~tkyte/Misc/RolesAndProcedures.html

Assume it provides details on why privs granted through a role on a table are not good enough for PL/SQL procs.
Tom Kyte
November 23, 2009 - 12:33 pm UTC

see home page for a link to where those articles moved.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library