DBMS_LOCK throwing error in procedure ie identifier must be declared - oracle

When I execute "dbms_lock.sleep", it runs successfully. However, when I put the it in a procedure then it throws error like 'identifier 'DBMS_LOCK must be declared''
exec dbms_lock.sleep ( 5 ); -- this is working
create procedure p1
...
dbms_lock.sleep ( 5 ); -- Error(264,2): PLS-00201: identifier 'DBMS_LOCK' must be declared
...
end;
/
The below is from dba_objects:
SYS DBMS_LOCK PACKAGE BODY
SYS DBMS_LOCK PACKAGE
PUBLIC DBMS_LOCK SYNONYM
SYS DBMS_OUTPUT PACKAGE
PUBLIC DBMS_OUTPUT SYNONYM
SYS DBMS_OUTPUT PACKAGE BODY

Privileges obtained through a role are not effective inside a PL/SQL block. The execute privilege on the package needs to be granted to the user directly, e.g.:
grant execute on dbms_lock to your_oracle_user;

Related

Oracle's dbms_metadata.get_ddl for type DIRECTORY: invalid input value for parameter SCHEMA

When I try to call dbms_metadata.get_ddl('TABLE', 'MYTABLE', 'MYSCHEMA') either in the pl/sql block or in the package procedure it works fine.
When I try to call dbms_metadata.get_ddl('TABLE', 'MYTABLE') (without schema explicitely provided) either in the pl/sql block or in the package procedure it works fine also.
When I try to call dbms_metadata.get_ddl('DIRECTORY', 'MYDIR') (without schema explicitely provided) in the pl/sql block it works fine also.
But,
When I try to call dbms_metadata.get_ddl('DIRECTORY', 'MYDIR', 'MYSCHEMA') either in the pl/sql block or in the package procedure it raises the error:
ORA-31600: invalid input value MYSCHEMA for parameter SCHEMA in function GET_DDL
When I try to call dbms_metadata.get_ddl('DIRECTORY', 'MYDIR') (without schema explicitely provided) in the package procedure it raises the error:
ORA-31603: object "MYDIR" of type DIRECTORY not found in schema "MYSCHEMA"
What is the problem?
EXECUTE_CATALOG_ROLE=true
SELECT_CATALOG_ROLE=true
'CREATE ANY DIRECTORY'=true
PL/SQL Release 12.2.0.1.0 - Production
You need to add the AUTHID CURRENT_USER clause (Docs)
create or replace procedure dir_ddl (dir_name in varchar2) AUTHID CURRENT_USER is
x clob;
begin
SELECT DBMS_METADATA.get_ddl ('DIRECTORY', dir_name) into x from dual;
dbms_output.put_line(x);
end dir_ddl;
/
set serveroutput on
exec dir_ddl('PLSHPROF_DIR')
And my output is...
Procedure DIR_DDL compiled
CREATE OR REPLACE DIRECTORY "PLSHPROF_DIR" AS '/home/oracle/profiler'
PL/SQL procedure successfully completed.
If I remove the AUTHID clause, I see the same error as you report.

Oracle kill sessions procedure

I want to create procedure which will kill all session. After I run the statement i get error:
[Warning] ORA-24344: success with compilation error 10/13 PL/SQL:
ORA-00942: table or view does not exist 6/6 PL/SQL: SQL Statement
ignored 15/31 PLS-00364: loop index variable 'V_KILL' use is invalid
15/9 PL/SQL: Statement ignored (1: 0): Warning: compiled but with
compilation errors
CREATE OR REPLACE PROCEDURE KILL_ORACLE_SESSIONS
IS
BEGIN
FOR v_kill IN
(SELECT
'alter system kill session '''
||sid||','||serial#||',#1'|| ''' immediate;' as statement
FROM
v$session
WHERE
sql_id='sql_id_here'
)
LOOP
dbms_output.put_line (v_kill.statement);
END LOOP;
END;
/
Where is the catch ?
Thanks
Most likely you don't have permissions to select view v$session because your user received this privilege by a ROLE. Privileges inside a PL/SQL block must be granted directly to the user (i.e. GRANT SELECT ON V$SESSION TO {username};). A role (for example DBA ROLE) does not apply inside PL/SQL.

Accessing a function against another owner in oracle?

