delete rows from the employee table - oracle

Create a procedure that deletes rows from the employee table. it should accept 1 parameter, department name; only delete the employee records belonging to that department. display how many employees were deleted else raise "DeptNotFoundException" and print the message "No Records Found.".
Employee:
column name datatype constraints
EMP_ID NUMBER(5) PK
EMP_NMAE VARCHAR2(25) NOT NULL
SALARY NUMBER(10,2)
DEPT VARCHAR2(25)
EMP_ID EMP_NAME SALARY DEPT
101 JOHN 54000 MECH
102 TOM 43000 CSE
103 WILLIAM 34560 MECH
104 STEVE 56000 CSE
105 SMITH 23450 IT
and my code is,
set serveroutput on;
create or replace procedure delete_EMPLOYEE(dept_name in EMPLOYEE.dept%type) is
deptnotfound EXCEPTION;
begin
delete from EMPLOYEE where dept=dept_name;
if (sql%found) then
dbms_output.put_line(sql%rowcount || 'Employee record(s) got deleted.');
else
raise DeptNoFound;
end if;
EXCEPTION
when DeptNoFound then
dbms_output.put_line('No Records Found.');
end delete_EMPLOYEE;
/
it compiled successfully but not the test case. only 1 test case is passed instead of 2. can anyone help me out, please....

SET SERVEROUTPUT ON;
CREATE OR RPLACE PROCEDURE DELETE_EMPLOYEE(V_DEPT IN EMPLOYEE.DEPT%TYPE)
IS
DEPTNOTFOUNDEXCEPTION EXCEPTION;
BEGIN
DELETE FROM EMPLOYEE WHERE DEPT = V_DEPT;
IF SQL%FOUND THEN
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' EMPLOYEE RECORS(S) GOT DELETED.');
ELSE
RAISE DEPTNOTFOUNDEXCEPTION;
END IF;
EXCEPTION
WHEN DEPTNOTFOUNDEXCEPTION THEN
DBMS_OUTPUT.PUT_LINE('NO RECORDS FOUND.');
END;
/

You have declared exception deptnotfound and while raising it you have spelled it incorrectly as deptnofound.

Related

Procedure to delete employee records from employee table

Create a procedure that deletes employee records from the Employee table. Get the department name as an input parameter. Delete the employee records who belongs to that department.
Display the count of employee records that were deleted. If the respective department was not found, then raise "DeptNotFoundException" and print the message 'No Records found.'
Assume the Employee table has been already created and a few records have been inserted.
EMPLOYEE:
Column name Data type Constraints
EMP_ID NUMBER(5) PK
EMP_NAME VARCHAR2(25) NOT NULL
SALARY NUMBER(10,2)
DEPT VARCHAR2(25)
EMP_ID EMP_NAME SALARY DEPT
------ -------- ------- -----
101 Tom 54000 MECH
102 William 43000 CSE
103 John 34560 MECH
104 Smith 56000 CSE
105 Steve 23450 IT
Functional Requirements:
PROCEDURE DELETE_EMPLOYEE( v_dept IN EMPLOYEE.dept%TYPE)
Sample Output:
2 Employee record(s) got deleted.
(Hint: Data is case sensitive. Use '/' to terminate the PLSQL block)
I have tried to solve this using my own logic, but it is showing error(out of 2 test only one is passed) can anyone point out the mistake?
set serveroutput on;
create or replace PROCEDURE DELETE_EMPLOYEE(v_dept IN EMPLOYEE.dept%TYPE)
is
temp number;
DEPTNOTFOUNDEXCEPTION Exception;
begin
select count(dept) into temp from EMPLOYEE where dept=v_dept;
delete from EMPLOYEE where dept=v_dept;
if temp>=1 then
dbms_output.put_line(temp||' Employee record(s) got deleted.');
else
raise DEPTNOTFOUNDEXCEPTION;
end if;
exception
when DEPTNOTFOUNDEXCEPTION then
dbms_output.put_line('No Records Found.');
end;
/
As others have pointed out, you can use SQL%ROWCOUNT and generally tidy up a bit but it doesn't look "wrong".
CREATE TABLE employee (
emp_id NUMBER(5) PRIMARY KEY
, emp_name VARCHAR2(25) NOT NULL
, salary NUMBER(10,2)
, dept VARCHAR2(25)
);
INSERT INTO employee (emp_id, emp_name, salary, dept) VALUES (101, 'Tom', 54000, 'MECH');
INSERT INTO employee (emp_id, emp_name, salary, dept) VALUES (102, 'William', 43000, 'CSE');
INSERT INTO employee (emp_id, emp_name, salary, dept) VALUES (103, 'John', 34560, 'MECH');
INSERT INTO employee (emp_id, emp_name, salary, dept) VALUES (104, 'Smith', 56000, 'CSE');
INSERT INTO employee (emp_id, emp_name, salary, dept) VALUES (105, 'Steve', 23450, 'IT');
DECLARE
dept_not_found EXCEPTION;
PROCEDURE delete_employee (p_dept IN employee.dept%TYPE) IS
BEGIN
DELETE employee
WHERE dept = p_dept;
IF SQL%ROWCOUNT = 0 THEN
RAISE dept_not_found;
END IF;
dbms_output.put_line(SQL%ROWCOUNT||' Employee record(s) got deleted.');
END delete_employee;
BEGIN
delete_employee('CSE');
delete_employee('ZZZ');
EXCEPTION
WHEN dept_not_found THEN
dbms_output.put_line('No Records Found.');
END;
/
2 Employee record(s) got deleted.
No Records Found.
PL/SQL procedure successfully completed.
Only an advice: Stop using raise when you want to output something. raise have always much more workload than:
if (sql%rowcount = 0) then
DBMS_Output.Put_Line('No Record Found');
return;
end if;

