0% found this document useful (0 votes)
14 views11 pages

Exp 24

Uploaded by

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

Exp 24

Uploaded by

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

DEPARTMENT OF INFORMATION TECHNOLOGY

Subject: Database Management System Subject Code: 313315


Semester: 3rd Course: IF3K-B
Laboratory No: L004C Name of Subject Teacher: Prerana
Jalgaonkar
Name of Student: Kamya Pawar Roll Id: 23202B0042

Practical No: 24
Title of Experiment Create Procedures and stored procedures for modularity

X. Practical related questions


1. Write syntax for creating PL/SQL Procedure.
Ans:
CREATE OR REPLACE PROCEDURE procedure_name

(parameter1_name parameter1_type [IN | OUT | IN OUT],

parameter2_name parameter2_type [IN | OUT | IN OUT], ... )

IS

-- Declaration Section

BEGIN

-- Executable Section

-- PL/SQL statements

END procedure_name;

2. Write steps to call a procedure in PL/SQL


Ans:
Steps to Call a Procedure in Oracle PL/SQL
Direct Call:

You can directly call the procedure by its name.

EXECUTE procedure_name(parameter1_value, parameter2_value, ...);

Anonymous Block:

You can call the procedure within an anonymous PL/SQL block.

BEGIN
procedure_name(parameter1_value, parameter2_value, ...);
END;

3. List types of parameters in Procedure and explain them.


Ans:
DEPARTMENT OF INFORMATION TECHNOLOGY
Types of Parameters in Oracle SQL Procedures
 IN Parameter:

Description: Passes values to the procedure; the procedure can read but not modify them.
Example: Passing a student's name to display it.

 OUT Parameter:
Description: Returns values from the procedure; the procedure can modify the value.
Example: Calculating and returning a student's total marks.

 IN OUT Parameter:

Description: Reads and modifies the value; the modified value is accessible after execution.
Example: Updating an account balance after applying interest.

XI. Exercise
1. Write a procedure emp_count () to count number of employees in department, use
dept_no as input parameter
Ans:
Step 1:
CREATE TABLE employees
( ename VARCHAR2(50),
dname VARCHAR2(50),
job VARCHAR2(50),
empno NUMBER,
hiredate VARCHAR2(10),
loc VARCHAR2(50) );
DEPARTMENT OF INFORMATION TECHNOLOGY
INSERT INTO employees VALUES ('ADAMS', 'RESEARCH', 'CLERK', 7876, '23-MAY-87',
'DALLAS');
INSERT INTO employees VALUES ('ALLEN', 'SALES', 'SALESMAN', 7499, '20-FEB-81',
'CHICAGO');
INSERT INTO employees VALUES ('BLAKE', 'SALES', 'MANAGER', 7698, '01-MAY-81',
'CHICAGO');
INSERT INTO employees VALUES ('CLARK', 'ACCOUNTING', 'MANAGER', 7782, '09-JUN-81',
'NEW YORK');
INSERT INTO employees VALUES ('FORD', 'RESEARCH', 'ANALYST', 7902, '03-DEC-81',
'DALLAS');
INSERT INTO employees VALUES ('JAMES', 'SALES', 'CLERK', 7900, '03-DEC-81', 'CHICAGO');
INSERT INTO employees VALUES ('JONES', 'RESEARCH', 'MANAGER', 7566, '02-APR-81',
'DALLAS');
INSERT INTO employees VALUES ('KING', 'ACCOUNTING', 'PRESIDENT', 7839, '17-NOV-81',
'NEW YORK');
INSERT INTO employees VALUES ('MARTIN', 'SALES', 'SALESMAN', 7654, '28-SEP-81',
'CHICAGO');
INSERT INTO employees VALUES ('MILLER', 'ACCOUNTING', 'CLERK', 7934, '23-JAN-82', 'NEW
YORK');
INSERT INTO employees VALUES ('SCOTT', 'RESEARCH', 'ANALYST', 7788, '19-APR-87',
'DALLAS');
INSERT INTO employees VALUES ('SMITH', 'RESEARCH', 'CLERK', 7369, '17-DEC-80',
'DALLAS');
INSERT INTO employees VALUES ('TURNER', 'SALES', 'SALESMAN', 7844, '08-SEP-81',
'CHICAGO');
INSERT INTO employees VALUES ('WARD', 'SALES', 'SALESMAN', 7521, '22-FEB-81',
'CHICAGO');
DEPARTMENT OF INFORMATION TECHNOLOGY
Step 2:
CREATE OR REPLACE PROCEDURE emp_count (dept_no IN NUMBER) IS

emp_count_value NUMBER;

BEGIN

SELECT COUNT(*) INTO emp_count_value

FROM employees

WHERE DEPT_NO = dept_no;

DBMS_OUTPUT.PUT_LINE('Number of employees in department ' || dept_no || ': ' ||


emp_count_value);

END ;
DEPARTMENT OF INFORMATION TECHNOLOGY
Step 3:
Calling emp_count Procedure
DECLARE
dept_number NUMBER := 10; -- Specify the department number
BEGIN
emp_count(dept_number); -- Call the procedure
END;
DEPARTMENT OF INFORMATION TECHNOLOGY
2. Create a stored procedure to accept name and greet user with name.
Ans:
Step 1:
CREATE OR REPLACE PROCEDURE greet_user (user_name IN VARCHAR2) IS

BEGIN

DBMS_OUTPUT.PUT_LINE('Hello, ' || user_name || '!');

END;
DEPARTMENT OF INFORMATION TECHNOLOGY
Step 2:
Calling greet_user Procedure

DECLARE
user_name VARCHAR2(50) := 'Prerana'; -- Specify the user name
BEGIN
greet_user(user_name); -- Call the procedure
END;
DEPARTMENT OF INFORMATION TECHNOLOGY
3. Write a PL/SQL procedure to insert any three records in EMP table.
Ans:
Step 1:
CREATE TABLE emp
( empno NUMBER(10) ,
ename VARCHAR2(50),
deptno NUMBER(10));

Step 2:

CREATE OR REPLACE PROCEDURE insert_emp_records IS

BEGIN

INSERT INTO EMP VALUES (1, 'Alice', 10);

INSERT INTO EMP VALUES (2, 'John', 20);

INSERT INTO EMP VALUES (3, 'Charlie', 30);

COMMIT; -- Make sure to commit the changes

END ;
DEPARTMENT OF INFORMATION TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY
Step 3:
Calling insert_emp_records Procedure

BEGIN
insert_emp_records(); -- Call the procedure to insert records
END;
DEPARTMENT OF INFORMATION TECHNOLOGY

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy