0% found this document useful (0 votes)
10 views11 pages

Dbms Exercise 5

PL/SQL is a procedural language extension to SQL that allows developers to combine SQL's power with procedural programming features. It consists of three main sections: the Declare section for variable declarations, the Execution section for program logic, and the Exception section for error handling. PL/SQL supports various programming constructs, including variables, cursors, and transaction control commands like COMMIT, ROLLBACK, and SAVEPOINT.

Uploaded by

ashraysuhas000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

Dbms Exercise 5

PL/SQL is a procedural language extension to SQL that allows developers to combine SQL's power with procedural programming features. It consists of three main sections: the Declare section for variable declarations, the Execution section for program logic, and the Exception section for error handling. PL/SQL supports various programming constructs, including variables, cursors, and transaction control commands like COMMIT, ROLLBACK, and SAVEPOINT.

Uploaded by

ashraysuhas000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

PL/SQL Introduction:

PL/SQL stands for procedural language extensions to the


Structured Query Language. PL/SQL is a block structured
language that enables developers to combine the power of SQL
with procedural statements. All the statements of a block are
passed to oracle engine all at once which increases processing
speed and decreases the traffic.
PL/SQL is a combination of SQL along with the
procedural features of programming languages.
Oracle uses a PL/SQL engine to processes the PL/SQL
statements.
PL/SQL includes procedural language elements like
conditions and loops. It allows declaration of constants
and variables, procedures and functions, types and
variable of those types and triggers.
Structure of PL/SQL:
Declare section starts with DECLARE keyword in which
variables, constants, records as cursors can be declared
which stores data temporarily. It basically consists
definition of PL/SQL identifiers. This part of the code is
optional.
Execution section starts with BEGIN and ends with END
keyword. This is a mandatory section and here the
program logic is written to perform any task like loops and
conditional statements. It supports all DML commands,
DDL commands and SQL*PLUS built-in functions as
well.
Exception section starts with EXCEPTION keyword. This
section is optional which contains statements that are
executed when a run-time error occurs. Any exceptions
can be handled in this section.
PL/SQL identifiers
There are several PL/SQL identifiers such as variables,
constants, procedures, cursors, triggers etc.
1. Variables: Like several other programming languages,
variables in PL/SQL must be declared prior to its use.
They should have a valid name and data type as well.

Syntax for declaration of variables:

variable_name datatype [NOT NULL := value ];


Explanation:

SET SERVEROUTPUT ON: It is used to display


the buffer used by the dbms_output.
var1 INTEGER : It is the declaration of variable,
named var1 which is of integer type. There are many
other data types that can be used like float, int, real,
smallint, long etc. It also supports variables used in
SQL as well like NUMBER(prec, scale), varchar,
varchar2 etc.
PL/SQL procedure successfully completed.: It is
displayed when the code is compiled and executed
successfully.
Slash (/) after END;: The slash (/) tells the
SQL*Plus to execute the block.
Assignment operator (:=) : It is used to assign a
value to a variable.
Displaying Output: The outputs are displayed by using
DBMS_OUTPUT which is a built-in package that enables the
user to display output, debugging information, and send
messages from PL/SQL blocks, subprograms, packages, and
triggers.
Let us see an example to see how to display a message using
PL/SQL:

Explanation:
dbms_output.put_line: This command is used to direct the
PL/SQL output to a screen.
Using Comments: Like in many other programming
languages, in PL/SQL also, comments can be put within the
code which has no effect in the code.
There are two syntaxes to create comments in PL/SQL:
Single Line Comment: To create a single line comment, the
symbol – – is used.
Multi Line Comment: To create comments that span over
several lines, the symbol /* and */ is used.
Taking input from user: Just like in other programming
languages, in PL/SQL also, we can take input from the user and
store it in a variable.
Let us see an example to show how to take input from users in
PL/SQL:

Cursors:
A PL/SQL construct called a cursor lets you name a work area
and access its stored information.
There are two kinds of cursors: implicit and explicit. PL/SQL
implicitly declares a cursor for all SQL data manipulation
statements, including queries that return only one row. For
queries that return more than one row, you can explicitly
declare a cursor to process the rows individually. An example
follows:
The set of rows returned by a multi-row query is called the
result set. Its size is the number of rows that meet your search
criteria. As Figure shows, an explicit cursor "points" to the
current row in the result set. This allows your program to
process the rows one at a time.

Exercise – 5
5a) Creation of simple PL/SQL program which includes
declaration section, executable section and exception –
Handling section (Ex. Student marks can be selected from the
table and printed for those who secured first class and an
exception can be raised if no records were found)
Aim: To create a PL/SQL program
Create a table named students with attributes sid and sname
and sclass, sclass means first, second, third like that. Insert
some data into the table.
ii) Insert data into student table and use COMMIT,
ROLLBACK and SAVEPOINT in PL/SQL block.
Create table:

Savepoint:
Savepoint is a command in SQL that is used with the
rollback command.
It is a command in Transaction Control Language that is
used to mark the transaction in a table.
Consider you are making a very long table, and you want
to roll back only to a certain position in a table then; this
can be achieved using the savepoint.
If you made a transaction in a table, you could mark the
transaction as a certain name, and later on, if you want to
roll back to that point, you can do it easily by using the
transaction's name.
Savepoint is helpful when we want to roll back only a
small part of a table and not the whole table. In simple
words, we can say savepoint is a bookmark in SQL.
Rollback:
Rollback refers to the process of returning a database to
its previous state.
Commit:

A transaction is made permanent by issuing the SQL


command COMMIT. The general syntax for the
COMMIT command is −

COMMIT;

PL/SQL program to insert data and to use savepoint


rollback and commit.

SQL> set serveroutput on;


SQL> begin
savepoint g;
insert into stu values(7,’ruhi','cse');
exception
when dup_val_on_index then
rollback to g;
commit;
end;
/
SQL>Rollback to t;

You might also like