Using List of values against REGEXP or Like - oracle

This is my present code
SELECT
OBJECT_NAME FROM ALL_OBJECTS WHERE
REGEXP_LIKE ( OBJECT_NAME,
'^table1|^table2|^table3|..'
)
AND OBJECT_TYPE = 'TABLE'
I want to avoid writing too many text against Regexp , so i created table which has all the table_name within it.
so that i can do something like
SELECT
OBJECT_NAME FROM ALL_OBJECTS WHERE OBJECT_NAME LIKE (SELECT NAME FROM TABLE_WITH_LIST)
AND OBJECT_TYPE = 'TABLE'
Any way i can achieve this ?

Yes, of course:
SELECT object_name
FROM all_objects o,
table_with_list x
WHERE o.object_name LIKE x.name
AND object_type = 'TABLE'
You will have to load table_with_list with patterns LIKE recognizes (with the % where you want it)

Related

How can I list the objects (views, mostly) I updated in the last 2 weeks in Oracle?

I ran this:
SELECT *
FROM sys.all_views
where view_name like 'V%DIMFE%'
but I would like to have the 'last changed' date. is that possible ? (I don't have DBA permissions, but I guess I could ask a colleague to run it for me).
select last_ddl_time
from all_objects
where object_type = 'VIEW'
should give the hint
Thanks to #Oguen answer, I ended up with this:
select owner, object_type, object_name, last_ddl_time, created
from all_objects
where object_type IN ('VIEW', 'TABLE')
--and owner = 'XYZ'
and object_name like '%ABC%'
and last_ddl_time >= trunc(sysdate - 15) --last 15 days
order by object_type, object_name;

This query looks clunky. Is there a better way to do this?

I've written a query that works, but looks super clunky. Also, the table only has a few hundred records in it right now, but will in the future have hundreds of thousands of records. It might get into the millions, but I'm not sure. So, performance might become an issue.
I'm wondering if there is a cleaner way to do this.
Thanks.
with objects as
(
select object_type, object_name
from pt_objectshistory
where export_guid = 'PTGAA5V0H2U1XAQYFLQ0QXGWF0OY7Z'
),
distinct_objects as
(select distinct * from objects),
o_count as
(select count(*) ocount from objects),
do_count as
(select count(*) docount from distinct_objects)
select
o_count.ocount,
do_count.docount,
o_count.ocount - do_count.docount delta
from o_count join do_count on 1=1
There is no group by, so you just count the number of (distinct) rows in the table
select count(*), count(distinct object_type || object_name)
from pt_objectshistory
where export_guid = 'PTGAA5V0H2U1XAQYFLQ0QXGWF0OY7Z'
Thanks to #MT0, object_type || object_name could be ambiguous and discard distinct rows, when there are none like in 'abc' || 'def' and 'ab' || 'cdef'.
So depending on the data you have, adding a separator could be helpful, e.g. object_type || ';' || object_name.
You should also look on performance and test, if this solution (concatenating columns) is really faster than a count of a subselect/CTE.
You may group by the object_type, object_name pairs and then sum up the result
select sum(gcount) as ocount, count(*) as docount
from (select count(*) as gcount, object_type, object_name
from pt_objectshistory
where export_guid = 'PTGAA5V0H2U1XAQYFLQ0QXGWF0OY7Z'
group by object_type, object_name) temp

How to get a list of native Oracle Functions Like (NVL, ABS etc)

I've trying with this command bellow, but does not work as i expected:
select OBJECT_NAME, OWNER from SYS.ALL_OBJECTS where upper(OBJECT_TYPE) = upper('FUNCTION') order by OWNER, OBJECT_NAME
is there any way to achieve that?
Check this one:
select distinct PROCEDURE_NAME
from ALL_PROCEDURES
where OWNER = 'SYS'
and OBJECT_NAME = 'STANDARD';

How to get Column list of Synonym in Oracle

I need to get column list of SYNONYMS in ORACLE. The table on which synonym is created is in other schema. Can anyone help me in this problem?
This will return a list of all synonyms in the db:
select * from DBA_SYNONYMS
order by synonym_name
This will return a list of synonyms with a 'like' name:
select * from DBA_SYNONYMS
where upper(synonym_name) like upper('%SYNONYM_NAME_HERE%')
order by synonym_name
Relevant column names in response:
SYNONYM_NAME
TABLE_NAME
Here is a query that I use to see synonyms and their targets.
You will need SELECT privileges on DBA_SYNONYMS and DBA_OBJECTS.
select decode(owner, 'PUBLIC', 'PUBLIC SYNONYM', 'SYNONYM') as objtype,
decode(owner, 'PUBLIC', '', owner) as objowner,
synonym_name as objname,
synonym_name || ' => ' ||
case
when db_link is null then '(' || (
select o1.object_type from dba_objects o1 where o1.owner = table_owner and o1.object_name = table_name and o1.object_type in ('VIEW','TABLE','SYNONYM','SEQUENCE','FUNCTION','PROCEDURE','PACKAGE','MATERIALIZED VIEW','JAVA CLASS','TYPE')
and not exists (select 1 from dba_objects o2 where o2.owner = o1.owner and o2.object_name = o1.object_name and o1.object_type = 'TABLE' and o2.object_type = 'MATERIALIZED VIEW')
) || ') ' || table_owner || '.' || table_name
else decode(table_owner, null, '', table_owner || '.') || table_name || decode(db_link, null, '', '#' || db_link)
end as objdesc
from dba_synonyms
where OWNER != 'PUBLIC'
order by 1,2,3,4
It's a bit wordy, but it makes nice output like this:
OBJTYPE OBJOWNER OBJNAME OBJDESC
------- ----------- -------------------- -----------------------------------------------------------------------------
SYNONYM SYSTEM PRODUCT_USER_PROFILE PRODUCT_USER_PROFILE => (TABLE) SYSTEM.SQLPLUS_PRODUCT_PROFILE
SYNONYM SYSTEM TAB TAB => (VIEW) SYS.TAB
This is actually part of a larger query I use for finding objects of various types, hence some of the redundant information.
I filtered out PUBLIC because that is one huge list.
it's never too late to respond
select * from all_tab_columns#&SYNONYM_DB_LINK
where upper(table_name) like '&TARGET_TABLE_NAME'
order by owner, table_name, column_id
In OWNER is the synonym owner
select * from all_synonyms
where table_owner=upper(:table_owner) and table_name=upper(:table_name)
What do you mean under "Column list"?
select * from all_tab_cols where table_name in
(select TABLE_NAME from all_synonyms where owner = <SCHEMA_NAME> )

Which Oracle view contains all constraints together?

I'm trying to get CONSTRAINTS from user_objects table like this:
select CASE object_type
WHEN 'DATABASE LINK' then 'dblinks'
WHEN 'FUNCTION' then 'functions'
WHEN 'INDEX' then 'indexes'
WHEN 'PACKAGE' then 'packages'
WHEN 'PROCEDURE' then 'procedures'
WHEN 'SEQUENCE' then 'sequences'
WHEN 'TABLE' then 'tables'
WHEN 'TRIGGER' then 'triggers'
WHEN 'VIEW' then 'views'
WHEN 'SYNONYM' then 'synonyms'
WHEN 'GRANT' then 'grants'
WHEN 'CONSTRAINT' then 'constraints'
ELSE object_type
END||'|'||
CASE object_type
WHEN 'DATABASE LINK' then 'DB_LINK'
ELSE object_type
END||'|'||object_name
from user_objects
where object_name not like 'BIN$%'
and object_type not like '%PARTITION'
and object_type not in ('PACKAGE BODY')
order by object_type
;
select distinct object_type
from user_objects
;
But..... USER_OBJECTS has only these types FUNCTION
INDEX, PACKAGE, PACKAGE BODY, PROCEDURE, SEQUENCE, TABLE, TRIGGER, VIEW because select distinct object_type from user_objects; returned them. So this query is not giving my the constraints at all.
Is there a way to get all constraints from Oracle? Which Oracle view should I use?
select * from user_constraints
Constraints aren't objects. So they're in a different view, namely USER_CONSTRAINTS. For foreign constraints, you'll need a self join:
select * from user_constraints c
left join user_constraints r on r.owner = c.r_owner and r.constraint_name = c.r_constraint_name
where c.constraint_type = 'R';
Some details can also be found in USER_CONS_COLUMNS.

Resources