Chapter5 Example Exercise
Chapter5 Example Exercise
1) Write the code that will prompt you for the employee’s last name and then will display
message about his/her pay with the appropriate note regarding the salary range:
Less than $3,000 POOR
Less than $6,000 FAIR
Less than $10,000 GOOD
Less or equal than $15,000 EXCELLENT
More than $15,000 WOW
You also need to be prepared for a name that does NOT exist and for the case when more than
one person holds the same name.
There is more than one employee with such last name: GRANT
PL/SQL procedure successfully completed.
2) Write the code that will prompt you for the Student Id and also for Section Id and then
will display message about the Letter Grade for that student and that course section (based on the
numeric value for the Final Grade in table Enrollment).
You also need to be prepared for an input that will yield NO output and also to deal with
the situation where Final Grade is not entered yet (blank value in the table). In that case Letter
Grade should be ‘I’.
Letter grade is : A
PL/SQL procedure successfully completed.
Letter grade is : I
PL/SQL procedure successfully completed.
Write the code that will prompt you for the employee’s Salary and then will count number of
characters in his/her last name. Then will populate the table TEST’ column NOTE with that
many ‘X’ characters like this length. Column RESULT will store this number and column LAST
will store the last name of that employee. Use WHILE loop.
Then display content of the table TEST and finally undo your population.
You also need to be prepared for a salary that does NOT exist and for the case when more
than one person gets the same salary.
Rollback complete.
no rows selected
Rollback complete.
no rows selected
Rollback complete.
ANSWERS
1)
SET SERVEROUTPUT ON
DECLARE
pay employees.salary%TYPE;
FROM employees
note := 'POOR';
note := 'FAIR';
note := 'GOOD';
note := 'EXCELLENT';
ELSE
note := 'WOW';
END IF;
EXCEPTION
DBMS_OUTPUT.PUT_LINE('There is more than one employee with such last name: ' ||
lname);
END;
2)
SET SERVEROUTPUT ON
SET VERIFY OFF
DECLARE
v_id student.student_id %TYPE := &student;
v_sec enrollment.section_id%TYPE := §ion;
v_fingrade NUMBER(3);
v_letgrade CHAR(1);
BEGIN
SELECT final_grade INTO v_fingrade
FROM Enrollment
WHERE student_id = v_id
AND section_id = v_sec;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('This student OR this section does NOT exist');
END;
3)
CREATE TABLE test (result NUMBER(3), note VARCHAR2(50), last VARCHAR2(20) )
SET SERVEROUTPUT ON
SET VERIFY OFF
DECLARE
counter NUMBER(3) := 1;
a VARCHAR2(30);
b NUMBER(2) := 0;
lname employees.last_name%TYPE;
pay employees.salary%TYPE := &pay;
BEGIN
SELECT last_name, LENGTH(last_name) INTO lname, b
FROM employees
WHERE salary = pay;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Salary value of $' || pay || ' does NOT exist');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('There is more than one employee with such a salary: ' ||
pay);
END;
/
SELECT * FROM test;
ROLLBACK;
4)
DECLARE
-- NO counter declared here
a VARCHAR2(30);
b NUMBER(2) := 0;
lname employees.last_name%TYPE;
pay employees.salary%TYPE := &pay;
BEGIN
SELECT last_name, LENGTH(last_name) INTO lname, b
FROM employees
WHERE salary = pay;