Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing another user's table within an Oracle Stored Procedure

I'm writing a stored procedure to copy data from one user's table to another schema. Basically, it is a series of INSERT .. SELECT statements such as this:

INSERT INTO GESCHAEFTE
  SELECT *
    FROM TURAT03.GESCHAEFTE
   WHERE kong_nr = 1234;

This works fine when issueing from sqlplus (or TOAD for me ;-)) so I know that I have sufficient privileges, but when this is part of stored procedure like this:

CREATE OR REPLACE FUNCTION COPY_KONG
    (pKongNr IN NUMBER)
    RETURN NUMBER
    AUTHID CURRENT_USER
IS
BEGIN
   INSERT INTO GESCHAEFTE
      SELECT *
       FROM TURAT03.GESCHAEFTE
       WHERE kong_nr = pKongNr;
END;

I get an Oracle error:

[Error] ORA-00942 (11: 22): PL/SQL: ORA-00942: table or view does not exist

As you can see, I've already inserted an AUTHID, but to no avail.

What else can I do? I'm pretty much at the end of my ideas here.

like image 889
Thorsten Avatar asked Dec 09 '10 14:12

Thorsten


People also ask

What is execute privilege in Oracle?

A user with the EXECUTE object privilege for a package can execute any public procedure or function in the package and access or modify the value of any public package variable. Specific EXECUTE privileges cannot be granted for a package's constructs.

How do you check if a user has access to a schema in Oracle?

select * from dba_role_privs where grantee = 'SCHEMA_NAME'; All the role granted to the schema will be listed. Thanks Szilagyi Donat for the answer.

Can I call a stored procedure from another in Oracle?

CALL test_sp_1(); An anonymous PL/SQL block is PL/SQL that is not inside a named procedure, function, trigger, etc. It can be used to call your procedure.


1 Answers

The owner of a procedure must be granted privilege to access the underlying objects directly, not through a role. To have the same level of access as your procedures, use the following commands:

SET ROLE NONE;

To access another table from a procedure, you need to be granted SELECT directly, not through a role:

GRANT SELECT ON TURAT03.GESCHAEFTE TO <your_user>;

This article by Tom Kyte contains additional info.

like image 183
Vincent Malgrat Avatar answered Oct 08 '22 14:10

Vincent Malgrat