Saturday, October 16, 2010
ORA 12154 error in windows 7
Wednesday, March 10, 2010
NUMBER data type in oracle PL SQL
The Syntax is as follows:
NUMBER(precision,scale)
where precision is the total number of digits and scale is the number of digits to the right of decimal point.
The maximum precision of a number is 38 digits. If the precision is not mentioned it defaults to 38.
The scale ranges from -84 to 127. The scale determines the rounding of decimal digits.
More information can be found in the following link:
http://download.oracle.com/docs/cd/B10500_01/appdev.920/a96624/03_types.htm
Wednesday, February 24, 2010
Display the plan of query in SQL*plus/sqlplus
SET AUTOTRACE ON
SQL> set autotrace on
SQL> select count(*) from employee;
COUNT(*)
----------
54405
Execution Plan
---------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=1683 Card=1)
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (FULL) OF 'EMPLOYEE' (TABLE) (Cost=1683 Card=54
276)
Statistics
---------------------------------------------------------
1 recursive calls
0 db block gets
7682 consistent gets
7625 physical reads
0 redo size
534 bytes sent via SQL*Net to client
664 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
The SET AUTOTRACE OFF will turn off the display of plan.
Display elpased time of a query in SQL*plus/sqlplus
SQL> set timing on
SQL> select count(*) from employee;
COUNT(*)
----------
155667
Elapsed: 00:00:00.21
SQL>set timing off
The set timing on will display the elapsed time and the set timing off won’t display the elapsed time.
Thursday, February 18, 2010
Read a csv file from unix shell script
The csv file contains the following data
100,TOM,Manager
200,Rachel,CEO
The script will read all the columns by considering the field separator as comma.I've specified the IFS as ','.
/************Start of the scipt **********************/
function readfile {
FILENAME="$1"
exec 8<$FILENAME
while read -u8 line
do
IFS=','
set $line
echo $2
done
}
readfile emp.csv
/************End of the scipt **********************/
The function readfile will accept a input parameter file name and it read the second column in the file. the script should be customized according to the requirements
Tuesday, February 16, 2010
Connect to SQL*PLUS(sqlplus) using shell script
function Execsql
{
STATEMENT="$*"
echo $STATEMENT
sqlplus -s "username/pwd@sid" > /dev/null << END
spool result.out
$STATEMENT;
quit
END
}
Execsql "Select sysdate from dual"
The function Execsql will accept an argument which will be executed by connecting to the DB.
The result will be spooled in result.out file.
The label END should be properly alligned.
Sunday, December 20, 2009
Oracle user's session privileges
What we can do as per our privileges. Most of the time we work without knowing our capabilties.
The SESSION_PRIVS will show us the assigend privileges to that user.
SQL > SELECT * FROM SESSION_PRIVS;
PRIVILEGE
1 CREATE SESSION
2 ALTER SESSION
For that user those are the assigend privileges.
Saturday, November 28, 2009
Rename a column name in oracle
Lets say the EMPLOYEE table has the following structure
E_name varchar2(100)
E_no number
I need to change the E_no as Employee_no then
ALTER TABLE EMPLOYEE RENAME COLUMN E_no as Employee_no
I think there's no way to rename multiple columns in a single statement
Tuesday, October 27, 2009
Granting Debugging priviliges in oracle
In test mode the stored programs can be debugged step by step. To do that the user should have Debugging priviliges.
The command for granting debugging priviliges is
GRANT DEBUG ANY PROCEDURE , DEBUG CONNECT SESSION TO USER_NAME
Tuesday, October 13, 2009
Unlock an user account in oracle
But to check my luck I've logged into Database as another user and executed the following command
ALTER USER USER_NAME ACCOUNT UNLOCK
As the priviliges were there the account got unlocked.
Thursday, October 8, 2009
Checking the existence of a user in oracle database
SELECT * FROM ALL_USERS
OR
SELECT * FROM DBA_USERS
Thursday, September 10, 2009
How rownum works in oracle
But it started haunting me. So I googled it and found the answer.
The ROWNUM will be initialized to one before execution of query.
Oracle always increment the rownum value after fetching every row.
So the select * from employees where rownum <=2 works whereas the >=2 or >2 or =2 won’t work.
The explanation I found from http://www.oracle.com/
Code snippet from http://www.oracle.com/:
select * from emp where ROWNUM <= 5 order by sal desc;
The above query will execute like
ROWNUM = 1
for x in ( select * from emp )
loop
exit when NOT(ROWNUM <= 5)
OUTPUT record to temp
ROWNUM = ROWNUM+1
end loop
SORT TEMP
It’s a pretty good explanation about rownum
http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
Friday, July 31, 2009
Query to find locked objects in oracle
The following query will find the objects which have been locked by the user.
The query will return session id which can be used to alter the session which can release the lock.
SELECT SESS.OSUSER,
OBJ.OBJECT_NAME, SESS.SID,
sess.SERIAL#,SESS.USERNAME, SESS.MACHINE, SESS.TERMINAL, SESS.PROGRAM FROM V$LOCKED_OBJECT LO,V$SESSION SESS,DBA_OBJECTS OBJ WHERE LO.SESSION_ID=SESS.SID AND LO.OBJECT_ID=OBJ.OBJECT_ID
Then the session can be killed by the following alter command
ALTER SYSTEM KILL SESSION ‘sid, serial#’
Sid and serial# should be taken from the above query to find the locked objects
Thursday, July 30, 2009
Materialized view log
The syntax for creating materialized view log is
CREATE MATERIALIZED VIEW LOG on TABLE_NAME
Once the log is created then we can create materialized view with fast refresh option.
For more idea about it ,please cisit the following site:
http://stanford.edu/dept/itss/docs/oracle/10g/server.101/b10759/statements_6003.htm
Saturday, July 25, 2009
Refrshing a materialized view in oracle
BEGIN
DBMS_SNAPSHOT.REFRESH('VIEW_NAME','c');
END;
The first parameter is the view's name and the second parameter is the refresh option.
c - complete refresh
f - fast refresh
If you exexcute the above block the view will be refreshed.
Wednesday, July 22, 2009
Load images using SQL loader:
load data
infile image.csv
into table image1
append
fields terminated by ','
(
imageid,
filename,
image_data lobfile(filename) terminated by eof
)
The infile will mention the source data what’s the name of the file to be loaded and it should look like this :
1,image010.jpg
2,image020.jpg
The following command should be used to load the images:
Sqldr username/password@DB control=image.ctl
ORA-12014 error while creating materialized view

