Oracle Spin

Day-to-Day Experiences

Archive for January, 2008

Grant Full Access to all tables and views within a Schema

Posted by Alex Lima on January 30, 2008

set serveroutput on
set serveroutput on size 1000000
DECLARE

vcount1 number := 0;
vcount2 number := 0;
v_tablename varchar2(32);
v_viewname varchar2(32);
vschema varchar2(30) := ‘&schema’;
vrole varchar2(30) := ‘&role’;
# — vprivilege varchar2(30) := ‘&privilege’;

BEGIN

vschema := UPPER(vschema);
vrole := UPPER(vrole);

for ctable in ( select owner, table_name from dba_tables where owner like vschema )
loop
vcount1:= vcount1+1;
v_tablename :=ctable.table_name;
begin
–dbms_output.put_line(ctable.owner||’.'||ctable.table_name);
execute immediate ‘grant select,update, insert,delete on ‘||ctable.owner||’.'||ctable.table_name||’ to ‘||vrole;
EXCEPTION — exception handlers begin
WHEN OTHERS THEN — handles all other errors
dbms_output.put_line(‘err_num: ‘||SQLCODE);
dbms_output.put_line(‘err_msg: ‘||SUBSTR(SQLERRM, 1, 100));
dbms_output.put_line(‘table_name: ‘||v_tablename);
vcount1:= vcount1-1;
END;
end loop;

dbms_output.put_line(‘—– ‘);
dbms_output.put_line(‘Counter Tables is: ‘||vcount1);
dbms_output.put_line(‘—– ‘);

for cview in ( select owner, view_name from dba_views where owner like vschema )
loop
vcount2:= vcount2+1;
v_viewname :=cview.view_name;
begin
–dbms_output.put_line(cview.owner||’.'||cview.view_name);
execute immediate ‘grant select on ‘||cview.owner||’.'||cview.view_name||’ to ‘||vrole;
EXCEPTION — exception handlers begin
WHEN OTHERS THEN — handles all other errors
dbms_output.put_line(‘err_num: ‘||SQLCODE);
dbms_output.put_line(‘err_msg: ‘||SUBSTR(SQLERRM, 1, 100));
dbms_output.put_line(‘view_name: ‘||v_viewname);
vcount2:= vcount2-1;
END;
end loop;
dbms_output.put_line(”);
dbms_output.put_line(‘Counter Views is: ‘||vcount2);
EXCEPTION — exception handlers begin
WHEN OTHERS THEN — handles all other errors
dbms_output.put_line(‘err_num: ‘||SQLCODE);
dbms_output.put_line(‘err_msg: ‘||SUBSTR(SQLERRM, 1, 100));
dbms_output.put_line(‘view_name: ‘||v_viewname);

END;
/

Posted in Scripts | Tagged: , , | Leave a Comment »

Check Oracle Password for Expiration

Posted by Alex Lima on January 30, 2008

This simple script will check if a user password is expiring in the next 120 days.

set pagesize 500
set linesize 200
set trimspool on
column “EXPIRE DATE” format a20
select username as “USER NAME”, expiry_date as “EXPIRE DATE”, account_status
from dba_users
where expiry_date < sysdate+120
and account_status IN ( ‘OPEN’, ‘EXPIRED(GRACE)’ )
order by account_status, expiry_date, username
/

Posted in Scripts | Tagged: , , , | Leave a Comment »

Killing an Oracle thread on Windows

Posted by Amin Jaffer on January 26, 2008

On Windows for each instance all oracle sessions are thread based and are part of one Oracle process (oracle.exe), so killing oracle.exe would crash your entire instance. Therefore using the utility orakill.exe one can kill user’s session. Using the following SQL one can find the thread id of the oracle process.

SQL> SELECT a.username, a.osuser, b.spid ThreadID, a.sid, a.serial# FROM v$session a, v$process b WHERE a.paddr = b.addr AND a.username IS NOT NULL;

USERNAME OSUSER ThreadID PROGRAM
————— —————————— ——— ——————————
SYS oracle 2990 rman (TNS V1-V3)
SCOTT mary 1384

So using orakill kill session mary’s session.
c:> orakill TESTDB 1384

Posted in General DBA | Tagged: , , | 2 Comments »

List users who are using permanent tablespace instead of temporary tablespace

Posted by Amin Jaffer on January 26, 2008

– If sorting is performed on permanent tablespace it can affect performance because it’s in nologging mode, reduces backup and recovery as it doesn’t get backed-up

SELECT username, temporary_tablespace, b.contents
FROM dba_users a, dba_tablespaces b
WHERE a.temporary_tablespace = b.tablespace_name
AND b.contents ‘TEMPORARY’;

– Using the following ALTER statement one can modify user’s temporary tablespace
SQL> ALTER USER <USER> TEMPORARY TABLESPACE <temporary tablespace name>;

Posted in General DBA | Tagged: , , , | Leave a Comment »

Archive log history by the hour

Posted by Amin Jaffer on January 26, 2008

– Print # of archive logs for yesterday by the hour
– can be used to determine to schedule archive logs backups, to view number of times log switches occurs

set linesize 200
set trimspool on
set feedback off

Column 00 format 999
Column 01 format 999
Column 02 format 999
Column 03 format 999
Column 04 format 999
Column 05 format 999
Column 06 format 999
Column 07 format 999
Column 08 format 999
Column 09 format 999
Column 10 format 999
Column 11 format 999
Column 12 format 999
Column 13 format 999
Column 14 format 999
Column 15 format 999
Column 16 format 999
Column 17 format 999
Column 18 format 999
Column 19 format 999
Column 20 format 999
Column 21 format 999
Column 22 format 999
Column 23 format 999

select trunc(first_time) AS Day,
sum(DECODE(to_char(first_time, ‘HH24′), ‘00′, 1, 0)) AS “00″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘01′, 1, 0)) AS “01″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘02′, 1, 0)) AS “02″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘03′, 1, 0)) AS “03″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘04′, 1, 0)) AS “04″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘05′, 1, 0)) AS “05″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘06′, 1, 0)) AS “06″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘07′, 1, 0)) AS “07″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘08′, 1, 0)) AS “08″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘09′, 1, 0)) AS “09″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘10′, 1, 0)) AS “10″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘11′, 1, 0)) AS “11″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘12′, 1, 0)) AS “12″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘13′, 1, 0)) AS “13″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘14′, 1, 0)) AS “14″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘15′, 1, 0)) AS “15″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘16′, 1, 0)) AS “16″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘17′, 1, 0)) AS “17″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘18′, 1, 0)) AS “18″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘19′, 1, 0)) AS “19″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘20′, 1, 0)) AS “20″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘21′, 1, 0)) AS “21″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘22′, 1, 0)) AS “22″,
sum(DECODE(to_char(first_time, ‘HH24′), ‘13′, 1, 0)) AS “23″
FROM v$log_history
WHERE trunc(FIRST_TIME) = trunc(sysdate – 1)
GROUP BY trunc(first_time);

