You are on page 1of 12

a1

===========================================================
DECLARE
v_num1 Number;
v_num2 Number;
v_sum Number;
BEGIN
V_num1 := 10; -&Number1
V_num2 := 20; -&Number1
V_sum := V_num1 + V_num2;
Dbms_output.Put_Line('The Sum of number is: '||V_sum);
END;
/
============================================================
a2
============================================================
DECLARE
v_empno Number(4);
v_empname varchar2(10);
BEGIN
Select empno, ename into v_empno, v_empname from emp where empno=1001;
dbms_output.put_line('Employee Number is:' || v_empno);
dbms_output.put_line('Employee Name is:' || v_empname);
END;
/
==============================================================
a3
==============================================================
DECLARE
v_empno emp.empno%type;
v_empname emp.ename%type;
v_sal emp.sal%type;
BEGIN
Select empno, ename, sal into v_empno, v_empname, v_sal from emp where empno=784
4;
dbms_output.put_line('Employee Number is:' || v_empno);
dbms_output.put_line('Employee Name is:' || v_empname);
dbms_output.put_line('Employee Salary is:' || v_sal);
END;
/
==============================================================
a4
==============================================================
DECLARE
v_emp emp%rowtype;
BEGIN

Select * into v_emp from emp where empno=7844;


dbms_output.put_line('Employee Number is:' || v_emp.empno);
dbms_output.put_line('Employee Name is:' || v_emp.ename);
dbms_output.put_line('Employee Salary is:' || v_emp.sal);
END;
/
==============================================================
a5
==============================================================
DECLARE
v_empno emp.empno%type;
v_empname emp.ename%type;
v_sal emp.sal%type;
BEGIN
v_empno := &EmpNo;
Select empno, ename, sal into v_empno, v_empname, v_sal from emp where empno=v_e
mpno;
dbms_output.put_line('Employee Number is:' || v_empno);
dbms_output.put_line('Employee Name is:' || v_empname);
dbms_output.put_line('Employee Salary is:' || v_sal);
END;
/
===============================================================
a6
===============================================================
DECLARE
v_empno emp.empno%type;
v_empname emp.ename%type;
v_sal emp.sal%type;
v_dept dept.deptno%type;
v_loc dept.loc%type;
BEGIN
v_empno := &EmpNo;
select emp.deptno into v_dept from emp where emp.empno=v_empno;
Select dept.loc into v_loc from dept where dept.deptno=v_dept;
if v_loc='chicago' then
dbms_output.put_line('Employee city is:' || v_loc);
else
dbms_output.put_line('Employee City is:' || v_loc);
end if;
END;
/
===============================================================
a7
===============================================================
DECLARE
v_num1 Number;
v_num2 Number;

BEGIN
V_num1 :=&Number1;
V_num2 :=&Number2;
if v_num1 > 0 and v_num2 > 0
then
Dbms_output.Put_Line('first quanrant');
elsif v_num1 < 0 and v_num2 > 0
then
Dbms_output.Put_Line('second quanrant');
elsif v_num1 < 0 and v_num2 < 0
then
Dbms_output.Put_Line('third quanrant');
elsif v_num1 > 0 and v_num2 < 0
then
Dbms_output.Put_Line('forth quanrant');
else
Dbms_output.Put_Line('origin');
end if;
END;
/
===============================================================
a8
===============================================================
DECLARE
i Number;
BEGIN
i:=1;
loop
Dbms_output.Put_Line(i);
i:=i+1;
exit when i=10;
end loop;
END;
/
================================================================
a9
================================================================
DECLARE
i Number;
BEGIN
i:=1;
while i < 10
loop
Dbms_output.Put_Line(i);
i:=i+1;
end loop;
END;
/
================================================================
a10

