Wednesday, October 19, 2005

Killing Oracle Sessions

Sessions can be killed from within oracle using the ALTER SYSTEM KILL SESSION syntax.

First identify the offending session as follows:

SELECT s.sid,
s.serial#,
s.osuser,
s.program
FROM v$session s;

SID SERIAL# OSUSER PROGRAM
---------- ---------- ------------------------------ ---------------
1 1 SYSTEM ORACLE.EXE
2 1 SYSTEM ORACLE.EXE
3 1 SYSTEM ORACLE.EXE
4 1 SYSTEM ORACLE.EXE
5 1 SYSTEM ORACLE.EXE
6 1 SYSTEM ORACLE.EXE
20 60 SYSTEM DBSNMP.EXE
43 11215 USER1 SQLPLUSW.EXE
33 5337 USER2 SQLPLUSW.EXE

The SID and SERIAL# values of the relevant session can then be substituted into the following statement:

SQL> ALTER SYSTEM KILL SESSION 'sid,serial#';

In some situations the Oracle.exe is not able to kill the session immediately. In these cases the session will be "marked for kill". It will then be killed as soon as possible. If the marked session persists for some time you may consider killing the process at the operating system level.
The NT Approach
To kill the session via the NT operating system, first identify the session as follows:

SELECT s.sid,
p.spid,
s.osuser,
s.program
FROM v$process p,
v$session s
WHERE p.addr = s.paddr;

SID SPID OSUSER PROGRAM
---------- --------- ------------------------------ ---------------
1 310 SYSTEM ORACLE.EXE
2 300 SYSTEM ORACLE.EXE
3 309 SYSTEM ORACLE.EXE
4 299 SYSTEM ORACLE.EXE
5 302 SYSTEM ORACLE.EXE
6 350 SYSTEM ORACLE.EXE
20 412 SYSTEM DBSNMP.EXE
43 410 USER1 SQLPLUSW.EXE
33 364 USER2 SQLPLUSW.EXE

The SID and SPID values of the relevant session can then be substituted into the following command issued from the command line:

C:> orakill ORACLE_SID spid

The session thread should be killed immediately and all resources released.
The UNIX Approach
To kill the session via the UNIX operating system, first identify the session in the same way as the NT approach, then substitute the relevant SPID into the following command:

% kill -9 spid

If in doubt check that the SPID matches the UNIX PROCESSID shown using:

% ps -ef | grep ora

The session thread should be killed immediately and all resources released.

see the link http://www.oracle-base.com/articles/8i/KillingOracleSessions.php for more information.

Monday, September 12, 2005

Java Modulo

When the following method is executed it determines the values of add or even only for
+ve integers and not for -Ne integers. Because when we do modulo of -Ve odd numbers we get a remainder of -1 and not 1.

public static boolean isOdd(int i) {
return i%2==1;
}

Wednesday, August 17, 2005

Unix Shell Script Execution

To execute unix shell scripts if the following error occurs
bash: mailnimmi: command not found
where mailnimmi is the name of the script then we need to execute the file with the following command.
./mailnimmi

Thursday, July 14, 2005

Oracle External Tables

In oracle External Tables can be used to read data from external flat files. The flat file(s) is mapped to the external table in the oracle schema. The following is an example to create an external table which is mapped to two flat files emp_ext1.dat and emp_ext2.dat which are present in the directory defined by ext_tables.
CREATE TABLE emp_ext
(
empcode NUMBER(4),
empname VARCHAR2(25),
deptname VARCHAR2(25),
hiredate date
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY ext_tables
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
)
LOCATION ('emp_ext1.dat','emp_ext2.dat')
)
REJECT LIMIT UNLIMITED;


A directory can be defined in oracle using the following command.
CREATE OR REPLACE DIRECTORY EXT_TABLES AS 'C:\Nirmala';

All the error messages corresponding to External Tables can be found at this site
http://www.cs.utah.edu/classes/cs5530/oracle/doc/B10501_01/server.920/a96525/kupus.htm

More about External Tables can be found on the oracle pl/sql online documentation at
http://www.cs.utah.edu/classes/cs5530/oracle/doc/B10501_01/index.htm

