Wednesday, 23 November 2011

Accessing Oracle from Java

Introduction
You’re writing a Java program and you want to access a database. What should you use? JDBC
and/or SQLJ. These are both APIs (or drivers or packages… whatever you want to call them) used
to access a database. Why two API’s rather than one? The simple answer is that each one is better
for specific situations. Actually, you only need JDBC, but as we’ll see later on in this paper, you’ll
want to use SQLJ as well.
JDBC is an vendor-independent standard developed by Javasoft which was modeled after ODBC
(developed by Microsoft). It stands for Java Database Connectivity. JDBC is today the
de facto
method for accessing a database from Java. Several vendors, including Oracle, Intersolv, and
Weblogic develop several types (the various Oracle drivers will be explained in more detail a few
paragraphs from now) of JDBC drivers. Oracle’s, of course, are the best (I’m the product manager
for Oracle’s JDBC drivers so it is my duty to brainwash you).
SQLJ is a new standard for embedding SQL directly into Java programs. It is the product of a joint
standards effort by Oracle, IBM, Sybase, Tandem, and Informix. For those you who are familiar
with Pro*C (embedded SQL in C), you can view SQLJ as Pro*Java. Why isn’t it called Pro*Java
then? Pro*anything implies an Oracle-specific technology (Pro*prietary?) while SQLJ is an open
standard. As we’ll see later in this paper, SQLJ simplifies the task of writing, managing, and
debugging database-backed Java applications to the point that it is almost pleasurable.
JDBC
The guts of a JDBC program
Before delving into JDBC any deeper, it’s important that you take a look and understand how a
simple JDBC program works.
import java.sql.*;
public class JDBCExample {
public static void main(String args[]) throws SQLException {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn=
DriverManager.getConnection("jdbc:oracle:thin:@dlsun137:5521:sol1",
" scott", "tiger");
Statement stmt = conn.createStatement();
ResultSet rs =
stmt.executeQuery("select ename, empno, sal from emp");
while(rs.next()){
String name= rs.getString(1);
int number = rs.getInt(2);
double salary = rs.getDouble(3);
System.out.println(name+" "+number+" "+salary);
}
rs.close();
conn.close();
}
}
The above example is a complete Java program. If you understand how it works, you’re well on
your way to becoming a JDBC expert. The first line in the main method of the is:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
All you really have to know is that this call lets JDBC know that you are using Oracle’s drivers.
The next call is:
Connection conn = DriverManager.getConnection(
"jdbc:oracle:oci8:@server:1521:orcl",“scott”, “tiger”);
As you may have guessed, this call creates a connection
conn to a database. The
jdbc:oracle:oci8
driver is will be explained in the next section) JDBC driver to establish the connection and
communicate with the database. The
connect to the
Finally, the connection is being made to the schema
Now that the connection is made, we’re ready to make SQL queries to the database. In the example,
a query is made by the line:
part of the connect string says to use an oracle OCI-based (what an OCIbased@server:1521:orcl part of the string specifies toorcl database instance on machine server whose listener is on port 1521.scott with the password tiger.
ResultSet rs = stmt.executeQuery(“select ename, empno, sal from emp”);
The query string “select ename, empno, sal, from emp” queries the ename, empno, and sal columns
from the table emp. The executeQuery method of the connection object passes the query string to
SQL. If you were to type this string directly to a SQLPlus or Server Manager prompt you would get
something like:
ENAME EMPNO SAL
--------------- --------------- ---------------
SCOTT 12 12000
ADAM 2 10000
. . .
. . .
. . .
LESLIE 9 15000
In Java, this “result” will be stored in a
“result” is accessed by the following code:
ResultSet object (rs in this example). The data in this
while(rs.next())
{
String name = rs.getString(1);
int number = rs.getInt(2);
double salary = rs.getDouble(3);
System.out.println(name+” “+number+” “+salary);
}
The
while loop loops through the “result” rs row by row. If there are any rows left, the call to
rs.next()
return
result set would point to the row:
returns true and “points” rs to the next row. Otherwise, rs.next() wouldfalse and the while loop would terminate. So, after the first call to rs.next(), the
SCOTT 12 12000
and when the result set points to the row:
LESLIE 9 15000
the next call to
Once in a given row, the
data (where XXX can be most any Java type). In the above code, the methods
rs.next() would return false.getXXX methods of the result set can be used to actually access the SQLgetString,
getInt
to map the corresponding SQL type to. In the above example, the data in the
accessed using the call
set and maps it to a Java
the data in the column would be of type
accessed using the call
result set and maps it to a Java
would be of type
(which would be sal in this example) and maps it to a Java double.
The while loop in this example outputs the following:
, and getDouble are used. The XXX part of the getXXX lets JDBC know what Java typeename column isrs.getString(1). This call gets the data in the first column of the resultString. This is a natural mapping for the ename column because in SQL,VARCHAR2. Likewise, the data in the empno column isrs.getInt(2). This call gets the data from the second column of theint. Once again, this makes sense since the corresponding SQL dataNUMBER. Finally, the call rs.getDouble(3) gets the data from the third column
SCOTT 12 12000
ADAM 2 10000
.
.
.
LESLIE 9 15000
The last lines of the program,
done with the JDBC result set and connection and that the database connection and corresponding
session should be ended.
As I said before, if you have a decent understanding of the above code example, you are ready to
write JDBC. Everything else in this paper from here on out is just icing on the cake.
rs.close() and conn.close(), lets JDBC know that you are
Oracle’s JDBC Drivers
Now that you have a reasonable understanding of what JDBC is and how it can be used, let’s focus
on Oracle’s JDBC offering. Oracle offers three types of JDBC drivers: a 100% Java thin driver, an
OCI-based fat driver, and a server-side (a.k.a. KPRB) driver. All three of these drivers currently
comply with the JDBC 1.2.2 specification and are well on their way to complying with JDBC 2.0 as
well. In addition, the drivers are compatible with JDK 1.0.2, and 1.1.6. The thin and fat JDBC
drivers can be used against Oracle7.2 and higher databases. Finally, all three drivers use identical
API’s, thus providing maximum flexibility in how JDBC applications can be deployed.
As already mentioned, Oracle’s thin driver is a 100% Java implementation. Because of this, it is ideal
for applications that need to be 100% Java such as applets. Another benefit of the thin driver is that,
at only 300K (150K compressed), it is extremely small in size making it even more attractive for use
in downloadable applications such as applets. In terms of performance, the thin driver is slightly
slower than its OCI-based big brother (this should be expected since the OCI-based driver does a lot
of the data marshaling in C).
Oracle’s fat driver is a JDBC layer implemented on top of the Oracle Call Interface (OCI). OCI is a
C library for accessing Oracle databases. Because the fat driver needs OCI to be pre-installed, it
cannot be used for 100% Java and/or downloadable applications. This driver is ideal, however, for
use in middle-tier applications such as Java servlets.
Oracle’s server-side driver is the new addition to the family. Now that Oracle8
applications to run inside the database, these applications need some way to actually access the data
in the database (the main reason to run Java in the database is if the program deals a lot with data).
The server-side drivers were created with this use in mind. Because these drivers “know” that the
code is in the database, they don’t have to explicitly open up a connection. Also, they completely
avoid latency due to the network or interprocess communication by directly accessing SQL data (as
you may already know, Java in Oracle8
instance).
As touched on earlier in this section, all three of the aforementioned drivers use the same API. This
gives Java/JDBC developers a great deal of flexibility in how they deploy their Java applications.
For example, say a client-server application is written using the JDBC-OCI driver. In this case the
client would be the combination of the Java application and the driver and the server would be an
Oracle database. Then Oracle8
performance gains if she runs the data-intensive pieces of her application within the database. This
task would literally be as simple as loading the data intensive Java classes into the database and
changing the lines that establish connections to:
i allows Javai runs in the same memory and process space as the databasei comes out, and the developer realizes that she might get significant
Connection conn =
new oracle.jdbc.driver.OracleDriver(). defaultConnection();
Likewise, the developer could instead decide that she would like to deploy her application as a Java
applet. In this case, the only JDBC modification she would have to make is to specify to use the thin
rather than OCI-based driver in the connect string. In today’s world, this flexibility cannot be
overemphasized. It allows developers to be confident that their application can quickly and easily be
adapted to the “deployment configuration of the month.”
JDBC is not perfect
Before you call your mother and let her know about all the virtues of JDBC (which have been so
expertly taught to you) let me warn you that it does have some drawbacks. First and foremost,
errors in SQL statements can only be caught at runtime. This is because the Java compiler used to
compile the JDBC code has no knowledge of SQL syntax or semantics. Second, writing complex
SQL statements in JDBC can get messy. This is illustrated in the following code:
String name="SALMAN";
int id=37;
float salary=2500;
PreparedStatement stmt = conn.prepareStatement
("insert into emp (ename, empno, sal) values (?, ?, ?)");
stmt.setString (1, name);
stmt.setInt(2, id);
stmt.setFloat(3, salary);
stmt.execute ();
stmt.close ();
This first three lines just define three Java variables
name, id, and salary whose values are
“SALMAN”
statement. A prepared statement should be used whenever values within a SQL statement need to
by dynamically set (the same prepared statement can be used over and over with different variable
values). The
, 37, and 20000 respectively. The next line then defines something call a prepared“?”s in this prepared statement can be viewed as placeholders for Java variables. The
“?”
s are then given values in the lines:
stmt.setString (1, name);
stmt.setInt(2, id);
stmt.setFloat(3, salary);
The first
“?” in the prepared statement is set to the Java variable name (which holds the String
“SALMAN”
third
statement example, as promised, is messy and the SQL statement that it executes is not even that
complicated. I’m sure you could imagine the complexity of multi-line SQL calls which use many
more Java variables.
This example begs the question, “is there anything you can do to make SQL queries in Java that 1)
are easier to write and read and 2) that can be debugged before runtime?”
ANSWER: Use SQLJ
). The second “?” is set to the int variable id (has a value of 37115) . Finally, the“?” is set to the float variable salary (which has a value of 20000). This prepared
SQLJ
To give you a very quick feel for what SQLJ looks like and why it is so good, let’s take a look at a
snippet of SQLJ that performs the exact same SQL call as the “messy” JDBC example of the last
section.
String name=“SALMAN”;
int id=37;
float salary=20000;
#sql {insert into emp (ename, empno, sal) values (:name,:id,:salary)};
At a first glance, it is easy to see that this SQLJ code is significantly simpler that the JDBC
equivalent. This mainly stems from the fact that SQLJ allows for Java bind variables and
expressions (preceded by a “:”) to be embedded within SQL statements. In the above example the
Java variables
SQLJ also addresses the issue of debugging SQL statements before runtime. Because SQLJ code is
not pure Java syntax, it needs to be run through a translator before it can be compiled to Java
bytecode (this is outlined in more detail in the next section). This translation step allows embedded
SQL statements to be analyzed both semantically and syntactically, thus catching SQL errors at
translate time rather than runtime (as is the case with JDBC).
At this point you’re probably thinking, “If SQLJ addresses both of the drawbacks of JDBC, why do I
even need to know about JDBC?” There are several reasons why you should know about both.
First, they are complementary APIs. SQLJ and JDBC can and should be used together. JDBC
must be used for dynamic SQL (when the SQL statements are generated dynamically at runtime)
while SQLJ is ideal for static SQL (when the shape of the SQL query is known at application
development time).
Second, Oracle’s current incarnation of SQLJ runs on top of JDBC. This allows SQLJ to be
deployed in any configuration as JDBC (thin, fat, or server-side) but it also means that the SQLJ
developer needs to know about the various types of JDBC drivers.
name, id, and salary are directly embedded in the SQL statement.
Developing a SQLJ Application
There are three steps to build a SQLJ application:
1. Write the SQLJ program
2. Translate the SQLJ program.
3. Run the SQLJ program.
(Take a minute to and try to memorize these steps. It may seem overwhelming at first, but I think
you’re smart enough to handle this level of complexity.)
Writing a SQLJ program
Just as we did with JDBC, let us take a look at the pieces of an actual SQLJ program:
import java.sql.*;
import sqlj.runtime.ref.DefaultContext;
import oracle.sqlj.runtime.Oracle;
#sql iterator MyIter (String ename, int empno, float sal);
public class MyExample
{
public static void main (String args[]) throws SQLException
{
Oracle.connect
("jdbc:oracle:thin:@oow11:5521:sol2", " scott", "tiger");
#sql { insert into emp (ename, empno, sal)
values ('SALMAN', 32, 20000) };
MyIter iter;
#sql iter={ select ename, empno, sal from emp };
while (iter.next()) {
System.out.println
(iter.ename()+" "+iter.empno()+" "+iter.sal());
}
}
}
The first line of the main method is:
Oracle.connect("jdbc:oracle:thin:@oow11:5521:sol2", " scott", "tiger");
Step 1:
Write SQLJ program
Step 2:
Translate & compile program .sqlj .class
Step 3:
Run program
This call establishes a default database connection for SQLJ to use.
Now we’re at the exciting part! We’re ready to write a simple, easier-than-JDBC, SQLJ clause. The
first clause is:
#sql { insert into emp (ename, empno, sal) values
('SALMAN', 32, 20000)};
This SQLJ code is doing exactly what you think it is; it’s inserting a row into the emp table.
The next clause is:
#sql miter={select ename, empno, sal from emp};
As you may have guessed, this statement is simply making a
“Wait! I see a query, but I don’t see a result set to put the data into. Where does the data from the
query go?” For your information, we don’t use result sets in SQLJ; we use
visualize an iterator the same way as a result set. The main difference between the two is that a
given iterator can only store the output of a specific type of query while a result set can store the
output of any query. Because of this, iterator types need to be defined for different types of queries.
In the above example, the following line defines an iterator type called
select query. So now you ask,iterators. You shouldMyIter:
#sql iterator MyIter (String ename, int empno, float sal);
By looking at this definition, we can see that this type of iterator can store results whose first column
can be mapped to a Java
third row can be mapped to a Java
String, whose second column can be mapped to a Java int, and whosefloat. This definition also names these three rows ename,
empno
positional iterators). An instance of
, and sal respectively (MyIter is a named iterator. See the documentation to learn aboutMyIter is then created by the line:
MyIter miter;
Finally, the miter instance is populated with data in the select query.
Now that we have a populated iterator, we need to actually access the data in it. This happens in the
following snippet of code:
while (miter.next())
{
System.out.println(miter.ename()+" "+miter.empno()+" "+miter.sal());
}
This
while loop pretty much does the same thing as the while loop in the JDBC example. The
next()
is not at the last row of the data, it returns
Otherwise, it returns
different then how it is done for a result set. The iterator in the example has methods which
correspond to the names of the columns in the iterator. By calling these methods, the data in that
column is returned. As you may remember, we had to use the
object to do the same thing.
At this point, I believe congratulations are in order. Assuming that you have diligently read the last
few paragraphs, you are ready to run home an be a SQLJ-head (Be proud! I consider myself a SQLJhead
so you’re in excellent company). The rest of the paper is downhill from here.
method of an iterator plays the same role the next() method of a result set. If the iteratortrue and points the interator to the next row.false. How the data is accessed from a given row in an iterator is slightlygetXXX() methods of a result set
Translating and Compiling a SQLJ program
Let’s take a look at how difficult it is to translate and compile a SQLJ program:
>sqlj MyExample.sqlj
There are a host of options which can be specified in addition to the arguments given above, but the
above call is enough.
Translating and compiling a SQLJ application is so simple that it almost doesn’t deserve its own
section. The only reason why it does is that what the translator does for you is really quite
interesting.
While translating a .sqlj file into a .java file., the SQLJ translator actually checks to see if your
embedded-SQL makes sense. It analyzes your code from a syntactic and semantic point of view. It
can even check to see if the tables, columns, and Java types that you are using are appropriate. How
does it do this? It actually connects to an exemplar (fancy word for example) database schema. This
schema doesn’t have to have identical data to the one that the program will eventually run against,
but the tables in it should have the same shapes and names (If you want the translator to connect to
an exemplar database, you have to specify a URL of a database to connect to along with the
appropriate username and password).
Besides translate-time SQL checking, the translator also generates a serialized object file (ends with
the .ser extension). This file contains a description of the SQL queries made in the SQLJ program.
It has two purposes. First, the .java files generated from the SQLJ translator make calls to a Java
library called the SQLJ runtime. The classes in this library use the serialized object file to figure out
what JDBC calls to make. Second, the serialized object file can be used to customize the SQLJ
program for a specific vendors database (this is done automatically by the SQLJ translator) .
Compiling the .java files generated by the translator is taken care of by the SQLJ translator, and is,
thus, transparent to most SQLJ users. Nothing fancy is happening behind the scenes. The JDK
javac compiler is run on all of the .java files. The compilation process generates Java bytecode so,
after this step, the SQLJ program can be run with the simple call:
>java MyExample
Now your SQLJ program is a lean, mean, Oracle-database-accessing, machine!
Conclusion
How does a Java application access a database? It can use JDBC and /or SQLJ. JDBC is the
de facto
API for accessing databases from Java. It provides a rich set of interfaces for passing SQL
statements to databases and manipulating SQL data in Java. SQLJ is embedded SQL in Java. SQLJ
simplifies writing, reading, and debugging SQL calls from Java.
JDBC and SQLJ are complementary (i.e. it isn’t an either/or proposition). JDBC is required for
dynamic SQL while SQLJ is preferable for static SQL. Also, Oracle’s current implementation of
SQLJ runs on top of JDBC.
Oracle offers three different JDBC drivers: thin, fat, and server-side. The thin driver is 100% Java
and is ideal for Java applets or any Java applications which need to be completely platform
independent. The fat driver is written on top of OCI. Because of this, it is more appropriate for
middle-tier environments such as application servers. Now that Oracle8
run inside the database, these programs need a way to access data. This functionality is provided by
Oracle’s server-side (also know as KPRB) driver. Because SQLJ runs on top of JDBC, SQLJ
programs can be deployed in thin, fat, or server-side configurations as well. Also, all three drivers
implement the same API, thus providing maximum flexibility in how Java applications are deployed
and re-deployed.
Last, but not least, everything mentioned in this paper (everything being JDBC and SQLJ) is
standards based and is not an Oracle proprietary solution.
We, my friend, are now at the end of our accessing-databases-from-Java journey. It’s time to get off
the train, find our children and reflect on where we’ve been and where we’re going. If you’re like
me, JDBC/SQLJ will change your life forever and provide you with that little beacon of hope which
is so often needed in this cold and heartless world. So remember, when life gets rough … use JDBC
and SQLJ!
i allows Java programs to

Tuesday, 22 November 2011

Recurring Deposite Calculation

--- Recurring Deposite scheme

DECLARE
/* Recurring Deposit Scheme */
tenure NUMBER:=12;
rate NUMBER(10,8):=10;
amount NUMBER(20,8):=3000;
tot_amount NUMBER(20,8):=0;
tot_prin NUMBER(20,8):=0;
tot_int NUMBER(20,8):=0;
ints NUMBER(20,2);
l_sum NUMBER(20,8);
l_rate NUMBER(10,8);
c_int NUMBER(20,8):=0;
BEGIN

FOR i in 1..tenure
LOOP
l_sum :=amount*power((1+(rate/1200)),i);
ints :=l_sum-amount;
dbms_output.put_line(' '||i||' '||amount||' '||ints||' '||l_sum);
tot_amount:=tot_amount+l_sum;
END LOOP;
tot_prin :=amount*tenure;
tot_int :=tot_amount-tot_prin;
c_int :=amount*tenure*(tenure/2)*(rate/1200);
dbms_output.put_line('Total Mat. Amount '||tot_amount);
dbms_output.put_line('Total Principal '||tot_prin);
dbms_output.put_line('Total Interest '||tot_int);
dbms_output.put_line('Cross Check Interest '||c_int);
END;

Rule 72

---In finance, the rule of 72, the rule of 70 and the rule of 69 are methods for estimating an investment's
--doubling time. The rule number is divided by the interest percentage per period to obtain the approximate
-- number of periods (usually years) required for doubling.
DECLARE
--- Rule 72
ROI     NUMBER:=12;
TERM    NUMBER;
MONTHS  NUMBER;
AMT     NUMBER;
BEGIN
TERM:=72/ROI;

dbms_output.put_line('Total Time in years :'||TERM);
dbms_output.put_line('Total months '||72*12/ROI);
dbms_output.put_line('---------------------------------- ');
dbms_output.put_line('                                   ');


END;

Time value of money -Chess borad calculation

------- By Shrikesh
DECLARE
vs NUMBER:=100;
gs NUMBER:=0;
BEGIN
dbms_output.put_line('This is the chess board method for investment.
                           In which each cell is double of previous one ');
FOR i in 1..64
LOOP
gs:=gs+vs;
dbms_output.put_line('Investment for month '||i||' Amount -> '||vs||' Total Sum till '||gs);
vs:=vs*2;

IF i/12 in (1,2,3,4,5) then
dbms_output.put_line('Year '||i/12||' Completed and average on toal sum '||gs/i);
END IF;
END LOOP;
EXCEPTION
WHEN others then
dbms_output.put_line('Error :'||SQLERRM||SQLCODE);
END;

Monday, 21 November 2011

Who called me....

OWA_UTIL.WHO_CALLED_ME



proc_test

create or replace procedure proc_testas
v_owner
v_name
v_lineno
v_caller_t
VARCHAR2(100);VARCHAR2(100);NUMBER(5);VARCHAR2(100);begin
dbms_output.put_line(

OWA_UTIL.WHO_CALLED_ME(
owner => v_owner,

lineno => v_lineno,
caller_t => v_caller_t);

Dbms_Output.Put_Line(
Dbms_Output.Put_Line(
Dbms_Output.Put_Line(
Dbms_Output.Put_Line(
'Program Name: ' || $$PLSQL_UNIT || '. Line: ' || $$PLSQL_LINE);name => v_name,'owner : ' || v_owner );'name : ' || v_name );'lineno : ' || v_lineno );'caller_t : ' || v_caller_t);end
/
CREATE PACKAGE emp_bonus AS
  PROCEDURE calc_bonus (date_hired emp.hiredate%TYPE);
END emp_bonus;
/
 
CREATE OR REPLACE PACKAGE BODY emp_bonus AS
  PROCEDURE calc_bonus (date_hired emp.hiredate%TYPE) IS
    BEGIN
      proc_test;      DBMS_OUTPUT.PUT_LINE ('Employees hired on ' || date_hired || ' get bonus.');
      DBMS_OUTPUT.PUT_LINE('Program Name: ' || $$PLSQL_UNIT || '. Line: ' || $$PLSQL_LINE);
    END;
END emp_bonus;
/
begin
  emp_bonus.calc_bonus(sysdate);
end;
/


Output:
Program
Name: PROC_TEST. Line: 11owner : SCOTTname
lineno :
: EMP_BONUS4caller_t : PACKAGE BODYEmployees hired
Program

Happy Reading
on 15-MAR-2011 get bonus.Name: EMP_BONUS. Line: 6
;
Procedure proc_test is being called by emp_bonus.calc_bonus procedure.is displaying this info using the OWA_UTIL.WHO_CALLED_ME procedure.

Cloning a Database


Cloning a database allows the creation of replicas of a production system for the purposes of training, development or the practising of disaster recovery procedures. Here we are considering two databases Sid LIVE and Sid DEVP . Here we are assuming both the database running in different machines or in the same machine with different location of database files.
Here we are doing the cloning manually not through any tool (Like RMAN ..).
STEPS TO BE PERFORM IN LIVE DATABASE.
1. We start by taking a tracefile backup of the control file of database (LIVE) that is running in the fully open state:
That command produces a text file in the directory to be specified as your USER_DUMP_DEST:
SQL> show parameter user_dump_dest
This will give the result like
User_dump_dest string /oracle/admin/ORACLE_SID(LIVE)/udump
SQL> alter database backup controlfile to trace resetlogs;
Database altered.

SQL> exit
$ cd /oracle/admin/
ORACLE_SID(LIVE)/udump
$ls –ltr *.trc
This will give u the file required to create new control file for DEVP database.Like ora_22213.trc.Change it to sql file .
$ mv ora_22213.trc ora_22213.sql
STEPS TO BE PERFORM IN DEVP DATABASE
1. Shut down the database DEVP cleanly. Apparently this is quit important.
Login as sysdba
SQL > shutdown immediate
………………………………
Database dismounted
Database shutdown
Oracle Instance Shut Down
2. Delete all the datafiles ,controlfiles and redolog files from the location specified for
the database DEVP.
3. Copy the file ora_22213.sql from LIVE environment to DEVP env.
mv /oracle/admin/ORACLE_SID(LIVE)/udump
/oracle/admin/ORACLE_SID(DEVP)/udump
4. Now edit the ora_22213.sql file. Here snapshot of the file are given,how it looks
like.
STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE “LIVE” RESETLOGS ARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1
‘/oracle/oradata/LIVE/redo01.log’ SIZE 10M,
GROUP 2 ‘/oracle/oradata/LIVE/redo02.log’ SIZE 10M,
GROUP 3 ‘/oracle/oradata/LIVE/redo03.log’ SIZE 10M—STANDBY LOGFILE
DATAFILE
’/oracle/oradata/LIVE/system01.dbf’,
’/oracle/oradata/LIVE/undotbs01.dbf’,
’/oracle/oradata/LIVE/sysaux01.dbf’,
’/oracle/oradata/LIVE/users01.dbf’
CHARACTER SET WE8ISO8859P1
;
-- Commands to re-create incarnation table—Below log names MUST be changed to existing filenames on—disk. Any one log file from each branch can be used to—re-create incarnation records.—ALTER DATABASE REGISTER LOGFILE
Recovery is required if any of the datafiles are restored backups,
-- or if the last shutdown was not normal or immediate.
RECOVER DATABASE USING BACKUP CONTROLFILE
—Database can now be opened zeroing the online logs.
ALTER DATABASE OPEN RESETLOGS;
-- Commands to add tempfiles to temporary tablespaces.—Online tempfiles have complete space information.—Other tempfiles may require adjustment.
ALTER TABLESPACE TEMP ADD TEMPFILE ‘/oracle/oradata/LIVE/temp01.dbf’
SIZE 15728640 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;
-- End of tempfile additions.

Change CREATE CONTROLFILE REUSE DATABASE “LIVE” RESETLOGS ARCHIVELOG to CREATE CONTROLFILE SET DATABASE “DEVP” RESETLOGS NO ARCHIVELOG
Change wherever the /oradata/LIVE to /oradata/DEVP.
You also need to remove the reference to the script to recovering the database USING BACKUP CONTROLFILE. You’re actually going to be re-creating a brand new controlfile, not using a backup one, so that line should just read RECOVER DATABASE.
You should therefore end up with a script which reads like this:
STARTUP NOMOUNT
CREATE CONTROLFILE
SET DATABASE “LIVE” RESETLOGS NOARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 ‘/oracle/oradata/
DEVP/redo01.log’ SIZE 10M,
GROUP 2 ‘/oracle/oradata/
DEVP/redo02.log’ SIZE 10M,
GROUP 3 ‘/oracle/oradata/
DEVP/redo03.log’ SIZE 10M—STANDBY LOGFILE
DATAFILE
’/oracle/oradata/
DEVP /system01.dbf’,
’/oracle/oradata/
DEVP /undotbs01.dbf’,
’/oracle/oradata/
DEVP /sysaux01.dbf’,
’/oracle/oradata/
DEVP /users01.dbf’

CHARACTER SET WE8ISO8859P1
;
-- Commands to re-create incarnation table
—Below log names MUST be changed to existing filenames on—disk. Any one log file from each branch can be used to—re-create incarnation records.—ALTER DATABASE REGISTER LOGFILE
Recovery is required if any of the datafiles are restored backups,
-- or if the last shutdown was not normal or immediate.
—RECOVER DATABASE—Database can now be opened zeroing the online logs.
ALTER DATABASE OPEN RESETLOGS;
-- Commands to add tempfiles to temporary tablespaces.—Online tempfiles have complete space information.—Other tempfiles may require adjustment.
ALTER TABLESPACE TEMP ADD TEMPFILE ‘/oracle/oradata/DEVP/temp01.dbf’
SIZE 15728640 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;
-- End of tempfile additions.—
Now for copying the datafile from LIVE to DEVP env.
1. Shutdown the database LIVE cleanly
Login as sysdba SQL>shutdown immediate.
2. copy all the datafile from the location like ‘/oracle/oradata/ LIVE /system01.dbf’ to
‘/oracle/oradata/ DEVP /system01.dbf’. like
Mv /oracle/oradata/ LIVE/* /oracle/oradata/ DEVP
Go to DEVP area and run
Cd /oracle/admin/ORACLE_SID(DEVP)/udump
SQL>ora_22213.sql
This will create the new control file and start the database.

Get DDL from Database

DECLARE
l_obj_name      VARCHAR2(2000);
l_obj_type      VARCHAR2(2000);
l_obj_ddl       CLOB;
begin

for k in (SELECT 'select '||''''||object_name||''','''||object_type||''''||','||'dbms_metadata.get_ddl('''||object_type||''','''||
object_name||''','''||owner||''')' || 'from dual' sqlscr from all_objects
 where OWNER='TANGIBLE' AND object_name NOT LIKE 'BIN%'
 and OBJECT_TYPE in ('INDEX','PACKAGE','PROCEDURE','SEQUENCE','SYNONYM','TABLE','TRIGGER','TYPE','VIEW')
                  order by object_type)
LOOP
execute immediate k.sqlscr into l_obj_name ,l_obj_type,l_obj_ddl;
dbms_output.put_line('REM  Object Name  '||l_obj_name||'  Object Type   '||l_obj_type);
dbms_output.put_line(l_obj_ddl);
INSERT INTO SSP VALUES (l_obj_name ,l_obj_type,l_obj_ddl);

END LOOP;
end ;
Note : Sript can be modified as per requirement.

Analyze Table based on record count in table

DECLARE
   l_table_name   cat.table_name%TYPE;
   l_sql          VARCHAR2 (2000);
   l_cnt          NUMBER;
BEGIN
   DBMS_OUTPUT.put_line (RPAD (' ', 60, '-'));
   DBMS_OUTPUT.put_line (RPAD ('TABLE_NAME', 50) || 'COUNT');
   DBMS_OUTPUT.put_line (RPAD (' ', 60, '-'));

   FOR i IN (  SELECT table_name, table_type
                 FROM cat
                WHERE table_type = 'TABLE'
             ORDER BY table_name)
   LOOP
   BEGIN
      l_sql := 'SELECT count(*) from ' || i.table_name;

      EXECUTE IMMEDIATE l_sql INTO l_cnt;
     
   IF l_cnt>10000 THEN
     
      EXECUTE IMMEDIATE 'ANALYZE TABLE '||i.table_name||' compute statistics';
     
   end if;
     
      DBMS_OUTPUT.put_line (RPAD (i.table_name, 50) || l_cnt);
  
  
   EXCEPTION
   WHEN OTHERS THEN
   dbms_output.put_line('Error :'||i.table_name ||'  '||SQLCODE||':  '||SQLERRM);
  
   END;
   END LOOP;
END;

Note : Script can be modified as per requirement.

Loan Amortization schedule script

DECLARE

p number:=3133300.00; -- principal amout
n number:=20 ; -- payament period in years
r number:=9.25 ; -- rate of interest yearly
ins number:=0.00 ; -- monthly installment
pins number:=0.00 ; -- monthle principal paid
iins number:=0.00 ; -- monthly interest paid
ins1 number:=0.00 ; -- interest of first month
dateofloan date :=to_date('01/10/2009','dd/mm/yyyy');

BEGIN---- Calculating rate of interest for period
dbms_output.put_line('==================================================== ');
dbms_output.put_line('Total Loan Amount : '||p);
dbms_output.put_line('Rate of Interest(Yearly) : '||r);r:=round(r/1200,8);
dbms_output.put_line('Applied Rate of Interest : '||r);----- Calculating period
dbms_output.put_line('Periods (Years) : '||n);n:=n*12;
dbms_output.put_line('Total Installments : '||n);------ Calculating monthly installment
ins:=round((p*r*power((1+r),240))/(power((1+r),240)-1),0);
ins1:=round (p*r,0);
dbms_output.put_line('Monthly Insatllment : '||round(ins,0));
dbms_output.put_line('==================================================== ');
dbms_output.put_line('*********** Amortization Schedule **************** ');
dbms_output.put_line('---------------------------------------------------------------------------------------------------------------------------------------');
dbms_output.put_line('Date '||' | Amount for cal |'||' Installment |'||' Interst paid |'||' Principal Paid |'||' Outstanding Amount till date ');
dbms_output.put_line('---------------------------------------------------------------------------------------------------------------------------------------');

For i in 1..n
loop
dbms_output.put_line(add_months(to_date(dateofloan,'dd/mm/yyyy'),i-1)||' |'||rtrim(p,12)||' '||'|'||ins||' |'||ins1||' |'||(ins-ins1)||' |'||(p-ins+ins1));
p:=p-ins+ins1;
ins1:=round (p*r,0);
end loop;

dbms_output.put_line('---------------------------------------------------------------------------------------------------------------------------------------');

EXCEPTION
when others then
dbms_output.put_line('Error : '||SQLERRM);

END;

By
Shrikesh Pandey