Sie sind auf Seite 1von 4

CURSOR

Oracle creates a memory area, known as context area, for processing an SQL statement, which contains
all information needed for processing the statement, for example, number of rows processed, etc.
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.
There are two types of cursors:
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;

=====================================================================================
EXCEPTION
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:
1. System-defined exceptions
2. User-defined exceptions
=====================================================================================



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.
Benefits of Triggers
Triggers can be written for the following purposes:
Generating some derived column values automatically
Enforcing referential integrity
Event logging and storing information on table access
Auditing
Synchronous replication of tables
Imposing security authorizations
Preventing invalid transactions









transaction
A database transaction is an atomic unit of work that may consist of one or more related SQL
statements. It is called atomic because the database modifications brought about by the SQL statements
that constitute a transaction can collectively be either committed, i.e., made permanent to the database
or rolled back (undone) from the database.
A successfully executed SQL statement and a committed transaction are not same. Even if an SQL
statement is executed successfully, unless the transaction containing the statement is committed, it can
be rolled back and all changes made by the statement(s) can be undone.
Starting an Ending a Transaction
A transaction has a beginning and an end. A transaction starts when one of the following events take
place:
The first SQL statement is performed after connecting to the database.
At each new SQL statement issued after a transaction is completed.
A transaction ends when one of the following events take place:
A COMMIT or a ROLLBACK statement is issued.
A DDL statement, like CREATE TABLE statement, is issued; because in that case a COMMIT is
automatically performed.
A DCL statement, such as a GRANT statement, is issued; because in that case a COMMIT is automatically
performed.
User disconnects from the database.
User exits from SQL*PLUS by issuing the EXIT command, a COMMIT is automatically performed.
SQL*Plus terminates abnormally, a ROLLBACK is automatically performed.
A DML statement fails; in that case a ROLLBACK is automatically performed for undoing that DML
statement.
Committing a Transaction
A transaction is made permanent by issuing the SQL command COMMIT. The general syntax for the
COMMIT command is:
COMMIT;

=====================================================================================
Stored Procedures are pre-compile objects which are compiled for first time and its compiled
format is saved which executes (compiled code) whenever it is called. But Function is compiled and
executed every time when it is called. For more about stored procedure and function refer the
articles Different types of Stored Procedure and Different types of Function.
Basic Difference
1.

Function must return a value but in Stored Procedure it is optional( Procedure can return zero or n
values).
2.

Functions can have only input parameters for it whereas Procedures can have input/output
parameters .
3.

Functions can be called from Procedure whereas Procedures cannot be called from Function.
Advance Difference
1.

Procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statement in it whereas Function
allows only SELECT statement in it.
2.

Procedures cannot be utilized in a SELECT statement whereas Function can be embedded in a
SELECT statement.
3.

Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT
section whereas Function can be.
4.

The most important feature of stored procedures over function is to retention and reuse the
execution plan while in case of function it will be compiled every time.
5.

Functions that return tables can be treated as another rowset. This can be used in JOINs with other
tables.
6.

Inline Function can be though of as views that take parameters and can be used in JOINs and other
Rowset operations.
7.

Exception can be handled by try-catch block in a Procedure whereas try-catch block cannot be used
in a Function.
8.

We can go for Transaction Management in Procedure whereas we can't go in Function.

Das könnte Ihnen auch gefallen