You are on page 1of 9

Virtusa Corp.

2000 West Park Drive Westboro, MA 01581 Phone : (508) 389-7300 Fax : (508) 366 9901 E-mail : info@virtusa.com URL : www.virtusa.com

PL SQL Training Activity 2


. . . . . . . . . .

PL SQL Training -Activity 2

PL SQL Training - Activity 2

1. Retrieve the first 5 employees with a job history.

DECLARE CURSOR c IS select e.first_name,j.job_id from job_history j, employees e where e.employee_id=j.employee_id and rownum<6; ed c%ROWTYPE; BEGIN DBMS_OUTPUT.PUT_LINE('employees details '); For ed in c LOOP DBMS_OUTPUT.PUT_LINE(ed.first_name||' '|| ed.job_id); END LOOP; END; / 2. Develop a PL/SQL block that determines the top employees with respect to salaries. a. Accept a number n from the user where n represents the number of top n earners from the new_emp table. b. There should be no duplication in the salaries. If two employees earn the same salary, the salary should be picked up only once. c. The output shown represents the five highest salaries in the new_emp table. Ans: declare Salary_h employees.salary%type; type c is ref cursor; refcur c; begin
Copyright Virtusa Corporation CONFIDENTIAL < Date>

PL SQL Training - Activity 2

for i in 1..5 loop open refcur for select distinct(a.salary) from employees a where i=(select count(distinct(b.salary))from employees b where (a.salary<=b.salary)); fetch refcur into salary_h; dbms_output.put_line(salary_h); end loop; close refcur; end; 3. Develop a Procedure which will add an employee record into an Employee Database. i)create or replace procedure emppro as cursor c is select * from employees; crec c%rowtype; begin open c; insert into employees(employee_id,last_name,email,hire_date,job_id) values(21,'nut','san@gm',sysdate,'ST_CLERK'); commit; close c; end emppro; ii)execute emppro iii)select * from employees where employee_id=21 4.Develop a Procedure which will update the eligibility status of Credit Holders to N, whose Credit Limit exceeds the maximum range. CREATE OR REPLACE PROCEDURE UpdateCol AS
Copyright Virtusa Corporation CONFIDENTIAL < Date>

PL SQL Training - Activity 2

maxrange number(8,2):=&maxCredit; CURSOR StatusCursor IS SELECT credit FROM CreditHolders; sc StatusCursor%ROWTYPE; BEGIN FOR sc IN StatusCursor LOOP UPDATE CreditHolders SET status='N' WHERE credit>=maxrange; UPDATE CreditHolders SET status='Y' WHERE credit<maxrange; END LOOP; END UpdateCol;

5.Create a procedure called QUERY_EMP to query the NEW_EMP table, retrieving the salary and job ID for an employee when provided with the employee ID. a. Create a procedure that returns a value from the SALARY and JOB_ID columns for a specified employee ID. create or replace procedure query_emp ( eid in number, sal_id out number, job_id out varchar2) as Begin select salary,job_id into sal_id,job_id from employees where employee_id=eid; dbms_output.put_line(to_char(sal_id)||to_char(job_id)); exception when no_data_found then dbms_output.put_line(' no data found '); end query_emp;

Copyright Virtusa Corporation CONFIDENTIAL < Date>

PL SQL Training - Activity 2

b. Use bind variables for the two OUT parameters salary

and job ID. variable sal_id number; variable job_id varchar2(10);

\ SQL> exec query_emp(121,al_id,:job_id);

PL/SQL procedure successfully completed.

SQL> print sal_id;

Sal_id ---------35877.32

SQL> print job_id; JOB_ID -------------------------------ST_MAN SQL>

Copyright Virtusa Corporation CONFIDENTIAL < Date>

PL SQL Training - Activity 2

c. Compile the code; invoke the procedure to display the salary and job ID for employee ID 4455. declare eid employees. Employees_id%type :=&enumber; sal_id employees.salary%type; job_id employees.job_id%type; begin query_emp(eid,salary,job_id); dbms_output.put_line(' salary is : '||sal_id); dbms_output.put_line(' job_id is : '|| job_id); end; 6. Develop a Function which will increase salary by 15% of all employees. i)CREATE OR REPLACE FUNCTION Incrsalary(salary NUMBER) RETURN NUMBER IS newsalary NUMBER; BEGIN newsalary:=salary+0.15*salary; RETURN newsalary; END; / ii)DECLARE CURSOR salaryCursor IS SELECT empno,salary FROM emp2; sc salaryCursor%ROWTYPE; BEGIN FOR sc IN salaryCursor LOOP
Copyright Virtusa Corporation CONFIDENTIAL < Date>

PL SQL Training - Activity 2

UPDATE emp2 SET salary=Incrsalary(sc.salary) WHERE empno=sc.empno; END LOOP; END;

7. Create a procedure, NEW_EMP to insert a new employee into the EMPLOYEES table. The procedure should contain a call to the VALID_DEPTID function to check whether the department ID specified for the new employee exists in the DEPARTMENTS table.

i) CREATE OR REPLACE FUNCTION CheckDeptno(eno NUMBER) RETURN INTEGER IS CURSOR EmpData IS SELECT deptno FROM dept; v_Data EmpData%ROWTYPE; BEGIN FOR v_Data IN EmpData LOOP IF eno = v_Data.deptno THEN RETURN 1; ELSE RETURN 0; END IF; END LOOP;d END; ii) CREATE OR REPLACE PROCEDURE New_Emp (
Copyright Virtusa Corporation CONFIDENTIAL < Date>

PL SQL Training - Activity 2

p_empno emp2.empno%TYPE, p_ename emp2.ename%TYPE, p_job emp2.job%TYPE, p_hiredate emp2.hiredate%TYPE, p_salary emp2.salary%TYPE, p_comm emp2.comm%TYPE, p_deptno emp2.deptno%TYPE ) AS flag INTEGER:=1; BEGIN flag:=CheckDeptno(p_deptno); IF flag=1 THEN INSERT INTO emp2(empno,ename,job,hiredate,salary,comm,deptno) VALUES(p_empno,p_ename,p_job,p_hiredate,p_salary,p_comm,p_ deptno); ELSE DBMS_OUTPUT.PUT_LINE('Invalid dept number'); END IF; END; /
Copyright Virtusa Corporation CONFIDENTIAL < Date>

PL SQL Training - Activity 2

iii)EXECUTE New_Emp (2500,'SREE','ASST','16-dec95',4500,NULL,60);

Copyright Virtusa Corporation CONFIDENTIAL < Date>