0% found this document useful (0 votes)
16 views4 pages

Send

Uploaded by

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

Send

Uploaded by

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

create table dept(idDept number primary key, nomDept varchar2(100), Pays

varchar2(100));
create table emp (id number primary key, nomEmp varchar2(100), salaire number,
deptID number references Dept (idDept));

insert into dept values (1, 'Baabda', 'Liban');


insert into dept values (2, 'Zahle', 'Liban');
insert into dept values (3, 'Dijon', 'France');
insert into dept values (4, 'Grenoble', 'France');

insert into emp values (1, 'Bob', 1000, 1);

insert into emp values (2, 'Alice', 1200, 1);


insert into emp values (3, 'Khalil', 900, 2);
insert into emp values (4, 'Samer', 1100, 2);
insert into emp values (5, 'Fadi', 1100, 1);
insert into emp values (6, 'Fawziya', 900, 3);
insert into emp values (7, 'Toto', 1500, 3);
insert into emp values (8, 'Titi', 1400, 3);
insert into emp values (9, 'Tony', 1700, 4);
insert into emp values (10, 'Fan', 1600, 4);

PL/SQL

solve the following questions:


1. Create a stored procedure that allows you to select the name of an employee (id
IN integer)

2. Create a function that returns the name of an employee (id IN integer)

3. Create a stored procedure that insert a department.

4. Create a function fnewid which allows to know the next id of the employee who
must be registered.

5. Create a stored procedure that allows you to register employees.


This procedure should be defined as follows:
CREATE OR REPLACE PROCEDURE enreg_empl (vprenom IN varchar, vsalaire IN number,
vdept IN varchar)
* id is the value returned by the fnewid function
You are not obliged (for the moment) to test the existence of a department using
this procedure, however it will be interesting if you propose a solution

6. Create a function nbempl which allows to return the number of employees per
department (given that the name of the department as a parameter for the function)

7. Create a stored procedure that returns the maximum and minimum salary.

8. Write a stored procedure pro_emp_salary which takes two employee identifiers


(id_1, id_2) as parameters and displays who has the highest salary.

9. Write a function fct_emp_salary which takes two employee identifiers (id_1,


id_2) as parameters and displays who has the number of employees who have a salary
lower than these two employees.

10. Develop a function fct_test_salary which allows you to take a numerical value
as a parameter and test it against the average salary of the employees. It is
necessary to treat the three cases (value larger than the average, smaller and
equal to the average). Note that the value returned by the function must be in the
form of a character string.
Test the result against the employees of the emp table.

-- 1. Create a stored procedure that allows you to select the name of an employee
(id IN integer)
CREATE OR REPLACE PROCEDURE get_emp_name (p_id IN NUMBER)
IS
v_emp_name VARCHAR2(100);
BEGIN
SELECT nomEmp INTO v_emp_name FROM emp WHERE id = p_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
END;
/

-- 2. Create a function that returns the name of an employee (id IN integer)


CREATE OR REPLACE FUNCTION get_emp_name_func (p_id IN NUMBER)
RETURN VARCHAR2
IS
v_emp_name VARCHAR2(100);
BEGIN
SELECT nomEmp INTO v_emp_name FROM emp WHERE id = p_id;
RETURN v_emp_name;
END;
/

-- 3. Create a stored procedure that insert a department


CREATE OR REPLACE PROCEDURE insert_dept (p_id IN NUMBER, p_nomDept IN VARCHAR2,
p_Pays IN VARCHAR2)
IS
BEGIN
INSERT INTO dept (idDept, nomDept, Pays) VALUES (p_id, p_nomDept, p_Pays);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Department inserted successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error inserting department: ' || SQLERRM);
END;
/

-- 4. Create a function fnewid which allows to know the next id of the employee who
must be registered
CREATE OR REPLACE FUNCTION fnewid RETURN NUMBER
IS
v_next_id NUMBER;
BEGIN
SELECT MAX(id) + 1 INTO v_next_id FROM emp;
RETURN COALESCE(v_next_id, 1); -- Handle case when emp table is empty
END;
/

