In Oracle there is table named customers. If we want to know what views depend on the customer table how can we find that out?
You could query the sys.all_dependancies object.
This will output the referencing object which is the name of the view using the table specified
SELECT referenced_owner || '.' || referenced_name as table_name,
referenced_type as type,
owner || '.' || name as referencing_object,
type as referencing_type
FROM sys.all_dependencies
WHERE referenced_type = 'VIEW'
AND referenced_name = 'customers' -- put your table/view name here
Related
In Oracle SQL developer GUI, I opened a table and a window with attributes appear. Here is a window tab named Dependencies.
I found this query:
select OWNER
, name
, type
, referenced_name
, referenced_type
from all_dependencies;
But didn't show output for all owners like as Oracle SQL Developer.
How can I obtain these results via a query SQL with all owners?
Thanks!
With a query like:
select owner, name, type, referenced_owner, referenced_name, referenced_type
from dba_dependencies
where referenced_owner = user and referenced_name = 'YOUR_TABLE_NAME';
or using bind variables:
var object_owner varchar2(30);
var object_name varchar2(30);
exec :object_owner := user;
exec :object_name := 'YOUR_TABLE_NAME';
select owner, name, type, referenced_owner, referenced_name, referenced_type
from dba_dependencies
where referenced_owner = :object_owner and referenced_name = :object_name ;
You can actually see the queries that SQL Developer runs under the hood. If you go to to the View menu and choose Log, or hit CtrlShiftL (assuming you're using Windows) you'll get a docked window which by default is titled "Messages - Log". At the bottom of that are two tabs, with "Messages" selected. If you click "Statements" instead you can see the statement and bind variables used.
In this case there are three statements issued when you refresh the dependencies tab, two of which are variations on what I've shown above - they get a few more columns, and check dependencies going both ways:
select owner, name, type, referenced_owner, referenced_name, referenced_type ,
owner sdev_link_owner, name sdev_link_name, type sdev_link_type
from Dba_DEPENDENCIES
where referenced_owner = :OBJECT_OWNER and referenced_name = :OBJECT_NAME
select owner, name, type, referenced_owner, referenced_name, referenced_type ,
referenced_owner sdev_link_owner, referenced_name sdev_link_name, referenced_type sdev_link_type
from Dba_DEPENDENCIES
where owner = :OBJECT_OWNER and name = :OBJECT_NAME
They are a good place to start if you want to figure out how replicate what you can see.
If you are connected as a user that doesn't have the privileges necessary to see the dba_dependencies table, SQL Developer instead falls back to all_dependencies:
select owner, name, type, referenced_owner, referenced_name, referenced_type ,
owner sdev_link_owner, name sdev_link_name, type sdev_link_type
from ALL_DEPENDENCIES
where referenced_owner = :OBJECT_OWNER and referenced_name = :OBJECT_NAME
select owner, name, type, referenced_owner, referenced_name, referenced_type ,
referenced_owner sdev_link_owner, referenced_name sdev_link_name, referenced_type sdev_link_type
from Dba_DEPENDENCIES
where owner = :OBJECT_OWNER and name = :OBJECT_NAME
which will only show information about objects you have select/execute privileges against. In the first query I showed above you can just change dba_dependencies to all_dependencies to see the equivalent (visible) results.
If you run the SQL manually as the same user you're connected to SQL Developer as, you'll see the same results.
How I can Get for a specific Table its columns Names ?
I tried this :
SELECT column_name
FROM USER_TAB_COLUMNS
WHERE table_name = 'x' ;
But it doesn't work.
Try this :
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TABLENAME'
Hope this helps.
EDIT :
The Oracle equivalent for information_schema.COLUMNS is USER_TAB_COLS for tables owned by the current user, ALL_TAB_COLS or DBA_TAB_COLS for tables owned by all users.
Tablespace is not equivalent to a schema, neither do you have to provide the tablespace name.
Providing the schema/username would be of use if you want to query ALL_TAB_COLS or DBA_TAB_COLS for columns OF tables owned by a specific user. in your case, I'd imagine the query would look something like:
String sqlStr= "
SELECT column_name
FROM all_tab_cols
WHERE table_name = 'users'
AND owner = ' || +_db+ || '
AND column_name NOT IN ( 'password', 'version', 'id' )
"
Note that with this approach, you risk SQL injection.
Source : Oracle query to fetch column names
String comparisons in Oracle are case sensitive by default, and table and column names are upper case by default, so you need to make sure that the capitalization of the table name you are searching for matches the the way it's stored in the database, so unless your table was named with mixed case or all lower case try making sure your string is all upper case.
SELECT column_name from USER_TAB_COLUMNS
WHERE table_name = 'X'; -- not 'x'
Additionally, if you don't own the table, then you need to use either ALL_TAB_COLUMNS or DBA_TAB_COLUMNS instead of USER_TAB_COLUMNS since USER_TAB_COLUMNS only lists details for tables owned by your current schema.
You can just describe the table:
desc x;
Try to queryUSER_TAB_COLUMNS view
SELECT column_name
FROM USER_TAB_COLUMNS
WHERE table_name = 'TABLE_NAME'
In a legacy database infrastructure, how do I best find views that access a certain table or column? I'm currently refactoring certain tables (i.e. delete unused columns) and I want to find all views that still rely on those columns and will break, if I remove the columns.
Is there any tool/feature to search through all view definitions in Oracle SQL Developer?
You can use something like function dependent_views, code below. Example usage:
select dependent_views('CUSTOMER_NAME', 'CUSTOMERS') list from dual
Output:
LIST
-----------------
SCOTT.V_PERSONS
Function searches dependendent views in ALL_DEPENDENCIES, next searches TEXT column from ALL_VIEWS for occurence of column_name.
Note: Because all_dependences may not contain full data of dependent objects (for instance when view was created by execute immediate) - my function may not find this object.
Also if column_name is substring of other column - function may return to many views.
create or replace function dependent_views
(i_column varchar2, i_table varchar2, i_owner varchar2 default USER)
return varchar2 is
o_ret varchar2(4000) := '';
v_text long := '';
begin
for o in (
select * from all_dependencies
where referenced_name = upper(i_table)
and referenced_owner = upper(i_owner)
and type = 'VIEW')
loop
begin
select text into v_text from all_views
where view_name = o.name and owner = o.owner;
exception when no_data_found then
null;
end;
if upper(v_text) like '%'||upper(i_column)||'%' then
o_ret := o_ret||o.owner||'.'||o.name||' ';
end if;
end loop;
return o_ret;
end dependent_views;
how do I best find views that access a certain table
You could query the [USER|ALL|DBA]_DEPENDENCIES view.
SELECT name ,
type ,
referenced_name ,
referenced_type
FROM user_dependencies
WHERE TYPE = 'VIEW'
AND NAME = '<VIEW_NAME>'
AND referenced_type = '<TABLE_NAME'>;
To get the result for all the views at once, remove the filter NAME = '<VIEW_NAME>'.
For example,
SQL> column name format a15
SQL> column type format a15
SQL> column referenced_name format a15
SQL> column referenced_type format a15
SQL> SELECT name ,
2 type ,
3 referenced_name ,
4 referenced_type
5 FROM user_dependencies
6 WHERE TYPE = 'VIEW';
NAME TYPE REFERENCED_NAME REFERENCED_TYPE
--------------- --------------- --------------- ---------------
EMP_CUSTOM_VIEW VIEW EMP TABLE
EMP_VIEW VIEW EMP TABLE
SQL>
Cause you search for all views that access certain table, this might help:
select
name,
type,
referenced_name,
referenced_type
from user_dependencies
where type = 'VIEW'
and referenced_type = 'TABLE'
I have this giant query which uses some text in it which look like variables but I have no idea what they are and I really can't figure it out. They aren't global or defined anywhere else in the oracle package. In particular below the variable(or whatever it is) called "has_value" is so confusing because it's used in multiple cases for queries across a LOT of tables.
PROCEDURE assemble_default_where(
i_search_id IN search_table.search_id%TYPE,
o_where_clause OUT VARCHAR2,
o_from_clause OUT VARCHAR2,
o_error_number OUT error_message.error_number%TYPE) IS
l_base VARCHAR2(30) := 'd';
CURSOR c_where_clause IS
SELECT DECODE
(sl.parameter_name,
'track Location', join_operator || ' ' || open_brackets || ' ' || not_operator || ' EXISTS(SELECT 1 FROM track_locations loc WHERE ' || l_base
|| '.plan_number = loc.plan_number AND ' || l_base || '.order_id = loc.order_id AND loc.t_id = NVL('''
|| track_location_rsect_id(has_value) || ''', loc.t_id) AND loc.tstatus = NVL(''' || track_tstatus_id(has_value)
FROM search_lines sl
WHERE search_id = i_search_id
ORDER BY line_no;
I have left out a bit of the query because it's not relevant to my question.
I want to know where join_operator, has_value and open_brackets come from and what they are???
There are several options:
Variable - defined in outer block.
Variable - defined in package body.
Variable - defined in package specification.
Column - the same column names could be in many tables.
Function - in the invoker's or definer's schema.
Library - in the invoker's or definer's schema.
Operator - in the invoker's or definer's schema.
Synonym - to a function, operator, or library.
In practice you probably would have already noticed if it was #1, #2, #3, or #4. And #6 and #7 are very rare. I would guess it is a function or a synonym to a function.
To rule out variables, search through the code either in your IDE or with this SQL:
select * from dba_source where lower(text) like '%join_operator%';
To rule out objects, search though all the objects with this SQL:
select * from dba_objects where object_name = 'JOIN_OPERATOR';
UPDATE
PL/SQL and DBA_DEPENDENCIES can also help identify objects.
Sample schema
alter session set plscope_settings = 'IDENTIFIERS:ALL';
create table search_lines(
search_id number, line_no number, parameter_name varchar2(100));
create or replace function join_operator_function return varchar2 is
begin
return 'asdf';
end;
/
create synonym join_operator for join_operator_function;
create or replace PROCEDURE assemble_default_where
is
CURSOR c_where_clause IS
SELECT DECODE(sl.parameter_name, 'track Location', join_operator)
FROM search_lines sl
ORDER BY line_no;
begin
null;
end;
/
PL/SCOPE example
WITH v AS (
SELECT Line,
Col,
INITCAP(NAME) Name,
LOWER(TYPE) Type,
LOWER(USAGE) Usage,
USAGE_ID,
USAGE_CONTEXT_ID
FROM USER_IDENTIFIERS
WHERE Object_Name = 'ASSEMBLE_DEFAULT_WHERE'
AND Object_Type = 'PROCEDURE'
)
SELECT RPAD(LPAD(' ', 2*(Level-1)) ||
Name, 30, '.')||' '||
RPAD(Type, 30)||
RPAD(Usage, 30)
IDENTIFIER_USAGE_CONTEXTS
FROM v
START WITH USAGE_CONTEXT_ID = 0
CONNECT BY PRIOR USAGE_ID = USAGE_CONTEXT_ID
ORDER SIBLINGS BY Line, Col
/
Assemble_Default_Where........ procedure declaration
Assemble_Default_Where...... procedure definition
C_Where_Clause............ cursor declaration
Join_Operator_Function.. function call
DBA_DEPENDENCIES example
select referenced_owner, referenced_name, referenced_type
from dba_dependencies
where owner = user
and name = 'ASSEMBLE_DEFAULT_WHERE';
REFERENCED_OWNER REFERENCED_NAME REFERENCED_TYPE
---------------- --------------- ---------------
SYS STANDARD PACKAGE
SYS SYS_STUB_FOR_PURITY_ANALYSIS PACKAGE
JHELLER_DBA JOIN_OPERATOR SYNONYM
JHELLER_DBA JOIN_OPERATOR_FUNCTION FUNCTION
JHELLER_DBA SEARCH_LINES TABLE
I've come up with a query that displays all tables and views of a specific owner. What I would like to do now, but am having an issue with, is that I would like to have a second column which line by line will indicate whether the field is a "table" or "view". Is this possible? if so, how might I go about this?
select table_name
from all_tables
where owner = '<owner>'
UNION
select view_name
from all_views
where owner = '<owner>'
order by table_name;
I'd prefer the xxx_objects views myself for this purpose (as Justin says), but if you specifically need other data from the table and view views, you can add extra info thus:
select 'Table' AS object_type, table_name
from all_tables
where owner = '<owner>'
UNION ALL
select 'View' AS object_type, view_name
from all_views
where owner = '<owner>'
order by table_name;
Note I've changed it to use UNION ALL because there will be no collisions between the two result sets.
I'd use all_objects instead
select object_name, object_type
from all_objects
where object_type in ('TABLE', 'VIEW')
and owner = <<schema name>>
order by object_name