================================================================
DECLARE
BEGIN
for i in 1..10
loop
Dbms_output.Put_Line(i);
end loop;
END;
/
===============================================================
a11
===============================================================
DECLARE
v_s Varchar2(20);
v_len Number;
BEGIN
v_s :='&String';
v_len :=length(v_s);
Dbms_output.Put_Line('The length of the string is:' || v_len);
END;
/
================================================================
a12
================================================================
DECLARE
v_s Varchar2(50);
v_pos Number;
BEGIN
v_s :='Tesco Hindustan Center';
v_pos :=instr(v_s,'Hin',1);
Dbms_output.Put_Line(v_pos);
END;
/
================================================================
a13
================================================================
DECLARE
v_emp emp%rowtype;
CURSOR c_emp IS
select * from emp order by sal;
BEGIN
OPEN c_emp;

LOOP
FETCH c_emp INTO v_emp;
Dbms_output.Put_Line(v_emp.empno || ' ' || v_emp.ename || ' ' || v_emp.sal);
exit when c_emp%notfound;
end loop;
close c_emp;
END;
/
===================================================================
a14
===================================================================
DECLARE
v_eno emp.empno%type;
v_ename emp.ename%type;
v_dept emp.deptno%type;
v_dname dept.dname%type;
CURSOR c_emp IS
select emp.empno,emp.ename,dept.deptno,dept.dname from emp inner join dept
on emp.deptno = dept.deptno;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO v_eno,v_ename,v_dept,v_dname;
Dbms_output.Put_Line(v_eno || ' ' || v_ename || ' ' || v_dept || ' ' || v_dnam
e);
exit when c_emp%notfound;
end loop;
close c_emp;
END;
/
======================================================================
a15
======================================================================
DECLARE
TYPE v_r is RECORD
(
v_dept dept.deptno%type,
v_dname dept.dname%type,
v_loc dept.loc%type
);
v_dept v_r;
CURSOR c_d is
select * from dept;
BEGIN

open c_d;
Loop
fetch c_d into v_dept;
Dbms_output.Put_Line(v_dept.v_dept || ' ' || v_dept.v_dname || ' ' || v_dept.
v_loc);
exit when c_d%notfound;
end loop;
close c_d;
END;
/
=======================================================================
a16
=======================================================================
DECLARE
Type v_empt is table of number index by binary_integer;
v_emptab v_empt;
v_eno emp.empno%type;
i NUmber;
J Number;
cursor c_emp is
select empno from emp;
BEGIN
i :=1;
j :=1;
open c_emp;
Loop
fetch c_emp into v_eno;
v_emptab(i) := v_eno;
exit when c_emp%notfound;
i := i + 1;
end loop;
Loop
Dbms_output.Put_Line(v_emptab(j));
j := j + 1;
exit when j=i;
end loop;
close c_emp;
END;
/
=======================================================================
a17
=======================================================================
DECLARE
cursor c_emp is
select * from emp;
emp_data c_emp%rowtype;

BEGIN
for v_emp in c_emp
Loop
emp_data := v_emp;
Dbms_output.Put_Line(emp_data.empno || ' ' || emp_data.sal);
end loop;
END;
/
======================================================================
a18
======================================================================
DECLARE
cursor c_dept(p_dept dept.deptno%type) is
select * from dept where dept.deptno = p_dept;
v_dept dept%rowtype;
v_dno dept.deptno%type;
BEGIN
v_dno := &DeptNo;
open c_dept(v_dno);
fetch c_dept into v_dept;
Dbms_output.Put_Line(v_dept.deptno || ' ' || v_dept.dname || ' ' || v_dept.loc);
close c_dept;
END;
/
=====================================================================
a19
=====================================================================
DECLARE
cursor c_dept(p_dept dept.deptno%type) is
select * from dept where dept.deptno = p_dept;
v_dept dept%rowtype;
v_dno dept.deptno%type;
BEGIN
v_dno := &DeptNo;
open c_dept(v_dno);
fetch c_dept into v_dept;
Dbms_output.Put_Line(v_dept.deptno || ' ' || v_dept.dname || ' ' || v_dept.loc);
close c_dept;
EXCEPTION
WHEN OTHERS then
Dbms_output.Put_Line('department not found');
END;
/