-- 5. Create a stored procedure that allows you to register employees


CREATE OR REPLACE PROCEDURE enreg_empl (vprenom IN VARCHAR2, vsalaire IN NUMBER,
vdept IN VARCHAR2)
IS
v_id NUMBER := fnewid();
BEGIN
INSERT INTO emp (id, nomEmp, salaire, deptID) VALUES (v_id, vprenom, vsalaire,
(SELECT idDept FROM dept WHERE nomDept = vdept));
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee ' || vprenom || ' registered successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error registering employee: ' || SQLERRM);
END;
/

-- 6. Create a function nbempl which allows to return the number of employees per
department
CREATE OR REPLACE FUNCTION nbempl (p_dept_name IN VARCHAR2) RETURN NUMBER
IS
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count FROM emp e JOIN dept d ON e.deptID = d.idDept
WHERE d.nomDept = p_dept_name;
RETURN v_count;
END;
/

-- 7. Create a stored procedure that returns the maximum and minimum salary
CREATE OR REPLACE PROCEDURE max_min_salary
IS
v_max_salary NUMBER;
v_min_salary NUMBER;
BEGIN
SELECT MAX(salaire), MIN(salaire) INTO v_max_salary, v_min_salary FROM emp;
DBMS_OUTPUT.PUT_LINE('Maximum Salary: ' || v_max_salary);
DBMS_OUTPUT.PUT_LINE('Minimum Salary: ' || v_min_salary);
END;
/

-- 8. Write a stored procedure pro_emp_salary which takes two employee identifiers


(id_1, id_2) as parameters and displays who has the highest salary
CREATE OR REPLACE PROCEDURE pro_emp_salary (p_id_1 IN NUMBER, p_id_2 IN NUMBER)
IS
v_salary_1 NUMBER;
v_salary_2 NUMBER;
BEGIN
SELECT salaire INTO v_salary_1 FROM emp WHERE id = p_id_1;
SELECT salaire INTO v_salary_2 FROM emp WHERE id = p_id_2;

IF v_salary_1 > v_salary_2 THEN


DBMS_OUTPUT.PUT_LINE('Employee with ID ' || p_id_1 || ' has the highest
salary.');
ELSIF v_salary_1 < v_salary_2 THEN
DBMS_OUTPUT.PUT_LINE('Employee with ID ' || p_id_2 || ' has the highest
salary.');
ELSE
DBMS_OUTPUT.PUT_LINE('Both employees have the same salary.');
END IF;
END;
/

-- 9. Write a function fct_emp_salary which takes two employee identifiers (id_1,


id_2) as parameters and displays who has the number of employees who have a salary
lower than these two employees
CREATE OR REPLACE FUNCTION fct_emp_salary (p_id_1 IN NUMBER, p_id_2 IN NUMBER)
RETURN VARCHAR2
IS
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count FROM emp WHERE salaire < (SELECT salaire FROM emp
WHERE id = p_id_1) AND salaire < (SELECT salaire FROM emp WHERE id = p_id_2);

IF v_count = 0 THEN
RETURN 'Both employees have the lowest salary.';
ELSE
RETURN 'There are ' || v_count || ' employees with a lower salary than
both.';
END IF;
END;
/

-- 10. Develop a function fct_test_salary which allows you to take a numerical


value as a parameter and test it against the average salary of the employees
CREATE OR REPLACE FUNCTION fct_test_salary (p_value IN NUMBER) RETURN VARCHAR2
IS
v_avg_salary NUMBER;
BEGIN
SELECT AVG(salaire) INTO v_avg_salary FROM emp;

IF p_value > v_avg_salary THEN


RETURN 'Value larger than the average salary.';
ELSIF p_value < v_avg_salary THEN
RETURN 'Value smaller than the average salary.';
ELSE
RETURN 'Value equal to the average salary.';
END IF;
END;
/

You might also like