You are on page 1of 9

Experiment No: - 8

LAB MANUAL
PART A
(PART A: TO BE REFFERED BY STUDENTS)

Experiment Name: - Stored Procedure

Aim: - Performing practical by using stored procedure.

Resource required: - Oracle 9i - iSQLplus

Theory: -

● What is Subprograms?

-Subprogram is a sequence of statements that are repeated more than once in a


program.
- After the subprogram is created and stored in the database, it can be invoked any
number of times and from multiple applications.
- PL/SQL has two types of subprograms: 1. Procedures
2. Functions.
Procedure:
- A procedure is a type of subprogram that performs an action.
- It can be stored in the database, as a schema object, for repeated execution.

Syntax for creating procedures:

CREATE [OR REPLACE] PROCEDURE procedure_name


[ (parameter1 [mode1] datatype1,
Parameter2 [mode2] datatype2,
. . . . )]
IS | AS
PL/SQL Block;

- The CREATE clause enables to create stand-alone procedures, which are stored in
database.

Formal Versus Actual Parameters:


- Formal Parameters: variable declared in the parameter list of a subprogram
specification.

Example:
CREATE PROCEDURE raise_sal (p_id NUMBER, p_amount NUMBER)
…..
END raise_sal;

- Actual Parameters: variables or expressions referenced in the parameter list of a


subprogram call.
Example:
raise_sal (v_id, 2000)

- Actual parameters are evaluated and results are assigned to formal parameters
during the subprogram call.
- It is good practice to use different names for formal and actual parameters.
- The formal and actual parameters should be of compatible data types.

Procedural Parameter Modes:


You can transfer values to and from the calling environment through parameters.
There are three modes for each parameter:
Types of Parameter Description8
IN (default) Passes a constant value from the calling environment into the
procedure.
OUT Passes a value from the procedure to the calling environment.
IN OUT Passes a value from the calling environment into the procedure
and a possibly different value from the procedure back to the
calling environment using the same parameter.

8.1 SAMPLE EXAMPLES:


Consider Relation/Table/Entity: Employees, Departments.
Following question for practice. And Students are required to write the output.

Q: IN Parameter:
CREATE OR REPLACE PROCEDURE raise_salary
(P_id IN employees.employee_id%TYPE)
IS
BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = p_id;
END raise_salary;
/
Output:

Q: EXECUTE raise_salary (176)


Output:
Q: OUT Parameter:
CREATE OR REPLACE PROCEDURE query_emp
(p_id IN employees.employee_id%TYPE,
p_name OUT employees.last_name%TYPE, p_salary
OUT employees.salary%TYPE, p_comm OUT
employee.commission_pct%TYPE)
IS
BEGIN
SELECT last_name, salary, commission_pct
INTO p_name, p_salary, p_comm
FROM employees
WHERE employee_id = p_id;
END query_emp;
/
Output:
Q: VARIABLE g_name VARCHAR2 (25)
VARIABLE g_sal NUMBER
VARIABLE g_comm NUMBER
EXECUTE query_emp (171,:g_name, :g_sal, :g_comm)

PRINT g_nameg_salg_comm
Output:

Q: IN OUT Parameter:
CREATE OR REPLACE PROCEDURE format_phone
(p_phone_no IN OUT VARCHAR2)
IS
BEGIN
p_phone_no := ‘(‘ || SUBSTR ( p_phone_no , 1, 3) ||
‘)’ || SUBSTR ( p_phone_no, 4, 3) ||
‘-‘|| SUBSTR (p_phone_no, 7);
END format_phone;
/
Output:

Q:
VARIABLE g_phone_no VARCHAR2 (15)
BEGIN
:g_phone_no := ‘8006330575’;
END
/
PRINT g_pone_no
EXECUTE format_phone (:g_phone_no)
PRINT g_phone_no
/
Output:
Q: Local Subprograms:
CREATE OR REPLACE PROCEDURE leave_emp2
(p_id IN employees.employee_id%TYPE)
IS
PROCEDURE log_exec
IS
BEGIN
INSERT INTO log_table (user_id, log_date)
VALUES (USER, SYSDATE);
END log_exec;
BEGIN
DELETE FROM employees
WHERE employee_id = p-id;
log_exec;
END leave_emp2;
/
Output:
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the Blackboard or emailed to the concerned lab
in charge faculties at the end of the practical in case the there is no Black board access
available)

