0% found this document useful (0 votes)
22 views27 pages

Dbms Print 4 To 11-1

The document discusses implementing group functions with different operators such as aggregate, group by, having and order by on a table. It also discusses nested and correlated nested queries using set operators and comparisons. Finally it discusses creating views, synonyms, sequences, indexes and savepoints as well as various types of joins like outer and inner joins.

Uploaded by

lovelybot19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views27 pages

Dbms Print 4 To 11-1

The document discusses implementing group functions with different operators such as aggregate, group by, having and order by on a table. It also discusses nested and correlated nested queries using set operators and comparisons. Finally it discusses creating views, synonyms, sequences, indexes and savepoints as well as various types of joins like outer and inner joins.

Uploaded by

lovelybot19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

GROUP FUNCTIONS WITH DIFFERENT OPERATORS

WEEK 4A:
4. a) AIM:- Implement group functions with different operators such as aggregate operators, group by, having
and order by.
IMPLEMENTATION:
SQL> create table t8 (id int,name varchar(20),age int, salary int);

Table created.

SQL> desc t8
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER(38)
NAME VARCHAR2(20)
AGE NUMBER(38)
SALARY NUMBER(38)

SQL> insert into t8 values(01,'sree',25,5000);

1 row created.

SQL> insert into t8 values(02,'raj',27,7000);

1 row created.

SQL> insert into t8 values(03,'navdee',24,4000);

1 row created.

SQL> insert into t8 values(04,'ram',26,9000);

1 row created.

SQL> insert into t8 values(05,'varu',26,6000);

1 row created.

SQL> insert into t8 values(06,'pavan',26,8000);

1 row created.

SQL> insert into t8 values(07,'shiny',26,10000);


1 row created.

SQL> insert into t8 values(08,'rishi',28,15000);

1 row created.

SQL> selet*from t8;


SP2-0734: unknown command beginning "selet*from..." - rest of line ignored.
SQL> select*from t8
2 ;

ID NAME AGE SALARY


---------- -------------------- ---------- ----------
1 sree 25 5000
2 raj 27 7000
3 navdee 24 4000
4 ram 26 9000
5 varu 26 6000
6 pavan 26 8000
7 shiny 26 10000
8 rishi 28 15000

8 rows selected.

SQL> select count (id)


2 from t8;

COUNT(ID)
----------
8

SQL> select count(salary) from t8 where salary>10000;

COUNT(SALARY)
-------------
1

SQL> select count(salary) from t8 where salary<10000;

COUNT(SALARY)
-------------
6

SQL> select max(salary) from t8;


MAX(SALARY)
-----------
15000

SQL> select min(salary) from t8;

MIN(SALARY)
-----------
4000

SQL> select avg(salary) from t8;

AVG(SALARY)
-----------
8000

SQL> select id,count(id) from t8 where age=26 group by id;

ID COUNT(ID)
---------- ----------
6 1
4 1
5 1
7 1

SQL> select id, name from t8 order by id asc;

ID NAME
---------- --------------------
1 sree
2 raj
3 navdee
4 ram
5 varu
6 pavan
7 shiny
8 rishi

8 rows selected.

SQL> select id, name,age from t8 order by id asc;

ID NAME AGE
---------- -------------------- ----------
1 sree 25
2 raj 27
3 navdeep 24
4 ram 26
5 varu 26
6 pavan 26
7 shiny 26
8 rishi 28

8 rows selected.

SQL> select id,count(id) from t8 where age=26 group by id having max(age)<25;

no rows selected

SQL> select id,count(id) from t8 where age=26 group by id having max(age)<27;

ID COUNT(ID)
---------- ----------
6 1
4 1
5 1
7 1

SQL> select id from t8 where salary like 9000;

ID
----------
4
RESULT:-
NESTED & CORRELATED NESTED QUERIES
(Using set & set comparison operators.)
WEEK 4b:
4.b) AIM:-Implement nested and correlated nested queries using set operators and set comparison operators.
IMPLEMENTATION:
SQL> create table eee(id number(2),department varchar(5));

Table created.
SQL> insert into eee values(1,'cse');

1 row created.

SQL> insert into eee values(2,'ece');

1 row created.

SQL> insert into eee values(3,'eee');

1 row created.

SQL> insert into eee values(4,'civil');

1 row created.

SQL> insert into eee values(5,'mech');

1 row created.
SQL> select * from eee;

