Chapter 4 Practice
1) Write the code that will prompt you for the employee’s Id and then will calculate the Annual
Income based on the value of “commission_pct” column. If that value does exist it should be
doubled (like some kind of reward for Good Sales year). Both numbers should be displayed.
Provide two examples where first employee is receiving the commission, while the other one is
not. Here is the output.
Enter value for empid: 175
Employee Hutton has an annual income of 158400
PL/SQL procedure successfully completed.
COM_PCT
.5
Enter value for empid: 145
Employee Higgins has an annual income of 144000
PL/SQL procedure successfully completed.
COM_PCT
0
2) You will write PL/SQL code that will insert a NEW employee with the following data:
Employee_id: Value is greater by 10 than the highest Id so far
First_name: Tim
Last_name: Ho
Email: It will be in the form of ‘whatever is the User’s name’ with the domain ‘yahoo.ca’
Hire_date: The latest hire date for the chosen department
Job_d: FI_ACCOUNT
Salary: The Average pay for the chosen department
Department_id: You will be prompted for this Id and provide one of the existing values
Then you will verify the look of this new row (just for the columns that are not blank)
and then you will undo your insert. Here is the output.
Enter value for deptid: 60
PL/SQL procedure successfully completed.
EMPLOY FIRST_ LAST_NA HIRE_DA SALA DEPARTME
EMAIL
EE_ID NAME ME TE RY NT_ID
DBS501_093A40
216 Tim Ho 07-FEB-99 5760 60
@yahoo.ca
Rollback complete.
3) You need to modify departments with NO manager so that one of the VP’s becomes their
manager. Criteria is also that chosen VP had to be hired in the eighties.
Display firstly last name of that manager and then # of departments that have been modified.
And finally undo you change. Here is the output.
Name of VP is Kochhar
# of departments that got Kochhar as a manager is 16
PL/SQL procedure successfully completed.
Rollback complete.
ANSWERS
1)
SET SERVEROUTPUT ON
SET VERIFY OFF
VARIABLE com_pct NUMBER
DECLARE
lname employees.last_name%TYPE;
income employees.salary%TYPE;
empno employees.employee_id%TYPE := &empid;
BEGIN
SELECT commission_pct INTO :com_pct
FROM employees
WHERE employee_id = empno;
IF :com_pct IS NOT NULL THEN
:com_pct := :com_pct*2;
ELSE
:com_pct := 0;
END IF;
SELECT last_name, salary*12*(1+ :com_pct)
INTO lname, income
FROM employees
WHERE employee_id = empno;
DBMS_OUTPUT.PUT_LINE('Employee ' || lname || ' has an annual income of ' ||
TO_CHAR(income));
END;
/
PRINT com_pct
2)
DECLARE
mail_name VARCHAR2(40);
h_date employees.hire_date%TYPE;
avg_sal employees.salary%TYPE;
v_dept employees.department_id%TYPE := &deptid;
v_emp employees.employee_id%TYPE;
BEGIN
SELECT USER INTO mail_name
FROM DUAL;
SELECT MAX(hire_date), AVG(salary)
INTO h_date, avg_sal
FROM employees
WHERE department_id = v_dept;
SELECT MAX(employee_id) INTO v_emp
FROM employees;
v_emp := v_emp + 10;
mail_name := mail_name || '@yahoo.ca' ;
INSERT INTO employees VALUES
(v_emp,'Tim','Ho',mail_name,NULL,h_date,'FI_ACCOUNT',avg_sal,NULL,NULL,v_dept);
END;
/
SELECT employee_id, first_name, last_name, email,hire_date, salary, department_id
FROM employees
WHERE employee_id = (SELECT MAX(employee_id)
FROM employees);
ROLLBACK;
/
3)
SET SERVEROUTPUT ON
DECLARE
empid employees.employee_id%TYPE;
lname employees.last_name%TYPE;
BEGIN
SELECT employee_id, last_name
INTO empid, lname
FROM employees
WHERE job_id LIKE '%VP'
AND hire_date < '01-JAN-90';
DBMS_OUTPUT.PUT_LINE('Name of VP is ' || lname);
UPDATE departments
SET manager_id = empid
WHERE manager_id IS NULL;
DBMS_OUTPUT.PUT_LINE('# of departments that got ' || lname || ' as a manager is ' || SQL
%ROWCOUNT) ;
END;
/
ROLLBACK