Solved

Dynamic SQL using DBMS_SQL

Posted on 2000-03-02
6
2,242 Views
Last Modified: 2010-05-18
If I get to SQL*Plus and type:
SET ROLE ALL EXCEPT R1;
SELECT * FROM SESSION_ROLES;
I see I have all except R1.  But if I execute the following:

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

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

0
Comment
Question by:EugeneGardner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Accepted Solution

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

 rwarsh
0
 
LVL 3

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.

Regards
  rwarsh
0
 
LVL 6

Expert Comment

by:mshaikh
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:syakobson
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.
0
 
LVL 1

Author Comment

by:EugeneGardner
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.

Eugene.
0
 
LVL 1

Author Comment

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

Overview
--------
 
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.
 

Information
-----------
 
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.

0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses

617 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