by http://webgeektutorials.blogspot.com

Monday, May 14, 2012

Oracle DBA Script : Mapping Database

Maps out the database physically with sizes and paths of all the physical files .It will give the names and sizes of rollback segments also..

Code:
spool dbmap.rpt
start title132.sql "Database Layout " "dbmap.sql"
prompt ================================
prompt Tablespace/Datafile Listing
prompt ================================
prompt
prompt
column "Location" format A60;
column "Tablespace Name" format A15;
column "Size(M)" format 999,990;

break on "Tablespace Name" skip 1 nodup;
compute sum of "Size(M)" on "Tablespace Name";
SELECT tablespace_name "Tablespace Name",
file_name "Location", bytes/1048576"Size(M)"
FROM sys.dba_data_files
Order by tablespace_name;
Prompt
Prompt ================================
Prompt Redo Log Listing
Prompt ================================
Prompt
Prompt
column "Group" format 999;
column "File Location" format A50;
column "Bytes (M)" format 99,990;

break on "Group" skip 1 nodup;

Oracle DBA Script : Export table data into text file

Creating a ascii text file of oracle table data with pipe delimited columns.

Code:
#!/bin/ksh
#First line in the .txt file is column names
USR=username/password
echo "set feedback off\n set pagesize 0\n
select 'XXTT' || table_name from user_tables ;" | sqlplus -s $USR | grep '^XXTT' | cut -c5- |
while read TABL
do
STR="nothing"
STR1="nothing"
echo "desc $TABL\n" | sqlplus -s $USR | tail +3 | grep "^[A-Z,a-z]" | awk '{print $1}' |
while read COLUM
do
if [ "$STR" = "nothing" ] then
STR=$COLUM
STR1=$COLUM
else
STR="$STR || '|' || $COLUM"
STR1="$STR1|$COLUM"
fi

Oracle DBA Script : Check error message in alert log

This Script search for Oracle error messages in last 100 lines in the alert log file , send a email message to concerned and keep log to a file.
You should pass name of ORACLE_SID as a parameter.
For eg: Ck_alerlog FINL