ID DEPAR
---------- -----
1 cse
2 ece
3 eee
4 civil
5 mech

SQL> select name from employeee where exists (select * from eee where employeee.id=eee.id and
salary>4500);

NAME
----------
aliha
kathrin

SQL> select * from employeee where exists (select * from eee where employeee.id=eee.id and salary>4500);

ID NAME AGE ADDRESS SALARY


---------- ---------- ---------- ---------- ----------
4 aliha 29 uk 6500
5 kathrin 34 bangalore 8500

SQL> select * from employeee where exists (select * from eee where employeee.id=eee.id );

ID NAME AGE ADDRESS SALARY


---------- ---------- ---------- ---------- ----------
1 john 20 us 2000
2 stephan 26 dubai 1500
3 david 27 bangkok 2000
4 aliha 29 uk 6500
5 kathrin 34 bangalore 8500
SQL> select * from employeee where exists (select * from eee where employeee.id<>eee.id );

ID NAME AGE ADDRESS SALARY


---------- ---------- ---------- ---------- ----------
1 john 20 us 2000
2 stephan 26 dubai 1500
3 david 27 bangkok 2000
4 aliha 29 uk 6500
5 kathrin 34 bangalore 8500
6 harry 42 china 4500
7 jack 25 mizoram 10000

7 rows selected.
CREATION OF VIEWS, SYNONYMS, SEQUENCE, INDEXES,SAVEPOINT

WEEK 5A:
5. a) AIM: To execute and verify queries for creating views, synonyms, sequence, indexes and save point.
IMPLEMENTATION:
SQL> create table prod07( pod_id number(1),prd_name varchar2(15), price number(4));

Table created.

SQL> insert into prod07 values(1,'penpencil',40);

1 row created.

SQL> insert into prod07 values(2,'carpet',4500);

1 row created.

SQL> insert into prod07 values(3,'ink pen',200);

1 row created.

SQL> insert into prod07 values(4,'ball pen',20);

1 row created.

SQL> insert into prod07 values(5,'ribbons',150);


1 row created.
SQL> select * from prod07;
POD_ID PRD_NAME PRICE
---------- --------------- ----------
1 penpencil 40
2 carpet 4500
3 ink pen 200
4 ball pen 20
5 ribbons 150
VIEW:
SQL> CREATE VIEW productsabove120 AS SELECT prd_name,price FROM prod07 WHERE price > 120;

View created.

SQL> select * from productsabove120;

PRD_NAME PRICE
--------------- ----------
carpet 4500
ink pen 200
ribbons 150

SEQUENCE:
SQL> CREATE SEQUENCE sequence_1
2 start with 1
3 increment by 1
4 minvalue 0
5 maxvalue 100
6 cycle;

Sequence created.
INDEX:

SQL> CREATE INDEX priceindex ON PROD07 ( price);

Index created.
SAVEPOINT:

SQL> SAVEPOINT sp1;

Savepoint created.
SQL> DELETE FROM prod07 WHERE PRICE=20;
1 row deleted
.
SQL> ROLLBACK TO sp1;
Rollback complete.
VARIOUS TYPES OF JOINS
WEEK 5b:
5. b) AIM: Implement various types of joins - outer join and inner join.
SQL> select eno,ename,dname from employeef5 e join deptf5 d on (e.deptno=d.deptno);

ENO ENAME DNAME


---------- -------------------- --------------
1 Ram Accounting
2 Sree Accounting
3 Rishi Sales
4 Vinee Sales
5 Prabhas Accounting
6 Sravya Sales
7 Raj Research
8 Harshith Research
9 Varun Operations
10 Venky Accounting
11 Reva Research

ENO ENAME DNAME


---------- -------------------- --------------
12 sai Research

SQL> select eno,ename,dname from employeef5 e join deptf5 d on (e.deptno>d.deptno);

ENO ENAME DNAME


---------- -------------------- --------------
11 Reva Accounting
12 Sai Accounting
8 Harshith Accounting
9 Varun Accounting
4 Vinee Accounting
3 Rishii Accounting
7 Raj Accounting
10 Venky Accounting
4 Vinee Research
3 Rishi Research
7 Raj Research

ENO ENAME DNAME


---------- -------------------- --------------
10 Venky Research
10 Venky Sales

13 rows selected.

SQL> select eno,ename,dname from employeef5 e left join deptf5 d on (e.deptno=d.deptno);


