Professional Documents
Culture Documents
Cursor is a location in the memory where the SQL statement is processed. That is when ever we send an
SQL statement; the system allocates some memory by giving a name. After processing the statement it
automatically cleans that memory. There are four STEPS , which are processed by the system
whenever we send an SQL statement they are
2.OPENING cursor :
3.Fetching cursor:
4.Closing cursor : This statement CLOSE the memory allocated for the cursor
2. Explicit Cursors
Implicit Cursors
Oracle implicitly opens a cursor to process each sql statement not associated with explicitly declared
cursor. Pl/sql lets you refer to the most recent implicit cursors as the sql cursor.
1. sql%rowcount
1. sql%found
1. sql%notfound
1. %isopen
Example : 1
write a program to check whether there is atleast one row satisfying the given SELECT
statement or not using implicit cursors
Declare
e emp%rowtype;
Begin
if sql%found then
dbms_output.put_line('~~~~~~~~~~~~~~~~~~~~');
dbms_output.put_line('~~~~~~~~~~~~~~~~~~~~');
End if;
End;
Example : 2
begin
if sql%found then
dbms_output.put_line('~~~~~~~~~~~~~~~~~~~~');
else
dbms_output.put_line('~~~~~~~~~~~~~~~~~~~~');
end if;
end;
Example : 3
write a program to update the salary of those employees to 20% whose salary is greater than the
given salary, using implicit cursors
begin
if sql%found then
dbms_output.put_line('~~~~~~~~~~~~~~~~~~~~');
dbms_output.put_line('~~~~~~~~~~~~~~~~~~~~');
end if;
if sql%notfound then
dbms_output.put_line('~~~~~~~~~~~~~~~~~~~~');
end if;
end;
Explicit Cursors
The USER creates these cursors for APPLYING 2 or more DML operations on a table. using a cursor
we can fetch all the row values from the table into a variable for printing them or performing some action
with them
In an explicit cursor, there are four steps related to execute cursors by the user. It is also used to compare
a variable value with all values of the column.
1. Declaring a Cursor
2. OPENING a Cursor
3. Fetching a Cursor
4. CLOSING a Cursor
Declaring a Cursor
A cursor is defined in the declarative part of the PL/SQL block by naming it and associating it with a query
Syntax :
Declare
Opening a Cursor
The cursor must be initialized or opened with the open statement. Opening the cursor excutes the query
and identifies the result set which consists of all the rows that meets the query search criteria
Syntax :
Declare
Begin
-----------
End;
This statement is used to retrieve the cursors current row. Fetching can be excuted repeatedly until all
rows have been retrieved
Syntax :
Declare
Cursor <cursor_name> is <select Statement>
Begin
-----------
End;
Closing a Cursor
When the last row has been processed, the cursor is closed with the close statement. The close
statement disable the cursor
Syntax :
Declare
Begin
-----------
End;
Examples
1. Write a program using an explicit cursor, which displays all the rows from the table
Declare
e emp%rowtype;
Begin
Open c1;
Loop
Fetch c1 into e;
End loop;
Close c1;
End;
2.Write a program using an explicit cursor, to insert values into a table empc from emp table,
whose experience is >=23 years
Declare
e emp%rowtype;
x number:=0;
begin
open c2;
loop
fetch c2 into e;
x:=x+sql%rowcount;
end loop;
close c2;
end;
/
3.Write a program using an explicit cursor, to display first 5 records
Declare
e emp%rowtype;
x number;
begin
open c3;
loop
fetch c3 into e;
end loop;
close c3;
end;
A cursor for loop implicitly declares its loop index as a %rowtype, opens a cursor, repeatedly fetches the
rows of values from the result set into fields in the records and closes the cursor when all rows have been
processed or exit command is encountered
Syntax :
Declare
Begin
---------
end loop;
end;
/
Example : 1
Write a program to display all employee’s details working in the accepted department number
using cursor for loop
declare
e c1%rowtype;
begin
dbms_output.put_line('****************');
for e in c1 loop
end loop;
dbms_output.put_line('****************');
end;
Example : 2
Write a program to display all employee’s details working in the accepted department number and
also display all department details for the given department number using cursor for loops
declare
e c1%rowtype;
d c2%rowtype;
begin
for e in c1 loop
for d in c2 loop
dbms_output.put_line(d.dname||' '||d.loc
||' '||d.deptno);
end loop;
end;
Trigger
Trigger is a database object ie used to execute an action basing on an even. Triggers are event-
based PROGRAMS , which are executed when an event occurs or raises or fires
Trigger Type is defined by the type of triggering TRANSACTION and by the LEVEL at which the
trigger is executed
A row trigger is fired each time a row in the table is affected by the triggering statement. For example, if
an UPDATE statement updates multiple rows of a table, a row trigger is fired once for each row affected
by the update statement. If the triggering statement affects no rows, the trigger is not executed at all. Row
triggers should be used when some processing is required whenever a triggering statement affects a
single row in a table.
Row level triggers are created using the "For Each Row" Clause in the Create Trigger Command
Statement triggers
A statement trigger is fried once on behalf of the triggering statement, independent of the number of rows
the triggering statement affects (even if not rows are affected) statement triggers should be used when a
triggering statement affects rows in a table but the processing required is COMPLETELY independent
of the number of rows affected
Statement level triggers are the default TYPE of trigger created via Create Trigger Command
Syntax:
{before/after/instead of}
{insert/UPDATE /delete}
Declare
variables declarations
-----------------
BEGIN
Executable statements
-------------
Exception
Exception statements
---------------
end;
Syntax Explanation :
or replace : Recreates the trigger if it already exists. this option can be used to change the definition of
an existing trigger without requiring the USER to drop the trigger first
Before : Indicates that oracle fires the trigger before executing the trigger statement
After : Indicates that oracle fires the trigger After executing the trigger statement
Insert : Indicates that oracle fires the trigger whenever an INSERT statement adds a row to a table.
Delete : Indicates that oracle fires the trigger whenever a DELETE statement removes a row from the
table.
Update : Indicates that oracle fires the trigger whenever an UPDATE statement changes a value in one
of the columns specified in the OF clause. if the OF clause is omitted, the oracle fires the trigger
whenever an UPDATE statement changes a value in any column of the table.
for Each Row : Designates the trigger to be a row trigger. the oracle engine fires a row trigger once for
each row that is affected by the triggering statement and meets the optional trigger constraint defined in
the When clause. if this clause is omitted the trigger is a statement trigger.
When : specifies the trigger restriction. the trigger restriction contains a SQL condition that must be
satisfied for the oracle to fire the trigger.
1. DML Triggers
2. DDL Triggers and
3. Instead of Triggers
1. DML Triggers
These triggers are executed before or after. we apply any dml operations on a table.
When we create a table. the trigger definition is stored in the database, which is identified with the trigger
name. the code in the trigger is processed when we apply any command on the database or table
Examples :
1. First Create a trigger, next set the server on with the following statement (set Serveroutput on)
@ <trigger name>
1. Create A Trigger, Which Displays A Message When Ever You Insert A New Row In To Sample1
Table
Begin
End;
2. Create A Trigger, Which Displays A Message When Ever You Update An Existing Row In The
Table Sample1
Begin
End;
/
1. Create A Trigger, Which Displays A Message When Ever You Delete A Row From The
Table Sample1
Begin
End;
1. Create A Trigger, Which Displays A Message When Ever You Insert A New Row Into A Table
Sample1
Begin
End;
1. Create a trigger, which displays a message when ever you update a row in the table
sample1
Create or replace trigger updtrig2 before update on sample1 for each row
Begin
End;
1. Create A Trigger, Which Displays A Message When Ever You Delete A Row From The
Table Sample1
Create or replace trigger deltrig2 after delete on sample1 for each row
Begin
DDL TRIGGERS
1. Create A Trigger, Which Displays An Error Message When Ever You Create a New Table
which starts with Letter ‘A’
Begin
End if;
End;
1. Create A Trigger, Which Displays An Error Message When Ever You try to drop any Table
Begin
if ora_dict_obj_type='table' then
End if;
End;
1. Create A Trigger, Which Displays An Error Message When Ever You try to Alter any Table
begin
if ora_dict_obj_type='TABLE' then
end if;
end;
INSTEAD OF TRIGGER
Create a trigger, which inserts the given values in to the relevant table through a Composite view
Begin
end if;
end if;
End;
Stored Procedure
A procedure is a set of instructions(usually combining sql and plsql commands) saved for calling and
repeated execution
A procedure is a sub PROGRAM that performs a specific action. A procedure can be called from any
pl/sql block. A procedure has two parts one is Specification and Other is BODY
The procedure specification begins with the keyword procedure followed by procedure name and an
option LIST of arguments enclosed with in the parenthesis.
1. Declarative Part
2. Execution Part
3. Exception HANDLING Part
Syntax of Procedure
Create [ Or Replace ] Procedure < Procedure Name > ( Argument1 [Mode] <Data TYPE >,
Argument2 [Mode] <data type>,---)
Is / As
------------
Begin
Executable Statements;
---------------
[Exception
Exception Handling; ]
End;
1. IN : the In parameter lets the USER to pass values to the called sub programs with in the
sub PROGRAM , the IN parameter acts like a constant. There fore it cannot be modified.
1. OUT : The Out parameter lets the user returned values to the calling block. Inside the sub
program the out parameter acts like a un initialized variable. Its value can not be assigned to
another variable or itself
1. INOUT : the INOUT parameter lets the user pass initial values to the called sub program
and RETURN updated values to the calling block
Show ERRORS :
This Command is used to show the errors that are occurred during the procedure creation
Ex : when ever we create a procedure and executed, if it show procedure created with compilation errors,
the we can see those errors using the following statement
show Errors
Example :1
Sol. STEPS
Ed subprocedure1
Begin
End;
1. Next, Compile the above procedure to find errors in the procedure, with the following statement
@ subprocedure1
Begin
Dbms_output.put_line('This is Main Program');
Disp;
End;
1. Next, Run the above main procedure with the following statement
@mainprogram1
output :
SQL> @ mainprogram1
Example :2
Write a procedure to show a message when updated any row in a particular table
Sol. Steps
Ed subprocedure2
is
Begin
End;
/
1. Next, Compile the above procedure to find errors in the procedure, with the following statement
@ subprocedure2
Begin
upd(&empno);
End;
1. Next, Run the above main procedure with the following statement
@mainprogram2
output :
SQL> @ mainprogram2
old 2: upd(&empno);
new 2: upd(7369);
1 Record Updated...
Example 3:
Create a procedure which adds the given three numbers using “in” and “out” parameters
Sol. Steps
Ed subprocedure3
is
Begin
res:=n1+n2;
End;
1. Next, Compile the above procedure to find errors in the procedure, with the following statement
@ subprocedure3
Declare
a number;
b number;
c number;
Begin
a:=&a;
b:=&b;
sum_numbers(a,b,c);
End;
1. Next, Run the above main procedure with the following statement
@mainprogram3
output :
SQL> @ mainprogram3
old 6: a:=&a;
new 6: a:=10;
new 7: b:=20;
Example 4:
Create a procedure which Updates the sal with the increment value that you give according to the
given emp number
Sol. Steps
Ed subprocedure4
out NUMBER , i number)
Is
Begin
Dbms_output.put_line('RECORD Updated----');
End;
1. NEXT , Compile the above procedure to find errors in the procedure, with the following
statement
@ subprocedure4
Declare
e number:=&empno;
Incr_value number:=&increment;
s employee.sal%type;
Begin
SELECT sal into s from employee where empno=e;
Incr(e,s,incr_value);
End;
1. Next, Run the above main procedure with the following statement
@mainprogram4
output :
SQL> @ mainprogram4
RECORD Updated----
Syntax of A Function
Create [ Or Replace ] Function < Function Name > ( Argument1 [Mode] <Data type>, Argument2 [Mode]
<data type>,---) return data type
Is / As
------------
BEGIN
Executable Statements;
---------------
[Exception
Exception HANDLING ; ]
End;
EXAMPLE : 1
Sol :
ed function1
is
BEGIN
Return x+y;
End;
/
mainfunction1
Declare
A integer :=&a;
B integer :=&b;
C integer;
Begin
C:=addition(a,b);
End;
EXAMPLE : 2
wirte a function which accepts item NUMBER and quantity and then return the total amount by
fetching rate from the item table
is
begin
RETURN q*r;
end;
ed mainfunction2
declare
q1 NUMBER :=&quantity;
r1 number;
res number;
BEGIN
SELECT rate into r1 from item where itno=&itno;
res:=itembill(q1,r1);
exception
end;
Exception Handling
Types of Exceptions
These exceptions are used to handle some logical errors known to the system are pre defined. The
following are some of the important pre defined Exceptions
1. no_data_found
1. dup_val_on_index
This Exception raises when ever you try to store duplicate values into a
1. cur_already_open
This Exception raises when ever your program attempts to open an already
opened cursor.
automatically opens the cursor to which it refers. So Your program can not open that cursor inside the
loop
1. invalid_cursor
This Exception raises when ever your program attempts an illegal cursor operation, such as closing an un
opened cursor
1. zero_divide
This Exception raises when ever your program attempts to divide a number
by zero
1. program_error
1. storage_error
1. too_many_rows
This Exception raises when ever a select statement returns more than one
row
1. login_denied
This Exception raises when ever your program attempts to logon to oracle
1. value_error
This Exception raises when ever an arithmetic conversion or size constraint error occurs
For ex, when your program selects a column value into a variable. If the value is longer than the declared
length of the variable, pl/sql abords the assignment and raise the exception value_error
Programs On Exceptions
1. Write a program for handling an error when we try to open an already opened cursor
declare
x emp%rowtype;
begin
open c1;
fetch c1 into x;
dbms_output.put_line(x.empno||' '||x.ename);
--open c1;
close c1;
exception
when cursor_already_open then
end;
2. Write a program to display a message when no data found in the main table
declare
e emp%rowtype;
begin
exception
end;
3. Write a program for handling an error when we insert a duplicate value into a column.
declare
n sample%rowtype;
begin
dbms_output.put_line('Record Inserted....');
exception
4. Write a program for handling an error when we try to close an un opened cursor
declare
BEGIN
--open c1;
CLOSE c1;
exception
end;
/NOTE : if u remove comment for open c1 then output is cursor is opened, other wise the you will get
“sorry friend, no such cursor is opened ..”, since you r trying to CLOSE an unopened cursor
5. Write a program for handling an ERROR when we try to divide a value with zero
declare
a NUMBER :=&a;
b number:=&b;
c number;
BEGIN
c:=a/b;
dbms_output.put_line(c);
exception
dbms_output.put_line('Zero Divide ERROR' );
end;
6. Write a PROGRAM for handling an error when ever the program retrieves too many rows
declare
e emp%rowtype;
BEGIN
exception
end;
A user can define or create exceptions, which must be raised automatically, because the system does not
knows when to execute the user defined ERRORS . There are 3 steps while working with user defined
exceptions
1. Creating Exception
2. Raising Exception
3. HANDLING Exception
The main purpose of user defined exceptions is, we can display a message in predefined ERROR
format in the instances not known to the system or that are not predefined
Syntax :
Declare
Raise Exception;
Exception
Message
End;
Raise_Application_Error
This is a keyword, which is used to generate an error message in pre defined error format for the given
user defined error
Syntax :
Example :
Note : Error Number for (user defined Exceptions) must be in between –20001 to -20999
1. Write a program for creating an exception and raising it when the basic is <3000, while inserting
rows into a table and also handle it to perform any other action
Declare
invalid_salary exception;
s employee.sal%type:=&sal;
begin
if s<3000 then
raise invalid_salary;
else
end if;
exception
end;
2. Write a program for creating an exception and raising it when ever you try to insert any negative
number into a table
Declare
invalid_number exception;
e employee.empno%type:=&empno;
begin
if e<0 then
raise invalid_number;
else
dbms_output.put_line(' RECORD Inserted.....');
end if;
exception
end;
Control structures
CONTROL structures
Control structures are used to control the flow of PROGRAMS execution. The control structures are
classified into the following categories:
Conditional Control
Iterative Control
Conditional Control
PL/SQL supports the following types of Conditional control statements or SELECTION Statements
1.Simple If
The simple if statement is used to CHECK a simple CONDITION , if it is true it will execute the
statements that are present between "if "and "end if". Otherwise it will not execute those statements.
Syntax :
If ( Condition ) then
Statements ;
------------- ;
End If;
2.If...Else Statement
The if …else statement is used to define two blocks of statements, in ORDER to execute only one
block.
Syntax :
If (CONDITION ) then
------------------------- ;
Else
End if;
if the condition is true, it will execute the True Block statements otherwise it will execute the false block
statements.
3.If...ElsIf Statement
The if …elsIf statement is used to define SEVERAL blocks of statements, in order to execute only one
block.
Syntax :
If (condition 1) then
statements 1 ;
--------------- ;
statements 2;
------------- ;
statements 3;
------------;
statements n;
------------;
Else
statements;
------------;
End if;
if condition1 is true, it will execute statements1 otherwise it will check Condition2, if it is true it
will execute statements2. if not it will go to condition3, like this the process continues until the condition
matches. If no condition matches, it will execute the else block statements.
Eg 1. Write a program to check whether the given number is a positive number of a negative
number using simple if statement
declare
n number:=&n;
begin
if(n>=0) then
end if;
if(n<0) then
end if;
end;
Branching Statements
declare
x integer:=&x;
y integer:=&y;
p char(1):='&Operator';
r number;
begin
if p='+' then
goto Addition;
goto Subtraction;
elsif p='*' then
goto Multiplication;
goto Division;
else
goto Expt;
end if;
<<Addition>>
r:=x+y;
dbms_output.put_line(r);
return;
<<Subtraction>>
r:=x-y;
dbms_output.put_line(r);
RETURN ;
<<Multiplication>>
r:=x*y;
dbms_output.put_line(r);
return;
<<Division>>
r:=x/y;
dbms_output.put_line(r);
return;
<<Expt>>
dbms_output.put_line(sqlerrm);
end;
Loops
1. SIMPLE Loop
2. While Loop
3. For Loop
1.SIMPLE Loop
This loop is used to execute a series of statements as long as the CONDITION is false. If the condition
becomes true, it will exit from that loop
Syntax :
Loop
Statements;
Increment/decrement Operations
End Loop;
Declare
N number:=1;
Begin
Loop
Dbms_output.put_line(N);
N:=N+1;
End loop;
End;
2.While Loop
The while Loop is used to execute a series of statements as long as the condition is True. If
the condition becomes false, it will exit from that loop
Syntax :
Statements;
Increment/decrement Operations
End Loop;
Declare
N number:=1;
Begin
Dbms_output.put_line(N);
N:=N+1;
End loop;
End;
3.For Loop
The For loop is used to execute a set of statements as long as the condition is false. If the condition
becomes true, it will exit from that loop
Syntax :
Statements;
End Loop;
Declare
N number:=1;
Begin
Dbms_output.put_line(N);
End loop;
End;
Eg 5 :Write a program to print first 25 natural numbers using For loop in Reverse order
Declare
N number:=1;
Begin
Dbms_output.put_line(N);
End loop;
End;
Declare
i number:=1;
r number:=&number;
Begin
dbms_output.put_line(r||'*'|| i ||'='||r*i);
i:=i+1;
End loop;
End;
Materialized view
A materialized view is a database object that contains the results of a query. They are local copies of data
located remotely, or are used to create summary tables based on aggregations of a table's
data.Materialized view and the Query rewrite feature is added from ORACLE 8i.
A materialized view log is a schema object that records changes to a master table's data so that a
materialized view defined on the master table can be refreshed incrementally.
If you delete any RECORD from your Materialized view it is goanna impact your Source table
once it is refreshed.
Execute dbms_mview.refresh(‘MV’);
Refresh Complete : To perform a complete refresh of a materialized view, the server that manages the
materialized view executes the materialized view's defining query, which essentially recreates the
materialized view. To refresh the materialized view, the result set of the query replaces the existing
materialized view data. Oracle can perform a complete refresh for any materialized view. Depending on
the AMOUNT of data that satisfies the defining query, a complete refresh can take a substantially longer
amount of time to perform than a fast refresh.
execute DBMS_mview.refresh(List=>’MV’,Method=>’c’);
Refresh Fast :To perform a fast refresh, the master that manages the materialized view first identifies
the changes that occurred in the master since the most recent refresh of the materialized view and
then APPLIES these changes to the materialized view. Fast refreshes are more efficient than complete
refreshes when there are few changes to the master because the participating server and network
replicate a smaller amount of data.
The following statement creates the primary-key materialized view on the table emp located on a remote
database.
The following statement creates the rowid materialized view on table emp located on a remote database:
SQL Loader
SQL*Loader (sqlldr ) is the utility to use for high performance data loads. The data can be loaded from
any text file and inserted into the database.
During processing, SQL*Loader writes messages to the log file, bad rows to the bad file, and discarded
rows to the discard file.
The SQL*Loader control file contains information that describes how the data will be loaded. It contains
the table name, column data types, field delimiters, etc. It simply provides the guts for all SQL*Loader
processing.
SQL*Loader Options
SQL*Loader provides the following options, which can be specified either on the command line or within a
parameter file:
1. bad – A file that is created when at least one record from the input file is rejected. The rejected
data records are placed in this file. A record could be rejected for many reasons, including a non-
unique key or a required column being null.
2. bindsize – [256000] The size of the bind array in bytes.
3. columnarrayrows – [5000] Specifies the number of rows to allocate for direct path column
arrays.
4. control – The name of the control file. This file specifies the format of the data to be loaded.
5. data – The name of the file that contains the data to load.
6. direct – [FALSE] Specifies whether or not to use a direct path load or conventional.
7. discard – The name of the file that contains the discarded rows. Discarded rows are those that
fail the WHEN clause condition when selectively loading records.
8. discardmax – [ALL] The maximum number of discards to allow.
9. errors – [50] The number of errors to allow on the load.
10. external_table – [NOT_USED] Determines whether or not any data will be loaded using external
tables. The other valid options include GENERATE_ONLY and EXECUTE.
11. file – Used only with parallel loads, this parameter specifies the file to allocate extents from.
12. load – [ALL] The number of logical records to load.
13. log – The name of the file used by SQL*Loader to log results.
14. multithreading – The default is TRUE on multiple CPU systems and FALSE on single CPU
systems.
15. parfile – [Y] The name of the file that contains the parameter options for SQL*Loader.
16. parallel – [FALSE] Specifies a filename that contains index creation statements.
17. readsize – The size of the buffer used by SQL*Loader when reading data from the input file.
This value should match that of bindsize.
18. resumable – [N] Enables and disables resumable space allocation. When “Y”, the
parameters resumable_name and resumable_timeout are utilized.
19. resumable_name – User defined string that helps identify a resumable statement that has been
suspended. This parameter is ignored unless resumable = Y.
20. resumable_timeout – [7200 seconds] The time period in which an error must be fixed. This
parameter is ignored unless resumable = Y.
21. rows – [64] The number of rows to load before a commit is issued (conventional path only). For
direct path loads, rows are the number of rows to read from the data file before saving the data in
the datafiles.
22. silent – Suppress errors during data load. A value of ALL will suppress all load messages. Other
options include DISCARDS, ERRORS, FEEDBACK, HEADER, and PARTITIONS.
23. skip – [0] Allows the skipping of the specified number of logical records.
24. skip_unusable_indexes – [FALSE] Determines whether SQL*Loader skips the building of
indexes that are in an unusable state.
25. skip_index_maintenance – [FALSE] Stops index maintenance for direct path loads only.
26. streamsize – [256000] Specifies the size of direct path streams in bytes.
27. userid – The Oracle username and password.
1. To drive the concept of the SQL loader home.., let us assume that we have text file which is the
usual emp table.
2. Now open the cmd prompt and type sqlldr.
3. later the things should look some thing like this.
sqlldr# userid=username/pwd@ora
load data
in file ‘C:\emp.txt’
(empno,ename,job,mgr,sal,comm,deptno)
2. Index Columns :
SELECT INDEX_NAME,TABLE_OWNER,TABLE_NAME,COLUMN_NAME,
4. Constraints on a table:
select CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,SEARCH_CONDITION,
6. Text of a Trigger :
select trigger_name,trigger_type,triggering_event,table_owner,table_name,
7. Locks on Table:
select c.owner,c.object_name,c.object_type,c.object_id,b.sid,b.serial#,
c.object_id;
0- none
1 - null (NULL)
2 - row-S (SS)
3 - row-X (SX)
4 - share (S)
5 - S/Row-X (SSX)
6 - exclusive (X)
t.piece;
sn.STATISTIC# AND NAME LIKE '%CPU used by this session%' AND se.SID
BY VALUE DESC;
elapsed_seconds;
reads_per_execution,a.buffer_gets,a.disk_reads,a.executions,a.sorts,a.address
16.Oracle SQL query that show the opened connections group by the program that opens the
connection.
18.Oracle SQL query that shows Oracle users connected and the sessions number for user
select sid, sql_text from v$session s, v$sql q where sid in (* ,*) and
Provide the Sid for which you wish to see the SQL Text else list will be exhaustive.
22.query to find out which session is currently using the most undo
s.value desc;
24. query to find out which sessions are using space in the temporary tablespace.
Plan Table : A Plan table holds execution plans generated by the explain plan statements.Create Plan
table by running utlxplan.sql located in
$oracle_home/rdbms/admin.
for
<sql statements>;
select a.customer_name,a.customer_number,b.invoice_number,
b.invoice_type,b.invoice_data,b.total_amount,
c.line_number,c.part_number,c.quantity,c.unit_cost
where c.invoice_id=:b1
and c.line_number=:b2
and b.invoice_id=c.invoice_id
and a.customer_id=b.customer_id
Sql> @ explain.sql
it will give us execution plan of the query like whether the query is using the relevant INDEXES on the
joining columns or indexes to support the query are missing.
If joining columns doesn’t have index then it will do the FULL table scan if it is full table scan the cost will
be more then will create the indexes on the joining columns and will run the query it should give better
performance and also needs to analyze the tables if analyzation happened long back. The ANALYZE
statement can be used to gather statistics for a specific table, index or cluster using
If still have performance issue then will use HINTS, hint is NOTHING but a clue. We can use hints like
ALL_ROWS
One of the hints that 'invokes' the Cost based optimizer
ALL_ROWS is usually used for batch processing or data warehousing SYSTEMS .
FIRST_ROWS
One of the hints that 'invokes' the Cost based optimizer
FIRST_ROWS is usually used for OLTP systems.
/*+ use_hash */