You are on page 1of 4

Subhakanta Das

From:
Sent:
To:
Subject:

Subhakanta Das
Thursday, April 9, 2015 4:30 PM
Domnic DeSouza; Ruchi Shikhar; Dipti Prasad
Oracle Queries That might be useful.

Oracle Queries:
a. Get DDL for a table/index/any database object. (To be executed in script mode).
set heading off;
set echo off;
Set pages 999;
set long 90000;
spool ddl_list.sql
select dbms_metadata.get_ddl('TABLE','DEPT','SCOTT') from dual;
select dbms_metadata.get_ddl('INDEX','DEPT_IDX','SCOTT') from dual;
spool off;
b. Update one table using data from another table.
UPDATE TABLE1 a
SET (COL1,COL2) = (
SELECT COL1,COL2
FROM TABLE2 b
WHERE a.COL3 = b.COL3);
MERGE INTO PRODUCTION a
USING ( select id, name, count
from STAGING ) b
ON ( a.id = b.id )
WHEN MATCHED THEN
UPDATE SET a.name = b.name,
a.count = b.count
c. Reset a Database Sequence:
create or replace
procedure reset_sequence(p_seq in varchar2)
is
l_value number;
begin
1

//Get the maximum value in the sequence


execute immediate 'select ' || p_seq ||'.nextval from dual' INTO l_value;
//change the increment by value of sequence to ve of the maximum value.
execute immediate 'alter sequence ' || p_seq ||' increment by -' || l_value || ' minvalue 0';
//Get the next value of sequence,(maxvalue+(-maxvalue) = 0)
execute immediate 'select ' || p_seq ||'.nextval from dual' INTO l_value;
//Here we change back the increment by value of sequence to 1
execute immediate 'alter sequence ' || p_seq ||' increment by 1 minvalue 0';
end;
d. Find object Dependencies:

select owner,name,type,referenced_owner,referenced_name,referenced_type,dependency_type from


all_dependencies
where owner='OBAW' and type='PROCEDURE';

Here as you can see: the table w_customer_loc_d is referenced in the stored procedure
CW_FIX_CUSTOMER_LOCATIONS.

e. Find all the constraints on a given table:

select * from ALL_CONSTRAINTS where table_name='CW_OPPKPI_D';

Similiary for indexes:

select
index_name,index_type,table_name,uniqueness,compression,distinct_keys,num_rows,last_analyzed,user_stats,visibility
,partitioned from ALL_INDEXES where table_owner='OBAW';

List all the columns on which indexes are created


select * from ALL_IND_COLUMNS where table_owner='OBAW';

f.

Want to know which oracle object what amount of space?


//First analyze the table else statistics will be wrong
analyze table <table_name> compute statistics;
select blocks, empty_blocks,
avg_space, num_freelist_blocks
from user_tables
where table_name = <table_name>;
select extent_id, bytes, blocks
from user_extents
where
segment_name = <table_name>;
and segment_type = 'TABLE';

This o/p means:


The table has one extent of size 0.19MB divided among 6 blocks.
Empty Blocks: 3
Used Blocks: 3 (3 blocks contain data)
Avg Space free each block used:
K
Other way:

SELECT owner, segment_name, segment_type, partition_name, ROUND(bytes/(1024*1024),2) SIZE_MB,


tablespace_name
FROM DBA_SEGMENTS
WHERE SEGMENT_TYPE IN ('TABLE', 'TABLE PARTITION', 'TABLE SUBPARTITION',
'INDEX', 'INDEX PARTITION', 'INDEX SUBPARTITION', 'TEMPORARY', 'LOBINDEX', 'LOBSEGMENT', 'LOB
PARTITION')
--AND TABLESPACE_NAME LIKE 'COSTE%'
AND SEGMENT_NAME LIKE 'CW_OPPKPI_F%'
--AND partition_name LIKE 'P20100201%'
--AND segment_type = 'TABLE'
--AND OWNER = 'TARGET_POC'
--AND ROUND(bytes/(1024*1024),2) > 1000
ORDER BY bytes DESC;

Thanks and Regards,


Subhakanta Das
Phone: +91 9167967049

You might also like