Dynamic SQL using DBMS_SQL

Posted on 2000-03-02
Last Modified: 2010-05-18
If I get to SQL*Plus and type:
I see I have all except R1.  But if I execute the following:

create or replace procedure ds as
  cur integer;
  tmp integer;
  cur := dbms_sql.open_cursor;
  dbms_sql.parse (cur,'set role all except R1',dbms_sql.v7);
  tmp := dbms_sql.execute(cur);

I get no error messages yet the R1 role is still in my privilege domain.
What did I miss ?

Question by:EugeneGardner

Accepted Solution

Ron Warshawsky earned 100 total points
ID: 2577804
All roles are disabled during procedure run.


Expert Comment

by:Ron Warshawsky
ID: 2577813
Some additional info from ORACLE:

When a stored procedure is executed, it is executed under the privileges of the owner of the procedure without enabling any roles. Therefore, a select from the SESSION_ROLES view will return no rows if this query is executed within a procedure. Depending on the goal of the procedure, you may be able to select from DBA_ROLES/USER_ROLES, or possibly use the procedure IS_ROLE_ENABLED within the DBMS_SESSION package.


Expert Comment

ID: 2577889
EugeneGardner :

This is a bug in Oracle 8.0.6 and below and Oracle 8.1.5 and below. I don't think thet have a fix for this out yet.

This procedure should give a ORA-6565 error. The SET ROLE commad is not a valid command from within a procedure.
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Expert Comment

ID: 2577891
What is your Oracle version and platform? Are you sure you are not getting any errors when running your stored procedure? Since roles in stored procedures are ignored, statement SET ROLE (same as DBMS_SESSION.SET_ROLE) can not be used in stored procedures. You had to get ORA-06565: cannot execute SET ROLE from within stored procedure.

SQL> exec ds
begin ds; end;

ERROR at line 1:
ORA-06565: cannot execute SET ROLE from within stored procedure
ORA-06512: at "SYS.DBMS_SYS_SQL", line 787
ORA-06512: at "SYS.DBMS_SQL", line 328
ORA-06512: at "SYSTEM.DS", line 7
ORA-06512: at line 1

Bottom line. You can not set roles from within SP. All stored procedure can do is to check if role is enabled (via DBMS_SESSION.IS_ROLE_ENABLED).

Solomon Yakobson.

Author Comment

ID: 2578132
Thanks for the replies.  I'm running Oracle V7.1 (soon to go to V7.3) on VMS.  I definately do not get
ORA-06565: cannot execute SET ROLE from within stored procedure
or any other message.

The problem I have is that I have roles for each application and the Test Team need to hold <aplication>_USER role and no other to perform a valid QA test.  They can not just switch roles on and off as some applications are accessed via Forms and there is no trap door for them to switch all other roles off.  I guess I'll just have to create <application>_TEST accounts and ask them to use those, but that's not ideal as several testers could be testing the same application at the same time and may tread on each others' toes.


Author Comment

ID: 2600878
Oracle's Note:69483.1 says it all:

This article explains why in Oracle 8.0 you cannot use dbms_session.set_role
from within a procedure to change a user's role.  It is meant for PL/SQL
developers who are attempting to implement a set of procedures to control user privileges.

In Oracle 8.0, you cannot set roles in a stored procedure or trigger
using either the dbms_session.set_role, or by issuing the set role command
via the dbms_sql package.  

The following error is defined for situations such as this:

  ORA-06565 : cannot execute <name> from within stored procedure

With new features of Oracle8i, it is now possible to clarify the reason
for this restriction.  When a procedure is executed, it assumes the schema
identity of the user that created the procedure and the "current" user is
switched to the procedure owner. (This switch takes place even when the owner
and invoking user are the same in order to maintain consistency.)  Therefore,
a 'set role' issued within the procedure sets the role for the procedure owner, not the invoker.  

Hence, if the operation was allowed, it would have no effect since procedures
running under the schema of the owner run with no roles enabled.  When the
procedure exits,  the "current" user is switched back to the invoking user.
Therefore, the set role would be lost anyway.

An anonymous block is executed with roles enabled.  The current user remains as the invoking user - i.e. no switching takes place - so set role is both allowed and remains active after the execution of the block completes.

With Oracle8i, it is possible to specify whether a stored procedure executes with the identity of the procedure owner ("definer") or the identity of the user executing the procedure ("invoker").  If a procedure is run as the definer, you still receive the same error when attempting to set a role.

However, if a procedure is run as the invoker, set role succeeds and the
role is set after the procedure has completed.  When a procedure is run
with "invoker's rights", roles are not disabled and role operations are
permitted the same way they are in anonymous blocks.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
pl/sql - query very slow 26 74
make null the repeated levels 2 31
update statement in oracle 9 29
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question