Posted in Scripts | Tagged: , , , | Leave a Comment »

NetApp SnapShot Technology

Posted by Alex Lima on January 25, 2008

A few years ago I was introduced to the NetApp storage technology and it’s backup, clone, DR capabilities and etc. To be honest at the beginning I didn’t feel very confident on how would that work with Oracle internals and particularities like SCN, archived logs and others.

After several testing and trying different scenarios I had my first database in production using NetApp and just to add a bit of complexity it was a RAC database.

Well, It actually works very well. The FlexClone capability is awesome to replicate production data for testing and other purposes. SnapShot for local backup are amazing fast and SnapMirror and SnapValt for DR are just what I need.

NetApp has now SnapValidator which is what was missing for block corruption check.

Below is some information about SnapShots from NetApp man pages.

The snap family of commands provides a means to create and manage snapshots in each volume.
A snapshot is a read-only copy of the entire file system, as of the time the snapshot was created. The filer uses a copy-on-write technique to create snapshots very quickly without consuming any disk space. Only as blocks in the active file system are modified and written to new locations on disk does the snapshot begin to consume extra space.

Snapshots are exported to all CIFS or NFS clients. They can be accessed from each directory in the file system. From any directory, a user can access the set of snapshots from a hidden sub-directory that appears to a CIFS client as ~snapshot and to an NFS client as .snapshot. These hidden sub-directories are special in that they can be accessed from every directory, but they only show up in directory listings at an NFS mount point or at the root of CIFS share.

