CURSORS SQL
---CURSORS---
---Using cursors, we can retreive the first, next, previous,last rows in a resultset as well as the rows at a particular position.
--We can also see the current data and previous data using cursors.
--TYPES OF CURSORS:--
----1)KeySet Cursors
----2)Dynamic cursors
----3)Forward_only cursors.
---KeySet Cursors:--Used to view first, last, previous, next rows in a resultset.
----We can also view rows at a particular position.
----Data Changes cannot be viewed using KeySet Cursors.
----Dynamic Cursors:-Used to view first,last, previous, next rows in a resultset.
--Rows at a particular position cannot be viewed. Data changes can be viewed.
---Forward_only cursors:-Only next rows can be seen.
--First, Previous, last rows cannot be viewed, data changes cannot be seen
create table EmpMaster
(eno int primary key,
ename varchar(10))
--insert EmpMastervalues(200,'cd')
select * from EmpMaster
Create procedure curproc
as
declare
@a int,
@b varchar(10)
declare abc cursor keyset for select eno, ename from EmpMaster
open abc
/*fetch first from abc into @a, @b
print Convert(Varchar(10),@a)+' '+@b
fetch next from abc into @a, @b
print Convert(Varchar(10),@a)+' '+@b*/
fetch last from abc into @a,@b
print Convert(Varchar(10),@a)+' '+@b
insert EmpMaster(eno,ename) values(1679,'cv')
fetch last from abc into @a,@b
print Convert(Varchar(10),@a)+' '+@b
/*fetch absolute 3 from abc into @a,@b
print Convert(Varchar(10),@a)+' '+@b*/
close abc
deallocate abc
---Using cursors, we can retreive the first, next, previous,last rows in a resultset as well as the rows at a particular position.
--We can also see the current data and previous data using cursors.
--TYPES OF CURSORS:--
----1)KeySet Cursors
----2)Dynamic cursors
----3)Forward_only cursors.
---KeySet Cursors:--Used to view first, last, previous, next rows in a resultset.
----We can also view rows at a particular position.
----Data Changes cannot be viewed using KeySet Cursors.
----Dynamic Cursors:-Used to view first,last, previous, next rows in a resultset.
--Rows at a particular position cannot be viewed. Data changes can be viewed.
---Forward_only cursors:-Only next rows can be seen.
--First, Previous, last rows cannot be viewed, data changes cannot be seen
create table EmpMaster
(eno int primary key,
ename varchar(10))
--insert EmpMastervalues(200,'cd')
select * from EmpMaster
Create procedure curproc
as
declare
@a int,
@b varchar(10)
declare abc cursor keyset for select eno, ename from EmpMaster
open abc
/*fetch first from abc into @a, @b
print Convert(Varchar(10),@a)+' '+@b
fetch next from abc into @a, @b
print Convert(Varchar(10),@a)+' '+@b*/
fetch last from abc into @a,@b
print Convert(Varchar(10),@a)+' '+@b
insert EmpMaster(eno,ename) values(1679,'cv')
fetch last from abc into @a,@b
print Convert(Varchar(10),@a)+' '+@b
/*fetch absolute 3 from abc into @a,@b
print Convert(Varchar(10),@a)+' '+@b*/
close abc
deallocate abc
CURSORS SQL
Reviewed by Rupesh
on
19:45
Rating:
No comments: