Professional Documents
Culture Documents
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;
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;
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';
declare
v_date date;
v_day varchar2(10);
begin
v_date:='&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
Exception Propagation
___________________________
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;
------------------------------------------------------------------
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.
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.
---------------------------------
SELECT
SELECT with WHERE
SELECT with ORDER BY
SELECT with GROUP BY
SELECT with GROUP BY and HAVING
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
Input Output
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;
/
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;
----------------------------------------------
FOR LOOP
______________
Begin
For i in 1..10 Loop
DBMS_OUTPUT.PUT_LINE(i);
end loop;
end;
/
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
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);
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;
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
If SQL%found then
v_recs := SQL%ROWCOUNT;
End;
----------------------------------------
DECLARE
BEGIN
rollback;
END IF;
END;
/
--------------------------------------------------------
DECLARE
x NUMBER := 0;
BEGIN
x := x + 1;
IF x < 3 THEN
CONTINUE;
END IF;
DBMS_OUTPUT.PUT_LINE
END LOOP;
END;
/
----------------------------------------------
PROCEDURES
_______________
begin
If SQL%Found then
v_dummy := SQL%ROWCOUNT;
end if;
If v_dummy = 1 then
p_status := 'Y';
end if;
exception
p_status := 'N';
p_status := 'N';
end;
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';
add_dept(v_deptid,v_deptname,v_status);
else
dopl('Error occurred');
end if;
end;
-----------------------------------------------------------------------------------
-------
write a procedure to accept a date input and check if there are any training
programmes.
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
___________
begin
return (to_char(p_Date,'Month'));
end;
show err;
dopl (f_ext_month('08-Nov-2019'));
end;
---------------------------------------------------------------------------------
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;
/
---------------------------------------------------------------
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;
/
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;
if v_dep_check = 0 then
v_dept_name := f_dept_name(v_dept_id);
else
raise e_multi_rows;
end if;
exception
end;
/
------------------------------------------------------------
PACKAGE
_____________
/*Package specification*/
/*Package body*/
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;
/