Procedure to insert details by raising exceptions in PL/SQL

I want to create a procedure to insert employee details into Employee table. Before inserting, check whether the employee age is eligible or not. Employee age should be 18 or greater. Values are passed as argument to the procedure. If age valid, insert employee record into table and print the message "Age valid - Record inserted", else print the message "Age invalid - Record not inserted" by raising an exception.
Table: EMPLOYEE
Column name Data type Constraints
EMP_ID NUMBER(5) PK
EMP_NAME VARCHAR2(25) NOT NULL
AGE NUMBER(3)
Functional Requirement:
PROCEDURE CHECK_AGE_ELIGIBILITY(
v_id IN EMPLOYEE.EMPID%TYPE,
v_name IN EMPLOYEE.EMPNAME%TYPE,
v_age IN EMPLOYEE.AGE%TYPE)
I have written a code for this as-
set serveroutput on;
create or replace procedure check_age_eligibility(
v_id in employee.empid%type,
v_name in employee.empname%type,
v_age in employee.age%type)
is emp_rec employee%rowtype;
declare
Employee_age number;
BEGIN
SELECT trunc(MONTHS_BETWEEN(TO_DATE(sysdate,'DD-MON-YYYY'), TO_DATE(:new.DATE_OF_BIRTH,'DD-MON-YYYY'))/12)
INTO Employee_age FROM DUAL;
IF (Employee_age >= 18) THEN
update employee;
dbms_output.put_line('Age valid - Record inserted');
else
dbms_output.put_line('Age invalid - Record not inserted');
END IF;
end;
/
Please check for any errors since my code editor is not showing any output for this code. Thanks in advance!
You're overcomplicating it. There's no "date of birth" there; you're working with age (presumably expressed in years).
So: procedure:
SQL> create or replace procedure check_age_eligibility
2 (par_id in employee.emp_id%type,
3 par_emp_name in employee.emp_name%type,
4 par_age in employee.age%type
5 )
6 is
7 begin
8 if par_age >= 18 then
9 insert into employee (emp_id, emp_name, age)
10 values
11 (par_id, par_emp_name, par_age);
12
13 dbms_output.put_line('Age valid - record inserted');
14 else
15 raise_application_error(-20000, 'Age invalid - record not inserted');
16 end if;
17 end;
18 /
Procedure created.
Testing:
SQL> set serveroutput on
SQL> exec check_age_eligibility(1, 'Little', 12);
BEGIN check_age_eligibility(1, 'Little', 12); END;
*
ERROR at line 1:
ORA-20000: Age invalid - record not inserted
ORA-06512: at "SCOTT.CHECK_AGE_ELIGIBILITY", line 15
ORA-06512: at line 1
SQL> exec check_age_eligibility(1, 'Foot' , 20);
Age valid - record inserted
PL/SQL procedure successfully completed.
SQL> select * From employee;
EMP_ID EMP_NAME AGE
---------- -------------------- ----------
1 Foot 20
SQL>

To display the count of rows deleted using pl/sql

I want to create a procedure that deletes rows from the Employee table. It should accept 1 parameter that is department name; only delete the employee records belonging to that department. Display how many employees were deleted else raise "DeptNotFoundException" and print the message 'No Records found.'.
EMPLOYEE:
Column name Data type Constraints
EMP_ID NUMBER(5) PK
EMP_NAME VARCHAR2(25) NOT NULL
SALARY NUMBER(10,2)
DEPT VARCHAR2(25)
Sample Output:
2 Employee record(s) got deleted.
The code I tried is -
create or replace procedure DELETE_EMPLOYEE(v_dept IN EMPLOYEE.dept%TYPE)
is
begin
delete EMPLOYEE where dept = v_dept;
commit;
end;
/
It works fine to delete the rows, but I don't know how to find the count of the deleted rows and display it in the result, please help regarding this. Thank you for help.
Hint: sql%rowcount.
SQL> create or replace procedure DELETE_EMPLOYEE(v_dept IN EMPLOYEE.dept%TYPE)
2 is
3 begin
4 delete EMPLOYEE where dept = v_dept;
5 dbms_output.put_Line('deleted ' || sql%rowcount || ' row(s)');
6 end;
7 /
Procedure created.
SQL> set serveroutput on
SQL> exec delete_employee(10);
deleted 3 row(s)
PL/SQL procedure successfully completed.
SQL>
Also, I wouldn't commit in the procedure; let the caller decide and commit (or not).

