Write PL/SQL block that will accept employee number from user and deduct an amount of Rs.200 from the inputted employee, if he has a salary less than 1000 after Salary is deducted, it display message‘s Salary is less than 1000’. The process is to be fired on table employee (emp_no, name, salary).
solution.
DECLARE
v_emp_no employee.emp_no%TYPE;
v_salary employee.salary%TYPE;
BEGIN
-- Accept employee number from user
v_emp_no := &emp_no;
-- Retrieve employee's salary
SELECT salary INTO v_salary
FROM employee
WHERE emp_no = v_emp_no;
-- Deduct Rs. 200 from salary
v_salary := v_salary - 200;
-- Update the new salary in the database
UPDATE employee
SET salary = v_salary
WHERE emp_no = v_emp_no;
-- Commit the changes
COMMIT;
-- Check if salary is less than 1000 and display message
IF v_salary < 1000 THEN
DBMS_OUTPUT.PUT_LINE('Salary is less than 1000');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary updated successfully. New salary: ' || v_salary);
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Error: Employee not found.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred: ' || SQLERRM);
END;
/