I am having a function called fn_export and its owner is bhist. I am calling this function from ohist user using bhist.fn_export. While calling like this I am facing the below issue.
ORA-00942: table or view does not exist
ORA-06512: at "bhist.fn_export", line 442
ORA-06512: at line 20
I tried to verify all the tables in that function and I am able to access all those tables from ohist. I have execute grant on bhist.fn_export to ohist. Still I am having this issue. Can any one of you please help in resolving this issue?
Thanks,
Venkat
You need to grant EXECUTE privilege on this function to ohist user.
A syntax is:
GRANT EXECUTE ON function_name TO username;
You can connect as bhist user and grant the privilege using:
GRANT EXECUTE ON fn_export TO ohist;
You can also connect as SYS or SYSTEM, and use this command:
GRANT EXECUTE ON bhist.fn_export TO ohist;
See a below simple example (one user is named TEST and the other is named DEV):
SQL> connect test
Enter password:
Connected.
SQL> CREATE FUNCTION fn_export RETURN number AS
2 BEGIN
3 RETURN 20;
4 END;
5 /
Function created.
SQL> connect dev
Enter password:
Connected.
SQL> SELECT test.fn_export FROM dual;
SELECT test.fn_export FROM dual
*
ERROR at line 1:
ORA-00904: "TEST"."FN_EXPORT": invalid identifier
SQL> connect test
Enter password:
Connected.
SQL> grant execute on fn_export to dev;
Grant succeeded.
SQL> connect dev
Enter password:
Connected.
SQL> SELECT test.fn_export FROM dual;
FN_EXPORT
----------
20
SQL>

Insufficient Priviledges error when trying to execute the procedure from package

Step 1 : I have created one package with procedures to create context and set value to the context.
create or replace PACKAGE Context_check AS
PROCEDURE set_context_vpd_proc (V_ISID in varchar2);
procedure set_context (v_isid_a in varchar2);
END Context_check;
create or replace PACKAGE BODY Context_check AS
PROCEDURE set_context_vpd_proc (V_ISID in varchar2)
AS
v_STAT VARCHAR2(200);
v_chk varchar2(2000);
BEGIN
DBMS_SESSION.SET_CONTEXT('VPD_CTX', 'ISID', V_ISID );
--v_STAT := '';
EXCEPTION
WHEN NO_DATA_FOUND THEN NULL;
END;
procedure set_context (v_isid_a in varchar2)
as
begin
EXECUTE IMMEDIATE 'CREATE OR REPLACE CONTEXT VPD_CTX using set_context_vpd_proc';
set_context_vpd_proc (v_isid_a);
EXCEPTION
WHEN NO_DATA_FOUND THEN NULL;
end set_context;
END Context_check;
Step 2: When I am trying to executing the procedure I am getting an error
EXECUTE Context_check.set_context('Ana');
Error starting at line 43 in command:
EXECUTE Context_check.set_context('Ana')
Error report:
ORA-01031: insufficient privileges
ORA-06512: at "SYS.DBMS_SESSION", line 114
ORA-06512: at "SEC_ADMIN.CONTEXT_CHECK", line 8
ORA-06512: at "SEC_ADMIN.CONTEXT_CHECK", line 20
ORA-06512: at line 1
01031. 00000 - "insufficient privileges"
*Cause: An attempt was made to change the current username or password
without the appropriate privilege. This error also occurs if
attempting to install a database without the necessary operating
system privileges.
When Trusted Oracle is configure in DBMS MAC, this error may occur
if the user was granted the necessary privilege at a higher label
than the current login.
*Action: Ask the database administrator to perform the operation or grant
the required privileges.
For Trusted Oracle users getting this error although granted the
the appropriate privilege at a higher label, ask the database
administrator to regrant the privilege at the appropriate label.
I have already given all the grants on that package.Still I am not able to execute this procedure.
Note : If I create the same procedures as stand alone ,its working fine and setting the context.
You need to create a context using a package, not using a procedure inside of a package.
Instead of
EXECUTE IMMEDIATE 'CREATE OR REPLACE CONTEXT VPD_CTX using set_context_vpd_proc';
Write
EXECUTE IMMEDIATE 'CREATE OR REPLACE CONTEXT VPD_CTX using Context_check';