Code:
#!/usr/bin/sh
# Script Type: Shell (Bourne)
#
# Script name: ck_alertlog.sh
# Comments: Script checks last 100 lines of
# the alert log for specific
# Oracle errors, then pages or e-mails depending #on the error.
# Parameter: ORACLE_SID
# -------------------
# Revision Log
#
# 00/00/00 : Name of modifier - description
#of Modifications
#
#--------------------------------------------------
#-------------------------
SID=$1
DIR=/usr/local/bin/dbabin
ORACLE_SID=$SID; export ORACLE_SID
ORACLE_HOME=`grep -v "^[#]" /var/opt/oracle/oratab|grep $ORACLE_SID|cut -d: -f2
export ORACLE_HOME
cd $ORACLE_HOME/../..

ALERT_DEST=`pwd`
ALERT_DEST=$ALERT_DEST/admin/$SID/bdump
LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH
PATH=$ORACLE_HOME/bin:$PATH; export PATH
COLLECTOR=`tail -100 $ALERT_DEST/alert_$SID.log |egrep "ORA-255 |ORA-255:|ORA-0255|ORA-214|ORA-214:|ORA-00214|ORA-270|ORA-270:|ORA-00270|ORA-272|ORA-272:|ORA-00272|ORA-600|ORA-600:|ORA-00600|ORA-1122|ORA-1122:|ORA-01122|ORA-1578|ORA1578:|ORA-01578|ORA-1628|ORA-1628:|ORA-01628|ORA-1630|ORA-1630:|ORA-01630|ORA1631|ORA-1631:|ORA-01631|ORA-1632|ORA-1632:|ORA-01632|ORA-1650|ORA-1650:|ORA01650|ORA-1652|ORA-1652:|ORA-01652|ORA-1653|ORA-1653:|ORA-01653|ORA-1654|ORA1654:|ORA-01654|ORA-1655|ORA-1655:|ORA-01655"`
for i in $COLLECTOR
do

Oracle DBA Script : Change Sql Prompt

When you connect to sqlplus, you see the the following sql prompt. SQL>
By using this sql in the glogin.sql, you will see a prompt similar to the following:
SCOTT@DB-01>


Code:
The following code works on Oracle 8i (8.1.5, 8.1.6, 8.1.7).
You have to insert the following line of code in glogin.sql which is usually found in $ORACLE_HOME/sqlplus/admin

set termout off
set echo off
define X=NotConnected
define Y=DBNAME

Column Usr New_Value X
Column DBName New_Value Y
Select SYS_CONTEXT('USERENV','SESSION_USER') Usr From Dual;

--- The following does not work in 8.1.5 but works --- in 8.1.6 or above
Select SYS_CONTEXT('USERENV','DB_NAME') DBNAME From Dual;

Oracle DBA Script : Find out duplicate records in a Table

Code: 

SELECT primary_key FROM table_name
MINUS
(
SELECT DISTINCT primary_key
FROM table_name
)

You can also use * instead of the primary_key.
18. Description: This Script will help you to find out which Database you are presently
Connected to? If You are working on multiple Databases on different Servers using SQL*Net or Net8.
Code:

Oracle DBA Script : Return Information about constraint

Script prompts for a constraint name then it returns information about the constraint.

Code:

set lines 120
set serveroutput on size 100000
set scan on
clear buffer
set verify off
set feedback off
declare

l_constraint_name varchar2(30);
l_constraint_type varchar2(1);
l_table_name varchar2(30);
l_search_condition long;
l_related_constraint varchar2(30);
l_related_table varchar2(30);
l_rcolumn varchar2(30);
l_column varchar2(30);
l_query varchar2(400);
l_column_count integer;

cursor lc_constraint (p_constraint_name varchar2) is
select *
from dba_constraints
where constraint_name = p_constraint_name;

cursor lc_related_constraint(p_r_constraint_name varchar2)
is

Oracle DBA Script : Find Child table for given parent table

set echo off
set verify off
accept xTable prompt 'Enter Table Name: '
TTITLE LEFT 'Child Tables for the table: '&xTABLE
break on TABLE_NAME
SELECT B.TABLE_NAME, C.COLUMN_NAME,
C.POSITION
FROM USER_CONSTRAINTS A,
USER_CONSTRAINTS B,
USER_CONS_COLUMNS C
WHERE
A.CONSTRAINT_NAME=
B.R_CONSTRAINT_NAME
AND
A.TABLE_NAME = C.TABLE_NAME
AND
A.CONSTRAINT_NAME = C.CONSTRAINT_NAME
AND
A.TABLE_NAME = UPPER('&xTable')
ORDER BY B.TABLE_NAME, C.POSITION;

TTITLE LEFT 'Parent tables for the table: '&xTable

SELECT A.TABLE_NAME, C.COLUMN_NAME,
C.POSITION
FROM USER_CONSTRAINTS A,
USER_CONSTRAINTS B,
USER_CONS_COLUMNS C
WHERE
A.CONSTRAINT_NAME=B.R_CONSTRAINT_NAME
AND
B.TABLE_NAME = C.TABLE_NAME
AND
B.CONSTRAINT_NAME = C.CONSTRAINT_NAME
AND
B.TABLE_NAME = UPPER('&xTable')
ORDER BY A.TABLE_NAME, C.POSITION;

Oracle DBA Script : Find out two top values from a table

1. Finds the two highest salaries from table "emp".

Code:
select a.empno,a.sal
from emp a
where 2>
(
select count(*)
from emp
where sal>a.sal
);


2. Finds the two lowest salaries from table "emp".


Code:
select a.empno,a.sal
from emp a
where 2>
(
select count(*)
from emp
where sal<a.sal
)

Oracle DBA Script : Monitor File I/O

This script takes a snapshot of v$filestats at the current time and saves it. It then waits 10 seconds and takes another snapshot and reports on the delta.

Code:
col name for a50
set linesize 132
set pages 666
-- drop temporary table
drop table jh$filestats;
create table jh$filestats as
select file#, PHYBLKRD, PHYBLKWRT
from v$filestat;
prompt Waiting......
exec dbms_lock.sleep(10);

prompt NOTE: Only the top 10 files...
select * from (
select df.name, fs.phyblkrd - t.phyblkrd "Reads",fs.PHYBLKWRT - t.PHYBLKWRT "Writes",(fs.PHYBLKRD+fs.PHYBLKWRT) - (t.PHYBLKRD+t.PHYBLKWRT) "Total IO"
from v$filestat fs, v$datafile df, jh$filestats t
where df.file# = fs.file# and t.file# = fs.file#
and (fs.PHYBLKRD+fs.PHYBLKWRT) - (t.PHYBLKRD+t.PHYBLKWRT) > 0
order by "Total IO" desc )
where rownum <= 10
/

Oracle DBA Script : Show View Source

Code: 


SET HEADING OFF
SET ECHO OFF
SET FEEDBACK OFF
SET PAGESIZE 0
set numwidth 10

SELECT uv.view_name SORT1, 0 SORT2, 0 SORT3,
0 SORT4,
'create or replace view '||uv.view_name ||' ('
from dba_views uv
WHERE uv.owner = upper('&2')
and uv.view_name = upper('&1')
union all
SELECT utc.view_name SORT1, utc.column_id
SORT2, 0 SORT3, 0 SORT4,utc.column_name
from dba_tab_columns
WHERE utc.owner = upper('&2')
and utc.table_name = upper('&1')
and utc.column_id = 1
union all
SELECT utc.view_name SORT1, utc.column_id
SORT2, 0 SORT3, 0 SORT4,
' , '||utc.column_name
from dba_tab_columns
WHERE utc.owner = upper('&2')
and utc.table_name = upper('&1')
and utc.column_id <> 1
SELECT uv.view_name SORT1,999 SORT2,0
SORT3,0 SORT4,' )'
from dba_views uv
WHERE uv.owner = upper('&2')
and uv.view_name = upper('&1')
ORDER BY 1, 2, 3, 4;
SELECT uv.text
from dba_views uv
WHERE uv.owner = upper('&2')
and uv.view_name = upper('&1');
SELECT uv.view_name SORT1,999 SORT2,0
SORT3,0 SORT4,' ;'
from dba_views uv
WHERE uv.owner = upper('&2')
and uv.view_name = upper('&1');