ENO ENAME DNAME
---------- -------------------- --------------
11 Reva Accounting
5 Prabhas Accounting
2 Sree Accounting
1 Ram Accounting
12 Sai Research
11 Reva Research
9 Varun Research
8 Harshith Research
7 Raj Sales
4 Vinee Sales
3 Rishi Sales

ENO ENAME DNAME


---------- -------------------- --------------
10 Venky Operations

12 rows selected.

SQL> select eno,ename,dname from employeef5 e right join deptf5 d on (e.deptno=d.deptno);

ENO ENAME DNAME


---------- -------------------- --------------
1 Ram Accounting
2 Sree Accounting
3 Rishi Sales
4 Vinee Sales
5 Prabhas Accounting
6 Sravya Sales
7 Raj Research
8 Harshith Research
9 Varun Operations
10 Venky Accounting
12 Reva Research

ENO ENAME DNAME


---------- -------------------- --------------
13 Sai Research
12 rows selected.

SQL> select eno,ename,dname from employeef5 e natural join deptf5 d;

ENO ENAME DNAME


---------- -------------------- --------------
1 Ram Accounting
2 Sree Accounting
3 Rishii Sales
4 Vinee Sales
5 Prabhas Accounting
6 Sravya Sales
7 Raj Research
8 Harshith Research
9 Varun Operations
10 Venky Accounting
11 Reva Research

ENO ENAME DNAME


---------- -------------------- --------------
12 Sai Research

12 rows selected.
Basic PL/SQL:
WEEK 6:
6. a)AIM:
6. Construct PL/SQL block for the following.
a) To determine whether a number is palindrome
b) To determine whether a number is an Armstrong number
c) To find greatest of three numbers
d) To display Fibonacci series
IMPLEMENTATION:
SQL> set serveroutput on;
SQL> declare
2 a integer:=10;
3 b integer:=20;
4 c integer;
5 begin
6 c:=a+b;
7 dbms_output.put_line('value of c:'||c);
8 end;
9 /
value of c:30
PL/SQL procedure successfully completed.
1)To determine whether a number is an Armstrong number
SQL> declare
2 n number:=153;
3 s number:=0;
4 r number;
5 m number;
6 begin
7 m:=n;
8 while(n>0) loop
9 r:=mod(n,10);
10 s:=s+r*r*r;
11 n:=trunc(n/10);
12 end loop;
13 if (m=s) then
14 dbms_output.put_line('Yes');
15 else
16 dbms_output.put_line('No');
17 end if;
18 end;
19 /
Yes
PL/SQL procedure successfully completed.
2)To determine whether a number is palindrome
SQL> declare
2 n number;
3 m number;
4 rev number:=0;
5 rem number;
6 begin
7 n:=1221;
8 m:=n;
9 while(n>0) loop
10 rem:=mod(n,10);
11 rev:=(rev*10)+rem;
12 n:=trunc(n/10);
13 end loop;
14 if(m=rev) then
15 dbms_output.put_line('true');
16 else
17 dbms_output.put_line('false');
18 end if;
19 end;
20 /
true
PL/SQL procedure successfully completed.
3)To find greatest of three numbers
SQL> declare
2 a number:=46;
3 b number:=67;
4 c number:=109;
5 begin
6 if(a>b and a>c) then
7 dbms_output.put_line('Greatest number is'||a);
8 elsif(b>a and b>c) then
9 dbms_output.put_line('Greatest number is'||b);
10 else
11 dbms_output.put_line('Greatest number is'||c);
12 end if;
13 end;
14 /
Greatest number is109
PL/SQL procedure successfully completed.
4)To display Fibonacci series
SQL> declare
2 first number:=0;
3 second number:=1;
4 temp number;
5 n number:=8;
6 i number;
7 begin
8 dbms_output.put_line('Series:');
9 dbms_output.put_line(first);
10 dbms_output.put_line(second);
11 for i in 2..n loop
12 temp:=first+second;
13 first:=second;
14 second:=temp;
15 dbms_output.put_line(temp);
16 end loop;
17 end;
18 /
Series:
0
1
1
2
3
5
8
13
21
PL/SQL procedure successfully completed.
(Control Structures:)
SALARY OF SPECIFIC EMPLOYEE

WEEK 7A:

7.a)AIM: Write a program in PL/SQL to update the salary of a specific employee by 8% if the salary exceeds
the mid-range of the salary against this job and update up to mid-range if the salary is less than the mid-range
of the salary, and display a suitable message.

IMPLEMENTATION:
CREATE TABLE staff(employee_id number,e_name varchar(20),salary number);
INSERT INTO staff values(1,venky,16950);
INSERT INTO staff values(2,ram,15000);
INSERT INTO staff values(3,shyam,17000);
INSERT INTO staff values(4,raju,18200);
DECLARE
mid_range staff.salary%type;
eid staff.employee_id%type:=&eid;
e_salary staff.salary%type;
BEGIN
select median(salary) into mid_range from staff;
select salary into e_salary from staff where employee_id=eid;
if(e_salary>mid_range) then
update staff set salary=salary+(salary*0.08) where employee_id=eid;
select salary into e_salary from staff where employee_id=eid;
dbms_output.put_line('Employee with id='||eid||'salary is increased by 8% i.e., '||e_salary);
else
update staff set salary=mid_range where employee_id=eid;
dbms_output.put_line('Employee with id='||eid||'salary is updated to midrange('||mid_range||')');
end if;
commit;
end;
/
OUTPUT:
Enter value for eid: 2
old 3: eid staff.employee_id%type:=&eid;
new 3: eid staff.employee_id%type:=2;
Employee with id=2 salary is updated to midrange(17500)
STUDENT’S GRADE
(USING CASE STATEMENT)
WEEK 7b:
7. b) AIM: Write a PL/SQL program to display the description against a student‘s grade using CASE statement.
IMPLEMENTATION:
SQL> declare
2 grade varchar2(3):='A';
3 begin
4 case grade
5 when 'A' then dbms_output.put_line('Distinction');
6 when 'B' then dbms_output.put_line('First Class');
7 when 'C' then dbms_output.put_line('Second Class');
8 when 'D' then dbms_output.put_line('pass');
9 when 'E' then dbms_output.put_line('Fail');
10 else
11 dbms_output.put_line('invalid');
12 end case;
13 end;
14 /
Distinction

PL/SQL procedure successfully completed.


Exception Handling:
RUN TIME EXCEPTION
WEEK 8A:
8. a) AIM: Develop a PL/SQL program that displays the name and address of a student whose ID is
given. If there is no student with the given student ID in the database, the program should raise a run-
time exception NO_DATA_FOUND, which should be captured in the EXCEPTION block.
IMPLEMENTATION:
create table students(id number,name varchar(20),address varchar(20));
insert into students values(1,'hari','tpt');
insert into students values(2,'ravi','ctr');
insert into students values(3,'raju','kdp');
insert into students values(4,'ramu','tpt');
DECLARE
student_name STUDENTS.name%type;
student_address STUDENTS.address%type;
student_id STUDENTS.id%type:=&student_id;
BEGIN
select name, address into student_name,student_address from STUDENTS where id=student_id;
dbms_output.put_line('student id is '|| student_id||',name is '||student_name||',address is '||student_address);
EXCEPTION
when NO_DATA_FOUND THEN
dbms_output.put_line('There is no student with id='||student_id);
END;
/
USER DEFINED EXCEPTION
(salary of an employee)

WEEK 8b:
8.b) AIM: Construct the user-defined exceptions to get the salary of an employee and check it with the job‘s
salary range. If the salary is below the range, raise an exception BELOW_SALARY_RANGE. If the salary is above
the range, raise the exception ABOVE_SALARY_RANGE.

IMPLEMENTATION:

create table employees(id number,name varchar(20),salary int);


insert into employees values(1,'hari',18000);
insert into employees values(2,'ravi',35000);
insert into employees values(3,'raju',25000);
insert into employees values(4,'ramu',45000);

create table jobs(id number,minsalary int,maxsalary int);


insert into jobs values(1,10000,20000);
insert into jobs values(2,20000,30000);
insert into jobs values(3,30000,40000);
insert into jobs values(4,40000,50000);

