You are on page 1of 21

declare

v_opr1 number(2) :=10;


v_opr2 number(2) :=20;
v_result1 number(3);
v_result2 number(3);
v_result3 number(4);
v_result4 number(3);

begin
v_opr1:=&operand1;
v_opr2:=&operand2;
v_result1:=v_opr1+v_opr2;
v_result2:=v_opr1-v_opr2;
v_result3:=v_opr1*v_opr2;
v_result4:=v_opr1/v_opr2;

DBMS_OUTPUT.PUT_LINE('Output of addition of '||v_opr1||' and '||v_opr2||' is '||


v_result1);
DBMS_OUTPUT.PUT_LINE('Output of subtraction of '||v_opr1||' and '||v_opr2||' is '||
v_result2);
DBMS_OUTPUT.PUT_LINE('Output of multiplication of '||v_opr1||' and '||v_opr2||' is
'||v_result3);
DBMS_OUTPUT.PUT_LINE('Output of division of '||v_opr1||' and '||v_opr2||' is '||
v_result4);

END;
/
------------------------------------------------------------
declare
v_date date;

begin
DBMS_OUTPUT.PUT_LINE(trunc(v_date,'dd-mon-yy hh:mi:ss'));
end;
/
--to take date input and compare WITH sysdate

declare
v_date date;
begin
v_date:='&date';
if(v_date<trunc(sysdate)) then
DBMS_OUTPUT.PUT_LINE('Input date is previous date');
else
if(v_date>trunc(sysdate)) then
DBMS_OUTPUT.PUT_LINE('Input date is future date');
else
DBMS_OUTPUT.PUT_LINE('Input date is today''s date');
end if;
end if;
end;
/
----program to take accept dept_id and print salary
declare
v_deptid employees.department_id%TYPE;
V_count number(4);
begin
v_deptid:=&user_deptid;
select department_id,count(employee_id) into v_deptid,v_count from employees
where department_id=v_deptid group by department_id;
DBMS_OUTPUT.PUT_LINE('Total no. of employees in department '||v_deptid||' is
'||v_count);

end;
/

declare
v_deptid employees.department_id%TYPE;
V_count number(4);
begin
v_deptid:=&user_deptid;
select count(employee_id) into v_count from employees where
department_id=v_deptid;
DBMS_OUTPUT.PUT_LINE('Total no. of employees in department '||v_deptid||' is
'||v_count);

end;
/

declare
v_deptid employees.department_id%TYPE;
V_count number(4);
begin
v_cout:=&usercount;
select department_id into v_deptid from employees where
count(employee_id)=v_count;
DBMS_OUTPUT.PUT_LINE('Total no. of employees in department '||v_deptid||' is
'||v_count);

end;
/
write a program to accept empno,dept no and salary and validate whether eny
employee exists for he given combination.

declare
v_empno employees.employee_id%TYPE;
v_deptid employees.department_id%TYPE;
v_sal employees.salary%TYPE;
v_count number(2);

begin
v_empno:=&inputempno;
v_deptid:=&inputdeptid;
v_sal:=&inputsal;

select count(employee_id) into v_count from employees where


employee_id=v_empno and department_id=v_deptid and salary=v_sal;
if(v_count!=0) then
DBMS_OUTPUT.PUT_LINE('employee exists');
else
DBMS_OUTPUT.PUT_LINE('employee doesnot exists');

end if;
end;
/

--- write a program to display date and time separetly from sysdate;

declare
v_date varchar2(12);
v_time varchar2(10);

begin
--SELECT TO_CHAR(SYSDATE,'DD-MON-YY'),TO_CHAR(sysdate,'hh:mi:ss') into
v_date,v_time from dual;
select extract('dd-mon-yy',sysdate) into v_date,extract ('hh:mi:ss',sysdate)) into
v_time from dual;
DBMS_OUTPUT.PUT_LINE('Today''s date is '||v_date||' and time is '||v_time);
end;
/

----WRITE A PROGRAM TO take date as input and print the corresponding day.
declare
v_date date;
v_day varchar2(10);
begin
v_date:='&date';

select extract('day' FROM v_Date) into v_day from dual;


DBMS_OUTPUT.PUT_LINE(V_DAY);
END;
/

declare
v_date date;
v_day varchar2(10);
begin
v_date:='&date';

select TO_CHAR(V_DATE,'DAY') into v_day from dual;


DBMS_OUTPUT.PUT_LINE(V_DAY);
END;
/
-------

take date input and print 5 days after input date

declare
v_date date;

begin
v_date:='&date';
v_date := v_date+5;

DBMS_OUTPUT.PUT_LINE(V_date);
END;
/
declare
v_date date;
v_newdate date;

begin
v_date:='&date';
v_nd:=to_char(v_date,'DD');
v_nd:=v_nd+5;
v_newdate=to_date(v_nd,'DD');

DBMS_OUTPUT.PUT_LINE(V_newdate);
END;
/

------------------------------------------------

Exceptions

1. Predefined --- Name, error code


Internally raised

2. Non Predefined --- Error code,


Use Prgma Exception_Init to associate exception name with error code.
Internally raised

3. User defined exception


Declare exception
Define the criteria
Raise the exception explicitly

Exception Propagation
___________________________

When there are multiple/nested blocks of code is executed and


When in any of the inner block does not have exception handled at that block level
how will the exception be handled is the exception propagation.
Not having exception handled at inner block, tends to check for any exception(s)
handled in the immediate outer block, so on and so forth.

Pragma exception_init(exception_name,oracle defined error code);


This should be written in the declaration section

For user defined exceptions, use raise statement/built-in to raise the exception
explicitly.

EXCEPTION HANDLING
________________________
Declare
v_ename employees.first_name%TYPE;
v_deptid employees.department_id%TYPE;

Begin
v_deptid:=&v_deptid;

select first_name into v_ename from employees where department_id=v_deptid;


DBMS_OUTPUT.PUT_LINE('ONE ROW EXISTS');
Exception
when no_data_found then
DBMS_OUTPUT.PUT_LINE('No Employee found');
when too_many_rows then
DBMS_OUTPUT.PUT_LINE('Multiple employees exists for this department id');
when others then
DBMS_OUTPUT.PUT_LINE(sqlerrm||' '||sqlcode);
end;
/
-------------------------------------------------------------
Declare
e_value_error exception;
v_count number(3);
--pragma exception_init(e_value_error,-6502);
Begin
v_count:=&v_count;
if(v_count<1 or v_count>5) then
raise e_value_error;
else
DBMS_OUTPUT.PUT_LINE('Acceptable input');
end if;
exception
when e_value_error then
DBMS_OUTPUT.PUT_LINE('Input is not within the allowed limit');
end'
/

------------------------------------------------------------------
valiating user input
_____________________

Declare
v_ename employees.first_name%TYPE;
v_deptid employees.department_id%TYPE;
v_count number(2);

Begin

Begin
v_deptid:=&v_deptid;
select count(department_id) into v_count from departments where
department_id=v_deptid;
if(v_count=0) then
DBMS_OUTPUT.PUT_LINE('No such department exists');
else
select first_name into v_ename from employees where
department_id=v_deptid;
DBMS_OUTPUT.PUT_LINE('ONE ROW EXISTS');

end if;
end;

Exception
when no_data_found then
DBMS_OUTPUT.PUT_LINE('No Employee found');
when too_many_rows then
DBMS_OUTPUT.PUT_LINE('Multiple employees exists for this department id');
when others then
DBMS_OUTPUT.PUT_LINE(sqlerrm||' '||sqlcode);
end;
/

---------------------------------------------------------------
Write an anonymous block to accept Salary as user input.
Print how many employees are drawing a salary less than the input.
Display what is the minimum and maximum salary that is lesser than the salary
inputted by the user.

Declare
v_sal employees.salary%TYPE;
v_count number(3);
v_min employees.salary%TYPE;
v_max employees.salary%TYPE;

Begin
v_sal:=&v_sal;
select count(employee_id) into v_count from employees where salary<v_sal;
DBMS_OUTPUT.PUT_LINE('Total employees for this salary group is '||v_count);
select min(salary),max(salary) into v_min,v_max from employees where
salary<v_sal;
DBMS_OUTPUT.PUT_LINE('Minimum salary less than input sal is '||v_min||'
Maximum salary less than input salary is '||v_max);
end;
/

-----------------------------------------------------------------------------
Write an anonymous block to accept user input of a number. 1 is the lowest and 5
is the highest possible option.
Use the detail below to display the output based on the input.

Input Value Output Value

1 Poor
2 Ok
3 Neutral
4 Good
5 Best

Declare
v_num number(1);

Begin
v_num:=&v_num;
if(v_num=1) then
DBMS_OUTPUT.PUT_LINE('Poor');
elsif (v_num=2) then
DBMS_OUTPUT.PUT_LINE('ok');
elsif (v_num=3) then
DBMS_OUTPUT.PUT_LINE('Neutral');
elsif (v_num=4) then
DBMS_OUTPUT.PUT_LINE('Good');
elsif (v_num=5) then
DBMS_OUTPUT.PUT_LINE('Best');
end if;
end;
/
------------------------------------------------------------------

Write an anonymous block to accept the date of the month as input and display
whether today is greater than the input date or not.

declare
v_date date;

Begin
v_date:='&date';
if(v_date<sysdate) then
DBMS_OUTPUT.PUT_LINE('Today''s date is future date than input date');
else
DBMS_OUTPUT.PUT_LINE('Today''s date is past date than input date');
end if;
end;
/

---------------------------------
ASSIGNMENTS
____________
Write an anonymous block to display the following menu and accept a number input
from the user.
The allowed options are 1,2,3,4, and 5. If the user enters any option other than
this do not let the process proceed forward.

Syntaxes using One table

---------------------------------
SELECT
SELECT with WHERE
SELECT with ORDER BY
SELECT with GROUP BY
SELECT with GROUP BY and HAVING

If user selects 1 then the following has to be printed.

Select column1, column2,�columnN from Table1;

If user selects 2 then the following has to be printed

Select column1, column2,�columnN from Table1 Where Condition List;

Similar way come up with Syntax for option 3, 4 and 5 and display the same when the
user chooses the appropriate option.

Declare
v_choice Number(1);
e_input_error exception;

Begin

v_choice:=&v_choice;
if(v_choice<1 AND v_choice>5) then
raise e_input_error;
else
case
when v_choice=1 then
DBMS_OUTPUT.PUT_LINE('select employee_id,first_name into v_empno,v_name
from employees');
when v_choice=2 then
DBMS_OUTPUT.PUT_LINE('select first_name,salary into v_name,v_sal from
employees where employee_id=v_empno');
when v_choice=3 then
DBMS_OUTPUT.PUT_LINE('select first_name,salary into v_name,v_sal from
employees where salary>v_input_sal order by salary');
when v_choice=4 then
DBMS_OUTPUT.PUT_LINE('select count(department_id) into v_count from
departments group by department_id');
when v_choice=5 then
DBMS_OUTPUT.PUT_LINE('select count(employee_id) into v_count from
employees group by job_id having count(employee_id)=5');
else
DBMS_OUTPUT.PUT_LINE('enter the correct choice');
end case;
end if;
Exception
when e_input_error then
DBMS_OUTPUT.PUT_LINE('wrong input.Enter the choice between 1 and 5');
when no_data_found then
DBMS_OUTPUT.PUT_LINE('No Records found');
when too_many_rows then
DBMS_OUTPUT.PUT_LINE('Multiple Records found');
when others then
DBMS_OUTPUT.PUT_LINE(sqlerrm||' '||sqlcode);

end;
/
--------------------------------------------------------------

ASSignment 2

Write an anonymous block to handle the following scenario.


User is inputting a date and if it is a past date or today�s date then
the following message should be displayed and the program should stop.
"Invalid input. Please enter a future date to continue."
If the input is future date then the program should
calculate total no. of days between today and the input date excluding today and
the input date.
For eg.

If the program is executed today and the user is inputting 10-Nov-2019,


then exclude today(6th Nov) and 10th Nov and consider 7,8, and 9th of Nov.
So the output to be printed is 3 days to go

Input Output

10-Oct-2019 Invalid input. Please enter a future date to continue


07-Nov-2019 Invalid input. Please enter a future date to continue
10-Nov-2019 3 days to go

Declare
v_date number(2);
v_count number(2);
Begin
v_date :=&date;
if(v_date<(to_char(sysdate,'DD'))) then
DBMS_OUTPUT.PUT_LINE('Invalid input.Please enter a future date');
elsif(v_date=(to_char(sysdate,'DD'))) then
DBMS_OUTPUT.PUT_LINE('Invalid input.Please enter a future date');
elsif(v_date>(to_char(sysdate,'DD'))) then
select ((v_date-(to_char(sysdate,'DD')))-1) into v_count from dual;
DBMS_OUTPUT.PUT_LINE(v_count||' more days to go');
end if;
end;
/

2nd method(correct one)


________________

Declare
v_date date;
v_count number(2);
e_invalid_error exception;
Begin
v_date :='&date';
if((to_char(v_date,'DD'))>31) then
raise e_invalid_error;

elsif((to_char(v_date,'DD'))<(to_char(sysdate,'DD'))) then
DBMS_OUTPUT.PUT_LINE('Invalid input.Please enter a future date');

elsif((to_char(v_date,'DD'))=(to_char(sysdate,'DD'))) then
DBMS_OUTPUT.PUT_LINE('Invalid input.Please enter a future date');

elsif((to_char(v_date,'DD'))>(to_char(sysdate,'DD'))) then
select (((to_char(v_date,'DD'))-(to_char(sysdate,'DD')))-1) into v_count from
dual;
DBMS_OUTPUT.PUT_LINE(v_count||' more days to go');

end if;
Exception
when e_invalid_error then
DBMS_OUTPUT.PUT_LINE('please input the correct date where date value is less
than 31');
when others then
DBMS_OUTPUT.PUT_LINE(sqlerrm||' '||sqlcode);
end;
/

---------------------------------------------------
while loop
____________

declare
v_counter number:=0;
begin
while (v_counter<3) Loop
DBMS_OUTPUT.PUT_LINE(sysdate+v_counter);
v_counter:=v_counter+1;

end loop;
end;
/
----------------------------------------------
Basic Loop
_________________________

declare
v_counter number:=0;
begin
Loop
DBMS_OUTPUT.PUT_LINE(sysdate+v_counter);
v_counter:=v_counter+1;
if v_counter>2 then
exit;
end if;

end loop;
end;
/

declare
v_counter number:=0;
begin
Loop
DBMS_OUTPUT.PUT_LINE(sysdate+v_counter);
v_counter:=v_counter+1;

exit when v_counter>2;


end loop;
end;
/

----------------------------------------------
FOR LOOP
______________

Begin
For i in 1..10 Loop
DBMS_OUTPUT.PUT_LINE(i);

end loop;
end;
/

FOR LOOP Reverse


__________

Begin
For i in Reverse 1..10 Loop
DBMS_OUTPUT.PUT_LINE(i);

end loop;
end;
/

Declare
V_name varchar2(10);
v_new varchar2(1);
v_j number(20);
begin
V_name:='&v_name';
v_j:=length(v_name);
for i in 1..v_j LOOP
v_new:=substr(v_name,i,1);
DBMS_OUTPUT.PUT_LINE(v_new);
END LOOP;
end;
/

Declare
V_name varchar2(10);
v_new varchar2(20);
v_j number(20);
begin
V_name:='&v_name';
v_j:=length(v_name);
for i in 1..v_j LOOP
v_new:=substr(v_name,i,1);
DBMS_OUTPUT.PUT_LINE(v_new);
END LOOP;
end;
/

--------------------------------------------

CURSOR
___________
cursor for-loop

Declare
Cursor c1 is select city,state_province from locations;
c_rec c1%ROWTYPE;
begin
for c_rec in c1 Loop
DBMS_OUTPUT.PUT_LINE(c_rec.city);
DBMS_OUTPUT.PUT_LINE(c_rec.state_province);
exit when c1%notfound;
DBMS_OUTPUT.PUT_LINE(c1%rowcount);
end loop;
end;
/
----------------------------------------------------------------------
other way of writing cursors without cursor for loop

Declare

Cursor C1 is select first_name,salary from employees where salary < 3000;

c_emp_rec C1%ROWTYPE;

v_total_rec number(3);

begin

Open c1;

loop
Fetch c1 into c_emp_rec;

dopl(c_emp_rec.first_name);

dopl(c_emp_rec.salary);

exit when c1%notfound;

end loop;

dopl(c1%ROWCOUNT);

close c1;

end;

/
-----------------------------------------------------------------------------------
-------------------
write a plsql program to print the employee first name and their expereince in
years using cursors;

Declare
cursor c1 is select first_name,round((sysdate-hire_date)/365) experience from
employees;
c_rec c1%rowtype;
begin
for c_rec in c1 Loop
DBMS_OUTPUT.PUT_LINE(c_rec.first_name);
DBMS_OUTPUT.PUT_LINE(c_rec.experience);
exit when c1%notfound;
DBMS_OUTPUT.PUT_LINE(c1%rowcount);
end loop;
end;
/

------------------------------------------------------
write a plsql program to display all the employees and their manager names.

Declare
cursor c1 is select e.first_name employee,m.first_name manager from employees
e,employees m where e.manager_id=m.employee_id;
begin
for c_rec in c1 Loop
DBMS_OUTPUT.PUT_LINE(c_rec.employee);
DBMS_OUTPUT.PUT_LINE(c_rec.manager);
exit when c1%notfound;
DBMS_OUTPUT.PUT_LINE(c1%rowcount);
end loop;
end;
/

--------------------------------------------------------
write aplsql program to accept the department attributes from user.
use the inputs to add a new department to the departments table.
if the department_id entered exists,raise an user defined exception.
for validation use cursors.
Declare
cursor c1 is select department_id from departments;
cursor c2 is select manager_id from employees;
v_deptid departments.department_id%TYPE;
v_deptname departments.department_name%TYPE;
v_mgrid departments.manager_id%TYPE;
v_lid departments.location_id%TYPE;
e_input_error exception;
e_invalid_error exception;

begin
v_deptid:=&v_deptid;
v_deptname:='&v_deptname';
v_mgrid:=&v_mgrid;
v_lid:=&v_lid;

for c_rec in c1 LOOP


if(c_rec.department_id=v_deptid) then
raise e_input_error;
end if;
exit when c1%NOTFOUND;
end loop;
for c_rec1 in c2 LOOP
if(c_rec1.manager_id<>v_mgrid) then
raise e_invalid_error;
end if;
exit when c2%NOTFOUND;
end loop;
insert into departments values(v_deptid,v_deptname,v_mgrid,v_lid);
if SQL%FOUND then
DBMS_OUTPUT.PUT_LINE('New Record inserted');
end if;

exception
when e_input_error then
DBMS_OUTPUT.PUT_LINE('This Department id already exists');
when e_invalid_error then
DBMS_OUTPUT.PUT_LINE('Manager id doesnot exists');
when others then
DBMS_OUTPUT.PUT_LINE(sqlerrm||' '||sqlcode);
end;
/

------------------------------------------------------------------------
Declare

v_recs number(3) := 0;

Begin

Update employees set salary=15000 where department_id = 90;

If SQL%found then

v_recs := SQL%ROWCOUNT;

dopl('Total rows updated is-'||v_recs);


end if;

End;

----------------------------------------

DECLARE

v_dept_no NUMBER(4) := 270;

BEGIN

DELETE FROM departments WHERE department_id = v_dept_no;

IF SQL%FOUND THEN -- delete succeeded

DOPL('Department id 270 has been deleted');

rollback;

END IF;

END;
/

--------------------------------------------------------

DECLARE

x NUMBER := 0;

BEGIN

LOOP -- After CONTINUE statement, control resumes here

DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || TO_CHAR(x));

x := x + 1;

IF x < 3 THEN

CONTINUE;

END IF;

DBMS_OUTPUT.PUT_LINE

('Inside loop, after CONTINUE: x = ' || TO_CHAR(x));


EXIT WHEN x = 5;

END LOOP;

DBMS_OUTPUT.PUT_LINE (' After loop: x = ' || TO_CHAR(x));

END;

/
----------------------------------------------

PROCEDURES
_______________

Create Procedure add_dept(p_deptid IN number,p_dept_name IN varchar2,p_status OUT


char) is

v_dummy number(1) :=0;

begin

INSERT INTO DEPARTMENTS(DEPARTMENT_ID,DEPARTMENT_NAME)


VALUES(p_deptid,p_dept_name);

If SQL%Found then

v_dummy := SQL%ROWCOUNT;

end if;

If v_dummy = 1 then

p_status := 'Y';

end if;

exception

When dup_val_on_index then

p_status := 'N';

When Others then

p_status := 'N';

end;

-- Anonymous block for calling the procedure �


Declare

v_deptid departments.department_id%TYPE;

v_deptname departments.department_name%TYPE;

v_status varchar2(1);

begin

v_deptid := &v_deptid;

v_deptname := '&v_deptname';

--Call the procedure

add_dept(v_deptid,v_deptname,v_status);

If v_status = 'Y' then

dopl('A new department with id '||v_deptid||' inserted successfully');

elsif v_status = 'N' then

dopl('A new department with id '||v_deptid||' could not be inserted


successfully');

else

dopl('Error occurred');

end if;

end;
-----------------------------------------------------------------------------------
-------
write a procedure to accept a date input and check if there are any training
programmes.

create or replace Procedure check_date(p_date IN date,p_status OUT char) is


v_count number(2):=0;

Begin
select count(t_req_id) into v_count from trainee where
trunc(dot)=trunc(p_date);
If v_count>=1 then
p_status:='Y';
else
p_status:='N';
end if;
exception
when others then
p_status:=null;
end;
/
/*
create or replace Procedure check_date(p_date IN date,p_status OUT char) is
v_count number(2):=0;
v_date date;
Begin
select DOT into v_date from trainee where trunc(dot)=trunc(p_date);
v_count:=SQL%ROWCOUNT;
If v_count>=1 then
p_status:='Y';
end if;
exception
when no_data_found then
p_status:='N';
when too_many_rows then
p_status:='N';
end;
/
*/

declare
v_date date;
v_status char(1);

Begin
v_date:='&v_date';
check_date(v_date,v_status);
if v_status ='Y' then
DBMS_OUTPUT.PUT_LINE('Training is scheduled');
elsif v_status='N' then
DBMS_OUTPUT.PUT_LINE('No trainings scheduled');
end if;
Exception
when others then
DBMS_OUTPUT.PUT_LINE('Error');
end;
/

-----------------------------------------------------------------
FUNCTIONS
___________

Create or replace function f_ext_month(p_date date) return char is

begin

return (to_char(p_Date,'Month'));

end;

show err;

--wrapper script/anonymous block to call the function.


begin

dopl (f_ext_month('08-Nov-2019'));

end;

---------------------------------------------------------------------------------

create or replace function f_find(p_empid number) return varchar2 is


v_name employees.first_name%TYPE;

begin
select first_name into v_name from employees where employee_id=p_empid;
return v_name;
exception
when no_data_found then
return null;
end;
/
show err;
/

declare
v_empid employees.employee_id%TYPE;
v_empname employees.first_name%TYPE;
v_count number(3);
e_multi_rows exception;
begin
v_empid:=&v_empid;
select count(employee_id) into v_count from employees where
employee_id=v_empid;
if v_count=0 then
DBMS_OUTPUT.PUT_LINE('can''t process further');
elsif v_count=1 then
v_empname:=f_find(v_empid);
DBMS_OUTPUT.PUT_LINE('employee_name '||v_empname);
else
raise e_multi_rows;
end if;
exception
when e_multi_rows then
DBMS_OUTPUT.PUT_LINE('multiple rows exists');
when others then
DBMS_OUTPUT.PUT_LINE('Error');
end;
/
---------------------------------------------------------------

create or replace function f_dept_name(p_deptid number) return varchar2 is


v_name departments.department_name%TYPE;

begin
select department_name into v_name from departments where
department_id=p_deptid;
return v_name;
exception
when no_data_found then
return null;
end;
/
show err;
/

Wrapper script for department example.

Declare

v_dept_id departments.department_id%TYPE;

v_dept_name departments.department_name%TYPE;

v_dep_check number(3);

e_multi_rows exception;

Begin

v_dept_id := &v_dept_id;

select count(department_id) into v_dep_check from departments where


department_id=v_dept_id;

if v_dep_check = 0 then

dopl('Cant proceed further');

elsif v_dep_check = 1 then

v_dept_name := f_dept_name(v_dept_id);

dopl('Department Id :'||v_dept_id||' Department Name: '||v_dept_name);

else

raise e_multi_rows;

end if;

exception

when e_multi_rows then

dopl('Multiple departments with this number exists.Unable to process');

end;

/
------------------------------------------------------------
PACKAGE
_____________

/*Package specification*/

create or replace package p_emp_pk is


Function f_find(p_empid number) return varchar2;
end;
/

/*Package body*/

create or replace package body p_emp_pk is


Function f_find(p_empid number) return varchar2 is
v_name employees.first_name%TYPE;

begin
select first_name into v_name from employees where employee_id=p_empid;
return v_name;
exception
when no_data_found then
return null;
end;

end;
/

/* anonymous block*/

declare
v_ename employees.first_name%TYPE;
begin
v_ename:=p_emp_pk.f_find(100);
if v_ename is not null then
DBMS_OUTPUT.PUT_LINE(v_ename);
else
DBMS_OUTPUT.PUT_LINE('regret!error occurred');
end if;
end;
/

_______________________________________________________________________

declare
v_inputstr varchar2(50);
v_revstr varchar2(50);

Begin
v_inputstr:='&v_inputstr';
DBMS_OUTPUT.PUT_LINE('Input string is '||v_inputstr);
for i IN 1..length(v_inputstr) LOOP
v_revstr:=v_revstr||substr(v_inputstr,-i,1);
DBMS_OUTPUT.PUT_LINE('Reversed strng is '||v_revstr);
end Loop;
end;
/

You might also like