Friday, April 08, 2005

Compiling invalid objects

The following command can be used to compile all invalid objects in a schema.
exec dbms_utility.compile_schema( 'SCHEMA NAME');

to re compile a proceedure individually you can use the folloeing:
alter procedure proc_name compile;

Thursday, January 20, 2005

Threads Learning

When we extend thread we can start the thread from outside where as if we implement runnable interface we need to start the thread inside the constructor of the implementing class.

Saturday, January 15, 2005

Working with java.io

This program displays all the subdirecotries and files in a given directory.



import java.io.*;

/*
*Program to list all the directories and files
under the given directory
*/


public class DirFiles{


public static void main(String[] args) {
File fil = new File(args[0]); //Input directory
from the command prompt

DirLevel dirlvl = new DirLevel(fil , 0);
checkIfDirectory(dirlvl );
}

//Recursive Method to get all the
files and directories under a given directory
static void checkIfDirectory(DirLevel fname){


for(int k=0;k<fname.level;k++){
System.out.print(" ");
}


if(fname.filename.isDirectory()) {
File filarray[] = fname.filename.listFiles();

System.out.println(fname.filename.getName());

for(int i=0;i<filarray.length;i++){


DirLevel dir1 = new DirLevel(filarray[i] , fname.level+1);
checkIfDirectory(dir1 );

}
}
else {

System.out.println(fname.filename.getName());
}

}


}


/*
*This calss is defined to determine the level
from the parent directory
*/


class DirLevel {
File filename ;
int level;
DirLevel(File flname , int lvl){
filename = flname;
level = lvl;

}
}



************************************************************************************
************************************************************************************
************************************************************************************




import java.io.*;

/*
*Program to list all the directories and files under the given
directory. This also gives the number of lines present in each file.
*/


public class DirFilesCount {


public static void main(String[] args) {
File fil = new File(args[0]);
DirLevel dirlvl = new DirLevel(fil , 0);


checkIfDirectory(dirlvl );
}

//Recursive Method to get all
the directories and files and their line count, under a given directory

static void checkIfDirectory(DirLevel fname){

for(int k=0;k<fname.level;k++){

System.out.print(" ");
}


if(fname.filename.isDirectory()) {

File filarray[] = fname.filename.listFiles();
System.out.println(fname.filename.getName());

for(int i=0;i<filarray.length;i++){

DirLevel dir1 = new DirLevel(filarray[i] , fname.level+1);
checkIfDirectory(dir1 );
}
}

else {


BufferedReader buffreader=null ;

//Try catch block to count
the no number of line of all the files and print their names as well as thier count.
try{

buffreader = new BufferedReader(new FileReader(fname.filename));

int linecount=0;
String line;

do {
line = buffreader.readLine();

if(line!=null) {

linecount++;
}

}while(line!=null);

System.out.println(fname.filename.getName()+" ---------->" + linecount+" lines");


}catch (IOException e){
System.out.println("the follwing error occured"+e);
}
finally{
if(buffreader!= null){

try{
buffreader.close();
}catch (IOException e){
System.out.println("Error while closing the file "+e);
}
}

}
}
}
}



************************************************************************************
************************************************************************************
************************************************************************************



import java.io.*;

/*
*Program to list all the directories and files under the given
directory. This also gives the number of lines present in each file.
*/


public class DirFilesCount {


public static void main(String[] args) {
File fil = new File(args[0]);
DirLevel dirlvl = new DirLevel(fil , 0);


checkIfDirectory(dirlvl );
}

//Recursive Method to get all the directories and files and their line count,
under a given directory

static void checkIfDirectory(DirLevel fname){

for(int k=0;k<fname.level;k++){

System.out.print(" ");
}

if(fname.filename.isDirectory()) {

File filarray[] = fname.filename.listFiles();
System.out.println(fname.filename.getName());

for(int i=0;i<filarray.length;i++){

DirLevel dir1 = new DirLevel(filarray[i] , fname.level+1);
checkIfDirectory(dir1 );
}
}

else {


BufferedReader buffreader=null ;

//Try catch block to count
the no number of line of all the files and print their names as well as thier count.
try{

buffreader = new BufferedReader(new FileReader(fname.filename));

int linecount=0;
String line;

do {
line = buffreader.readLine();

if(line!=null) {

linecount++;
}

}while(line!=null);

System.out.println(fname.filename.getName()+" ---------->" + linecount+" lines");


}catch (IOException e){
System.out.println("the follwing error occured"+e);
}
finally{
if(buffreader!= null){

try{
buffreader.close();
}catch (IOException e){
System.out.println("Error while closing the file "+e);
}
}

}
}
}
}


