Sunteți pe pagina 1din 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;

S-ar putea să vă placă și