diff --git a/active_rollback.sql b/active_rollback.sql index dffeaa4..bfd31f7 100644 --- a/active_rollback.sql +++ b/active_rollback.sql @@ -1,20 +1,20 @@ -REM Filename : active_rollback.sql -REM Author : Craig Richards -REM Created : -REM Version : 1.0 -REM Modifications : +REM Script Name : active_rollback.sql +REM Author : Craig Richards +REM Created : +REM Last Modified : +REM Version : 1.0 REM -REM Description : Shows active rollback segments +REM Modifications : +REM +REM Description : Shows active rollback segments -SELECT SUBSTR(r.name,1,5) RSN,s.sid SID, - SUBSTR(nvl(s.username, 'No Tran'),1,7) USR, - s.osuser OSUSR, s.terminal TERM, o.rssize/(1024*1024) RSIZE -FROM v$lock l, v$session s, v$rollname r, v$rollstat o -WHERE l.sid = s.sid(+) -AND TRUNC(l.id1/65536) = r.usn -AND o.usn = r.usn -AND l.type = 'TX' -AND l.lmode = 6 +SELECT SUBSTR(r.name,1,5) RSN,s.sid SID, SUBSTR(nvl(s.username, 'No Tran'),1,7) USR, s.osuser OSUSR, s.terminal TERM, o.rssize/(1024*1024) RSIZE +FROM v$lock l, v$session s, v$rollname r, v$rollstat o +WHER l.sid = s.sid(+) +AND TRUNC(l.id1/65536) = r.usn +AND o.usn = r.usn +AND l.type = 'TX' +AND l.lmode = 6 ORDER BY r.name; REM End of Script diff --git a/ascii.sql b/ascii.sql new file mode 100644 index 0000000..e54b188 --- /dev/null +++ b/ascii.sql @@ -0,0 +1,32 @@ +REM Script Name : ascii.sql +REM Author : Craig Richards +REM Created : 06-February-2008 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Shows the ASCII chart + +SET SERVEROUTPUT ON SIZE 10240 + +DECLARE + i NUMBER; + j NUMBER; + k NUMBER; +BEGIN + FOR i IN 2..15 LOOP + FOR j IN 1..16 LOOP + k:=i*16+j; + DBMS_OUTPUT.PUT((to_char(k,'000'))||':'||chr(k)||' '); + IF k mod 8 = 0 THEN + DBMS_OUTPUT.PUT_LINE(''); + END IF; + END LOOP; + END LOOP; +END; +/ + +SHOW ERRORS + +REM End of Script diff --git a/client_info.sql b/client_info.sql new file mode 100644 index 0000000..e71e6a9 --- /dev/null +++ b/client_info.sql @@ -0,0 +1,20 @@ +REM Script Name : client_info.sql +REM Author : Craig Richards +REM Created : +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Shows the client connections within the oracle database + +COLUMN username FORMAT A15 WORD_WRAPPED +COLUMN module FORMAT A15 WORD_WRAPPED +COLUMN action FORMAT A15 WORD_WRAPPED +COLUMN client_info FORMAT A30 WORD_WRAPPED + +SELECT username||'('||sid||','||serial#||')' username, module, action, client_info +FROM v$session +WHERE module||action||client_info IS NOT NULL; + +REM End of Script diff --git a/db_buffer_hr.sql b/db_buffer_hr.sql new file mode 100644 index 0000000..0d8503f --- /dev/null +++ b/db_buffer_hr.sql @@ -0,0 +1,19 @@ +REM Script Name : db_buffer_hr.sql +REM Author : Craig Richards +REM Created : +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Displays the buffer cache hit ratio + +COLUMN BUFFER_POOL_NAME FORMAT A20 + +SELECT name BUFFER_POOL_NAME, consistent_gets Consistent, db_block_gets Dbblockgets, +physical_reads Physrds, +ROUND(100*(1 - (physical_reads/(consistent_gets + db_block_gets))),2) HitRatio +FROM v$buffer_pool_statistics +WHERE (consistent_gets + db_block_gets) != 0; + +REM End of Script diff --git a/db_cache_advice.sql b/db_cache_advice.sql new file mode 100644 index 0000000..b510610 --- /dev/null +++ b/db_cache_advice.sql @@ -0,0 +1,36 @@ +REM Script Name : db_cache_advice.sql +REM Author : Craig Richards +REM Created : 05-March-2009 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Shows whether or not to change the setting for DB_CACHE + +COLUMN size_for_estimate FORMAT 999,999,999,999 HEADING 'Cache Size(M)' +COLUMN buffers_for_estimate FORMAT 999,999,999 HEADING 'Buffers' +COLUMN estd_physical_read_factor FORMAT 999.90 HEADING 'Estd Phys|Read Factor' +COLUMN estd_physical_reads FORMAT 999,999,999 HEADING 'Estd Phys| Reads' + +SET ECHO OFF +SET FEEDBACK OFF +SET PAGESIZE 50 +SET LINESIZE 200 + +SPOOL db_cache_advice.txt + +SELECT size_for_estimate, buffers_for_estimate,estd_physical_read_factor,estd_physical_reads +FROM v$db_cache_advice +WHERE name = 'DEFAULT' +AND block_size = (SELECT value FROM v$parameter WHERE name = 'db_block_size') +AND advice_status = 'ON'; + +SPOOL OFF + +SET FEEDBACK 6 +SET PAGESIZE 24 + +CLEAR COLUMNS + +REM End of Script diff --git a/db_report.sql b/db_report.sql new file mode 100644 index 0000000..9f248e9 --- /dev/null +++ b/db_report.sql @@ -0,0 +1,221 @@ +REM Script Name : db_report.sql +REM Author : Craig Richards +REM Created : 07-December-2006 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : + +SET PAUSE OFF +SET HEADING OFF + +ACCEPT dbsid PROMPT "Enter SID for report : "; +SPOOL /admin/output/db_report_&dbsid + +PROMPT Database Information +PROMPT ==================== + +SELECT banner from v$version; + +SET HEADING ON + +select DBID,name,LOG_MODE,created from v$database; + +SET HEADING OFF + +PROMPT +PROMPT Who Has the Dba privilege +PROMPT ========================= + +SELECT grantee FROM dba_role_privs WHERE granted_role = 'DBA'; + +PROMPT +PROMPT Who owns the recovery catalog +PROMPT ============================= + +SELECT grantee FROM dba_role_privs WHERE granted_role = 'RECOVERY_CATALOG_OWNER'; + +SET FEEDBACK OFF + +PROMPT +PROMPT Map of datafiles, controlfiles, redo logs +PROMPT ========================================= + +PROMPT +PROMPT Datafiles +PROMPT ========= + +SELECT file_name FROM dba_data_files; + +PROMPT +PROMPT Control Files +PROMPT ============= + +COLUMN name FORMAT A40 + +SELECT name FROM v$controlfile; + +CLEAR COLUMNS + +PROMPT +PROMPT Redo Logs +PROMPT ========= + +SELECT member FROM v$logfile; + +SET PAUSE OFF +SET FEEDBACK OFF +SET PAGESIZE 66 +SET TERMOUT OFF +SET TRIMSPOOL ON + +TTITLE SKIP 2 CENTER 'FREE - Free space by Tablespace' skip 2 + +COLUMN dummy NOPRINT +COLUMN pct_used FORMAT 999.9 HEADING "%|Used" +COLUMN name FORMAT A16 HEADING "Tablespace Name" +COLUMN bytes FORMAT 9,999,999,999,999 HEADING "Total Bytes" +COLUMN used FORMAT 9,999,999,999,999 HEADING "Used" +COLUMN free FORMAT 999,999,999,999 HEADING "Free" + +BREAK ON REPORT + +COMPUTE SUM of bytes ON REPORT +COMPUTE SUM of free ON REPORT +COMPUTE SUM of used ON REPORT + +SELECT a.tablespace_name name, b.tablespace_name dummy, + SUM(b.bytes)/COUNT( DISTINCT a.file_id||'.'||a.block_id ) bytes, + SUM(b.bytes)/COUNT( DISTINCT a.file_id||'.'||a.block_id ) - + SUM(a.bytes)/COUNT( DISTINCT b.file_id ) used, + SUM(a.bytes)/COUNT( DISTINCT b.file_id ) free, + 100 * ( (SUM(b.bytes)/COUNT( DISTINCT a.file_id||'.'||a.block_id )) - + (SUM(a.bytes)/COUNT( DISTINCT b.file_id ) )) / + (SUM(b.bytes)/COUNT( DISTINCT a.file_id||'.'||a.block_id )) pct_used +FROM sys.dba_free_space a, sys.dba_data_files b +WHERE a.tablespace_name = b.tablespace_name +GROUP BY a.tablespace_name, b.tablespace_name; +SET TERMOUT ON + +TTITLE OFF + +PROMPT +PROMPT User Information +PROMPT ================ +PROMPT + +COLUMN username FORMAT A20 +COLUMN profile FORMAT A10 +COLUMN default_tablespace FORMAT A15 +COLUMN temporary_tablespace FORMAT A15 + +select username, default_tablespace, temporary_tablespace,profile, created from dba_users; + +CLEAR COLUMNS + +PROMPT +PROMPT Database Parameters +PROMPT =================== + +COLUMN name FORMAT A40 +COLUMN value FORMAT A60 + +SET LINES 200 + +select name, value from v$parameter; + +CLEAR COLUMNS + +SET LINES 80 + +SET PAUSE OFF + +COLUMN segment_name FORMAT A15 +COLUMN tablespace_name FORMAT A15 + +PROMPT +PROMPT Rollback Segments +PROMPT ================= + +SELECT segment_name, tablespace_name, segment_id, status +FROM dba_rollback_segs; + +SET PAUSE OFF +SET FEEDBACK OFF + +COLUMN name FORMAT A20 + +SELECT name, optsize, shrinks, aveshrink, extends,wraps +FROM v$rollstat, v$rollname +WHERE v$rollstat.usn=v$rollname.usn; + +PROMPT +PROMPT Invalid Objects in the database +PROMPT =============================== + +SET LINES 200 + +COLUMN owner FORMAT A20 +COLUMN object_name FORMAT A30 +COLUMN object_type FORMAT A30 + +select owner, object_name, object_type from dba_objects where status = 'INVALID'; + +CLEAR COLUMNS + +SET LINESIZE 80 + +PROMPT +PROMPT SGA Settings +PROMPT ============ + +SHOW SGA; + +PROMPT +PROMPT Tablespace Information +PROMPT ====================== + +COLUMN tablespace_name FORMAT A25 + +SET LINES 200 +SET HEADING ON + +select tablespace_name, initial_extent, next_extent,min_extents,max_extents,extent_management from dba_tablespaces; + +COLUMN file_name FORMAT A80 + +select file_name from dba_data_files where AUTOEXTENSIBLE = 'YES'; + +CLEAR COLUMNS + +SET LINES 80 + +PROMPT +PROMPT DBMS SCHEDULER +PROMPT ============== + +SELECT owner, job_name, job_type FROM dba_scheduler_jobs; + +PROMPT +PROMPT Undo Stats +PROMPT ========== +PROMPT + +SET LINES 200 + +SELECT TO_CHAR(MIN(Begin_Time), 'DD-MON-YYYY HH24:MI:SS') "Begin Time", + TO_CHAR(MAX(End_Time), 'DD-MON-YYYY HH24:MI:SS') "End Time", + SUM(Undoblks) "Total Undo Blocks Used", + SUM(Txncount) "Total Num Trans Exec", + MAX(Maxquerylen) "Longest Query(in secs)", + MAX(Maxconcurrency) "Highest Concurrent Trans Count", + SUM(Ssolderrcnt), SUM(Nospaceerrcnt) +FROM v$undostat; + +SPOOL OFF + +REM exit + +REM End of Script diff --git a/dbfile_seqread.sql b/dbfile_seqread.sql index 18f5a33..710c97e 100644 --- a/dbfile_seqread.sql +++ b/dbfile_seqread.sql @@ -1,10 +1,12 @@ -REM Filename : dbfile_seqread.sql -REM Author : Craig Richards -REM Created : 06-August-2008 -REM Version : 1.0 -REM Modifications : +REM Script Name : dbfile_seqread.sql +REM Author : Craig Richards +REM Created : 06-August-2008 +REM Last Modified : +REM Version : 1.0 REM -REM Description : Shows the waits for dbfile_sequential_reads +REM Modifications : +REM +REM Description : Shows the waits for dbfile_sequential_reads SET LINES 200 SET PAUSE OFF diff --git a/dbfile_seqread_object.sql b/dbfile_seqread_object.sql new file mode 100644 index 0000000..4d16772 --- /dev/null +++ b/dbfile_seqread_object.sql @@ -0,0 +1,30 @@ +REM Script Name : dbfile_seqread_object.sql +REM Author : Craig Richards +REM Created : 06-August-2008 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Shows the object causing the waits + +SELECT b.sid, nvl(substr(a.object_name,1,30),'P1='||b.p1||' P2='||b.p2||' P3='||b.p3) object_name, + a.subobject_name, + a.object_type +FROM dba_objects a, v$session_wait b, x$bh c +WHERE c.obj = a.object_id(+) +AND b.p1 = c.file#(+) +AND b.p2 = c.dbablk(+) +AND b.event = 'db file sequential read' +UNION +SELECT b.sid, nvl(substr(a.object_name,1,30), 'P1='||b.p1||' P2='||b.p2||' P3='||b.p3) object_name, + a.subobject_name, + a.object_type +FROM dba_objects a, v$session_wait b, x$bh c +WHERE c.obj = a.data_object_id(+) +AND b.p1 = c.file#(+) +AND b.p2 = c.dbablk(+) +AND b.event = 'db file sequential read' +ORDER BY 1; + +REM End of Script diff --git a/dbv_list.sql b/dbv_list.sql new file mode 100644 index 0000000..710aa30 --- /dev/null +++ b/dbv_list.sql @@ -0,0 +1,26 @@ +REM Script Name : dbv_list.sql +REM Author : Craig Richards +REM Created : +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Creates a spool file of database files you can then use to use DBV utility + +SET HEADING OFF +SET ECHO OFF +SET FLUSH OFF +SET PAGESIZE 9999 +SET LINESIZE 132 + +SPOOL check_files_with_dbv.lis + +SELECT 'dbv file='||d.file_name||' blocksize='||block_size||' feedback=1000 logfile='||d.file_id||'.lis' +FROM dba_data_files d, dba_tablespaces t +WHERE d.tablespace_name = t.tablespace_name +ORDER BY d.file_id desc; + +SPOOL OFF + +REM End of Script diff --git a/ddl_extract.sql b/ddl_extract.sql new file mode 100644 index 0000000..5f5ac10 --- /dev/null +++ b/ddl_extract.sql @@ -0,0 +1,35 @@ +REM Script Name : ddl_extract.sql +REM Author : Craig Richards +REM Created : 12-January-2009 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Extracts the DDL statement for the object when the parameters are passed, Object Name and Object Type + +REM extract_object.sql + +SET ECHO OFF +SET HEADING OFF +SET FEEDBACK OFF +SET VERIFY OFF +SET LINESIZE 10000 +SET LONG 2000000000 +SET LONGCHUNKSIZE 16000 +SET TRIMSPOOL ON +SET PAGESIZE 0 + +COLUMN test FORMAT A10000 + +DEFINE _object_file = '&1' +DEFINE _object_name = '&2' +DEFINE _object_type = '&3' + +SPOOL &_object_file + +SELECT DBMS_METADATA.GET_DDL('&&_object_type', '&&_object_name',USER) test FROM DUAL; + +SPOOL OFF + +REM End of Script diff --git a/kill_oracle.sh b/kill_oracle.sh new file mode 100644 index 0000000..cbbdb76 --- /dev/null +++ b/kill_oracle.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +# Name : kill_oracle.sh + +# Author : Craig Richards +# Created : 31-May-2012 +# Last Modified : +# Version : 1.0 +# Modifications : + +# Description : This will kill all the oracle processes associated with a given sid + +# Instructions : kill_oracle.sh $ORACLE_SID + +################################# +# Start of procedures/functions # +################################# + +funct_check_params() # Function Name +{ # Start of the function + if [ ${NARG} -ne 1 ]; then # If the number of arguments is not one, then output a message + echo "$0 : Not enough Parameters passed, you need to supply an ORACLE_SID" + exit 1 # Quit the program + elif # Else if + + # If the argument passed is -h or --h then display the following message in the echo statement + + [[ ${SID} = "-h" ]] || [[ ${SID} = "--h" ]]; then + echo "Usage: You need to add an ORACLE_SID, e.g $0 BOSSLIVE" + exit 1 # Quit the program + fi # End of the if statement +} # End of the function + +funct_check_user() # Function Name +{ # Start of the function + if [ `/usr/ucb/whoami` != 'oracle' ]; then # Check the user is Oracle + echo "Error: you must run this as oracle" # Display a message if its another user + exit 99 # Quit the program + fi +} # End of the function + +funct_kill_oracle() # Function Name +{ # Start of the function + get_processes=`ps -ef | grep "ora_" | grep ${SID} | grep -v grep` ; export get_processes # Set the variable get_processes to show the processes that will be killed + echo -e "\nProcesses that will be killed is \n\n ${get_processes}" + ps -ef | grep "ora_" | grep ${SID} | grep -v grep | awk '{print $2}' | xargs kill -9 # Kill all the processes associated with the given sid +} # End of the function + +################ +# Main Program # +################ + +# Variable Settings + +SID=$1 # Set the variable KEYWORD as the first argument passed +NARG=$# # Set the variable NARG to a number of arguments on the command line + +{ # Start of the main program + funct_check_params # Call the function funct_check_params + funct_check_user # Call the function funct_check_user + funct_kill_oracle # Call the function funct_kill_oracle +} # End of the main program + +## End of Script diff --git a/log_switches.sql b/log_switches.sql new file mode 100644 index 0000000..915bbe0 --- /dev/null +++ b/log_switches.sql @@ -0,0 +1,52 @@ +REM Script Name : log_switches.sql +REM Author : Craig Richards +REM Created : 18 March 2013 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Show information about recent log switches in the database, orginally got FROM http://www.dba-oracle.com/tips_oracle_v$_log_history.htm + +COLUMN c1 format a10 HEADING "Month" +COLUMN c2 format a25 HEADING "Archive Date" +COLUMN c3 format 999 HEADING "Switches" + +COMPUTE AVG of C on A +COMPUTE AVG of C on REPORT + +BREAK ON A skip 1 ON REPORT SKIP 1 + +SELECT TO_CHAR(TRUNC(first_time), 'Month') c1, + TO_CHAR(TRUNC(first_time), 'Day : DD-Mon-YYYY') c2, COUNT(*) c3 +FROM v$log_history +WHERE TRUNC(first_time) > last_day(sysdate-100) +1 +GROUP BY TRUNC(first_time); + + +REM Daily COUNT and Size of Redo Log Space (Single Instance) + +SELECT A.*, ROUND(A.COUNT#*B.AVG#/1024/1024) Daily_Avg_Mb +FROM +( + SELECT + TO_CHAR(First_Time,'YYYY-MM-DD') DAY, + COUNT(1) COUNT#, + MIN(RECID) Min#, + MAX(RECID) Max# +FROM + v$log_history +GROUP BY + TO_CHAR(First_Time,'YYYY-MM-DD') +ORDER BY 1 DESC +) A, +( +SELECT +AVG(BYTES) AVG#, +COUNT(1) COUNT#, +MAX(BYTES) Max_Bytes, +MIN(BYTES) Min_Bytes +FROM v$log +) B +; + diff --git a/monitor_cursors.sql b/monitor_cursors.sql new file mode 100644 index 0000000..df50eec --- /dev/null +++ b/monitor_cursors.sql @@ -0,0 +1,19 @@ +REM Script Name : monitor_cursors.sql +REM Author : Craig Richards +REM Created : 17 July 2013 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Shows the amount of cursors currently open + +SET LINESIZE 200 + +COLUMN max_open_cursor FORMAT a30 + +SELECT MAX(a.value) AS highest_open_cursor, p.value AS max_open_cursor +FROM v$sesstat a, v$statname b, v$parameter p +WHERE a.statistic# = b.statistic# +AND b.name = 'opened cursors current' AND p.name= 'open_cursors' +GROUP BY p.value; \ No newline at end of file diff --git a/oracle_connection.sh b/oracle_connection.sh new file mode 100644 index 0000000..e8cfa6b --- /dev/null +++ b/oracle_connection.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# Name : oracle_connections.sh + +# Author : Craig Richards +# Created : 30-May-2012 +# Last Modified : +# Version : 1.0 +# Modifications : + +# Description : This will show all of the client connections to the database + +# Instructions : oracle_connections.sh $ORACLE_SID + +################################# +# Start of procedures/functions # +################################# + +funct_check_params() # Function Name +{ # Start of the function + if [ ${NARG} -ne 1 ]; then # If the number of arguments is not one, then output a message + echo "$0 : Not enough Parameters passed, you need to supply an ORACLE_SID" + exit 1 # Quit the program + elif # Else if + + # If the argument passed is -h or --h then display the following message in the echo statement + + [[ ${SID} = "-h" ]] || [[ ${SID} = "--h" ]]; then + echo "Usage: You need to add an ORACLE_SID, e.g $0 BOSSLIVE" + exit 1 # Quit the program + fi # End of the if statement +} # End of the function + +funct_count_connections() # Function Name +{ # Start of the function + numconnections=`ps -ef | grep ${SID}| grep -v grep | grep -v ora_ | wc -l` ; export numconnections # Set the variable numconnections as running the UNIX one liner + echo " You have ${numconnections} connections to the database ${SID}" # Display the amount of connections to the database +} # End of the function + +################ +# Main Program # +################ + +# Variable Settings + +SID=$1 # Set the variable KEYWORD as the first argument passed +NARG=$# # Set the variable NARG to a number of arguments on the command line + +{ # Start of the main program + funct_check_params # Call the function funct_check_params + funct_count_connections # Call the function funct_file_files +} # End of the main program + +## End of Script diff --git a/proc_datafiles.sql b/proc_datafiles.sql new file mode 100644 index 0000000..868d10e --- /dev/null +++ b/proc_datafiles.sql @@ -0,0 +1,62 @@ +REM Filename : proc_datafiles.sql +REM Author : Craig Richards +REM Created : 19th December 2012 +REM Version : 1.0 +REM Modifications : +REM + +REM Instructions : Show all the datafiles and there sizes for the given tablespace name + +CREATE OR REPLACE PROCEDURE cr_datafiles (inp_tblspc VARCHAR2:=NULL) +AUTHID CURRENT_USER +AS + +-- Define Exception + + ERROR exception; + +-- Variable Declaration + + lv_file_name sys.dba_data_files.file_name%TYPE; + lv_bytes sys.dba_data_files.bytes%TYPE; + +-- Create the cursors + + CURSOR c_files IS + SELECT rpad(file_name,50,' '), bytes + FROM sys.dba_data_files + WHERE UPPER(tablespace_name) = UPPER(inp_tblspc) + ORDER BY bytes; + +-- Output the Information + + BEGIN + + -- If the parameter passed is blank, or nothing is passed in the raise the error + + IF inp_tblspc IS NULL OR NVL(LENGTH(TRIM(inp_tblspc)),0)=0 THEN RAISE ERROR; + ELSE + OPEN c_files; + DBMS_OUTPUT.PUT_LINE(CHR(10)); + DBMS_OUTPUT.PUT_LINE('Datafiles for : ' || inp_tblspc); + DBMS_OUTPUT.PUT_LINE(CHR(10)); + DBMS_OUTPUT.PUT_LINE('FILENAME' || CHR(9) || CHR(9) || CHR(9) || CHR(9) || CHR(9) || CHR(9) || CHR(9) || 'BYTES' ); + DBMS_OUTPUT.PUT_LINE('--------' || CHR(9) || CHR(9) || CHR(9) || CHR(9) || CHR(9) || CHR(9) || CHR(9) || '-----' ); + LOOP + FETCH c_files INTO lv_file_name, lv_bytes; + EXIT WHEN c_files%NOTFOUND; + DBMS_OUTPUT.PUT_LINE(lv_file_name|| CHR(9) || CHR(9) || lv_bytes); + END LOOP; + CLOSE c_files; + END IF; + +-- Exception + + EXCEPTION + WHEN ERROR THEN + DBMS_OUTPUT.PUT_LINE (CHR(10) || 'ORA-77777 : You need to pass a tablespace'); + DBMS_OUTPUT.PUT_LINE ('ie exec cr_datafiles(''EBOND1'');'); +END cr_datafiles; +/ +SHOW ERROR + diff --git a/proc_extents.sql b/proc_extents.sql new file mode 100644 index 0000000..655d038 --- /dev/null +++ b/proc_extents.sql @@ -0,0 +1,47 @@ +REM Script Name : proc_extents.sql +REM Author : Craig Richards +REM Created : 16-Mar-2009 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Creates a procedure which shows the largest available extent for each tablespace + +CREATE OR REPLACE PROCEDURE extents +AUTHID CURRENT_USER +AS + +-- Variable Declaration + + LV_TABLESPACE_NAME dba_free_space.tablespace_name%TYPE; + LV_BYTES dba_free_space.bytes%TYPE; + +-- Create the cursor + + CURSOR c_extent IS + SELECT rpad(tablespace_name,15,' '), MAX(bytes) + FROM dba_free_space + GROUP BY tablespace_name + UNION + SELECT rpad(tablespace_name,15,' '), MAX(bytes) + FROM dba_temp_files + GROUP BY tablespace_name; + +-- Output the information + + BEGIN + DBMS_OUTPUT.PUT_LINE('Tablespace Name'||CHR(9)||CHR(9) ||'Bytes'); + DBMS_OUTPUT.PUT_LINE('==============='||CHR(9)||CHR(9) ||'====='); + OPEN c_extent; + LOOP + FETCH c_extent INTO LV_TABLESPACE_NAME,LV_BYTES; + EXIT WHEN c_extent%NOTFOUND; + DBMS_OUTPUT.PUT_LINE(LV_TABLESPACE_NAME||CHR(9)||CHR(9) ||LV_BYTES); + END LOOP; + CLOSE c_extent; + + END extents; +/ + +REM End of Script diff --git a/proc_free_bytes.sql b/proc_free_bytes.sql new file mode 100644 index 0000000..5d12361 --- /dev/null +++ b/proc_free_bytes.sql @@ -0,0 +1,61 @@ +REM Filename : proc_free_bytes.sql +REM Author : Craig Richards +REM Created : 19th December 2012 +REM Version : 1.0 +REM Modifications : +REM + +REM Instructions : Show all the free bytes in the given tablespace + +CREATE OR REPLACE PROCEDURE cr_free_bytes (inp_tblspc VARCHAR2:=NULL) +AUTHID CURRENT_USER +AS + +-- Define Exception + + ERROR EXCEPTION; + +-- Variable Declaration + + lv_bytes sys.dba_free_space.bytes%TYPE; + +-- Create the cursors + + CURSOR c_free IS + SELECT bytes + FROM sys.dba_free_space + WHERE UPPER(tablespace_name) = UPPER(inp_tblspc) + ORDER BY bytes; + +-- Output the Information + + BEGIN + + -- If the parameter passed is blank, or nothing is passed in the raise the error + + IF inp_tblspc IS NULL OR NVL(LENGTH(TRIM(inp_tblspc)),0)=0 THEN RAISE ERROR; + ELSE + OPEN c_free; + DBMS_OUTPUT.PUT_LINE(CHR(10)); + DBMS_OUTPUT.PUT_LINE('Tablespace : ' || inp_tblspc); + DBMS_OUTPUT.PUT_LINE(CHR(10)); + DBMS_OUTPUT.PUT_LINE('BYTES' ); + DBMS_OUTPUT.PUT_LINE('=====' ); + LOOP + FETCH c_free INTO lv_bytes; + EXIT WHEN c_free%NOTFOUND; + DBMS_OUTPUT.PUT_LINE(lv_bytes); + END LOOP; + CLOSE c_free; + END IF; + +-- Exception + + EXCEPTION + WHEN ERROR THEN + DBMS_OUTPUT.PUT_LINE (CHR(10) || 'ORA-77777 : You need to pass a tablespace'); + DBMS_OUTPUT.PUT_LINE ('ie exec cr_datafile(''EBOND1'');'); +END cr_free_bytes; +/ +SHOW ERROR + diff --git a/proc_open_cursors.sql b/proc_open_cursors.sql new file mode 100644 index 0000000..6888c41 --- /dev/null +++ b/proc_open_cursors.sql @@ -0,0 +1,44 @@ +REM Script Name : tp_open_cursors.sql +REM Author : Craig Richards +REM Created : 27 September 2013 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Shows the open cursors and the maximum number of cursors + +CREATE OR REPLACE PROCEDURE tp_open_cursors +AS + +-- Variable Declaration + + lv_highest sys.v$sesstat.value%TYPE; + lv_max sys.v$parameter.value%TYPE; + lv_percentage NUMBER; + +-- Create the cursors + + CURSOR c_open_cursor IS + SELECT MAX(a.value) AS highest_open_cursor, p.value AS max_open_cursor + FROM v$sesstat a, v$statname b, v$parameter p + WHERE a.statistic# = b.statistic# + AND b.name = 'opened cursors current' AND p.name= 'open_cursors' + GROUP BY p.value; + +-- Output the Information + + BEGIN + DBMS_OUTPUT.PUT_LINE(CHR(10)||'Open ' || CHR(9) || 'Max Cursors'); + DBMS_OUTPUT.PUT_LINE('==== ' || CHR(9) || '==========='); + OPEN c_open_cursor; + LOOP + FETCH c_open_cursor into lv_highest,lv_max; + EXIT WHEN c_open_cursor%NOTFOUND; + DBMS_OUTPUT.PUT_LINE(lv_highest||CHR(9)||lv_max); + END LOOP; + CLOSE c_open_cursor; + lv_percentage := (lv_highest / lv_max *100); + DBMS_OUTPUT.PUT_LINE (CHR(10) || 'You are using ' || TRUNC(lv_percentage,2) || '% of the open_cursors parameter'); +END tp_open_cursors; +/ diff --git a/proc_show_events.sql b/proc_show_events.sql new file mode 100644 index 0000000..6ac362a --- /dev/null +++ b/proc_show_events.sql @@ -0,0 +1,24 @@ +REM Script Name : proc_show_events.sql +REM Author : Craig Richards +REM Created : 11 April 2013 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Create a procedure you can call to show what events are set in the database, this is for sessions mainly, but if they are set in the database they will exist in all sessions + +CREATE OR REPLACE PROCEDURE cr_show_events +AS + event_level NUMBER; +BEGIN + FOR i IN 10000..10999 LOOP + sys.dbms_system.read_ev(i,event_level); + IF (event_level > 0) THEN + DBMS_OUTPUT.ENABLE(1000000); + DBMS_OUTPUT.PUT_LINE('Event '||to_char(i)||' set at level '|| + TO_CHAR(event_level)); + END IF; + END LOOP; +END; +/ diff --git a/proc_table_pk.sql b/proc_table_pk.sql new file mode 100644 index 0000000..0712f19 --- /dev/null +++ b/proc_table_pk.sql @@ -0,0 +1,44 @@ +REM Script Name : proc_table_pk.sql +REM Author : Craig Richards +REM Created : 16th November 2012 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Create a procedure you can call to check the primary key for a table name passed in + +CREATE OR REPLACE PROCEDURE table_pk (p_tab VARCHAR2) +AUTHID CURRENT_USER +AS + +-- Variable Declaration + + LV_TABLE_NAME dba_cons_columns.table_name%TYPE; + LV_CONSTRAINT_NAME dba_cons_columns.constraint_name%TYPE; + LV_COLUMN_NAME dba_cons_columns.column_name%TYPE; + LV_POSITION dba_cons_columns.position%TYPE; + LV_STATUS dba_constraints.status%TYPE; + +-- Create the cursor + + CURSOR c_tab_pk IS + SELECT dbcc.table_name,dbcc.constraint_name,dbcc.column_name,dbcc.position,dbc.status + FROM dba_cons_columns dbcc, dba_constraints dbc + WHERE dbcc.constraint_name = dbc.constraint_name + AND dbcc.table_name = dbc.table_name + AND dbc.constraint_TYPE = 'P' + AND dbc.table_name = UPPER(p_tab); + + -- Output the information + + BEGIN + OPEN c_tab_pk; + LOOP + FETCH c_tab_pk INTO LV_TABLE_NAME, LV_CONSTRAINT_NAME, LV_COLUMN_NAME, LV_POSITION, LV_STATUS; + EXIT WHEN c_tab_pk%NOTFOUND; + DBMS_OUTPUT.PUT_LINE(lv_table_name||CHR(9)||lv_constraint_name||CHR(9)||lv_column_name||CHR(9)||lv_position||CHR(9)||lv_status); + END LOOP; + CLOSE c_tab_pk; + END table_pk; +/ diff --git a/proc_update_mail.sql b/proc_update_mail.sql new file mode 100644 index 0000000..61e31f5 --- /dev/null +++ b/proc_update_mail.sql @@ -0,0 +1,53 @@ +REM Script Name : tp_datamgt_bonds.sql +REM Author : Craig Richards +REM Created : 20 May 2013 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : This procedure updates data + +CREATE OR REPLACE PROCEDURE tp_datamgt_bonds +AUTHID CURRENT_USER +AS + +--Variable Declaration + +num_rows NUMBER; +v_From VARCHAR2(80) := 'From@mail.com'; +v_Recipient VARCHAR2(80) := 'To@mail.com'; +v_Subject VARCHAR2(80) := 'Your Update Statement'; +v_Mail_Host VARCHAR2(30) := 'mailhost'; +v_Mail_Conn UTL_SMTP.Connection; +CRLF VARCHAR2(2) := CHR(13)||CHR(10); + +BEGIN + +-- Update SQL Statement + + UPDATE table + SET active_flag = 'N' + WHERE a=b; + + num_rows := SQL%ROWCOUNT; + +-- Send an email with the amount of rows that have been updated + + v_Mail_Conn := UTL_SMTP.Open_Connection(v_Mail_Host, 25); + UTL_SMTP.Helo(v_Mail_Conn, v_Mail_Host); + UTL_SMTP.Mail(v_Mail_Conn, v_From); + UTL_SMTP.Rcpt(v_Mail_Conn, v_Recipient); + UTL_SMTP.Data(v_Mail_Conn,'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || CRLF || + 'From: ' || v_From || CRLF || + 'Subject: '|| v_Subject || CRLF || + 'To: ' || v_Recipient || CRLF ||CRLF ||'Procedure Ran and there were ' || num_rows || ' rows updated '|| CRLF || CRLF || 'Regards' ||CRLF ||'DBA Team'); + +-- Exception Code + +EXCEPTION +WHEN UTL_SMTP.Transient_Error OR UTL_SMTP.Permanent_Error THEN + RAISE_APPLICATION_ERROR(-20000, 'Unable to send mail: '||SQLERRM); + DBMS_OUTPUT.PUT_LINE (num_rows || ' Rows Updated'); +END; +/ diff --git a/rebuild_indexes.sql b/rebuild_indexes.sql new file mode 100644 index 0000000..b3cfdb1 --- /dev/null +++ b/rebuild_indexes.sql @@ -0,0 +1,51 @@ +REM Script Name : rebuild_indexes.sql +REM Author : Craig Richards +REM Created : 20 May 2013 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : This procedure generates and runs the codes to rebuild the indexes for a given table + +CREATE OR REPLACE PROCEDURE rebuild_indexes (p_owner IN VARCHAR2:=NULL, p_table_name IN VARCHAR2:=NULL, p_tblspc IN VARCHAR2:=NULL) +AS + +-- Variable Declaration + +ERROR EXCEPTION; +rebuild_indexes VARCHAR2(2000); + +BEGIN + +-- Check the inputs exist + + IF p_owner IS NULL OR NVL(length(trim(p_owner)),0)=0 THEN raise error; + ELSIF p_table_name IS NULL OR NVL(length(trim(p_table_name)),0)=0 THEN raise error; + ELSIF p_tblspc IS NULL OR NVL(length(trim(p_tblspc)),0)=0 THEN raise error; + END IF; + +-- Check the Tablespace + + IF p_tblspc = 'REORG' THEN + tp_resize_reorg(2000); + END IF; + + DBMS_OUTPUT.PUT_LINE(' The Commands that were executed wereis :' || CHR(10)); + + FOR indexes_to_rebuild IN + (SELECT index_name FROM dba_indexes WHERE owner = p_owner AND table_name = p_table_name) + LOOP + rebuild_indexes := 'ALTER INDEX ' ||p_owner||'.'||indexes_to_rebuild.index_name|| ' rebuild tablespace '|| p_tblspc; + DBMS_OUTPUT.PUT_LINE(rebuild_indexes); + EXECUTE IMMEDIATE rebuild_indexes; + END LOOP; + + EXCEPTION + WHEN error THEN + DBMS_OUTPUT.PUT_LINE (CHR(10) || 'ORA-77777 : You need to pass the OWNER, TABLE NAME, TABLESPACE TO REBUILD TO'); + DBMS_OUTPUT.PUT_LINE ('ie exec tp_rebuild_indexes(''SCOTT'',''EMP'',''REORG'');'); +END tp_rebuild_indexes; +/ + + diff --git a/redo_log_switches.sql b/redo_log_switches.sql new file mode 100644 index 0000000..388e2b3 --- /dev/null +++ b/redo_log_switches.sql @@ -0,0 +1,34 @@ +REM Script Name : redo_log_switches.sql +REM Author : Craig Richards +REM Created : 30 November 2012 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Show the redo log switching in the database, two different ways to display the information + +COLUMN c1 FORMAT A10 HEADING "Month" +COLUMN c2 FORMAT A25 HEADING "Archive Date" +COLUMN c3 FORMAT 999 HEADING "Switches" + +COMPUTE AVG of C ON A +COMPUTE AVG of C ON REPORT + +BREAK ON A SKIP 1 ON REPORT SKIP 1 + +SELECT TO_CHAR(TRUNC(first_time), 'Month') c1, TO_CHAR(TRUNC(first_time), 'Day : DD-MON-YYYY') c2, COUNT(*) c3 +FROM v$log_history +WHERE TRUNC(first_time) > last_day(SYSDATE-30) +1 +GROUP BY trunc(first_time); + +-- Daily Count and Size of Redo Log Space (Single Instance) + +SELECT A.*, ROUND(A.COUNT#*B.AVG#/1024/1024) Daily_Avg_Mb +FROM (SELECT TO_CHAR(First_Time,'YYYY-MM-DD') DAY, COUNT(1) COUNT#, MIN(RECID) MIN#, MAX(RECID) MAX# +FROM v$log_history +GROUP BY TO_CHAR(First_Time,'YYYY-MM-DD') +ORDER BY 1) A, +( +SELECT AVG(BYTES) AVG#, COUNT(1) COUNT#, MAX(BYTES) Max_Bytes, MIN(BYTES) Min_Bytes +FROM v$log) B; \ No newline at end of file diff --git a/show_events.sql b/show_events.sql new file mode 100644 index 0000000..90d58b1 --- /dev/null +++ b/show_events.sql @@ -0,0 +1,24 @@ +REM Script Name : show_events.sql +REM Author : Craig Richards +REM Created : 11 April 2013 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : Shows what events are set in the database, this is for sessions mainly, but if they are set in the database they will exist in all sessions + +SET SERVEROUTPUT ON; + +DECLARE + event_level NUMBER; +BEGIN + FOR i IN 10000..10999 LOOP + sys.dbms_system.read_ev(i,event_level); + IF (event_level > 0) THEN + DBMS_OUTPUT.PUT_LINE('Event '||to_char(i)||' set at level '|| + TO_CHAR(event_level)); + END IF; + END LOOP; +END; +/ diff --git a/test_table.sql b/test_table.sql new file mode 100644 index 0000000..5dee4ab --- /dev/null +++ b/test_table.sql @@ -0,0 +1,29 @@ +REM Filename : test_table.sql +REM Author : Craig Richards +REM Created : 18-Mar-2009 +REM Version : 1.0 +REM Modifications : +REM +REM Description : Creates a test table and inserts 1000 rows + +CREATE OR REPLACE PROCEDURE test_table +AS + +-- Variable Declaration + +lv_count NUMBER; +lv_one VARCHAR2(30) :='TESTONE'; +lv_two VARCHAR2(30) :='TESTTWO'; +lv_three VARCHAR2(30) :='TESTTHREE'; +lv_four VARCHAR2(30) :='TESTFOUR'; + +BEGIN + EXECUTE IMMEDIATE('drop table test'); + EXECUTE IMMEDIATE('Create table test(test1 varchar2(30), test2 varchar2(30), test3 varchar2(30), test4 varchar2(30)) tablespace tools'); + FOR i in 1..1000 LOOP + insert into test (test1,test2,test3,test4) values (lv_one,lv_two,lv_three,lv_four); + END LOOP; +END; +/ + +REM End of Script diff --git a/tp_stats.sql b/tp_stats.sql new file mode 100644 index 0000000..e31dd2c --- /dev/null +++ b/tp_stats.sql @@ -0,0 +1,31 @@ +REM Script Name : tp_stats.sql +REM Author : Craig Richards +REM Created : 17 January 2013 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : + +REM Procedure to gather the statistics, this will be called via the DBMS_SCHEDULER + +CREATE OR REPLACE PROCEDURE GATHER_RIMS_STALE_STATISTICS +AS +BEGIN + dbms_stats.gather_schema_stats(ownname=>'RIMS',estimate_percent=>NULL,options=>'GATHER STALE',cascade=>TRUE); +END; +/ + +REM DBMS_SCHEDULER job to call the procedure + +BEGIN + dbms_scheduler.create_job + (job_name=>'GATHER_STALE_RIMS_STATISTICS', + repeat_interval => 'FREQ=DAILY; BYDAY=MON,TUE,WED,THU,FRI; BYHOUR=4; BYMINUTE=00', + job_type=>'stored_procedure', + job_action=>'sys.GATHER_RIMS_STALE_STATISTICS', + enabled=>TRUE, + comments=>'GATHER_RIMS_STALE_STATISTICS'); +END; +/ \ No newline at end of file diff --git a/user_privs.sql b/user_privs.sql new file mode 100644 index 0000000..853ee79 --- /dev/null +++ b/user_privs.sql @@ -0,0 +1,40 @@ +REM Script Name : user_privs.sql +REM Author : Craig Richards +REM Created : 20 May 2013 +REM Last Modified : +REM Version : 1.0 +REM +REM Modifications : +REM +REM Description : This code show all privileges for a given user or users, if you pass multiple users you need to do it like USER1','USER2','USER3 etc + +SET PAGESIZE 200 +SET LINESIZE 200 +SET VERIFY OFF + +COLUMN granted_by_role FORMAT a20 +COLUMN obj_owner FORMAT a15 + +UNDEFINE Users; + +SELECT a.grantee, b.privilege, a.granted_role AS GRANTED_BY_ROLE, NULL AS OBJECT, NULL AS OBJ_OWNER +FROM dba_role_privs a +JOIN dba_sys_privs b +ON a.granted_role=b.grantee +WHERE a.grantee IN ('&&Users') +UNION +SELECT grantee, privilege, 'n/a', NULL, NULL +FROM dba_sys_privs +WHERE grantee IN ('&&Users') +UNION +SELECT grantee, privilege, ' ', table_name, owner +FROM DBA_TAB_PRIVS +WHERE grantee IN ('&&Users') +UNION +SELECT username, 'QUOTA', DECODE(max_bytes, -1, 'unlimited', max_bytes/1024/1024) MAX_MB, 'TABLESPACE: ' || tablespace_name, NULL +FROM DBA_TS_QUOTAS +WHERE username IN ('&&Users') +ORDER BY grantee, privilege; + +SELECT grantee,granted_role,admin_option FROM dba_role_privs WHERE grantee IN ('&&Users'); +