Saturday, January 08, 2005

Query to find the day ina date

The following query can be used to get the day of any date

SELECT TO_CHAR(TO_DATE('01-JAN-05','DD-MON-RR'),'Dy') as day FROM dual;


Friday, January 07, 2005

INSTR function

The instr function can be used to find if the given string is present in a particular column or not.
Example for this is given below.

SQL> SELECT ename
FROM emp
WHERE instr(ename , 'LL')>0;

ENAME
----------
ALLEN
MILLER

Using INSTR will be much faster than using like in these kind of scenarios.


variable refcursor

Variabel can be declared as reference cursor in sql and record sets can be stored into it. A simle exapmple is shown below.


SQL> variable test_cur refcursor
SQL> declare
2 v_sql varchar2(3000);
3 begin
4 v_sql:='select * from client_pricing order by 2,1';
5 open :test_cur for v_sql;
6 end;
7 /

PL/SQL procedure successfully completed.

SQL> print test_cur

FUNCTION_ID CLIENT_ID PRICING
----------- ---------- ----------
1 10 44
1 10 22
1 20 33
2 20 33
1 30 33
2 30 55
1 40 22
2 40 77
1 50 44
2 50 99

10 rows selected.

SQL> spool off

Tuesday, January 04, 2005

Restrictions on LONG Datatype

The following are the restrictions on the LONG datatype:
1. There can be only one column of datatype LONG in a table.
2. You can not use the LONG datatype column for GROUP BY, ORDER BY, WHERE or CONNECT BY clauses.
3. Character functions can not be applied to LONG columns.

Note:PL/SQL LONG varoabel is different from LONG datatype for columns.

Monday, January 03, 2005

Auto Trace

To set autotrace option run the file @D:\Oracle\Ora81\RDBMS\ADMIN\UTLXPLAN.SQL.

This sql creates the required table for setting up auto trace.

update scott.up_ml set cl3=null
where rowid in ( select rid
from ( select b.rowid rid,
row_number() over
(partition by cl1,cl2,cl3 ORDER BY cl1,cl2,cl3) rn
from scott.up_ml b
)
where rn <> 1 )


update scott.up_ml a set cl3 = null where rowid > (select min(rowid) from scott.up_ml b where a.cl1=b.cl1 and b.cl2=b.cl2);


Auto trace outputs

Execution Plan
----------------------------------------------------------
0 UPDATE STATEMENT Optimizer=CHOOSE
1 0 UPDATE OF 'UP_ML'
2 1 NESTED LOOPS
3 2 VIEW OF 'VW_NSO_1'
4 3 SORT (UNIQUE)
5 4 VIEW
6 5 WINDOW (SORT)
7 6 TABLE ACCESS (FULL) OF 'UP_ML'
8 2 TABLE ACCESS (BY USER ROWID) OF 'UP_ML'




Statistics
----------------------------------------------------------
0 recursive calls
4 db block gets
8 consistent gets
0 physical reads
860 redo size
619 bytes sent via SQL*Net to client
689 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
3 rows processed



*************************************************************************************

Execution Plan
----------------------------------------------------------
0 UPDATE STATEMENT Optimizer=CHOOSE
1 0 UPDATE OF 'UP_ML'
2 1 FILTER
3 2 TABLE ACCESS (FULL) OF 'UP_ML'
4 2 SORT (AGGREGATE)
5 4 TABLE ACCESS (FULL) OF 'UP_ML'




Statistics
----------------------------------------------------------
0 recursive calls
4 db block gets
12 consistent gets
0 physical reads
808 redo size
621 bytes sent via SQL*Net to client
618 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
3 rows processed