Each volume on the filer can have up to 31 snapshots at one time. Because of the copy-on-write technique used to update disk blocks, deleting a snapshot will generally not free as much space as its size would seem to indicate. Blocks in the snapshot may be shared with other snapshots, or with the active file system, and thus may be unavailable for reuse even after the snapshot is deleted.

The snap commands are persistent across reboots. Do not include snap commands in the /etc/rc. If you include a snap command in the /etc/rc file, the same snap command you enter through the command line interface does not persist across a reboot and is overridden by the one in the /etc/rc file.

Automatic snapshots
Automatic snapshots can be scheduled to occur weekly, daily, or hourly. Weekly snapshots are named weekly.N, where N is “0″ for the most recent snapshot, “1″ for the next most recent, and so on. Daily snapshots are named daily.N and hourly snapshots hourly.N. Whenever a new snapshot of a particular type is created and the number of existing snapshots of that type exceeds the limit specified by the sched option described below, then the oldest snapshot is deleted and the existing ones are renamed. If, for example, you specified that a maximum of 8 hourly snapshots were to be saved using the sched command, then on the hour, hourly.7 would be deleted, hourly.0 would be renamed to hourly.1, and so on.

Command Options:

snap list [ vol_name ]
snap create vol_name name
snap delete vol_name name
snap rename vol_name from to
snap reserve [ vol_name [ percent ] ]
snap restore [ -f ] [ -t vol | file ] [ -s snapshot_name ] [ -r restore_as_path ] vol_name | restore_from_path
snap sched [ vol_name [ weeks [ days [ hours[@list] ] ] ] ]

Source from this post: NetApp man pages

Posted in NetApp for Oracle | Tagged: , | 1 Comment »

How to find materialized view log entries?

Posted by Alex Lima on January 25, 2008

We have faced a situation where replication with materialized view needs to be checked before the shutdown outage between source and destination.
This is a little script where you can find if there is entries in the MV logs.

declare
–use dba_mview_logs for 9i and 10g
cursor cur is select log_table from dba_snapshot_logs;
num_rows number;
cnt number := 0;
begin
for x in cur loop
execute immediate ’select count(*) from ‘||x.log_table into num_rows;
if ( num_rows > 0 ) then
dbms_output.put_line(x.log_table||’ has ‘||num_rows||’ rows’);
cnt := cnt + 1;
end if;
end loop;
dbms_output.put_line(cnt||’ materialized view logs have entries’);
end;

Posted in Scripts | Tagged: , , , | Leave a Comment »

How to find datafiles with same name?

Posted by Alex Lima on January 24, 2008

Sometimes you want to check for datafiles with the same name in the database, maybe you are new to the env and have a major storage migration project or for any other reason.

Here is a little script to check if the database has datafiles with the same name in different directories.

set line 140
set pagesize 2000
set heading off
select count(*), substr(file_name,instr(file_name, ‘/’, -1))
from dba_data_files
group by substr(file_name,instr(file_name, ‘/’, -1))
having count(*) > 1
/

Posted in Scripts | Tagged: , , , , , | Leave a Comment »

How to get Help from SQL*Plus

Posted by Alex Lima on January 24, 2008

A lot of people actually ask me why sql*plus doesn’t have a help feature like man in UNIX. Actually it does for all sql*plus command but not for the SQL language. Below I will show some help commands for SQL*Plus:

