0% found this document useful (0 votes)
51 views

Group C Assignments

This document provides information about PL/SQL stored procedures, functions, and triggers. It discusses creating and executing procedures and functions, and different types of parameters that can be passed to procedures. It also covers creating row-level and statement-level triggers in PL/SQL. Examples of creating each type of subprogram and running queries are referenced but not shown.

Uploaded by

Vaishnavi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Group C Assignments

This document provides information about PL/SQL stored procedures, functions, and triggers. It discusses creating and executing procedures and functions, and different types of parameters that can be passed to procedures. It also covers creating row-level and statement-level triggers in PL/SQL. Examples of creating each type of subprogram and running queries are referenced but not shown.

Uploaded by

Vaishnavi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Name: Vaishnavi Kailas Raut

Seat no. :S190088621

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:

• To study and implement PL/SQL procedure.

• To study and implement PL/SQL function.


Theory : A subprogram is a program unit/module that performs a particular task. These
subprograms are combined to form larger programs. This is basically called the 'Modular design'.
A subprogram can be invoked by another subprogram or program which is called the calling
program.

A subprogram can be created –

• At the schema level

• Inside a package

• Inside a PL/SQL block


At the schema level, subprogram is a standalone subprogram. It is created with the CREATE
PROCEDURE or the CREATE FUNCTION statement. It is stored in the database and can be
deleted with the DROP PROCEDURE or DROP FUNCTION statement.

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

A procedure is created with the CREATE OR REPLACE PROCEDURE statement. The


simplified syntax for the CREATE OR REPLACE PROCEDURE statement is as follows –

CREATE [OR REPLACE] PROCEDURE procedure_name

[(parameter_name [IN | OUT | IN OUT] type [, ...])]

{IS | AS}

BEGIN

< procedure_body >

END procedure_name;

Where, • procedure-name specifies the name of the procedure.

• [OR REPLACE] option allows the modification of an existing procedure.

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

• procedure-body contains the executable part.

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

CREATE OR REPLACE PROCEDURE greetings AS BEGIN dbms_output.put_line('Hello


World!'); END; /

Deleting a Standalone Procedure

A standalone procedure is deleted with the DROP PROCEDURE statement. Syntax for deleting
a procedure is –

DROP PROCEDURE procedure-name;

You can drop the greetings procedure by using the following statement –

DROP PROCEDURE greetings;

Parameter Modes in PL/SQL Subprograms

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.

// provide other example queries and outputs (screenshots)


for procedure taken into lab session
Functions:

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 –

CREATE [OR REPLACE] FUNCTION function_name

[(parameter_name [IN | OUT | IN OUT] type [, ...])]

RETURN return_datatype

{IS | AS}

BEGIN

< function_body >

END [function_name];

Where,

• function-name specifies the name of the function.

• [OR REPLACE] option allows the modification of an existing function.

• 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 function must contain a return statement.

• The RETURN clause specifies the data type you are going to return from the function.

• function-body contains the executable part.

• The AS keyword is used instead of the IS keyword for creating a standalone function.

//provide other example queries and outputs(screenshots)


taken into lab session
Conclusion:- We have studied and executed PL/SQL stored procedures and functions.
Assignment No :- 2
Aim :- Write and execute suitable database triggers .Consider row level and statement level
triggers.

Objective:

• To study and implement PL/SQL triggers.

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.

• 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 can 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 Creating Triggers

The syntax for creating a trigger is –

CREATE [OR REPLACE ] TRIGGER trigger_name


{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}

[OF col_name]

ON table_name

[REFERENCING OLD AS o NEW AS n]

[FOR EACH ROW]

WHEN (condition)

DECLARE Declaration-statements BEGIN Executable-statements

EXCEPTION Exception-handling-statements END;

Where,

• CREATE [OR REPLACE] TRIGGER trigger_name − Creates or replaces an existing trigger


with the trigger_name.

• {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.

//provide other example queries and outputs(screenshots)


taken into lab session
Conclusion:- We have studied and executed different types of database triggers.
Assignment No :- 3
Aim :- Write a PL/SQL block to implement all types of cursor.

Objective:

• To study and implement PL/SQL cursors

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

Returns the number of rows affected by an INSERT, UPDATE, or DELETE statement, or


returned by a SELECT INTO statement.

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


Declaring the Cursor

Declaring the cursor defines the cursor with a name and the associated SELECT statement. For
example

CURSOR c_customers IS SELECT id, name, address FROM customers;

Opening the Cursor

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

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

Closing the cursor means releasing the allocated memory. For example, we will close the above-
opened cursor as follows –

CLOSE c_customers;

//provide other example queries and outputs(screenshots)


taken into lab session

Conclusion:- We have studied and implemented cursors in PL/SQL.

You might also like