Dbms Exercise 5
Dbms Exercise 5
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:
COMMIT;