Wednesday, January 27, 2010

Shrink a Table

CREATE OR REPLACE PROCEDURE  .dpr_table_shrink
IS
CURSOR c_table_name
IS
SELECT table_name
FROM user_tables
WHERE TEMPORARY = 'N' AND row_movement = 'DISABLED';

CURSOR c_table_shrink
IS
SELECT table_name
FROM user_tables
WHERE TEMPORARY = 'N' AND row_movement = 'ENABLED';

table_id INTEGER;
table_status INTEGER;
owner_name VARCHAR2 (200);
BEGIN
SELECT SYS_CONTEXT ('USERENV', 'SESSION_USER')
INTO owner_name
FROM DUAL;

FOR i IN c_table_name LOOP
table_id := DBMS_SQL.open_cursor;
DBMS_SQL.parse (table_id,
'ALTER TABLE '
|| i.table_name
|| ' ENABLE ROW MOVEMENT',
DBMS_SQL.native
);
table_status := DBMS_SQL.EXECUTE (table_id);
DBMS_SQL.close_cursor (table_id);
END LOOP;

FOR i IN c_table_shrink LOOP
BEGIN
table_id := DBMS_SQL.open_cursor;
DBMS_SQL.parse (table_id,
'ALTER TABLE ' || i.table_name || ' SHRINK SPACE',
DBMS_SQL.native
);
table_status := DBMS_SQL.EXECUTE (table_id);
DBMS_SQL.close_cursor (table_id);
DBMS_OUTPUT.put_line ('Table shrink completed: ' || i.table_name);
EXCEPTION
WHEN OTHERS THEN
IF DBMS_SQL.is_open (table_id) THEN
DBMS_SQL.close_cursor (table_id);
END IF;

DBMS_OUTPUT.put_line ('Table can not shrink: ' || i.table_name);
END;
END LOOP;
END;
/

Wednesday, January 20, 2010

Compile a Invalid Object

CREATE OR REPLACE PROCEDURE Dpr_Compile_Obj
IS

CURSOR obj_cur IS
SELECT object_type,object_name
FROM user_objects
WHERE status = 'INVALID'
AND object_type IN ('PROCEDURE','FUNCTION','PACKAGE','PACKAGE_BODY','VIEW','TRIGGER')
ORDER BY object_type,object_name;

BEGIN
FOR obj_rec IN obj_cur LOOP
DBMS_DDL.ALTER_COMPILE (obj_rec.object_type,user,obj_rec.object_name);
END LOOP;
END;
/

Generate a CSV file from ORACLE Database

CREATE OR REPLACE Procedure Get_Csv( p_query in varchar2,
p_dir in varchar2 ,
p_filename in varchar2 )
Is
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(2000);
l_status integer;
l_colCnt number default 0;
l_separator char(1);
l_cnt number default 0;

Begin

l_output := utl_file.fopen( p_dir, p_filename||'.csv', 'w' );

dbms_sql.parse( l_theCursor, p_query, dbms_sql.native );

For i in 1 .. 255 Loop
Begin
dbms_sql.define_column( l_theCursor, i, l_columnValue, 2000 );
l_colCnt := i;
Exception
when others then
if ( sqlcode = -1007 ) then exit;
else
raise;
end if;
End;
End Loop;

dbms_sql.define_column( l_theCursor, 1, l_columnValue, 2000 );

l_status := dbms_sql.execute(l_theCursor);

Loop
Exit when ( dbms_sql.fetch_rows(l_theCursor) <= 0 );

l_separator := '';

for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
utl_file.put( l_output, l_separator || l_columnValue );
l_separator := ',';
end loop;

utl_file.new_line( l_output );
l_cnt := l_cnt+1;

End loop;

dbms_sql.close_cursor(l_theCursor);

utl_file.fclose( l_output );

End Get_Csv;
/

Monday, January 18, 2010

Analyze Table & Indexes

CREATE OR REPLACE PROCEDURE ANALYZE_TABLE_INDEXES IS
CURSOR c_table_name
IS
SELECT table_name
FROM user_tables
WHERE TEMPORARY = 'N';

CURSOR c_index_name (table_name1 IN VARCHAR2)
IS
SELECT index_name
FROM user_indexes
WHERE table_name = table_name1 AND status = 'VALID';

owner_name VARCHAR2 (200);
BEGIN
SELECT SYS_CONTEXT ('USERENV', 'SESSION_USER')
INTO owner_name
FROM DUAL;

FOR i IN c_table_name LOOP
DBMS_STATS.unlock_table_stats (ownname => owner_name,
tabname => i.table_name,
stattype => 'ALL'
);

BEGIN
DBMS_STATS.gather_table_stats
(ownname => owner_name,
tabname => i.table_name,
estimate_percent => DBMS_STATS.auto_sample_size,
CASCADE => DBMS_STATS.auto_cascade,
DEGREE => 2,
no_invalidate => DBMS_STATS.auto_invalidate,
granularity => 'AUTO',
method_opt => 'FOR ALL COLUMNS SIZE AUTO'
);

DBMS_OUTPUT.put_line ( 'Statistics gathered for table: '
|| i.table_name
);

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line
( 'Statistics can not gathered for table: '
|| i.table_name
);
END;

FOR j IN c_index_name (i.table_name) LOOP
BEGIN
DBMS_STATS.gather_index_stats
(ownname => owner_name,
indname => j.index_name,
estimate_percent => DBMS_STATS.auto_sample_size,
DEGREE => 2,
no_invalidate => DBMS_STATS.auto_invalidate,
granularity => 'AUTO'
);

DBMS_OUTPUT.put_line ( 'Statistics gathered for index: '
|| i.table_name
|| '.'
|| j.index_name
);

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line
( 'Statistics can not gathered for index: '
|| i.table_name
|| '.'
|| j.index_name
);
END;
END LOOP;

DBMS_STATS.lock_table_stats (ownname => owner_name,
tabname => i.table_name
);
END LOOP;
END;
/

Wednesday, January 13, 2010

How to create TNS_ADMIN environment variable?

01. First, you have to create a directory whose name is "0_library", which is on a "C" disk.

02. Then you'll have to copy one of TNSNAMES.ORA files into that directory

03. Now it makes sense to point the TNS_ADMIN environment variable to the "c:\0_library" directory             because, if it doesn't exist or if it is empty, the whole TNS_ADMIN story doesn't make any sense

04. When necessary, maintain this TNSNAMES.ORA file; the rest of them (which are in their \network\admin           directories) should be renamed into, for example, TNSNAMES_OLD.ORA.

For furthure more
http://www.orafaq.com/forum/m/376523/72104/?srch=lengthy#msg_376523