Group C Assignments
Group C Assignments
Group C: PL/SQL
Assignment No :- 1
Aim :- . Write and execute PL/SQL stored procedure and function to perform a suitable task on
the database. Demonstrate its use.
Objective:
• Inside a package
PL/SQL subprograms are named PL/SQL blocks that can be invoked with a set of parameters.
PL/SQL provides two kinds of subprograms –
• Functions − these subprograms return a single value; mainly used to compute and
return a value.
• Procedures − these subprograms do not return a value directly; mainly used to perform
an action.
Parts of a PL/SQL Subprogram- Each PL/SQL subprogram has a name, and may also have a
parameter list. Like anonymous PL/SQL blocks, the named blocks will also have the following
three parts –
1. Declarative Part - It is an optional part. However, the declarative part for a subprogram
does not start with the DECLARE keyword. It contains declarations of types, cursors, constants,
variables, exceptions, and nested subprograms. These items are local to the subprogram and
cease to exist when the subprogram completes execution.
2. Executable Part - This is a mandatory part and contains statements that perform the
designated action.
3. Exception - handling this is again an optional part. It contains the code that handles run-
time errors
Creating a Procedure
{IS | AS}
BEGIN
END procedure_name;
• The optional parameter list contains name, mode and types of the parameters. IN represents the
value that will be passed from outside and OUT represents the parameter that will be used to
return a value outside of the procedure.
• The AS keyword is used instead of the IS keyword for creating a standalone procedure.
Example
The following example creates a simple procedure that displays the string 'Hello World!' on the
screen when executed.
A standalone procedure is deleted with the DROP PROCEDURE statement. Syntax for deleting
a procedure is –
You can drop the greetings procedure by using the following statement –
The following table lists out the parameter modes in PL/SQL subprograms –
IN
An IN parameter lets you pass a value to the subprogram. It is a read-only parameter. Inside the
subprogram, an IN parameter acts like a constant. It cannot be assigned a value. You can pass a
constant, literal, initialized variable, or expression as an IN parameter. You can also initialize it
to a default value; however, in that case, it is omitted from the subprogram call. It is the default
mode of parameter passing. Parameters are passed by reference.
OUT
An OUT parameter returns a value to the calling program. Inside the subprogram, an OUT
parameter acts like a variable. You can change its value and reference the value after assigning it.
The actual parameter must be variable and it is passed by value.
IN OUT
An IN OUT parameter passes an initial value to a subprogram and returns an updated value to
the caller. It can be assigned a value and the value can be read. The actual parameter
corresponding to an IN OUT formal parameter must be a variable, not a constant or an
expression. Formal parameter must be assigned a value. Actual parameter is passed by value.
A function is same as a procedure except that it returns a value. Therefore, all the discussions of
the previous chapter are true for functions too.
Creating a Function
A standalone function is created using the CREATE FUNCTION statement. The simplified
syntax for the CREATE OR REPLACE PROCEDURE statement is as follows –
RETURN return_datatype
{IS | AS}
BEGIN
END [function_name];
Where,
• The optional parameter list contains name, mode and types of the parameters. IN represents the
value that will be passed from outside and OUT represents the parameter that will be used to
return a value outside of the procedure.
• The RETURN clause specifies the data type you are going to return from the function.
• The AS keyword is used instead of the IS keyword for creating a standalone function.
Objective:
Theory :
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 can be defined on the table, view, schema, or database with which the event is
associated.
Benefits of Triggers
• Auditing
[OF col_name]
ON table_name
WHEN (condition)
Where,
• {BEFORE | AFTER | INSTEAD OF} − This specifies when the trigger will be executed. The
INSTEAD OF clause is used for creating trigger on a view.
• {INSERT [OR] | UPDATE [OR] | DELETE} − This specifies the DML operation.
• [OF col_name] − This specifies the column name that will be updated.
• [ON table_name] − This specifies the name of the table associated with the trigger.
• [REFERENCING OLD AS o NEW AS n] − This allows you to refer new and old values for
various DML statements, such as INSERT, UPDATE, and DELETE.
• [FOR EACH ROW] − This specifies a row-level trigger, i.e., the trigger will be executed for
each row being affected. Otherwise the trigger will execute just once when the SQL statement
is executed, which is called a table level trigger.
• WHEN (condition) − this provides a condition for rows for which the trigger would fire. This
clause is valid only for row-level triggers.
Objective:
Theory :
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.
You can name a cursor so that it could be referred to in a program to fetch and process the rows
returned by the SQL statement, one at a time. There are two types of cursors – Implicit cursors
and Explicit 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.
In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor, which always has
attributes such as %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT. The SQL cursor
has additional attributes, %BULK_ROWCOUNT and %BULK_EXCEPTIONS, designed for
use with the FORALL statement. The following table provides the description of the most used
attributes –
%FOUND
Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or more rows or a
SELECT INTO statement returned one or more rows. Otherwise, it returns FALSE
%NOTFOUND
The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or DELETE
statement affected no rows, or a SELECT INTO statement returned no rows. Otherwise, it
returns FALSE.
%ISOPEN
Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor automatically
after executing its associated SQL statement.
%ROWCOUNT
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.
Declaring the cursor defines the cursor with a name and the associated SELECT statement. For
example
Opening the cursor allocates the memory for the cursor and makes it ready for fetching the rows
returned by the SQL statement into it. For example, we will open the above defined cursor as
follows –
OPEN c_customers;
Fetching the cursor involves accessing one row at a time. For example, we will fetch rows from
the above-opened cursor as follows –
FETCH c_customers INTO c_id, c_name, c_addr;
Closing the cursor means releasing the allocated memory. For example, we will close the above-
opened cursor as follows –
CLOSE c_customers;