Explicit Cursors
Explicit Cursors
20BCE2413
Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed, when there is no explicit
cursor for the statement. Programmers cannot control the implicit cursors and the information in it.
Whenever a DML statement (INSERT, UPDATE and DELETE) is issued, an implicit cursor is associated with this
statement. For INSERT operations, the cursor holds the data that needs to be inserted. For UPDATE and DELETE
operations, the cursor identifies the rows that would be affected.
Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over the context area.
An explicit cursor should be defined in the declaration section of the PL/SQL Block. It is created
on a SELECT Statement which returns more than one row.
The syntax for creating an explicit cursor is −
CURSOR cursor_name IS select_statement;
Working with an explicit cursor includes the following steps −
Declaring the cursor for initializing the memory
Opening the cursor for allocating the memory
Fetching the cursor for retrieving the data
Closing the cursor to release the allocated memory
SYNTAX
CURSOR c_customers IS
SELECT id, name, address FROM customers;
OPEN c_customers;
CLOSE c_customers;
1.
2.
Triggers
Triggers are stored programs, which are automatically executed or fired when some events occur. Triggers are, in fact, written to be
executed in response to any of the following events:
Triggers could be defined on the table, view, schema, or database with which the event is associated.
Exception handling
An error condition during a program execution is called an exception in PL/SQL. PL/SQL supports programmers to catch such
conditions using EXCEPTION block in the program and an appropriate action is taken against the error condition. There are two types of
exceptions:
System-defined exceptions
User-defined exceptions
Some of the popular System defined exceptions are out of memory and division by zero, having names like
STORAGE_ERROR and ZERO_DIVIDE respectively.
User-defined exceptions are declared in a package, subprogram, or within the declaration section of the PL/SQL block of code
and should be assigned names.
Once an exception occurs, the natural flow of execution is halted, and then the execution points to the exception section of the
PL/SQL code.
While the system defined exceptions are thrown by default, the user-defined ones have to be thrown explicitly by the
RAISE keyword.
Thus the exception handling helps to deal with the errors that are encountered during the run time execution and not while
compiling the program.
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
exception1-handling-statements
exception2-handling-statements
exception3-handling-statements
........
exception3-handling-statements
END;
While looping in SQL Server
SQL Server 2022 Express is a free edition of SQL Server, ideal for development and production for desktop, web, and small server
applications.
The SQL While loop is used to repeatedly execute a certain piece of SQL script.
This article covers some of the basic functionalities of the SQL While loop in Microsoft SQL Server, with
the help of examples.
WHILE condition
BEGIN
//SQL Statements
END;
The while loop in SQL begins with the WHILE keyword followed by the condition which returns a Boolean
value i.e. True or False.
The body of the while loop keeps executing unless the condition returns false. The body of a while loop in
SQL starts with a BEGIN block and ends with an END block.