0% found this document useful (0 votes)
42 views4 pages

DBMS Week10

Uploaded by

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

DBMS Week10

Uploaded by

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

DATABASE MANAGEMENT SYSTEMS LAB

WEEK-10
Name: Class:CSM-C
Rollno: Date:21/06/24

CURSORS: A cursor is a database object used to retrieve, manipulate, and


navigate through rows in a result set one at a time.
Declaring cursors:
Syntax:
DECLARE
CURSOR cursor_name IS
select_statement;
Program:
create table student(s_id number(10),s_name varchar(20),s_marks number(10))
insert into student values(1,'XYZ',90)
insert into student values(2,'PQR',95)
insert into student values(3,'ABC',93)
insert into student values(4,'JUNNU',98)
select * from student
Declare
id number;
name varchar(10);
marks number;
cursor cursor1 is
select s_id,s_name,s_marks from student;

Opening cursors:
Syntax:
Open cursor_name;

Program:
Open cursor1;
Fetching the data:
Syntax:
fetch cursor_name into declared variables;
Program:
Fetch cursor1 into id,name,marks;
exit when s1%notfound;

Closing cursor:
Syntax:
Close cursor_name;
Program:
Close cursor1;

Implict cursor:
Program:
create table employee(e_id number(10),e_name varchar(20),e_salary number(10))
insert into employee values(1,'ALPHA',20000)
insert into employee values(2,'BETA',30000)
insert into employee values(3,'GAMMA',40000)
insert into employee values(4,'THETHA',20000)
Begin
For A in (Select e_id, e_name, e_salary from employee) loop
Dbms_output.put_line(A.e_id || ' ' || A.e_name || ' ' || A.e_salary);
End loop;
End
Output:

Explict cursor:
Program:
create table student(s_id number(10),s_name varchar(20),s_marks number(10))
drop table student
insert into student values(1,'XYZ',90)
insert into student values(2,'PQR',90)
insert into student values(3,'ABC',90)
insert into student values(4,'JUNNU',90)
select * from student
declare
id number;
name varchar(10);
marks number;
cursor cursor1 is
select s_id,s_name,s_marks from student;
begin
open cursor1;
loop
fetch cursor1 into id,name,marks;
exit when cursor1%notfound;
dbms_output.put_line(id||' '||name||' '||marks);
end loop;
close cursor1;
end;
Output:

Updation with Cursors:


Program:
create table employee(e_id number(10),e_name varchar(20),e_salary number(10))
insert into employee values(1,'ALPHA',20000)
insert into employee values(2,'BETA',30000)
insert into employee values(3,'GAMMA',40000)
insert into employee values(4,'THETHA',20000)
DECLARE
id number;
name varchar(10);
salary number;
salary_1 number;
Cursor Cursor2 is
select e_id,e_name,e_salary from employee;
begin
open Cursor2;
loop
fetch Cursor2 into id,name,salary;
exit when Cursor2%notfound;
salary_1:=salary+1000;
update employee set e_salary=salary_1 where e_id=id;
dbms_output.put_line(id||' '||name||' '||salary);
end loop;
close Cursor2;
end;

OUTPUT:

You might also like