Unit-2 PL - SQL
Unit-2 PL - SQL
Introduction to PL/SQL
PL/SQL stands for “Procedural Language extensions to the Structured Query Language.”
PL/SQL is Oracle Corporation’s procedural extension for SQL and the Oracle relational
database. It is a high-performance, highly integrated database language.
Oracle PL/SQL is an extension of SQL language that combines the data manipulation power
of SQL with the processing power of procedural language to create super powerful SQL
queries. PL/SQL ensures seamless processing of SQL statements by enhancing the security,
portability, and robustness of the Database.
PL/SQL means instructing the compiler ‘what to do’ through SQL and ‘how to do’ through its
procedural way. Similar to other database languages, it gives more control to the
programmers by the use of loops, conditions and object-oriented concepts.
SQL PL/SQL
• SQL is a single query that is used to • PL/SQL is a block of codes that used to
perform DML and DDL operations. write the entire program blocks/
procedure/ function, etc.
• It is declarative, that defines what need to • PL/SQL is procedural that defines how
be done, rather than how things need to the things needs to be done.
be done.
• Execute as a single statement. • Execute as a whole block.
• Mainly used to manipulate data. • Mainly used to create an application.
• Interaction with a Database server. • No interaction with the database
server.
• Cannot contain PL/SQL code in it. • It is an extension of SQL, so that it can
contain SQL inside it.
PL/SQL Architecture
PL/SQL architecture consist of following three components, displayed in the diagram below:
PL/SQL Block:
PL/SQL block creates the structured logical blocks of code that describes the process to be
executed. Such a block consists of SQL statements and PL/SQL instructions that are then
passed to the oracle engine for execution.
PL/SQL Engine:
The PL/SQL block containing the procedural statements are executed by the PL/SQL
statement executor inside the PL/SQL engine. In simpler words, the PL/SQL engine provides
the environment in which the PL/SQL block is executed.
Database Server:
Last but not least, comes the Database Server. This can be Oracle DB server or MySQL DB
server. The DB server stores data in tables.
When an application program or a user invokes a PL/SQL block, it is submitted to the PL/SQL
engine available locally. PL/SQL engine extracts the Procedural Language statements of the
block. These statements are then executed separately by the PL/SQL processor. The SQL
In a block, the executable section is mandatory while the declaration and exception-
handling sections are optional.
A block without a name is an anonymous block. An anonymous block is not saved in the
Oracle Database server, so it is just for one-time use. However, PL/SQL anonymous blocks can
be useful for testing purposes.
Declaration section
Identifies variables, cursors, and subblocks that are referenced in the execution and exception
sections. Optional.
Execution section
Statements the PL/SQL runtime engine will execute at runtime. Mandatory.
Exception section
Handles exceptions to normal processing (warnings and error conditions). Optional.
Anonymous blocks:
In PL/SQL, That’s blocks which is not have header are known as anonymous blocks. These
blocks do not form the body of a function or triggers or procedure.
The following example shows a simple PL/SQL anonymous block with one executable section.
BEGIN
DBMS_OUTPUT.put_line (‘Hello World!’);
END;
The executable section calls the DMBS_OUTPUT.PUT_LINE procedure to display the “Hello
World” message on the screen.
Named blocks:
That’s PL/SQL blocks which having header or labels are known as Named blocks. These blocks
can either be subprograms like functions, procedures, packages or Triggers.
EXEC test;
You can define and use your own subtypes. The following program illustrates defining and
using a user-defined subtype −
DECLARE
SUBTYPE name IS char(20);
SUBTYPE message IS varchar2(100);
salutation name;
greetings message;
BEGIN
salutation := 'Reader ';
greetings := 'Welcome to the World of PL/SQL';
dbms_output.put_line('Hello ' || salutation || greetings);
END;
/
When the above code is executed at the SQL prompt, it produces the following result –
When you provide a size, scale or precision limit with the data type, it is called a constrained
declaration. Constrained declarations require less memory than unconstrained declarations.
For example −
counter integer := 0;
greetings varchar2(20) DEFAULT 'Have a Good Day';
DECLARE
a integer := 10;
b integer := 20;
c integer;
BEGIN
c := a + b;
dbms_output.put_line('Sum: ' || c);
END;
/
Output:
Statement processed.
Sum: 30
PL/SQL Operators
PL/SQL language is rich in built-in operators and provides the following types of operators −
• Arithmetic operators
• Relational operators
• Comparison operators
• Logical operators
• String operators
Arithmetic Operators
Following table shows all the arithmetic operators supported by PL/SQL. Let us
assume variable A holds 10 and variable B holds 5, then −
Relational Operators
Relational operators compare two expressions or values and return a Boolean result.
Following table shows all the relational operators supported by PL/SQL. Let us
assume variable A holds 10 and variable B holds 20, then −
Comparison Operators
Comparison operators are used for comparing one expression to another. The result is always
either TRUE, FALSE or NULL.
OperatorDescription Example
LIKE The LIKE operator compares a character, If 'Zara Ali' like 'Z% A_i' returns a
string, or CLOB value to a pattern and Boolean true, whereas, 'Nuha Ali'
returns TRUE if the value matches the like 'Z% A_i' returns a Boolean
pattern and FALSE if it does not. false.
BETWEEN The BETWEEN operator tests whether a If x = 10 then, x between 5 and 20
value lies in a specified range. x BETWEEN a returns true, x between 5 and 10
AND b means that x >= a and x <= b. returns true, but x between 11
and 20 returns false.
IN The IN operator tests set membership. x IN If x = 'm' then, x in ('a', 'b', 'c')
(set) means that x is equal to any member of returns Boolean false but x in
set. ('m', 'n', 'o') returns Boolean
true.
IS NULL The IS NULL operator returns the BOOLEAN If x = 'm', then 'x is null' returns
value TRUE if its operand is NULL or FALSE if Boolean false.
it is not NULL. Comparisons involving NULL
values always yield NULL.
Logical Operators
Following table shows the Logical operators supported by PL/SQL. All these operators work
on Boolean operands and produce Boolean results. Let us assume variable A holds true
and variable B holds false, then −
BEGIN
-- single-line comment, another comment
NULL;
/*
multi-line comment
that has another single-line comment
*/
END;
/
Example:
DECLARE
sid CONSTANT NUMBER := 101;
BEGIN
sid := 30; -- error
END;
IF Statements
IF statement allows you to either execute or skip a sequence of statements, depending on a
condition. The IF statement has the three forms:
– IF THEN
– IF THEN ELSE
– IF THEN ELSIF
IF condition THEN
statements;
END IF;
Example
DECLARE
age NUMBER :=21;
BEGIN
IF age>20 THEN
DBMS_OUTPUT.PUT_LINE('Age is greater than 20');
END IF;
END;
/
Output:
Statement processed.
Age is greater than 20
IF condition THEN
statements;
ELSE
else_statements;
END IF;
Example:
DECLARE
age NUMBER :=19;
BEGIN
IF age>20 THEN
DBMS_OUTPUT.PUT_LINE('Age is greater than 20');
ELSE
DBMS_OUTPUT.PUT_LINE('Age is less than 20');
END IF;
END;
/
Output:
Statement processed.
Age is less than 20
IF condition_1 THEN
statements_1
ELSIF condition_2 THEN
statements_2
ELSIF condition_3 THEN
statements_3
...
ELSE
else_statements
END IF;
Example
DECLARE
n_sales NUMBER := 300000;
n_commission NUMBER( 10, 2 ) := 0;
BEGIN
IF n_sales > 200000 THEN
n_commission := n_sales * 0.1;
ELSIF n_sales <= 200000 AND n_sales > 100000 THEN
n_commission := n_sales * 0.05;
ELSIF n_sales <= 100000 AND n_sales > 50000 THEN
n_commission := n_sales * 0.03;
ELSE
n_commission := n_sales * 0.02;
END IF;
DBMS_OUTPUT.PUT_LINE('Comission Amount is: ' || n_commission);
END;
/
Output:
Statement processed.
Comission Amount is: 30000
Nested IF Statement
IF condition_1 THEN
IF condition_2 THEN
nested_if_statements;
END IF;
ELSE
else_statements;
END IF;
DECLARE
sales NUMBER :=50000;
profit NUMBER :=15000;
BEGIN
IF sales>=50000 THEN
IF profit>20000 THEN
DBMS_OUTPUT.PUT_LINE('Excellent Sales!');
ELSE
DBMS_OUTPUT.PUT_LINE('Average Sales!');
END IF;
END IF;
END;
/
Output:
Statement processed.
Average Sales!
CASE selector
WHEN selector_value_1 THEN
statements_1
WHEN selector_value_1 THEN
statement_2
...
ELSE
else_statements
END CASE;
DECLARE
c_grade CHAR( 1 );
c_rank VARCHAR2( 20 );
BEGIN
c_grade := 'B';
CASE c_grade
WHEN 'A' THEN
c_rank := 'Excellent' ;
WHEN 'B' THEN
c_rank := 'Very Good' ;
WHEN 'C' THEN
c_rank := 'Good' ;
WHEN 'D' THEN
c_rank := 'Fair' ;
WHEN 'F' THEN
c_rank := 'Poor' ;
ELSE
c_rank := 'No such grade' ;
END CASE;
DBMS_OUTPUT.PUT_LINE( c_rank );
END;
Output:
Statement processed.
Very Good
CASE
WHEN condition_1 THEN statements_1
WHEN condition_2 THEN statements_2
...
WHEN condition_n THEN statements_n
[ ELSE
else_statements ]
END CASE;]
DECLARE
n_sales NUMBER;
n_commission NUMBER;
BEGIN
n_sales := 150000;
CASE
WHEN n_sales > 200000 THEN
n_commission := 0.2;
WHEN n_sales >= 100000 AND n_sales < 200000 THEN
n_commission := 0.15;
WHEN n_sales >= 50000 AND n_sales < 100000 THEN
n_commission := 0.1;
WHEN n_sales > 30000 THEN
n_commission := 0.05;
ELSE
n_commission := 0;
END CASE;
Output:
Statement processed.
Commission is 15%
GOTO statement
GOTO statement allows you to transfer control to a labeled block or statement. The following
illustrates the syntax of the GOTO statement:
GOTO label_name;
The label_name is the name of a label that identifies the target statement.
In the program, you surround the label name with double enclosing angle brackets as
shown below:
<<label_name>>;
BEGIN
GOTO second_message;
<<first_message>>
DBMS_OUTPUT.PUT_LINE( 'Hello' );
GOTO the_end;
<<second_message>>
DBMS_OUTPUT.PUT_LINE( 'PL/SQL GOTO Demo' );
GOTO first_message;
<<the_end>>
DBMS_OUTPUT.PUT_LINE( 'and good bye...' );
END;
Output:
PL/SQL GOTO Demo
Hello
and good Bye...
NULL Statement
The PL/SQL NULL statement has the following format:
NULL;
The NULL statement is a NULL keyword followed by a semicolon ( ;). The NULL statement
does nothing except that it passes control to the next statement.
The following code sends an email to employees whose job titles are Sales Representative.
Syntax:
<<label>> LOOP
statements;
END LOOP loop_label;
The LOOP statement executes the statements in its body and returns control to the top of the
loop. Typically, the body of the loop contains at least one EXIT or EXIT WHEN statement for
terminating the loop. Otherwise, the loop becomes an infinite loop.
The LOOP statement can have an optional label that appears at the beginning and the end
of the statement.
EXIT statement
The EXIT statement allows you to unconditionally exit the current iteration of a loop.
LOOP
EXIT;
END LOOP;
Typically, you use the EXIT statement with an IF statement to terminate a loop when a
condition is true:
LOOP
IF condition THEN
EXIT;
END IF;
END LOOP;
Output:
Statement processed.
Raazu Poudel
Raazu Poudel
Syntax:
EXIT WHEN condition;
Each time the control reaches the EXIT WHEN statement, the condition is evaluated. If the
condition evaluates to TRUE, then the loop terminates. Otherwise, the EXIT WHEN clause
does nothing. Inside the loop body, you must make the condition TRUE at some point to
prevent an infinite loop.
DECLARE
counter NUMBER :=0;
BEGIN
LOOP
counter :=counter+1;
DBMS_OUTPUT.PUT_LINE('Raazu Poudel');
EXIT WHEN counter>2;
END LOOP;
END;
/
Output:
Statement processed.
Raazu Poudel
Raazu Poudel
BEGIN
FOR l_counter IN 1..5
LOOP
DBMS_OUTPUT.PUT_LINE( l_counter );
END LOOP;
END;
Output:
1
2
3
4
5
DECLARE
l_step INTEGER := 2;
BEGIN
FOR l_counter IN 1..5 LOOP
dbms_output.put_line (l_counter*l_step);
END LOOP;
END;
Output:
2
4
6
8
10
BEGIN
FOR l_counter IN REVERSE 1..3
LOOP
DBMS_OUTPUT.PUT_LINE( l_counter );
END LOOP;
END;
Output:
3
2
1
WHILE Loop
WHILE loop statement to execute a sequence of statements as long as a specified condition
is TRUE.
WHILE condition
LOOP
statements;
END LOOP;
The following example illustrates how to use the WHILE loop statement:
DECLARE
n_counter NUMBER := 1;
BEGIN
WHILE n_counter <= 5
LOOP
DBMS_OUTPUT.PUT_LINE( 'Counter : ' || n_counter );
n_counter := n_counter + 1;
END LOOP;
END;
Output:
Counter : 1
Counter : 2
Counter : 3
Counter : 4
Counter : 5
DECLARE
n_counter NUMBER := 1;
BEGIN
WHILE n_counter <= 5
LOOP
DBMS_OUTPUT.PUT_LINE( 'Counter : ' || n_counter );
n_counter := n_counter + 1;
EXIT WHEN n_counter = 3;
END LOOP;
END;
Output:
Counter : 1
Counter : 2
CONTINUE statement
The CONTINUE statement allows you to exit the current loop iteration and immediately
continue on to the next iteration of that loop.
Syntax:
IF condition THEN
CONTINUE;
END IF;
The CONTINUE can be used in all loop constructs including LOOP, FOR LOOP and WHILE
LOOP.
The following is a simple example of using the CONTINUE statement to skip over loop body
execution for odd numbers:
BEGIN
FOR n_index IN 1 .. 10
LOOP
-- skip odd numbers
IF MOD( n_index, 2 ) = 1 THEN
CONTINUE;
END IF;
DBMS_OUTPUT.PUT_LINE( n_index );
END LOOP;
END;
Output:
2
4
6
8
10
The following example illustrates how to use the CONTINUE WHEN statement to skip over
loop body execution for even numbers:
BEGIN
FOR n_index IN 1 .. 10
LOOP
-- skip even numbers
CONTINUE
WHEN MOD( n_index, 2 ) = 0;
DBMS_OUTPUT.PUT_LINE( n_index );
END LOOP;
END;
Output:
1
3
5
7
9
Nested Loops
PL/SQL allows using one loop inside another loop.
LOOP
Sequence of statements1
LOOP
Sequence of statements2
END LOOP;
END LOOP;
The following program uses a nested basic loop to find the prime numbers from 2 to 100 −
DECLARE
i NUMBER;
j NUMBER;
BEGIN
i := 2;
LOOP
j:= 2;
LOOP
exit WHEN ((mod(i, j) = 0) or (j = i));
j := j +1;
END LOOP;
IF (j = i ) THEN
dbms_output.put_line(i || ' is prime');
END IF;
i := i + 1;
EXIT WHEN i = 50;
END LOOP;
END;
/
Output:
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
Each element in a varray has an index associated with it. It also has a maximum size that can
be changed dynamically.
The basic syntax for creating a VARRAY type within a PL/SQL block is −
For example −
Example – 1
DECLARE
Type numArray IS VARRAY(5) OF INTEGER;
arr numArray;
BEGIN
arr:=numArray(10,15,34,18,16);
FOR i IN 1..arr.count
LOOP
DBMS_OUTPUT.PUT_LINE(arr(i));
END LOOP;
END;
/
Output:
Statement processed.
10
15
34
18
16
DECLARE
type namesarray IS VARRAY(5) OF VARCHAR2(10);
type grades IS VARRAY(5) OF INTEGER;
names namesarray;
marks grades;
total integer;
BEGIN
names := namesarray('Kavita', 'Pritam', 'Ayan', 'Rishav',
'Aziz');
marks:= grades(98, 97, 78, 87, 92);
total := names.count;
dbms_output.put_line('Total '|| total || ' Students');
FOR i in 1 .. total LOOP
dbms_output.put_line('Student: ' || names(i) || '
Marks: ' || marks(i));
END LOOP;
END;
Output:
Total 5 Students
Student: Kavita Marks: 98
Student: Pritam Marks: 97
Student: Ayan Marks: 78
Student: Rishav Marks: 87
Student: Aziz Marks: 92
PL/SQL procedure successfully completed.
Strings in PL/SQL
PL/SQL is actually a sequence of characters with an optional size specification. The characters
could be numeric, letters, blank, special characters or a combination of all. PL/SQL offers three
kinds of strings −
• Fixed-length strings − In such strings, programmers specify the length while declaring
the string. The string is right-padded with spaces to the length so specified.
• Variable-length strings − In such strings, a maximum length up to 32,767, for the
string is specified and no padding takes place.
• Character large objects (CLOBs) − These are variable-length strings that can be up to
128 terabytes.
To include a single quote inside a string literal, you need to type two single quotes next to
one another. For example,
Oracle database provides numerous string datatypes, such as CHAR, NCHAR, VARCHAR2,
NVARCHAR2, CLOB, and NCLOB. The datatypes prefixed with an 'N' are 'national character
set' datatypes, that store Unicode character data.
DECLARE
name varchar2(20);
company varchar2(30);
introduction clob;
choice char(1);
BEGIN
name := 'John Smith';
company := 'Infotech';
introduction := ' Hello! I''m John Smith from Infotech.';
choice := 'y';
IF choice = 'y' THEN
dbms_output.put_line(name);
dbms_output.put_line(company);
dbms_output.put_line(introduction);
END IF;
END;
Output:
John Smith
Infotech
Hello! I'm John Smith from Infotech.
PL/SQL procedure successfully completed
DECLARE
greetings varchar2(11) := 'hello world';
BEGIN
dbms_output.put_line(UPPER(greetings));
dbms_output.put_line(LOWER(greetings));
dbms_output.put_line(INITCAP(greetings));
Output:
HELLO WORLD
hello world
Hello World
h
d
World
ello World
2
PL/SQL procedure successfully completed.
Example – 2
DECLARE
greetings varchar2(30) := '......Hello World.....';
BEGIN
dbms_output.put_line(RTRIM(greetings,'.'));
dbms_output.put_line(LTRIM(greetings, '.'));
dbms_output.put_line(TRIM( '.' from greetings));
END;
/
Nested block concept will help the programmer to improve the readability by separating the
complex things into each block and to handle the exception for each block inside the main
outer block.
A block can be nested into another block. This can be nested either in the execution part or
in the exception handling part. These block can also be labeled. One outer block can contain
many inner blocks. Each inner block is once again a PL/SQL block, hence all the properties and
characteristics of the inner block will be the same as outer block.
The below image gives the pictorial representation of nested block structure. Parent block is
the main block and child block is the nested block.
<<inner block>>
DECLARE
<Declarative section>
Example:
<<outer_block>>
DECLARE
BEGIN
DBMS_OUTPUT.PUT_LINE('Before inner block');
<<inner_block>>
DECLARE
BEGIN
DBMS_OUTPUT.PUT_LINE('Inside inner block');
END;
DBMS_OUTPUT.PUT_LINE('After inner block');
END;
Output:
Statement processed.
Before inner block
Inside inner block
After inner block
The following illustrates the syntax of the PL/SQL SELECT INTO statement:
SELECT
select_list
INTO
variable_list
FROM
table_name
WHERE
condition;
Table: employee
DECLARE
--ename VARCHAR2(30);
--If you are uncertain about type then
ename employee.name%TYPE;
BEGIN
SELECT name INTO ename
FROM employee
WHERE eid=103;
--displaying name
DBMS_OUTPUT.PUT_LINE(ename);
END;
/
Output:
Statement processed.
Hari
DECLARE
e_salary employee.salary%TYPE;
BEGIN
SELECT MAX(salary) INTO e_salary
FROM employee;
--displaying name
DBMS_OUTPUT.PUT_LINE('Max Salary is: '||e_salary);
END;
/
Output:
Statement processed.
Max Salary is: 55000
DECLARE
r_employee employee%ROWTYPE;
BEGIN
SELECT * INTO r_employee
FROM employee
WHERE eid=102;
Output:
Statement processed.
Employee Id: 102
Name: Shyam
Salary: 40000
DECLARE
eid employee.eid%TYPE;
ename employee.name%TYPE;
esalary employee.salary%TYPE;
BEGIN
SELECT eid,name,salary INTO eid,ename,esalary
FROM employee
WHERE eid=105;
--displaying values
DBMS_OUTPUT.PUT_LINE('Employee Id: ' || eid);
DBMS_OUTPUT.PUT_LINE('Name: ' || ename);
DBMS_OUTPUT.PUT_LINE('Salary: ' || esalary);
END;
/
Output:
Statement processed.
Employee Id: 105
Name: Sita
Salary: 55000
DECLARE
id NUMBER:=102;
myvar employee%ROWTYPE;
BEGIN
SELECT * INTO myvar
FROM employee
WHERE eid=id;
--displaying data
DBMS_OUTPUT.PUT_LINE('Employee Id: ' || myvar.eid);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || myvar.name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || myvar.salary);
END;
/
Output:
Statement processed.
Employee Id: 102
Employee Name: Shyam
Employee Salary: 40000
DECLARE
id employee.eid%TYPE:=&id;
name employee.name%TYPE:=&name;
salary employee.salary%TYPE:=&salary;
BEGIN
INSERT INTO employee
VALUES(id,name,salary);
END;
/
DECLARE
basic_salary NUMBER:=25000;
bonus NUMBER:= 5000;
total_salary NUMBER;
BEGIN
total_salary:=basic_salary+bonus;
INSERT INTO employee VALUES('110','Raaju',total_salary);
END;
/
DECLARE
e_id employee.eid%TYPE;
BEGIN
--selecting maximum id from table
SELECT MAX(eid) INTO e_id
FROM employee;
--inserting data
INSERT INTO employee VALUES(e_id+1,'Raaju Poudel',34000);
END;
/
Using Sequence:
In Oracle, you can create an autonumber field by using sequences. A sequence is an object
in Oracle that is used to generate a number sequence. This can be useful when you need to
create a unique number to act as a primary key.
Syntax:
CREATE SEQUENCE sequence_name
MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value;
Dropping Sequence:
DROP SEQUENCE myseq;
BEGIN
INSERT INTO employee
VALUES(myseq.NEXTVAL,'Rajasvi Poudel',45000);
END;
/
Let’s calculate 10% commission on salary and update for eid 103.
DECLARE
e_salary employee.salary%TYPE;
e_commission NUMBER;
BEGIN
SELECT salary INTO e_salary
FROM employee WHERE eid=103;
e_commission:=(e_salary*10)/100;
--updating after calculation
UPDATE employee SET commission=e_commission
WHERE eid=103;
END;
/
DECLARE
e_salary employee.salary%TYPE;
e_commission NUMBER;
BEGIN
FOR val IN 101..105
LOOP
--calulating equal 10% commission
SELECT salary INTO e_salary FROM employee WHERE eid=val;
e_commission:=(e_salary*10)/100;
--updating after calculation
UPDATE employee SET commission=e_commission WHERE eid=val;
END LOOP;
END;
/
DECLARE
e_salary employee.salary%TYPE;
e_commission NUMBER;
BEGIN
FOR val IN 101..105
LOOP
SELECT salary INTO e_salary FROM employee WHERE eid=val;
--applying condition
IF e_salary>=50000 THEN
e_commission:=(e_salary*15)/100;
ELSIF e_salary>=40000 AND e_salary<50000 THEN
e_commission:=(e_salary*10)/100;
ELSE
e_commission:=(e_salary*5)/100;
END IF;
COMMIT
• COMMIT will save all the DML changes since the last committed state
• When the user issues COMMIT, it is known as end of transaction
• Transaction can be defined as logical unit of work
• All DDL commands are automatically commits
commit;
commit emp999;
SAVEPOINT
• You can rollback up to SAVEPOINT
• Transaction is a unit of work, SAVEPOINT is a sub-unit of work
• You cannot COMMITto a SAVEPOINT, commit will save all DML changes since the last
committed state
• When you ROKLLBACK or COMMIT, then intermediate SAVEPOINT are automatically
cleared
• You can only ROLLBACK sequentially
• Within a transaction, you can have Two SAVEPOINT with the same name; the latest
SAVEPOINT will supersede the previous one
SAVEPOINT abc;
ROLLBACK to abc;
ROLLBACK
• ROLLBACK will undo the DML changes since the last committed state
• only DML commands are affected by ROLLBACK and COMMIT
ROLLBACK;
ROLLBACK emp999;
PL/SQL Example:
Let’s consider following table student:
BEGIN
INSERT INTO student VALUES(103,'Hari');
COMMIT;
BEGIN
UPDATE student SET name='Raaju' WHERE sid=103;
SAVEPOINT test1;
ROLLBACK TO test1;
END;
/