DECLARE
min_salary employees.salary%type;
max_salary min_salary%type;
e_id employees.id%type:=&e_id;
e_salary max_salary%type;
BELOW_SALARY_RANGE EXCEPTION;
ABOVE_SALARY_RANGE EXCEPTION;
BEGIN
select minsalary,maxsalary into min_salary,max_salary from jobs where id=e_id;
select SALARY into e_salary from employees where id=e_id;
CASE
when e_salary<min_salary then raise BELOW_SALARY_RANGE;
when e_salary>max_salary then raise ABOVE_SALARY_RANGE;
else dbms_output.put_line('Employee salary is in the range '
||min_salary||' '||max_salary);
end CASE;
EXCEPTION
when BELOW_SALARY_RANGE then dbms_output.put_line('Employee salary is below the salary range');
when ABOVE_SALARY_RANGE then dbms_output.put_line('Employee salary is above the salary range');
END;
/
Functions:
WEEK 9A:
9. a) AIM: Write a function that accepts two numbers A and B and performs the following operations.
o Addition
o Subtraction
o Multiplication
o Division
IMPLEMENTATION:
declare
a number:=&a;
b number:=&b;
c number;
d number;
e number;
f number;
function add(x number,y number)
return number
is
z number;
begin
z:=x+y;
return z;
end;
function sub(x number,y number)
return number
is
z number;
begin
z:=x-y;
return z;
end;
function mul(x number,y number)
return number
is
z number;
begin
z:=x*y;
return z;
end;
function div(x number,y number)
return number
is
z number;
begin
z:=x/y;
return z;
end;
begin
c:=add(a,b);
d:=sub(a,b);
e:=mul(a,b);
f:=div(a,b);
dbms_output.put_line('sum is '||c);
dbms_output.put_line('difference is '||d);
dbms_output.put_line('multiplication is '||e);
dbms_output.put_line('division is '||f);
end;
/
UPDATES SALARY OF AN EMPLOYEE
(Using incr function)
WEEK 9B:
9.b)AIM: Write a PL/SQL block that updates salary of an employee in Employee table by using incr function
which takes employee number as argument and calculates increment and returns increment based on the
following criteria. If salary <= 3000, increment = 30% of salary If salary > 3000 and <= 6000, increment = 20%
of salary else increment = 10% of salary.
IMPLEMENTATION:
create table emp(empno number,sal number);
insert into emp values (1,3000);
insert into emp values (2,4500);
insert into emp values (3,6000);
insert into emp values (4,2800);
DECLARE
e_id emp.empno%type:=&e_id;
e_sal emp.sal%type;
e_incr emp.sal%type;
e_values number;
function incr(e_id in number,e_incr out number) return number
is e_salary emp.sal%type:=0;
begin
select sal into e_salary from emp where empno=e_id;
if(e_salary<=3000) then
e_salary:=e_salary*0.3;
e_incr:=30;
elsif(e_salary>3000 and e_salary<=6000) then
e_salary:=e_salary*0.2;
e_incr:=20;
else
e_salary:=e_salary*0.1;
e_incr:=10;
end if;
return e_salary;
end;
BEGIN
select sal into e_sal from emp where empno=e_id;
dbms_output.put_line('Employee salary before incrementing'||e_sal);
e_values:=incr(e_id,e_incr);
update emp set sal=sal +e_values where empno=e_id;
commit;
select sal into e_sal from emp where empno=e_id;
dbms_output.put_line('Employess salary after incrementing by;'||e_incr||'% is'||e_sal);
EXCEPTION
WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('EMPLOYEE DATA NOT FOUND');
when others then dbms_output.put_line('ERROR');
END;
/
Procedures:
(DISPLAYS THEIR SUM)
WEEK 10A:
10. a)AIM: Write a procedure that accepts two numbers and displays their sum.
IMPLEMENTATION:
SQL> set serveroutput on;
SQL> create procedure f5(a in number,b in number)
2 as
3 begin
4 dbms_output.put_line('result is '||(a+b));
5 end;
6 /
Procedure created.

SQL> exec f5(2,3);


result is 5

PL/SQL procedure successfully completed.

321
Starred
Snoozed
Sent

Drafts
PROCEDURES WITH PARAMETERS
WEEK 10b:
10.b)AIM: Write procedures to demonstrate IN, IN OUT and OUT parameters.
IMPLEMENTATION:
SQL> set serveroutput on;
SQL> declare
2 a number:=&a;
3 b number:=&b;
4 c number;
5 procedure p5(a in number,b in out number,c out number)
6 as
7 begin
8 c:=a+b;
9 b:=a-b;
10 end;
11 begin
12 p5(a,b,c);
13 dbms_output.put_line('The value of c is '||c||'The value of b is '||b);
14 end;
15 /
Enter value for a: 15
old 2: a number:=&a;
new 2: a number:=15;
Enter value for b: 24
old 3: b number:=&b;
new 3: b number:=24;
The value of c is 39 The value of b is -9

