0% found this document useful (0 votes)
458 views2 pages

Hop

The document discusses several PL/SQL blocks that use cursors to retrieve and manipulate employee data from the EMPLOYEE table. The blocks demonstrate: 1) Using an explicit cursor to print employees' names, salaries, and hire dates that meet certain criteria. 2) Using a cursor with a parameter to print employees' names and salaries greater than the parameter value. 3) Using a cursor with the FOR UPDATE clause to update salaries by a percentage amount for employees in a particular department. 4) Handling predefined exceptions when retrieving an employee's data by qualification ID.

Uploaded by

Mindy Kennedy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
458 views2 pages

Hop

The document discusses several PL/SQL blocks that use cursors to retrieve and manipulate employee data from the EMPLOYEE table. The blocks demonstrate: 1) Using an explicit cursor to print employees' names, salaries, and hire dates that meet certain criteria. 2) Using a cursor with a parameter to print employees' names and salaries greater than the parameter value. 3) Using a cursor with the FOR UPDATE clause to update salaries by a percentage amount for employees in a particular department. 4) Handling predefined exceptions when retrieving an employee's data by qualification ID.

Uploaded by

Mindy Kennedy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

--Create a PL/SQL block to declare a cursor to select last name, first name, salary, and hire date from

the EMPLOYEE table. -Retrieve each row from the cursor and print the employees information if the employees salary is greater than $50,000 and the hire date is before 31-DEC1997 (explicit cursor problem). OUTPUT: Smith, John makes $265000 Hired: 15-APR-60 Houston, Larry makes $150000 Hired: 19-MAY-67 Roberts, Sandi makes $75000 Hired: 02-DEC-91 McCall, Alex makes $66500 Hired: 10-MAY-97 Dev, Derek makes $80000 Hired: 15-MAR-95 PL/SQL procedure successfully completed.

--Create a PL/SQL block that declares a cursor. -Pass a parameter to the cursor of the type that is the same as the Salary column in EMPLOYEE table to the cursor. Open the cursor with a value for the parameter. -Retrieve information into the cursor for a salary higher than the parameter value. -Use a loop to print each employees information from the cursor (cursor with parameter problem). OUTPUT: Enter value for v_salary: 75000 Smith, John makes $265000 Houston, Larry makes $150000 Dev, Derek makes $80000 PL/SQL procedure successfully completed.

--Create a PL/SQL block to increase salary of employees in department 10. -The salary increase is 15% for the employees making less than $100,000 and 10% for the employees making $100,000 or more. -Use a cursor with a FOR UPDATE clause. -Update the salary with a WHERE CURRENT OF clause in a cursor FOR loop (cursor FOR loop problem).

DECLARE CURSOR empcur IS SELECT Lname, Fname, Salary, HireDate FROM employee; last employee.Lname%TYPE; first employee.Fname%TYPE; sal employee.Salary%TYPE; hire employee.HireDate%TYPE; BEGIN OPEN empcur; FETCH empcur INTO last, first, sal, hire; WHILE empcur%FOUND LOOP IF sal > 50000 AND hire < '31-DEC-1997' THEN DBMS_OUTPUT.PUT(last||', '||first||' makes $'||sal); DBMS_OUTPUT.PUT_LINE(' Hired: '||hire); END IF; FETCH empcur INTO last, first, sal, hire; END LOOP; CLOSE empcur; END; DECLARE CURSOR empcur (sal employee.Salary%TYPE) IS SELECT Lname, Fname, Salary FROM employee WHERE Salary > sal; sal_param employee.Salary%TYPE := &v_salary; last employee.Lname%TYPE; first employee.Fname%TYPE; sal employee.Salary%TYPE; BEGIN OPEN empcur (sal_param); FETCH empcur INTO last, first, sal; WHILE empcur%FOUND LOOP DBMS_OUTPUT.PUT_LINE(last||', '||first||' makes $'||sal); FETCH empcur INTO last, first, sal; END LOOP; CLOSE empcur; END; DECLARE CURSOR emp_cur IS SELECT EmployeeId, SALARY FROM employee WHERE DeptId = 10 FOR UPDATE; incr NUMBER; BEGIN FOR emp_rec IN emp_cur LOOP IF emp_rec.Salary < 100000 THEN incr := 0.15; ELSE incr := 0.10; END IF; UPDATE employee SET Salary = Salary + Salary * incr WHERE CURRENT OF emp_cur; END LOOP; END; DECLARE first employee.Fname%TYPE; last employee.Lname%TYPE; qual employee.QualId%TYPE; Sal employee.Salary%TYPE; v_qualId employee.QualId%TYPE := &Qualification_Id; BEGIN SELECT Lname, Fname, QualId, Salary INTO last, first, qual, sal FROM employee WHERE QualId = v_qualId; DBMS_OUTPUT.PUT_LINE(last || ', ' || first); DBMS_OUTPUT.PUT_LINE('Qualification: ' || qual); DBMS_OUTPUT.PUT_LINE('Salary: ' || sal); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ('No employees with such qualification'); WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE ('More than employee with qualification ' || v_qualId); END;

--Write a PL/SQL block to retrieve employees from the EMPLOYEE table based on a qualification Id. -If the qualification Id returns more than one row, handle the exception with the appropriate handler and print the message More than one employee with such qualification. -If the qualification Id returns no employee, handle the exception with the appropriate handler and display the message No employees with such qualification. -If the qualification Id returns one employee, then print that employees name, qualification and salary (predefined server exception problem). OUTPUT: Enter value for qualification_id: 7 No employees with such qualification PL/SQL procedure successfully completed. SQL> / Enter value for qualification_id: 1 More than employee with qualification 1 PL/SQL procedure successfully completed. SQL> / Enter value for qualification_id: 5 Garner, Stanley Qualification: 5 Salary: 45000 PL/SQL procedure successfully completed. --PROCEDURES: FIRST Example CREATE OR REPLACE PROCEDURE DEPENDENT_INFO IS CURSOR DEP_CUR IS SELECT LNAME, FNAME, COUNT(DEPENDENTID) CNT FROM EMPLOYEE E, DEPENDENT D WHERE E.EMPLOYEEID = D.EMPLOYEEID GROUP BY LNAME, FNAME; BEGIN FOR DEP_REC IN DEP_CUR LOOP IF DEP_REC.CNT >= 2 THEN DBMS_OUTPUT.PUT_LINE(DEP_REC.LNAME || ',' || DEP_REC.FNAME. || 'HAS || D); END IF;

END LOOP; END;

You might also like