Find all objects inside a Package or Stored proc - sql

I'm not sure if this question make sense. But thought of asking this here.
Assume I have procedure like below,
CREATE OR REPLACE
PROCEDURE PROCEDURE1 AS
BEGIN
SELECT COLA INTO COL **Payment**.TAB1 ;
SELECT COLA INTO COL **Checkout**.TAB1 ;
END PROCEDURE1;
In the above code. The proc is there in the schema "Order" as you can see this proc is using the data of Payment and Checkout. So here Payment and checkout are different schema.
My quesiton is,
Is there a way to identify to get the list of ther schema being used inside the procedure or package?
Please help out.

Assume we have this procedure:
create procedure my_proc
is
l_cursor sys_refcursor;
begin
open l_cursor for select * from all_objects;
close l_cursor;
open l_cursor for select * from all_tables;
close l_cursor;
open l_cursor for select * from user_objects;
close l_cursor;
open l_cursor for select * from user_tables;
close l_cursor;
end my_proc;
So both implicit and explicit references can be found like this:
select NAME, referenced_name, referenced_type, referenced_owner from all_dependencies
where name = 'MY_PROC'
NAME REFERENCED_NAME REFERENCED_TYPE REFERENCED_OWNER
-------------------------------------------------------------------------
MY_PROC STANDARD PACKAGE SYS
MY_PROC USER_TABLES SYNONYM PUBLIC
MY_PROC USER_OBJECTS SYNONYM PUBLIC
MY_PROC ALL_TABLES SYNONYM PUBLIC
MY_PROC ALL_OBJECTS SYNONYM PUBLIC
MY_PROC SYS_STUB_FOR_PURITY_ANALYSIS PACKAGE SYS
So REFERENCED_OWNER is the schema that is owner of the object.

Related

How to listed all the tables of a User in a database? n

Hi so i have the following plan :
i want to Write a script, which calls another script. A parameter V_USERNAME should be passed from the 1st script to the 2nd script.( Using the keyword DEFINE)
My code look like :
##C:\Users\pe.k\Documents\script2.sql &p_v_username
set serveroutput on
define p_v_username = "user";
In the 2nd script all tables of the user should be output. (Using the key word EXECUTE IMMEDIATE and a cursor).
The output control is to be done via a parameter in the script or when calling the script.
Example call:
SQL> #start_script1 MML
declare
&p_v_username varchar2(100);
v_result varchar2(100);
cursor cp_username (&p_v_username varchar2)
is
select owner, table_name
from all_tables
where owner = &p_v_username
order by owner, table_name;
begin
dbms_output.put_line('Alle Tabellen der User'); --l_username);
open cp_username(&p_v_username);
--loop
--fetch cp_username into v_result;
-- dbms_output.put_line(v_result);
--end loop;
close cp_username;
end;
/
And i have the errors and i am lost. I dont know how to do it
Your second procedure should be something like this:
BEGIN
DBMS_OUTPUT.PUT_LINE('Alle Tabellen der User'); --l_username);
FOR TABS IN (
SELECT OWNER, TABLE_NAME
FROM ALL_TABLES
WHERE OWNER = '&P_V_USERNAME'
ORDER BY OWNER, TABLE_NAME
) LOOP
DBMS_OUTPUT.PUT_LINE(TABS.TABLE_NAME);
END LOOP;
END;
/
In order to print the output generated by the DBMS_OUTPUT package from PL/SQL, make sure to set serveroutput on

oracle - concatenate in select for drop, create, grant - lost in ''''

I am new to Oracle database, got a task to write several selects, but I do not get somethow principle, how it works (or better to say does not work). Could you please help me to find a mistake and probably you have a link to some info or converter that helps such writings.
v_sql := 'SELECT ''DROP TABLE ''||object_name||'' as
select *
FROM all_objects
WHERE object_name LIKE '''%''|| v_date ||''%'''
and object_type = ''TABLE''
and owner =''||v_owner||''';
Are you trying to generate DROP statements for a specific user? Start with the below anonymous block and add to it.
--Generate DROP statements for all tables for a user.
declare
v_owner varchar2(128) := user;
begin
for objects in
(
select distinct 'DROP TABLE "'||owner||'"."'||object_name||'"' v_sql
from all_objects
where object_type = 'TABLE'
and owner = v_owner
order by v_sql
) loop
dbms_output.put_line(objects.v_sql);
end loop;
end;
/

