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

Part-2-Lab Maual - Dbms-Final

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

Part-2-Lab Maual - Dbms-Final

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

JAZAN UNIVERSITY

COLLEGE OF COMPUTER SCIENCE AND INFORMATION


TECHNOLOGY

LAB MANUAL (PART-2]

DATA BASE MANAGEMENT SYSTEMS

ITEC-212

Edited by
Ms.Padmanayaki Selvarajan

COURSE COORDINATOR
Lecturer, Department of Information
Technology and Security
College of Computer Science and
Information Technology

ITC 212-LAB MANUAL-PART-2 Page 1


2021-2022 [Spring], 1442-43(H)
PART-2
PL/SQL Procedures , Functions , Cursors and
Triggers
PL/SQL Procedures and Functions
Creation PL/SQL Cursors PL/SQL Triggers
Calling Declare
Reading , Updating and Deleting Open
Fetch
Close

ITC 212-LAB MANUAL-PART-2 Page 2


PROCEDURES

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;
/
When the above code is executed using the SQL prompt, it will produce the following result −
Procedure created.
Executing a Standalone Procedure
A standalone procedure can be called in two ways −
 Using the EXECUTE keyword
 Calling the name of the procedure from a PL/SQL block
The above procedure named 'greetings' can be called with the EXECUTE keyword as −
EXECUTE greetings;
The above call will display −
ITC 212-LAB MANUAL-PART-2 Page 3
Hello World

PL/SQL procedure successfully completed.


The procedure can also be called from another PL/SQL block −
BEGIN
greetings;
END;
/
The above call will display −
Hello World

PL/SQL procedure successfully completed.


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 −

S.No Parameter Mode & Description

IN
An IN parameter lets you pass a value to the subprogram. It is a read-only parameter.
1 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

2 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
3 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

ITC 212-LAB MANUAL-PART-2 Page 4


parameter is passed by value.

IN & OUT Mode Example 1


This program finds the minimum of two values. Here, the procedure takes two numbers using the
IN mode and returns their minimum using the OUT parameters.
DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
/

When the above code is executed at the SQL prompt, it produces the following result −
Minimum of (23, 45) : 23

PL/SQL procedure successfully completed.

IN & OUT Mode Example 2


This procedure computes the square of value of a passed value. This example shows how we
can use the same parameter to accept a value and then return another result.
DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
/

When the above code is executed at the SQL prompt, it produces the following result −
Square of (23): 529

PL/SQL procedure successfully completed.

ITC 212-LAB MANUAL-PART-2 Page 5


FUNCTIONS

Function is a standalone PL/SQL subprogram. Like PL/SQL procedure, functions have


a unique name by which it can be referred. These are stored as PL/SQL database
objects.

PROGRAM

ITC 212-LAB MANUAL-PART-2 Page 6


PL/SQL procedure successfully completed.

RESULT:
Thus, the functions and stored procedures are executed in SQL.

CURSOR

In SQL procedures, a cursor make it possible to define a result set (a set of data rows)
and perform complex logic on a row by row basis.

Implicit cursors

Whenever Oracle executes an SQL statement such as SELECT INTO, INSERT, UPDATE,
and DELETE, it automatically creates an implicit cursor.

Oracle internally manages the whole execution cycle of implicit cursors and reveals only the
cursor’s information and statuses such as SQL%ROWCOUNT, SQL%ISOPEN, SQL%FOUND,
and SQL%NOTFOUND.

Explicit cursors

An explicit cursor is an SELECT statement declared explicitly in the declaration section of the
current block or a package specification.

For an explicit cursor, you have control over its execution cycle from OPEN, FETCH, and CLOSE

i) Declare a cursor

CURSOR cursor_name IS query;

In this syntax:

 First, specify the name of the cursor after the CURSOR keyword.
 Second, define a query to fetch data after the IS keyword.

ii) Open a cursor

Before start fetching rows from the cursor, you must open it. To open a cursor, you use the
following syntax:

OPEN Cursor_name;

ITC 212-LAB MANUAL-PART-2 Page 7


In this syntax, the cursor_name is the name of the cursor declared in the declaration section.

When you open a cursor, Oracle parses the query, binds variables, and executes the associated
SQL statement.

Oracle also determines an execution plan, associates host variables and cursor parameters with
the placeholders in the SQL statement, determines the result set, and sets the cursor to the first
row in the result set.

iii) Fetch from a cursor

The FETCH statement places the contents of the current row into variables. The syntax of FETCH

statement is as follows:

FETCH cursor_name INTO variable_list;

To retrieve all rows in a result set, you need to fetch each row till the last one.

iv) Closing a cursor

After fetching all rows, you need to close the cursor with the CLOSE statement:

CLOSE cursor_name;

Closing a cursor instructs Oracle to release allocated memory at an appropriate time.

If you declare a cursor in an anonymous block, procedure, or function, the cursor will automatically

be closed when the execution of these objects end.

Explicit Cursor Attributes

A cursor has four attributes to which you can reference in the following format:

Cursor_name%attribute

where cursor_name is the name of the explicit cursor.

1) %ISOPEN

This attribute is TRUE if the cursor is open or FALSE if it is not.

2) %FOUND

This attribute has four values:

 NULL before the first fetch


 TRUE if a record was fetched successfully
 FALSE if no row returned

ITC 212-LAB MANUAL-PART-2 Page 8


 INVALID_CURSOR if the cursor is not opened

3) %NOTFOUND

This attribute has four values:

 NULL before the first fetch


 FALSE if a record was fetched successfully
 TRUE if no row returned
 INVALID_CURSOR if the cursor is not opened

4) %ROWCOUNT

The %ROWCOUNT attribute returns the number of rows fetched from the cursor. If the cursor is
not opened, this attribute returns INVALID_CURSOR.

Example Program Cursor :

Create customer table – name, age, address, salary using sql query.

ITC 212-LAB MANUAL-PART-2 Page 9


.

3. customers selected

4. PL/SQL procedure successfully completed.

5. Select * from customers;

+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Ahmedabad | 2500.00 |
| 2 | Khilan | 25 | Delhi | 2000.00 |
| 3 | kaushik | 23 | Kota | 2500.00 |
| 4 | Chaitali | 25 | Mumbai | 7000.00 |
| 5 | Hardik | 27 | Bhopal | 9000.00 |
| 6 | Komal | 22 | MP | 5000.00 |
+ + + + + +

Results: The cursor program is executed..

ITC 212-LAB MANUAL-PART-2 Page 10


TRIGGERS

Triggers provide a way of executing PL/SQL code on the occurrence of specific database events.
For example, you can maintain an audit log by setting triggers to fire when insert or update
operations are carried out on a table. The insert and update triggers add an entry to an audit
table whenever the table is altered.

PROCEDURE
step 1: Start

step 2: Initialize the trigger with specific table id.

step 3: Specify the operations (update, delete, insert) for


which

the trigger has to be executed.

step 4: Execute the trigger procedure for both


before and after sequences .

step 5: Carryout the operation on the table to check


for trigger execution.

step 6: stop

PROGRAM

ITC 212-LAB MANUAL-PART-2 Page 11


Code line 34-38: Update the location of "XXX" to 'FRANCE'. It raised
the exception because the DML statements are not allowed in the
complex view.

To avoid the error encounter during updating view in the previous step,
in this step we are going to use "instead of trigger."

ITC 212-LAB MANUAL-PART-2 Page 12


RESULT:

The trigger procedure has been executed successfully for both


before and after sequences.

ITC 212-LAB MANUAL-PART-2 Page 13

You might also like