Send
Send
varchar2(100));
create table emp (id number primary key, nomEmp varchar2(100), salaire number,
deptID number references Dept (idDept));
PL/SQL
4. Create a function fnewid which allows to know the next id of the employee who
must be registered.
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.
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;
/
-- 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;
/
-- 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;
/
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;
/