The error would be ‘ORA-12014 ‘
To overcome this error ‘WITH ROWID’ option should be used along with the refresh option.
Then you can create the materialized view.
CREATE MATERIALIZED VIEW VIEW_NAME
REFRESH COMPLETE ON DEMAND
WITH ROWIDASSELECT * FROM TABLE_NAME
Sunday, March 8, 2009
AWR report creation
Automatic workload Repository(AWR) help us in analyzing the SQL queries. We've our system in unix and this AWR report help us in analyzing the queries which have been executed in the system.
The following are the steps to create an awr report:
1) Login to the unix system using username and password.
2) sqlplus username/password@dbname (ex : scott/tiger@SYS)
3) Then execute the following command :
exec dbms_workload_repository.create_snapshot();
The above command will create a snapshot at time X.
4) After the monitoring time again execute the above command to create the snapshot at time Y.Now we've to two snapshot to create the report.
5) Execute the following command to create the report:
@?rdbms/admin/awrrpt.sql
6) On excecuting the above command a prompt screen will come to enter the number of days to show the snapshot id. Type it as 1 On pressing enter the snapshot ids that have been taken today will be displayed on the screen.
7)Then type of report to be generated has to be entered .Type it as html and press enter.
8) Then we've to enter the snapshot id. Enter the snapshot id as displayed on the screen.
9)Then enter the output file name. The output will be created as ouputfile.lst. The The output format would be in "lst" format. ftp the file to desktop and change the extension to html.Then the report can be view in internet explorer.
Tuesday, February 19, 2008
Query to create sequence of the following pattern 1,2 ,1,2,1,2… :
The following query will create sequence that will generate 1 and 2 continuously.
CREATE SEQUENCE seq1 START WITH 1 INCREMENT BY 1 MAXVALUE 2 CYCLE NOCACHE.
If the cache value is to mention in the statement, then it should satisfy the following two conditions:
1) The cache value should be greater than 1.
2) The cache value should be less than (MAXVALUE – MINVALUE).

