Find references to specific column in Oracle in Jobs and Procedures - oracle

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.

Related

Is there a way to give a "second name" to a table in Hive so that a user can refer to either name of the table and would retrieve the same thing?

I would like to be able to refer to tables with a certain naming schema to make my code uniform, but I am pulling tables from different environments with different naming schema. If I want all my tables to have names like example_table_1 and example_table_2, but the second one is something like TB_ex_2, is there a way to give that table an attribute so that I can also call select * from database.example_table_2, and it will know to refer to TB_ex_2?
I understand that I can alias tables, e.g. select * from TB_ex_2 example_table_2, but I am trying to avoid that. Renaming each table is also not an option, because those names need to be retained to identify which environment they are coming from.
Hive does not support synonyms. The workaround is to create a view:
CREATE VIEW table2
AS SELECT * from table1;
Also you can create many tables on top of the same location(data).

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:

How to compare table data structure

How to compare table data structure.
1. Any table added or deleted.
2. Any column in the tables added or deleted.
So my job is to verify if any table or columns are added/deleted on 1st of every month.
My plan is to run a sql query and take a copy of entire list of tables and it's data type only (NO DATA) and save it in txt file or something and use it as base line, and next month run the same sql query and get the results and compare the file. is it possible? please help with the sql query which can do this job.
This query will give you a list of all tables and their columns for a given user (just replace ABCD in this query for the user you have to audit and providing you have access to all that users tables this will work).
SELECT table_name,
column_name
FROM all_tab_columns
WHERE owner = 'ABCD'
ORDER
BY table_name,
column_id;
This answers your question but I have to agree with a_horse_with_no_name that is not a good way implement change control, most notably because the changes have already happened.
This query is very basic and doesn't give you all the information you'd need to see if a column has changed (or any information about other objects types etc), but then you only asked about additions and deletions of tables and columns and you can compare the output of this script to previous outputs to find the answer to your allotted task.

How to find all tables updated by a particular plsql method

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).

Resources