PL/SQL procedure successfully completed.


DISPLAY THE EMPLOYEE NAME & NO. OF JOBS DONE IN PAST
(Using cursors)
WEEK 11A:
11. a)AIM:Write a block in PL/SQL to create a Cursor that displays the employee name and number of jobs he or
she has done in the past.
IMPLEMENTATION:

-- creating employee table with name employee


--e_name is the name of the employee
--previous_number_of_jobs is previous number of jobs the employee has done
create table employee( e_name varchar2(10),previous_number_of_jobs number(4));

--o/p: table is created


--now lets insert values

insert into employee(e_name,previous_number_of_jobs)


values ('david',4);
insert into employee(e_name,previous_number_of_jobs)
values ('valli',8);
insert into employee(e_name,previous_number_of_jobs)
values ('diana',3);
insert into employee(e_name,previous_number_of_jobs)
values ('Alexander',2);
insert into employee(e_name,previous_number_of_jobs)
values ('mukesh',10);

--o/p : rows are created


--now lets check details are entered correctly or not
--select is used to fetch details from table

select * from employee;

--observe output in console


--now run below program for given question in lab

set serveroutput on;


DECLARE
cursor c_emp is select * from employee;

--cursor keyword is used to create a cursor

temp_emp c_emp%rowtype;--creating temporary cursor


BEGIN
open c_emp;

--open is used to create a cursor in read only property

loop

--loop is used for fetching values of a cursor one by one

fetch c_emp into temp_emp;

--fetch keyword is used for reading values of a cursor

exit when c_emp%notfound;

--%not found is used to point the ending of cursor


--|| this symbol is used for concatinating string and variable or constant
dbms_output.put_line('Employee'||temp_emp.e_name||'has done'||
temp_emp.previous_number_of_jobs||'job(s) in the past');
end loop;
close c_emp;
END;
/

OUTPUT:
Employee david has done 4job(s) in the past
Employee valli has done 8job(s) in the past
Employee diana has done 3job(s) in the past
Employee Alexander's done 2job(s) in the past
Employee mukesh has done 10job(s) in the past
DISPLAY THE NAME & SALARY OF EACH EMPLOYEE
(Using cursors)

WEEK 11b:
11.b)AIM: Write a program in PL/SQL to create a cursor to display the name and salary of each employee in the
EMPLOYEES table whose salary is less than that specified by a passed-in parameter value.
IMPLEMENTATION:
-- creating employee table with name emp
--ename is the name of the employee and sal is the salary of employee

create table emp( ename varchar2(10),sal number(8));

--o/p: table is created


--now lets insert values

insert into emp(ename,sal)


values ('david',4800);
insert into emp(ename,sal)
values ('valli',4800);
insert into emp(ename,sal)
values ('diana',4200);
insert into emp(ename,sal)
values ('Alexander',3100);
insert into emp(ename,sal)
values ('mukesh',8000);

--o/p : rows are created


--now lets check details are entered correctly or not
--select is used to fetch details from table

select * from emp;


--observe output in console
--now run below program for above query in the question

set serveroutput on;


DECLARE --declare keyword is used for declaring program
cursor c_emp(c_sal emp.sal%type) is select * from emp where sal<c_sal;

--cursor key is used to create cursor .refer google to know more about cursor
--%type is used to keep same datatype of a column of a table to the variable

temp_emp c_emp%rowtype; --creating temporary cursor


salary emp.sal%type:=&salary;--creating salary datatype

--ampersand symbol is used for user input

BEGIN
open c_emp(salary);

--open property is used to specify that we intend to use for read only but not updating

dbms_output.put_line('Employee with salary below '||salary||' are');

loop --loop for fetching values of cursor one by one

-- fetch is used to read inputs from cursor

fetch c_emp into temp_emp;


--%not found is used to point cursor ending
exit when c_emp%notfound;
dbms_output.put_line('Employee : '||temp_emp.ename||' salary is : '|| temp_emp.sal);
end loop;
close c_emp;
END;
/

--observe output in console


OUTPUT:
enter value for salary :5000
Name:David Salary:4800
Name:Valli Salary:4800
Name:Diana Salary:4200
Name:Alexander Salary:3100

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy