Você está na página 1de 4

PL SQL Test Answers

Duration : 1 Hour Max Marks : 50


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Q1. In each of the following assignments, determine the datatype of the resulting expression. [6]
A. email := firstname || to_char(empno);
B. confirm := to_date('20-JAN-1999', 'DD-MON-YYYY');
C. sal := (1000*12) + 500
D. test := FALSE;
E. temp := temp1 < (temp2/ 3);
F. var := sysdate;
Answer:
A. Varchar
B. Date
C. Number
D. ???
E. Number(P,S)
F. Date

Q2. Write a PL/SQL block to accept a year and check whether it is a leap year. For example, if the year
entered is 1900, the output should be 1900 is not a leap year. Or if the year entered is 2000, the
output should be 2000 is a leap year.
[5]
Answer:
declare
v_year number := &year;
begin
if (mod(v_year,4)) = 0 and (mod(v_year,100)) != 0
or (mod(v_year,400)) = 0 then
dbms_output.put_line(v_year||' is a leap year.');
else
dbms_output.put_line(v_year||' is not a leap year.');
end if;
end;
/

Q3. Write a PL/SQL block to declare a variable called v_sal to store the salary of an employee. In the
executable part of the program, do the following: [9]
A. Store an employee name in a substitution variable.
B. Store his or her salary in the v_sal variable.
C. If the salary is less than 3,000, give the employee a raise of 500 and display the message
<Employee Name>s salary updated in the window.
D. If the salary is more than 3,000, print the employees salary in the format, <Employee Name>
earns ...
Answer:
declare
v_sal employees.salary%type;
v_name employees.first_name%type;
begin
select first_name, salary into v_name, v_sal from employees where
employee_id = &eid;
if (v_sal < 3000) then
v_sal := v_sal + 500;
dbms_output.put_line(v_name||'s salary updated to Rs.'||v_sal||'.');
else
dbms_output.put_line(v_name||' earns Rs.'||v_sal||'.');
end if;
end;
/

Q4. Create a PL/SQL block that declares a cursor called DATE_CUR. Pass a parameter of the DATE data
type to the cursor and print the details of all the employees who have joined after that date. [8]
DEFINE B_HIREDATE = 08-MAR-00

Answer:
declare
cursor date_cur (h_date date) is select * from employees
where hire_date > h_date;
begin
for e in date_cur('08-MAR-00') loop
Dbms_output.put_line
(e.employee_id||' '||e.first_name||' '||e.salary||' '||e.job_id);
exit when date_cur%notfound;
end loop;
end;
/
Q5. Query the employees table to find out whether the number of years that the employee has been
with the organization is greater than five; and if the salary is less than 3,500, raise an exception. Handle
the exception with an appropriate exception handler that inserts the following values into the analysis
table: employee last name, number of years of service, and the current salary. Otherwise display Not
due for a raise in the window. [10]

Answer:
create table analysis
(last_name varchar2(20), experience number(4,2), salary number(8,2))

declare
due exception;
v_eid employees.employee_id%type := &eid;
v_name employees.first_name%type;
v_sal employees.salary%type;
v_hdate employees.hire_date%type;
v_experience number(2);
begin
select employee_id, first_name, salary, hire_date into v_eid, v_name,
v_sal, v_hdate from employees where employee_id = v_eid;
v_experience := months_between(sysdate,v_hdate)/12;
if v_sal < 3500 and v_experience > 5 then
raise due;
else
dbms_output.put_line('Not due for a raise.');
end if;
exception
when due then
insert into analysis (last_name, experience, salary) values (v_name,
v_experience, v_sal);
end;
/

Q6. Create and invoke the Q_JOB function to return a job title. [12]
A. Create a function called Q_JOB to return a job title to a host variable.
B. Compile the code; create a host variable G_TITLE and invoke the function with job ID SA_REP.
Query the host variable to view the result.

Answer:
create function q_jobs return varchar2 is
title jobs.job_title%type;
begin
select job_title into title from jobs where job_id='sa_rep';
return title;
end;
/

declare
employee_name varchar2(35);
begin
employee_name := q_jobs;
dbms_output.put_line(employee_name);
end;
/

OR
Create a function called ANNUAL_COMP to return the annual salary by accepting two parameters: an
employees monthly salary and commission. The function should address NULL values.
A. Create and invoke the function ANNUAL_COMP, passing in values for monthly salary and
commission. Either or both values passed can be NULL, but the function should still/ return an
annual salary, which is not NULL. The annual salary is defined by the basic formula: (sal*12) +
(commission_pct*salary*12)
B. Use the function in a SELECT statement against the EMPLOYEES table for department 80.

Answer:
create function annual_comp
(v_sal in employees.salary%type,
v_comm in employees.commission_pct%type)
return number is
begin
return (nvl(v_sal,0) * 12 + (nvl(v_comm,0) *
nvl(v_sal,0)* 12));
end annual_comp;
select employee_id, first_name, annual_comp (salary,commission_pct)
CTC from employees where department_id=80

Você também pode gostar