SQL> ?

HELP
—-

Accesses this command line help system. Enter HELP INDEX or ? INDEX
for a list of topics. In iSQL*Plus, click the Help button to display
iSQL*Plus online help.
You can view SQL*Plus resources at http://otn.oracle.com/tech/sql_plus/
and the Oracle Database Library at http://otn.oracle.com/documentation/

HELP|? [topic]
SQL> ? index

Enter Help [topic] for help.

@ COPY PAUSE SHUTDOWN
@@ DEFINE PRINT SPOOL
/ DEL PROMPT SQLPLUS
ACCEPT DESCRIBE QUIT START
APPEND DISCONNECT RECOVER STARTUP
ARCHIVE LOG EDIT REMARK STORE
ATTRIBUTE EXECUTE REPFOOTER TIMING
BREAK EXIT REPHEADER TTITLE
BTITLE GET RESERVED WORDS (SQL) UNDEFINE
CHANGE HELP RESERVED WORDS (PL/SQL) VARIABLE
CLEAR HOST RUN WHENEVER OSERROR
COLUMN INPUT SAVE WHENEVER SQLERROR
COMPUTE LIST SET XQUERY
CONNECT PASSWORD SHOW
SQL> ? get

GET

Loads a SQL statement or PL/SQL block from a script into the SQL buffer.
In iSQL*Plus click the Load Script button to load a script into the
Workspace. The buffer has no command history list and does not record
SQL*Plus commands.

GET [FILE] file_name[.ext] [LIST | NOLIST]

Not available in iSQL*Plus
SQL> ? clear

CLEAR
—–

Resets or erases the current value or setting for the specified option.

CL[EAR] option …

where option represents one of the following clauses:
BRE[AKS]
BUFF[ER]
COL[UMNS]
COMP[UTES]
SCR[EEN]
SQL
TIMI[NG]

CLEAR SCREEN is not available in iSQL*Plus
SQL>

Posted in Scripts | Tagged: , | Leave a Comment »

SQL*PLUS change buffer command

Posted by Alex Lima on January 23, 2008

There are some cool SQL*PLUS command to help speed up your typing. Today I am showing the change and append buffer in the command line.

C:\oraclexe\app\oracle\product\10.2.0\server\BIN>sqlplus /nolog

SQL*Plus: Release 10.2.0.1.0 – Production on Wed Jan 23 23:04:08 2008

Copyright (c) 1982, 2005, Oracle. All rights reserved.

SQL> conn / as sysdba
Connected.
SQL>

#To change the buffer in the command line

SQL> select file_name from dba_data_files;

FILE_NAME
——————————————————————————–

C:\ORACLEXE\ORADATA\XE\USERS.DBF
C:\ORACLEXE\ORADATA\XE\SYSAUX.DBF
C:\ORACLEXE\ORADATA\XE\UNDO.DBF
C:\ORACLEXE\ORADATA\XE\SYSTEM.DBF

SQL> c/data/temp
1* select file_name from dba_temp_files
SQL> /

FILE_NAME
——————————————————————————–

C:\ORACLEXE\ORADATA\XE\TEMP.DBF

## To append the buffer in the command line

SQL> select file_name from dba_data_files;

FILE_NAME
———————————————————————-

C:\ORACLEXE\ORADATA\XE\USERS.DBF
C:\ORACLEXE\ORADATA\XE\SYSAUX.DBF
C:\ORACLEXE\ORADATA\XE\UNDO.DBF
C:\ORACLEXE\ORADATA\XE\SYSTEM.DBF

SQL> a where file_name like ‘USE%’;
1* select file_name from dba_data_files where file_name like ‘USE%’
SQL> /

no rows selected

SQL> c/USE/%USE
1* select file_name from dba_data_files where file_name like ‘%USE%’
SQL> /

FILE_NAME
———————————————————————-

C:\ORACLEXE\ORADATA\XE\USERS.DBF

SQL>

Posted in Scripts | Tagged: , , | Leave a Comment »