Roll No.: 39 Name: Devesh Rajbhar


Class: SE Comps A Batch:A2
Date of Experiment:29-04-2021 Date of Submission: 13-05-2021
Grade:

B.1.Execute the following

1. Create a procedure called ADD_JOB procedure to insert a new job into the JOBS table.
Provide the ID and title of the job, using two parameters.

2. Compile the code, and invoke the procedure with IT_DBA as job ID and Database
Administrator as job title. Query the JOBS table to view the results.

3. Invoke the procedure again, passing a job ID of ST_MAN and job title of Stock Manager.
What happens and why?

4. Create a procedure called QUERY_EMP to query the EMPLOYEES table, retrieving the
salary and job ID for an employee when provided with the employee ID.

B.2 Input and Output:


(Command and its output)

1. Command:
CREATE OR REPLACE PROCEDURE add_job(proc_jidjobs.job_id%TYPE,
proc_jtitlejobs.job_title%TYPE) IS BEGIN
INSERT INTO jobs(job_id,job_title)
VALUES(proc_jid, proc_jtitle);
END add_job;
/

Output:Procedure created.
2. Command:
EXEC add_job('MK_MAN','Marketing Manager');
SELECT *FROM jobs;

Output:
Statement processed.
JOB_ID JOB_TITLE
AD_PRES President
IT_PROG Programmer
SA_MAN Sales Manager
SA_REP Sales Representative
ST_MAN Stock Manager
ST_CLERK Stock Clerk
MK_MAN Marketing Manager
3. Command:
EXEC add_job('ST_MAN','Stock Manager');

Output:
ORA-00001: unique constraint (SQL_HSSZWRQTLGWGLDHSXIWFFCJJG.SYS_C0057707695)
violated ORA-06512: at "SQL_HSSZWRQTLGWGLDHSXIWFFCJJG.ADD_JOB", line 3 ORA-06512:
at line 1 ORA-06512: at "SYS.DBMS_SQL", line 1721

Here, we face a primary key constraint error called ‘unique constraint’ as the primary key
‘ST_MAN’ already exists in the table. So we cannot add another primary key with the same
name as primary keys are always unique.
4. Command:
CREATE OR REPLACE PROCEDURE query_emp(proc_eid
IN employees.employee_id%TYPE, proc_sal OUT
employees.salary%TYPE,proc_jid OUT
employees.job_id%TYPE) IS
BEGIN
SELECT salary,job_id
INTO proc_sal,proc_jid
FROM employees
WHERE employee_id=proc_eid;
END query_emp;
/
Output: Procedure created.
B.3 Observations and learning:
(Students are expected to comment on the output obtained with clear observations and
learning for each task/ sub part assigned)

In this experiment, we first created a procedure add_job() to add a new job title and job ID into
the pre-existing table ‘jobs’. We then added a new job ID and title, then experimented with
adding an already existing one to see the results.

We then created a procedure query_emp() to retrieve the salary and job ID of an employee when
the query is given the employee’s ID.

B.4 Conclusion:
(Students must write the conclusion as per the attainment of individual outcome listed above
and learning/observation noted in section B.3)

We have thus, created our own procedures and executed them to retrieve data.Hence, we have
successfully performed practical using stored procedures in this experiment.

B. 5 Questions:
1.What is procedure? And why it is used?

Ans.:

A stored procedure can be defined as a set of logically written statements, stored in the database
and is executed when called, to perform a specific task. It has a unique named block of code
which is compiled and stored in the database.

Following are some uses of stored procedure in SQL:

a. Improves database performance


b. Provides reusability and avoids redundancy
c. Maintains integrity
d. Maintains security
e. Saves memory

You might also like