Part-2-Lab Maual - Dbms-Final
Part-2-Lab Maual - Dbms-Final
ITEC-212
Edited by
Ms.Padmanayaki Selvarajan
COURSE COORDINATOR
Lecturer, Department of Information
Technology and Security
College of Computer Science and
Information Technology
Creating a Procedure
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
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
When the above code is executed at the SQL prompt, it produces the following result −
Minimum of (23, 45) : 23
When the above code is executed at the SQL prompt, it produces the following result −
Square of (23): 529
PROGRAM
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
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.
Before start fetching rows from the cursor, you must open it. To open a cursor, you use the
following syntax:
OPEN Cursor_name;
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.
The FETCH statement places the contents of the current row into variables. The syntax of FETCH
statement is as follows:
To retrieve all rows in a result set, you need to fetch each row till the last one.
After fetching all rows, you need to close the cursor with the CLOSE statement:
CLOSE cursor_name;
If you declare a cursor in an anonymous block, procedure, or function, the cursor will automatically
A cursor has four attributes to which you can reference in the following format:
Cursor_name%attribute
1) %ISOPEN
2) %FOUND
3) %NOTFOUND
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.
Create customer table – name, age, address, salary using sql query.
3. customers selected
+ + + + + +
| 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 |
+ + + + + +
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 6: stop
PROGRAM
To avoid the error encounter during updating view in the previous step,
in this step we are going to use "instead of trigger."