Count the no of records

Write a program that gives all employees in Mechanical department,
(i) 15% pay increase.
(ii) display a message displaying how many employees were awarded the increase. if no Employees found then print the message 'No Records found'.
Employee :
columnname Data type constraints
EMPID NUMBER(5) PK
EMP_NAME VARCHAR(25) NOT NULL
SALARY NUMBER(10,2)
DEPT VARCHAR(25)
EMP_ID EMP_NAME SALARY DEPT
101 TOM 54000 MECH
102 WILLIAM 43000 CSE
103 JOHN 34560 MECH
104 SMITH 56000 CSE
105 STEVE 23450 IT
Sample Output : 2 Employee got increment.
and i did like this,
create or replace procedure empsal as
emp employee%rowtype;
sal number ;
cursor cr is select * from employees where dept='mech';
begin
open cr;
loop
fetch cr into emp;
exit when ce%notfound;
sal:=emp.salary+(emp.salary*15/100);
update employee set salary=sal where dept='mech';
end loop;
close cr;
if(sql%found) then
dbms_output.put_line(sql%rowcount);
else
dbms_output.put_line('no records found');
end;
but its shows the COMPILATION ERROR
No need to do make it that complex.
SQL> set serveroutput on
SQL> create or replace procedure empsal as
2 begin
3 update employee set
4 salary = salary * 1.15
5 where dept = 'MECH';
6
7 dbms_output.put_line(case when sql%rowcount = 0 then 'No records found'
8 else sql%rowcount || ' employees got increment'
9 end);
10 end;
11 /
Procedure created.
SQL> exec empsal;
2 employees got increment
PL/SQL procedure successfully completed.
SQL>
As of your compilation errors:
if table name is employee, don't use employees (while declaring a cursor)
if cursor name is cr, don't use ce for it (exit statement)
if misses end if
As of logical errors:
if department name is MECH, don't reference it as mech (letter case matters)
doing it in a loop - and without where clause in update statement - you're increasing salary for everyone as many times as there are employees in the MECH department.
in if, you're referencing a cursor that is already closed so ... no use of it

Table is mutating

after i insert this code, i got an error telling that
ORA-04091: table (schema).EMPLOYEES is mutating, trigger/function may not see it
ORA-06512: at "HR.TRGADDEMP_1215034", line 7
ORA-04088: error during execution of trigger 'HR.TRGADDEMP_1215034'
What should i do?
CREATE OR REPLACE TRIGGER TrgAddEmp_1215034
AFTER INSERT ON Employees
FOR EACH ROW
DECLARE
v_name varchar2(35);
v_managerName varchar2(20);
v_dept varchar2(35);
BEGIN
Select first_name||' '||last_name
INTO v_name
FROM Employees where employee_id = :new.employee_id;
Select last_name
into v_managerName
from employees where employee_id = :new.manager_id;
Select department_name
into v_dept
from departments where department_id = :new.department_id;
IF v_managerName is NULL THEN
DBMS_OUTPUT.PUT_LINE('There is a new employee '|| v_name );
DBMS_OUTPUT.PUT_LINE('The supervisor for this employee has not been decided');
DBMS_OUTPUT.PUT_LINE('This employee is assigned to '||v_dept|| 'Department');
ELSIF v_dept is NULL THEN
DBMS_OUTPUT.PUT_LINE('There is a new employee '|| v_name );
DBMS_OUTPUT.PUT_LINE('This employee is supervised by ' || v_managerName);
DBMS_OUTPUT.PUT_LINE('The department for this employee has not been decided');
ELSE
DBMS_OUTPUT.PUT_LINE('There is a new employee '|| v_name );
DBMS_OUTPUT.PUT_LINE('This employee is supervised by ' || v_managerName);
DBMS_OUTPUT.PUT_LINE('This employee is assigned to '||v_dept|| 'Department');
END IF;
END;
You should insert a correct code instead of this :)
ORA-4091 means that your triggers tried to select data from it's own table. Just don't do it. Never. Row-level triggers will not allow that.
This code tries to select a data which you already have.
Why you're trying to select first_name & last_name when you have :new.first_name and :new.last_name? And so on...

Resources