I'm struggling with a problem in Oracle 12.1 regarding to procedures.
We are using a data isolation approach based on schemas. We use one schema as PUBLIC and each project has exactly the same tables and procedures, but different data.
Problem:
I need to get the schema name where the procedure was called in order to update a row for a table in the PUBLIC schema that holds all the different schemas.
Header definition
CREATE OR REPLACE PACKAGE PKG_DONE
AUTHID DEFINER
AS
PROCEDURE ACK_FROM_WEB(UUID INTERNAL.UUID%TYPE, USERNAME PUBLIC.USERS.USER_NAME%TYPE);
END PKG_DONE;
Body definition
CREATE OR REPLACE PACKAGE BODY PKG_DONE AS
PROCEDURE ACK_FROM_WEB(P_UUID INTERNAL.UUID%TYPE, P_USERNAME PUBLIC.USERS.USER_NAME%TYPE)
AS
BEGIN
INSERT INTO ACK_WEB_USER(UUID,USERNAME) VALUES (P_UUID,P_USERNAME);
UPDATE PUBLIC.ACK_CONFIRMATIONS SET ASSIGNED = P_USERNAME WHERE UUID = P_UUID AND SCHEMA_NAME = USER;
END ACK_FROM_WEB;
END PKG_DONE;
It works perfectly in our website as we set the session with the SCHEMA when operating, but now we need external confirmation, via email and it only works with PUBLIC schema for security reasons. I would like to retrieve the schema where I call the procedure instead of USER in the statement UPDATE PUBLIC.ACK_CONFIRMATIONS SET ASSIGNED = P_USERNAME WHERE UUID = P_UUID AND SCHEMA_NAME = USER;
.
Is there any command/dbms procedure or anything where I can get it?
Best Answer
You can use one of the functions of Oracles
owa_util
's package namedWHO_CALLED_ME
.To demonstrate this feature, we can create a table which will store the results of the call to Oracle's procedure.
As a test procedure which will insert some values we use:
The procedure
WHO_CALLED_ME
will return the schema's name as well as the package name and the line number inside the calling package body, caller-T is the type of database object which called our custom procedureP_CALLED_BY_T
. If the caller itself is just a procedure, the variablev_PackageName
will hold the procedure's name instead of a package name.