Execute SQL Stored procedure using Object_ID

Is there any way to accomplish this? For example, let's say the object_id for stored procedure X is 1234. I'm trying to do something like
EXEC ('EXEC Object_Name(1234)')
and it would run stored procedure X. Is there a way to accomplish this?
Thanks.
I guess you work with Oracle db.
To run procedure with known object_id you can use dictionary view all_objects and construct a call.
Here is an example:
declare
iObjectId number := 3927531;
sProcedureName varchar2(128);
sProcedureOwner varchar2(128);
begin
-- get the procedure name
for c in (
select object_name procedure_name,
owner procedure_owner
from all_objects
where object_type = 'PROCEDURE'
and object_id = iObjectId
) loop
sProcedureName := c.procedure_name;
sProcedureOwner := c.procedure_owner;
end loop;
if sProcedureName is not null then
execute immediate 'begin '||sProcedureOwner||'.'||sProcedureName||'; end;';
dbms_output.put_line('Procedure '||sProcedureName||' was executed successfully.');
else
dbms_output.put_line('Procedure was not found for object_id = '||iObjectId||'.');
end if;
end;
Keep in mind that procedure can have required parameters. In this case you can use the dictionary view all_arguments.
Hope that helps.

Create a simple store procedure in oracle 12c

I am looking to create a simple store procedureto return a list of all user names in a table called dba_users.
The select I am using is:
SELECT username FROM dba_users
When I create a PROCEDURE with the following syntax it is created, but fails to execute:
CREATE OR REPLACE PROCEDURE user_list_display
IS
BEGIN
SELECT username FROM dba_users
END;
For this I get
ORA-00900: invalid SQL statement:
EXECUTE user_list_display;
you had better using an implicit cursor with dbms_output.put_line:
SQL> set serveroutput on;
SQL> CREATE OR REPLACE PROCEDURE user_list_display IS
BEGIN
FOR c in ( SELECT username FROM dba_users )
LOOP
dbms_output.put_line(c.username);
END LOOP;
END;
/
SQL> exec user_list_display;
In Oracle12c, You can use DBMS_SQL.RETURN_RESULT.
CREATE OR REPLACE PROCEDURE user_list_display
IS
v_cursor SYS_REFCURSOR;
BEGIN
OPEN v_cursor FOR SELECT username FROM dba_users;
DBMS_SQL.RETURN_RESULT ( v_cursor );
END;
/
Then execute it as
EXEC user_list_display;
In lower versions,from SQL* Plus (or execute as script in SQL Developer ), you may use a REFCURSOR OUT variable and PRINT to display the result.
CREATE OR REPLACE PROCEDURE user_list_display( output OUT SYS_REFCURSOR )
IS
BEGIN
OPEN output FOR SELECT username FROM dba_users;
END;
/
You may run these 3 lines whenever you want to see the output.
VARIABLE output REFCURSOR
EXEC user_list_display(:output)
PRINT output

How to find out which procedures and functions are using a table?

I'm deleting a table (table a), and I want to know which of the functions, procedures and views are using my table (table a).
try this
select * from dba_source where text like '%:tablename%'
You can check in DBA_DEPENDENCIES table using below query:
select * from DBA_DEPENDENCIES where REFERENCED_NAME ='tableA' --YOUR TABLE NAME;
QUICK CHECK:
create table TEST (id number(5), name varchar2(50) );
--Table created
insert into TEST values(1,'mahi');
--1 row created.
commit;
--Commit complete.
create or replace procedure PROC_TEST As
v_name varchar2(50);
BEGIN
select name into v_name from TEST where id=1;
dbms_output.put_line('o/p : ' || ' ' || v_name);
END;
/
--Procedure created.
Exec PROC_TEST();
--o/p : mahi
--PL/SQL procedure successfully completed.
QUERY:
select * from DBA_DEPENDENCIES where REFERENCED_NAME = 'TEST';
i thing bellow query by "davegreen100" will work but need to place upper at both side so that it will work for upper as well as lower case.
select * from dba_source where upper(text) like upper('%:tablename%')