Home » SQL & PL/SQL » SQL & PL/SQL » Dynamic SQL - Insufficient privileges (Oracle - 11gR2 - windows 7)
icon4.gif  Dynamic SQL - Insufficient privileges [message #559966] Sun, 08 July 2012 06:57 Go to next message
nb.mopidevi@gmail.com
Messages: 4
Registered: January 2012
Junior Member
Hello Guys,
I am new to Dynamic SQL..
I create a procedure to get any DDL done against sample HR schema as follows.
it goes well!
Now when i try to test my procedure with some DDL command passing to the procedure i've created..
strange! oracle throws an error as in the /*ERROR!!!!*/ block..

I don't understand why i am facing such an error..
Any idea what goes wrong??
Thanks in advance Smile

/* Product an Version on my machine */
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

/* Get DDL done ! */
CREATE OR REPLACE PROCEDURE exec_ddl(ddl_string IN VARCHAR2)
IS
BEGIN
EXECUTE IMMEDIATE ddl_string;
END;
/

/* anonymous to test */
BEGIN
exec_ddl('CREATE TABLE sam_tab (sam_num NUMBER(4))');
END;
/

/* ERROR !!!! */
ERROR at line 1:
ORA-01031: insufficient privileges
ORA-06512: at "HR.EXEC_DDL", line 4
ORA-06512: at line 2

[EDITED by LF: fixed topic title typos]

[Updated on: Sun, 08 July 2012 09:16] by Moderator

Report message to a moderator

Re: Dyanamic SQL - Insufficient priviliges [message #559967 is a reply to message #559966] Sun, 08 July 2012 07:02 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
You don't have privileges to create tables.
Re: Dyanamic SQL - Insufficient priviliges [message #559970 is a reply to message #559967] Sun, 08 July 2012 07:09 Go to previous messageGo to next message
John Watson
Messages: 8981
Registered: January 2010
Location: Global Village
Senior Member
Welcome to the forum. Please read our OraFAQ Forum Guide and How to use [code] tags and make your code easier to read

Your problem will be that you have not been granted the CREATE TABLE privilege directly. You may have it through a role, but roles cannot be used in pl/sql. Like this:
orcl>
orcl> CREATE OR REPLACE PROCEDURE exec_ddl(ddl_string IN VARCHAR2)
  2  IS
  3  BEGIN
  4  EXECUTE IMMEDIATE ddl_string;
  5  END;
  6  /

Procedure created.

orcl> BEGIN
  2  exec_ddl('CREATE TABLE sam_tab (sam_num NUMBER(4))');
  3  END;
  4  /
BEGIN
*
ERROR at line 1:
ORA-01031: insufficient privileges
ORA-06512: at "JON.EXEC_DDL", line 4
ORA-06512: at line 2


orcl> grant create table to jon;

Grant succeeded.

orcl> BEGIN
  2  exec_ddl('CREATE TABLE sam_tab (sam_num NUMBER(4))');
  3  END;
  4  /

PL/SQL procedure successfully completed.

orcl>

Re: Dyanamic SQL - Insufficient priviliges [message #559972 is a reply to message #559967] Sun, 08 July 2012 07:21 Go to previous messageGo to next message
nb.mopidevi@gmail.com
Messages: 4
Registered: January 2012
Junior Member
I am thankful for your time sir,

my mistake I dint mentioned it before.
I am going smooth to create a table from sql * plus against the same schema as follows,

/* from the Prompt */

HR@orcl11gR2 > CREATE TABLE sam_tab (sam_num NUMBER(3));

Table created.

HR@orcl11gR2 >


Re: Dyanamic SQL - Insufficient priviliges [message #559973 is a reply to message #559970] Sun, 08 July 2012 07:27 Go to previous messageGo to next message
nb.mopidevi@gmail.com
Messages: 4
Registered: January 2012
Junior Member
Thank you John Watson for your valuable time,
Its correct,
IT's clear from query,

SYS@orcl11gR2 > select * from dba_sys_privs where grantee = 'HR';

GRANTEE PRIVILEGE ADM
------------------------------ ---------------------------------------- ---
HR CREATE VIEW NO
HR UNLIMITED TABLESPACE NO
HR CREATE DATABASE LINK NO
HR CREATE SEQUENCE NO
HR CREATE SESSION NO
HR ALTER SESSION NO
HR CREATE SYNONYM NO

Re: Dyanamic SQL - Insufficient priviliges [message #559976 is a reply to message #559973] Sun, 08 July 2012 08:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
John Watson wrote on Sun, 08 July 2012 14:09
Welcome to the forum. Please read our OraFAQ Forum Guide and How to use [code] tags and make your code easier to read

Your problem will be that you have not been granted the CREATE TABLE privilege directly. You may have it through a role, but roles cannot be used in pl/sql. Like this:...


[Updated on: Sun, 08 July 2012 08:02]

Report message to a moderator

Re: Dyanamic SQL - Insufficient priviliges [message #559980 is a reply to message #559976] Sun, 08 July 2012 10:20 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I am new to Dynamic SQL..
Dynamic SQL is over used & often abused; such as being attempted by you.

>I create a procedure to get any DDL done against sample HR schema as follows.
PL/SQL code is written & designed to be reused.
Writing DDL into PL/SQL devolves the code into run once procedure.
DDL should only be written as static SQL & run one time during application upgrade & maintenance.
Re: Dyanamic SQL - Insufficient priviliges [message #559990 is a reply to message #559980] Sun, 08 July 2012 22:18 Go to previous message
nb.mopidevi@gmail.com
Messages: 4
Registered: January 2012
Junior Member
Thank You BlackSwan..Your words are helpfull Smile
Previous Topic: retrieve data by checking any part of the string
Next Topic: Im beginner in PL/Sql
Goto Forum:
  


Current Time: Wed Aug 13 04:50:11 CDT 2025