How to find all tables updated by a particular plsql method - oracle

Is there any way to find all which tables are (potentially) updated by a particular PLSQL method, other than by code inspection?

No, not really.
ALL_DEPENDENCIES lists the tables statically referenced by a PL/SQL package but that will include both tables read and tebles updated, and it's for the entire package, not just a single procedure. Tables updated via dynamic SQL are not listed. Also, a procedure can call procedures outside the package, and they can in turn update additional tables.

Maybe query the all_source table:
select * from all_source
where name = 'procedure_name'
and upper(text) like upper('%update TABLE_NAME%');
While not exactly what you want, it will show what procedures, functions or packages contain the table_name right after an UPDATE (assuming they are on the same line).

Related

Table and Proc dependency

I am new to schema and not sure how the table is being populated (How the data is being inserted into the table ). How can we find out ?
This should work.
select *
from dba_source
where upper(text) like '%TABLE_NAME%'
But as I do not have DBA rights , can not execute this command. What is the other way to find this out ?
To see dependencies between objects you have access to you can query the all_dependencies data dictionary view. In this case:
select * from all_dependencies where referenced_name = 'YOUR_TABLE_NAME';
If the objects are in your own schema when you can use the user_dependencies view. If you want to see objects you don't have privileges against then you can use dba_dependencies, but it sounds like you are unlikely to have the privileges required to query that view, since you can't see dba_source.
Of course, that will only identify references within your stored PL/SQL code; it won't tell you about any external application code that is performing inserts directly against the database (as opposed to via CRUD procedures) or manual inserts.
And it will only tell you which objects have dependencies, you'll still need to dig through the object source code, either by querying all_source (or user_source if you're the owner) for the relevant type and name. I would avoid the possibility of false-positives from, say, comments that happen to mention the table name in code which doesn't access it. You could also do that outside the database - hopefully your code is under source control (right!?).
If you know the query you need to run but do not have the necessary privileges then perhaps you can write the query using USER_ or ALL_ views to validate the syntax then change the view to DBA_ and ask the DBA to run the query for you.

How can I create a table with same attributes of a View another system delivers me?

An external system granted to my system a view named V_EXT so that I can make selections on it, reading all its content:
SELECT *
FROM V_EXT;
this view has a lot of fields and I would like to create an empty table in my system with the exactly same attributes of this view (same names and same types). Is there a way to do this without simply guessing from the content I received what each attribute is?
I am using Oracle SQL Developer.
With Code.
create table objects_copy2
as
select *
from all_objects
where 1=2; -- add this line if you want NO data, otherwise you get all the data too
With SQL Developer specifically, it's actually harder. You would need to find the underlying OBJECT(s) used in the query. Then look up those data types, and manually build out your CREATE TABLE statement.
CREATE TABLE AS SELECT is the way to go. (Docs)
Note there are some limitations, for example this won't pick up Identity Column definitions from the source table used in the view.
An example:

Generate DDL for Oracle Stored Procedure Dependency Graph

With TOAD I know I can view the dependency (uses) graph of a stored procedure using the schema browser. And, the Oracle utility procedure deptree_fill can do something similar. What I want to do is script out all of the stored procedures, functions and table definition DLLs into a file that I can use to recreate those objects in another database. Is there a tool or an existing script for this purpose? My own searching has not found a solution. In my particular case the stored procedure uses a dozen other procedures, a few functions and twenty tables.
Edit 1
Maybe my original question was not clear. What I am looking for is something that will take the stored procedure I am interested in and script it and all of its dependency graph into one or more files.
The schema I am dealing with has hundreds of objects in it and the dependency graph has ~50 objects in it. So I'd rather not dig through large lists in TOAD or write an Oracle script myself if I can avoid it.
All sources can be extracted using the dbms_metadata package.
To get the source of a table:
select dbms_metadata.get_ddl('TABLE', 'SOME_TABLE')
from dual;
To get the source of a stored procedure:
select dbms_metadata.get_ddl('PROCEDURE', 'SOME_PROC')
from dual;
Using that you can create a SQL script that extracts everything and then spool the result to a file.
More details about the various functions in dbms_metadata can be found in the manual:
http://docs.oracle.com/cd/E11882_01/appdev.112/e25788/d_metada.htm#i1015856
Hmm, it is quite easy to find in google.
Get table DDL: How to get Oracle create table statement in SQL*Plus
Code of stored procedures can be found in table USER_SOURCE.
Also, for exporting schema to another DB you can use oracle utilities: http://docs.oracle.com/cd/B28359_01/server.111/b28319/exp_imp.htm#g1070082
In Toad see the Generate Schema Script window. You can get to it from the Database|Export menu. There are many options there to include/exclude what you want.

Find references to specific column in Oracle in Jobs and Procedures

I'm looking for a query allowing me to query all the tables, views, JOBS, and PROCEDURES in the oracle database. I've found some links to queries that will work for the tables and views but I need jobs and procedures. If one query can't be used for all this, I need at least job and procedures.
Here is what I found for the tables and views:
Select TABLE_NAME, COLUMN_NAME from user_tab_columns
TIA
My guess is that you want something like
SELECT name, type, line, text
FROM user_source
WHERE lower(text) like lower('%<<column name>>%');
That will show you any line of code in any pL/SQL object (package, package body, procedure, function, trigger, type, etc.) that contains the column name. If there are multiple tables with identically named columns (i.e. a column name is found in many different tables), all instances will be identified. There isn't a really great way, short of inspecting the code, to figure out which queries refer the name column in one particular table. You could potentially look to see whether the NAME and TYPE from USER_SOURCE appear in DBA_DEPENDENCIES as referencing the particular table you're interested in. But that just shows you table-level dependencies at an object level and your object may depend on a large number of different tables.

Oracle: search though all stored procs/triggers/other db code?

Is it possible to search through all of the programmatic objects (functions, stored procedures, triggers, etc) across multiple schemas in Oracle?
There are a number of tables that appear to be unused in my code, but I don't want to break anything by removing them without checking first.
It is possible to search through object code-- you'd generally use the DBMS_METADATA package to generate the DDL for the object and then search the CLOB. However, it doesn't sound like that's actually what you want to do.
If you are just trying to figure out whether a table is referenced by any code in your system, you would generally want to use the DBA_DEPENDENCIES view (or ALL_DEPENDENCIES or USER_DEPENDENCIES depending on your privileges and the scope of what you're looking for). Something like
SELECT *
FROM dba_dependencies
WHERE referenced_owner = 'SCOTT'
AND referenced_name = 'EMP'
AND referenced_type = 'TABLE'
will show you everything that depends on the EMP table in the SCOTT schema.
The only time you'd want to search code rather than looking at DBA_DEPENDENCIES would be when you had code that was doing dynamic SQL where the table name was hard-coded. But that's relatively unlikely in practice.
You can search the DBA_SOURCE view:
SELECT *
FROM dba_source
WHERE UPPER(text) LIKE '%YOUR_TABLE_NAME%';
Do this in Toad by selecting:
Search => Object Search
If you had Toad you could do this built-in. (I removed my schemas for privacy)

Resources