List all procedures along with the tables and columns used in that procedure in Oracle - oracle

Last night got a call from my team lead and he asked me to make the list of all the procedures along with the tables and columns used in Oracle.
I got a query to list all the procedures along with tables and dblink but couldn't get column names (along with DML if possible) used in that Procedure :
select DISTINCT OWNER, NAME, referenced_name, referenced_link_name, referenced_type
from dba_dependencies
where OWNER = 'OWNER_NAME';
My required output is as follows:
Owner_Name
Procedure_Name
Referenced_name
Referenced_link
Referenced_type
Column_Name,
dml_type(select/insert/update).
Please help if possible..

columns will be in DBA_TAB_COLUMNS.
as for the SQL statements, this is a bit more difficult, but doable if you have Diagnostics pack licensed; here's the outline (works in 11g or above):
DBA_HIST_ACTIVE_SESS_HISTORY.top_level_sql_id is the SQL id of the calling procedure,
so listing DBA_HIST_ACTIVE_SESS_HISTORY.sql_id for that top_level_sql_id is "all" the SQL from the execution of the procedure.
DBA_HIST_SQL_TEXT can be queried to get the sql text for the sql_id.
If you don't have the Diagnostics Pack, you'd have to instrument v$session for the top_level_sql_id's and sql_id's and the look in v$sql for the text.

Related

How to know if a db-link is used somewhere in a DB

I would like to know if it exists an Oracle command to know if a DB-LINK (name: myBDLink) is used somewhere in a DB and how to display the objects (views,materialized views, procedures, functions, ...) which use it.
Could you please help me with that ?
Thanks for your help
Well, you could try to query various system views and see whether any of them contains a string you're looking for. As you want to check the whole database, you'll probably connect as a privileged user and check dba_ views; otherwise, all_ or user_ would do.
For example, to check procedures, functions, packages, ...:
select owner, name, type, line
from dba_source
where owner not in ('SYS', 'SYSTEM')
and lower(text) like '%mydblink%';
To check views, you'll need a function which will search through its LONG datatype column (as you can't use it directly in SQL):
create or replace function f_long(par_view in varchar2, par_String in varchar2)
return varchar2
is
l_text varchar2(32000);
begin
select text
into l_text
from dba_views
where owner not in ('SYS', 'SYSTEM')
and view_name = par_view;
return case when instr(lower(l_text), lower(par_string)) > 0 then 1
else 0
end;
end;
/
and then
select owner, view_name
from dba_views
where f_long(view_name, 'mydblink') = 1;
I excluded SYS and SYSTEM as they should not contain anything of users' stuff. Perhaps you'd want to exclude some more users.
To see some more (re)sources, query the dictionary, e.g.
select table_name, comments
from dictionary;
TABLE_NAME COMMENTS
------------------------------ --------------------------------------------------
USER_CONS_COLUMNS Information about accessible columns in constraint
definitions
ALL_CONS_COLUMNS Information about accessible columns in constraint
definitions
DBA_CONS_COLUMNS Information about accessible columns in constraint
definitions
<snip>
There is no complete answer. How would the database know of code that is outside of the database? It can't. So if you have a sql script, or some application that does not rely on stored procedures to do everything, the database will not know of them.
That said, for dependencies that are in stored procedures in the database, you can try this:
select *
from dba_dependencies
where referenced_link_name is not null
;
To add to the other (correct) answers that have been posted by #Littlefoot and #EdStevens, a quick-and-dirty analysis can also be made against the Automated Workload Repository (AWR).
The benefit of this approach is it will find usages of remote objects from SQL submitted to the database whether that SQL is in DBA_SOURCE or not (e.g., is embedded in an external application).
-- Find any objects referenced across a database link (sort of)
select object_node, object_name, count(distinct sql_id) sql_id_count
from dba_hist_sql_plan
where object_type = 'REMOTE'
group by object_node, object_name
order by object_node, object_name
;
The problem is that AWR data isn't 100% complete. First of all, it's not kept around forever, so a database link last used more than a month (or two months or however long your DBAs keep AWR data for) wouldn't be seen. Second of all, AWR only takes snapshots periodically, say every hour. So it's theoretically possible for a SQL to use a database link and then get aged out of the library cache before the next AWR snapshot.
I think the chance of missing something due to that last bit is small on the systems I work with. But, if you have poorly written applications (i.e., no bind variables) and limited shared pool space, it's something to worry about.

how to get select statement query which was used to create table in oracle

