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

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%')

Related

TEMP table in stored procedure oracle 11g

Is there any way to create a temp table with the stored procedure and use the same in the ref cursor in the same stored procedure.
I wrote something like below, it's not working....
CREATE OR REPLACE PROCEDURE USP_TEST(
CUR_QUOTE OUT SYS_REFCURSOR) AS
BEGIN
CREATE GLOBAL TEMPORARY TABLE users1 ON COMMIT PRESERVE ROWS
AS
SELECT 'rb#bot.com' FROM DUAL;
OPEN CUR_QUOTE FOR
SELECT DISTINCT CREATEDBY
FROM QUOTE
WHERE TRUNC(DATEOFENQUIRY)=TRUNC(SYSDATE-1) AND CREATEDBY = users1.EMAIL;
END;
And delete the temp table at the end.
Please suggest with some sample code...
Keep coding :)
If you drop the table then the cursor is invalidated.
From 18c you can use private temporary tables:
create or replace procedure usp_test
( cur_quote out sys_refcursor )
as
begin
execute immediate
'create private temporary table ora$ptt_demo' ||chr(10)||
'on commit drop definition as' ||chr(10)||
'select sysdate -1 as dateofenquiry, ''rb#bot.com'' as createdby' ||chr(10)||
'from dual';
open cur_quote for
'select distinct createdby from ora$ptt_demo where trunc(dateofenquiry) = trunc(sysdate - 1)';
end;
Note that the table name must have the prefix defined by the PRIVATE_TEMP_TABLE_PREFIX parameter (default ORA$PTT_), and you must commit before calling the procedure a second time.

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;
/

ORACLE SQL: Looping over table

I have a table that contains a list of table names.
I would like to search each of these tables one by one to see if they contain a particular element (the primary key, specified at the start of the script).
I would like to return a list of all of the tables that this element is present in (ideally distinct).
I'm fairly new to this PL/SQL "not just a query" stuff. so i apologise in advance for the attrocious attempt you are about to see, but hopefully it illustrates what i'm going for:
PROCEDURE CHECK_FOR_ELEMENTS
BEGIN
DECLARE
ELEMENT_KEY varchar(5):=X78ehryfk;
RNUM_MAX int :=167;
----create output table for script
create or replace table ALL_TABLES CONTAINING_&ELEMENT_KEY
(ELEMENT_KEY VARCHAR(255),
TABLE_NAME varchar(255))
/
commit;
---begin loop over rnum;
FOR rnum_counter in 1..&RNUM_MAX
LOOP
--define this statement as variable TABLE_NAME_VAR
select table_name from (select * from (select table_name, rownum as rnum
from all_tables
where owner = 'RMS'
and table_name like 'ABC%'
and table_name not like '%STG'
and table_name not like '%BKP'
and num_rows>0
order by num_rows desc)
where rnum = rnum_counter
)INTO TABLE_NAME_VAR
;
----run below to collect row, if it exists, from table being searched
SQL_STMT:='INSERT INTO ALL_TABLES CONTAINING_&ELEMENT_KEY
SELECT distinct key,'||TABLE_NAME_VAR||' as UMF from
'||TABLE_NAME_VAR||
' where key like 'ELEMENT_KEY-%'
execute immediate SQL_STMT;
commit;
---insert row into table created for output
END LOOP
---loop over all tables
END;
The main error message i get is that TABLE_NAME_VAR is not a valid table name within the dynamic SQL statement. I've googled a bit and i now understand you can't use variables to input table names in this way.
Any help is greatly appreciated!
Thankyou!
Here, I tried to clean it up for you. Let me know if you still get errors.
create or replace PROCEDURE CHECK_FOR_ELEMENTS is
ELEMENT_KEY varchar2(14):='X78ehryfk';
RNUM_MAX int :=167;
TABLE_NAME_VAR varchar2(30);
SQL_STMT varchar2(4000);
BEGIN
----create output table for script
begin
execute immediate 'drop table ALL_TABLES_WITH_' || element_key;
exception when others then null;
end;
execute immediate 'create table ALL_TABLES_WITH_' || element_key || '
(ELEMENT_KEY VARCHAR2(255), -- does this need to be 255 characters?
TABLE_NAME varchar2(30))';
--- implicit cursor loop
FOR rnum_row in (select table_name, rownum as rnum
from all_tables
where owner = 'RMS'
and table_name like 'ABC%'
and table_name not like '%STG'
and table_name not like '%BKP'
and num_rows>0
order by num_rows desc)
LOOP
if rnum_row.rnum > RNUM_MAX
then exit;
end if;
TABLE_NAME_VAR := rnum_row.table_name;
----run below to collect row, if it exists, from table being searched
SQL_STMT:='INSERT INTO ALL_TABLES_WITH_' || element_key || '
(ELEMENT_KEY, TABLE_NAME)
SELECT distinct key, :1 as UMF from
'||TABLE_NAME_VAR||
' where key like :2';
execute immediate SQL_STMT using TABLE_NAME_VAR, element_key || '-%';
---insert row into table created for output
END LOOP;
commit;
---loop over all tables
END CHECK_FOR_ELEMENTS;
/

Copy tables form schema in another schema in Oracle

I want to write a procedure or a cursor.
Input data -- NAME OWNER.
We know name (OWNER) AND from table ALL_OBJECTS
Take the name of the table.
Tables > 30.
How to write correctly ?
CREATE OR REPLACE PROCEDURE USER_NAME
(
v_USER VARCHAR2
)
AS
v_sysdate VARCHAR2(10) := to_char(SYSDATE ,'MMDDYYYY');
v_table_name VARCHAR2(50);
BEGIN
SELECT
TABLE_NAME INTO v_table_name
FROM
ALL_OBJECTS F -- Table with two columnsю. OWNER AND NAME TABLES
WHERE
F.OWNER = v_USER;
--Name of tables and owner ALL_OBJECTS
EXECUTE IMMEDIATE 'CREATE TABLE USER_BANCU.'||v_USER||'_'||v_table_name||'__'||v_sysdate||to_char(sysdate,'HH24_MI_SS')||' AS
SELECT *
FROM '||v_USER||'.'||v_table_nam;
COMMIT;
END;
/
Try DBMS_METADATA_GET_DDL.
enter link description here

Find all objects inside a Package or Stored proc

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.