Professional Documents
Culture Documents
LAB MANUAL
PART A
(PART A: TO BE REFFERED BY STUDENTS)
Theory: -
● What is Subprograms?
- The CREATE clause enables to create stand-alone procedures, which are stored in
database.
Example:
CREATE PROCEDURE raise_sal (p_id NUMBER, p_amount NUMBER)
…..
END raise_sal;
- 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.
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:
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)
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.
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.