I created a table in oracle like
CREATE TABLE suppliers AS (SELECT * FROM companies WHERE id > 1000);
I would like to know the complete select statement which was used to create this table.
I have already tried get_ddl but it is not giving the select statement. Can you please let me know how to get the select statement?
If you're lucky one of these statements will show the DDL used to generate the table:
select *
from gv$sql
where lower(sql_fulltext) like '%create table suppliers%';
select *
from dba_hist_sqltext
where lower(sql_text) like '%create table%';
I used the word lucky because GV$SQL will usually only have results for a few hours or days, until the data is purged from the shared pool. DBA_HIST_SQLTEXT will only help if you have AWR enabled, the statement was run in the last X days that AWR is configured to hold data (the default is 8), the statement was run after the last snapshot collection (by default it happens every hour), and the statement ran long enough for AWR to think it's worth saving.
And for each table Oracle does not always store the full SQL. For security reasons, DDL statements are often truncated in the data dictionary. Don't be surprised if the text suddenly cuts off after the first N characters.
And depending on how the SQL is called the case and space may be different. Use lower and lots of wildcards to increase the chance of finding the statement.
TRY THIS:
select distinct table_name
from
all_tab_columns where column_name in
(
select column_name from
all_tab_columns
where table_name ='SUPPLIERS'
)
you can find table which created from table

Oracle - Log the executed query

I have to debug stored procedure which contains a few SQL queries. One of them contains an error. So, I need to execute this SQL query with parameters in other window. I found next query that would help me:
select v.SQL_TEXT
from v$sql v
Unfortunately, this field restricted by 1Kb. In my case I have quite big SQL query and Oracle truncates it. How to log the executed query? I use PL/SQL Developer 10 and Oracle 9i
Unfortunately, this field restricted by 1Kb
If you need the full SQL, then use the SQL_FULLTEXT which is a CLOB datatype instead of SQL_TEXT whcih is limited to first 1000 characters.
From documentation,
Column Datatype Description
------ -------------- ---------------------------------------
SQL_TEXT VARCHAR2(1000) First thousand characters of the SQL
text for the current cursor
SQL_FULLTEXT CLOB Full text for the SQL statement exposed
as a CLOB column. The full text of a SQL
statement can be retrieved using this
column instead of joining with the
V$SQL_TEXT dynamic performance view.
So, use:
SELECT SQL_FULLTEXT FROM v$sql;
By the way, seems like you are actually looking for tracing your session to get the complete details of the procedure and the SQL statements involved. I would suggest to trace the session with level 4 i.e. with the addition of bind variable values.
See How to generate trace file – SQL Trace and TKPROF in Oracle

Find stored proc's dependent elements in Oracle

I have add a parameter to an existing oracle stored proc. Is there a simple way to find other stored proc that would have to be modified?
I'm using Oracle SQL Developper.
You can query DBA_DEPENDENCIES (or ALL_DEPENDENCIES or USER_DEPENDENCIES) directly to get that information
SELECT owner,
name,
type
FROM all_dependencies
WHERE referenced_owner = <<owner of procedure>>
AND referenced_name = <<name of procedure>>
In SQL Developer, you can also pull up the procedure and click the "References" tab to get a list of the objects that reference the procedure.

Finding Oracle stored procedures

Since I am new to Oracle, please tell me what different ways to find packages, stored procedures, triggers, functions, indexes, tablespaces
Thanks
The following statement gives you an overview of all database objects in the current user:
SELECT
object_name,
object_type
FROM
user_objects;
If you are searching for documentation, you can look at Morgan's Library
You can download Oracle SQL Developer free. This allows you to explore all the objects in your database via a simple interface.
I shall walk you through the different shades of "all_object":
SQL> show user
USER is "C##SCOTT"
SQL>
Getting all the types of object:
select distinct object_type from all_objects;
EDITION
CONSUMER GROUP
SEQUENCE
SCHEDULE
PROCEDURE
OPERATOR
DESTINATION
WINDOW
SCHEDULER GROUP
PACKAGE
PROGRAM
LOB
XML SCHEMA
JAVA RESOURCE
JOB CLASS
DIRECTORY
TABLE
SYNONYM
INDEX
VIEW
FUNCTION
INDEXTYPE
JAVA CLASS
TYPE
EVALUATION CONTEXT
25 rows selected.
Now, you can zoom down to the "TABLE" type of object:
select object_name||','||object_id||','||owner from all_objects where object_type='TABLE' ;
Or all the objects of an owner:
select object_name||','||object_id||','||OBJECT_TYPE from all_objects where owner = 'SYS';
Trust me, you will learn more this way - anyone can click through any GUI tool, but to issue the SQL command, you will need some knowledge.

Resources