Oracle privilege missing for DBMS_SCHEDULER, ORA-27486 after GRANT CREATE JOB, CREATE EXTERNAL JOB

What additional privilege am I missing?
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL>
SQL> create user myUser identified by password default tablespace theData temporary tablespace temp;
User created.
SQL> grant connect, resource to myUser;
Grant succeeded.
SQL> GRANT READ,WRITE ON DIRECTORY tmp TO myUser;
Grant succeeded.
SQL> GRANT CREATE JOB TO myUser;
Grant succeeded.
SQL> GRANT CREATE EXTERNAL JOB TO myUser;
Grant succeeded.
SQL> connect myUser/password
Connected.
SQL>
SQL>
1 CREATE PROCEDURE shellScript
2 AS
3 /*-----------------------*/
4 v_sql UTL_FILE.FILE_TYPE;
5 v_shell UTL_FILE.FILE_TYPE;
6 /*=======================*/
7 BEGIN
8 /*=======================*/
9 -- write the sql script to /tmp/myUser-tmp-script.sql
10 v_sql:= UTL_FILE.FOPEN('TMP','myUser-tmp-script.sql','w');
11 UTL_FILE.PUT_LINE(v_sql,'select to_char(sysdate,''YYYYMMDDHR24MISS'') from dual'||';', FALSE);
12 UTL_FILE.FFLUSH(v_sql);
13 UTL_FILE.FCLOSE(v_sql);
14 -- write the shell script to /tmp/myUser-tmp-script.sh
15 v_shell:= UTL_FILE.FOPEN('TMP','myUser-tmp-script.sh','w');
16 UTL_FILE.PUT_LINE(v_shell,'#!/bin/bash', FALSE);
17 UTL_FILE.PUT_LINE(v_shell,'sqlplus myUser/password#sbox #/tmp/myUser-tmp-script.sql > /tmp/myUser-tmp-script.err', FALSE);
18 UTL_FILE.FFLUSH(v_shell);
19 UTL_FILE.FCLOSE(v_shell);
20 -- execute the shell script which executes the sql script
21 DBMS_SCHEDULER.PURGE_LOG(JOB_NAME=>'myJob');
22 DBMS_SCHEDULER.CREATE_JOB(JOB_NAME=>'myJob', JOB_TYPE=>'EXECUTABLE', JOB_ACTION=>'/bin/bash', NUMBER_OF_ARGUMENTS=>1, START_DATE=>SYSTIMESTAMP, ENABLED=>FALSE);
23 DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE('myJob', 1, '/tmp/myUser-tmp-script.sh');
24 DBMS_SCHEDULER.ENABLE('myJob');
25 USER_LOCK.SLEEP(500); -- give it 5 seconds to complete
26 -- clean up
27 UTL_FILE.FREMOVE('TMP', 'myUser-tmp-script.sh');
28 UTL_FILE.FREMOVE('TMP', 'myUser-tmp-script.sql');
29 /*=======================*/
30 END shellScript;
/
Procedure created.
SQL> SHOW ERRORS PROCEDURE shellScript
No errors.
SQL>
SQL>
SQL> execute shellScript;
BEGIN shellScript; END;
*
ERROR at line 1:
ORA-27486: insufficient privileges
ORA-06512: at "SYS.DBMS_ISCHED", line 411
ORA-06512: at "SYS.DBMS_ISCHED", line 452
ORA-06512: at "SYS.DBMS_SCHEDULER", line 1082
ORA-06512: at "MYUSER.SHELLSCRIPT", line 21
ORA-06512: at line 1
SQL>
According to TFM, PURGE_LOG requires the MANAGE SCHEDULER privilege:
GRANT MANAGE SCHEDULER TO xxx;
Wow, I found the problem... "myJob" was an existing package object in the database. I'm guessing my "insufficient privileges" were to replace the package object with a job object.
if you get the manage scheduler privilege, the next thing where this will fail is the none existing execute bits on the shell script. If the execute bits are in place, it will fail because it lacks the environment settings like PATH and ORACLE_HOME, needed to run SQL*Plus.
Besides that, why stick to 10g?
Oracle 11g has much better options to run external jobs, security implemented by credentials instead of some file in $ORACLE_HOME that defines the user to run the job.
There is some very nice reading available on this subject, see my profile.
I hope this helps,
Ronald.

Resources