=====================================================================
a20
=====================================================================
DECLARE
INVALID_LENGTH Exception;
v_empno Number(30);
BEGIN
v_empno := &EMPNo;
if length(v_empno) > 20 then
Raise INVALID_LENGTH;
else
Dbms_output.Put_Line('Employee No accepted');
end if;
EXCEPTION
when INVALID_LENGTH then
Dbms_output.Put_Line('Employee No too long, try again');
END;
/
====================================================================
a21
====================================================================
create or replace procedure dept_entry
(
p_depno in number,
p_dname in varchar2,
p_loc in varchar2
)
as
begin
insert into dept(deptno,dname,loc) values (p_depno,p_dname,p_loc);
commit;
end;
/
=====================================================================
a22
=====================================================================
DECLARE
v_deptno dept.deptno%type;
v_dname dept.dname%type;
v_loc dept.loc%type;
BEGIN
v_deptno := &DeptNo;
v_dname := '&DeptName';
v_loc := '&Location';
dept_entry(v_deptno,v_dname,v_loc);
end;
/
=====================================================================

a23
======================================================================
create or replace function func_add
(
p_num1 in number,
p_num2 in number
)
return number
as
begin
return p_num1 + p_num2;
end;
/
===================================================================
a24
===================================================================
DECLARE
v_num1 number;
v_num2 number;
v_sum number;
BEGIN
v_num1 := &Number1;
v_num2 := &Number2;
v_sum := func_add(v_num1,v_num2);
Dbms_output.Put_Line('Sum is:' || ' ' || v_sum);
END;
/
====================================================================
a25
====================================================================
DECLARE
TYPE r_emp is record
(
empno emp.empno%type,
ename emp.ename%type,
sal emp.sal%type
);
type t_emp is table of r_emp index by binary_integer;
v_r_emp r_emp;
v_t_emp t_emp;
i number;
cursor c_emp_data is select * from emp;
BEGIN
i :=1;
for rec in c_emp_data
loop
v_r_emp.empno := rec.empno; -- v_t_emp(i).empno :=rec.empno;
v_r_emp.ename := rec.ename; -- v_t_emp(i).ename :=rec.ename;
v_r_emp.sal := rec.sal; -- v_t_emp(i).sal :=rec.sal;

v_t_emp(i) := v_r_emp; -- not required if directly assigned


i := i + 1;
end loop;
for rec in v_t_emp.first..v_t_emp.last
loop
Dbms_output.Put_Line(v_t_emp(rec).empno || ' ' || v_t_emp(rec).ename || ' ' ||
v_t_emp(rec).sal);
end loop;
END;
/
==================================================================
a26
==================================================================
create or replace package abhi_emppackage
as
subtype t_ename is varchar2(25);
v_empno emp.empno%type;
v_ename t_ename;
cursor c_emp is select * from emp;
procedure calculatebonus(p_deptno number);
procedure updatesalary(p_empno number, p_sal number);
function getsalary(p_empno number) return number;
end abhi_emppackage;
/
=================================================================
a27
=================================================================
create or replace package body abhi_emppackage
as
v_sal number;
function fullname(p_fname char,p_lname char) return number
is
begin
end fullname;
procedure calculatebonus(p_deptno number)
as
begin
exception
end calculatebonus;
==================================================================
create table abhi_emp as select * from emp;
==================================================================
a28
==================================================================
create or replace trigger print_salary_changes
before
update on abhi_emp
for each row when (new.empno > 0)
declare
sal_diff number;
begin
sal_diff := :new.sal - :old.sal;

Dbms_output.Put_Line('old salary:' || :old.sal);


Dbms_output.Put_Line('new salary:' || :new.sal);
Dbms_output.Put_Line('diff in salary:' || sal_diff);
end;
/
=================================================================
update abhi_emp set sal = 5000 where empno = 7782;
=================================================================

You might also like