0% found this document useful (0 votes)
62 views6 pages

Explicit Cursors

1. Cursors allow programmers to control the context area in PL/SQL and hold rows returned by SQL statements. There are implicit cursors created automatically and explicit cursors that are programmer-defined. 2. Explicit cursors require declaring, opening, fetching from, and closing the cursor. Implicit cursors cannot be controlled by the programmer. 3. The document discusses triggers, exception handling, and while loops in SQL Server. Triggers automatically execute on database events. Exception handling allows catching errors at runtime. While loops repeatedly execute until the condition is false.

Uploaded by

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

Explicit Cursors

1. Cursors allow programmers to control the context area in PL/SQL and hold rows returned by SQL statements. There are implicit cursors created automatically and explicit cursors that are programmer-defined. 2. Explicit cursors require declaring, opening, fetching from, and closing the cursor. Implicit cursors cannot be controlled by the programmer. 3. The document discusses triggers, exception handling, and while loops in SQL Server. Triggers automatically execute on database events. Exception handling allows catching errors at runtime. While loops repeatedly execute until the condition is false.

Uploaded by

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

K Namithaa

20BCE2413

20-03-23 PRINCIPLES OF DATABASE MANAGEMENT


LAB ASSIGNMENT
EXP- 7,8,9,10
A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor. A cursor holds the rows
(one or more) returned by a SQL statement. The set of rows the cursor holds is referred to as the active set.

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;

FETCH c_customers INTO c_id, c_name, c_addr;

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:

 A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).

 A database definition (DDL) statement (CREATE, ALTER, or DROP).

 A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).

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.

Syntax for Exception Handling


The General Syntax for exception handling is as follows. Here you can list down as many as exceptions you want to handle. The default
exception will be handled using WHEN others THEN:

DECLARE

<declarations section>

BEGIN

<executable command(s)>

EXCEPTION

<exception handling goes here >

WHEN exception1 THEN

exception1-handling-statements

WHEN exception2 THEN

exception2-handling-statements

WHEN exception3 THEN

exception3-handling-statements

........

WHEN others THEN

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.

SQL While loop: Understanding While loops in SQL Server

 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.

SQL While loop syntax


The syntax of the SQL While loop is as follows:

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.

You might also like