<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
database	B-Language
cursor	I-Language
is	O
a	O
mechanism	O
that	O
enables	O
traversal	O
over	O
the	O
records	B-Application
in	O
a	O
database	O
.	O
</s>
<s>
Cursors	B-Language
facilitate	O
subsequent	O
processing	O
in	O
conjunction	O
with	O
the	O
traversal	O
,	O
such	O
as	O
retrieval	O
,	O
addition	O
and	O
removal	O
of	O
database	B-Application
records	I-Application
.	O
</s>
<s>
The	O
database	B-Language
cursor	I-Language
characteristic	O
of	O
traversal	O
makes	O
cursors	B-Language
akin	O
to	O
the	O
programming	O
language	O
concept	O
of	O
iterator	O
.	O
</s>
<s>
Cursors	B-Language
are	O
used	O
by	O
database	O
programmers	O
to	O
process	O
individual	O
rows	O
returned	O
by	O
database	B-Application
system	I-Application
queries	O
.	O
</s>
<s>
Cursors	B-Language
enable	O
manipulation	O
of	O
whole	O
result	B-Application
sets	I-Application
at	O
once	O
.	O
</s>
<s>
In	O
this	O
scenario	O
,	O
a	O
cursor	B-Language
enables	O
the	O
sequential	O
processing	O
of	O
rows	O
in	O
a	O
result	B-Application
set	I-Application
.	O
</s>
<s>
In	O
SQL	O
procedures	O
,	O
a	O
cursor	B-Language
makes	O
it	O
possible	O
to	O
define	O
a	O
result	B-Application
set	I-Application
(	O
a	O
set	O
of	O
data	O
rows	O
)	O
and	O
perform	O
complex	O
logic	O
on	O
a	O
row	O
by	O
row	O
basis	O
.	O
</s>
<s>
By	O
using	O
the	O
same	O
mechanics	O
,	O
a	O
SQL	O
procedure	O
can	O
also	O
define	O
a	O
result	B-Application
set	I-Application
and	O
return	O
it	O
directly	O
to	O
the	O
caller	O
of	O
the	O
SQL	O
procedure	O
or	O
to	O
a	O
client	O
application	O
.	O
</s>
<s>
A	O
cursor	B-Language
can	O
be	O
viewed	O
as	O
a	O
pointer	O
to	O
one	O
row	O
in	O
a	O
set	O
of	O
rows	O
.	O
</s>
<s>
The	O
cursor	B-Language
can	O
only	O
reference	O
one	O
row	O
at	O
a	O
time	O
,	O
but	O
can	O
move	O
to	O
other	O
rows	O
of	O
the	O
result	B-Application
set	I-Application
as	O
needed	O
.	O
</s>
<s>
To	O
use	O
cursors	B-Language
in	O
SQL	O
procedures	O
,	O
you	O
need	O
to	O
do	O
the	O
following	O
:	O
</s>
<s>
Declare	O
a	O
cursor	B-Language
that	O
defines	O
a	O
result	B-Application
set	I-Application
.	O
</s>
<s>
Open	O
the	O
cursor	B-Language
to	O
establish	O
the	O
result	B-Application
set	I-Application
.	O
</s>
<s>
Fetch	O
the	O
data	O
into	O
local	O
variables	O
as	O
needed	O
from	O
the	O
cursor	B-Language
,	O
one	O
row	O
at	O
a	O
time	O
.	O
</s>
<s>
Close	O
the	O
cursor	B-Language
when	O
done	O
.	O
</s>
<s>
This	O
section	O
introduces	O
the	O
ways	O
the	O
SQL:2003	B-Language
standard	O
defines	O
how	O
to	O
use	O
cursors	B-Language
in	O
applications	O
in	O
embedded	O
SQL	O
.	O
</s>
<s>
Not	O
all	O
application	O
bindings	O
for	O
relational	O
database	B-Application
systems	I-Application
adhere	O
to	O
that	O
standard	O
,	O
and	O
some	O
(	O
such	O
as	O
CLI	O
or	O
JDBC	B-Language
)	O
use	O
a	O
different	O
interface	O
.	O
</s>
<s>
A	O
programmer	O
makes	O
a	O
cursor	B-Language
known	O
to	O
the	O
DBMS	B-Application
by	O
using	O
a	O
DECLARE	O
...	O
CURSOR	B-Language
statement	O
and	O
assigning	O
the	O
cursor	B-Language
a	O
(	O
compulsory	O
)	O
name	O
:	O
</s>
<s>
DECLARE	O
cursor_name	O
CURSOR	B-Language
IS	O
SELECT	O
...	O
FROM	O
...	O
</s>
<s>
Before	O
code	O
can	O
access	O
the	O
data	O
,	O
it	O
must	O
open	O
the	O
cursor	B-Language
with	O
the	O
OPEN	O
statement	O
.	O
</s>
<s>
Directly	O
following	O
a	O
successful	O
opening	O
,	O
the	O
cursor	B-Language
is	O
positioned	O
before	O
the	O
first	O
row	O
in	O
the	O
result	B-Application
set	I-Application
.	O
</s>
<s>
Applications	O
position	O
cursors	B-Language
on	O
a	O
specific	O
row	O
in	O
the	O
result	B-Application
set	I-Application
with	O
the	O
FETCH	O
statement	O
.	O
</s>
<s>
Once	O
an	O
application	O
has	O
processed	O
all	O
available	O
rows	O
or	O
the	O
fetch	O
operation	O
is	O
to	O
be	O
positioned	O
on	O
a	O
non-existing	O
row	O
(	O
compare	O
scrollable	O
cursors	B-Language
below	O
)	O
,	O
the	O
DBMS	B-Application
returns	O
a	O
SQLSTATE	O
'	O
02000	O
 '	O
(	O
usually	O
accompanied	O
by	O
an	O
SQLCODE	O
+100	O
)	O
to	O
indicate	O
the	O
end	O
of	O
the	O
result	B-Application
set	I-Application
.	O
</s>
<s>
The	O
final	O
step	O
involves	O
closing	O
the	O
cursor	B-Language
using	O
the	O
CLOSE	O
statement	O
:	O
</s>
<s>
After	O
closing	O
a	O
cursor	B-Language
,	O
a	O
program	O
can	O
open	O
it	O
again	O
,	O
which	O
implies	O
that	O
the	O
DBMS	B-Application
re-evaluates	O
the	O
same	O
query	O
or	O
a	O
different	O
query	O
and	O
builds	O
a	O
new	O
result	B-Application
set	I-Application
.	O
</s>
<s>
Programmers	O
may	O
declare	O
cursors	B-Language
as	O
scrollable	O
or	O
not	O
scrollable	O
.	O
</s>
<s>
The	O
scrollability	O
indicates	O
the	O
direction	O
in	O
which	O
a	O
cursor	B-Language
can	O
move	O
.	O
</s>
<s>
With	O
a	O
non-scrollable	O
(	O
or	O
forward-only	O
)	O
cursor	B-Language
,	O
you	O
can	O
FETCH	O
each	O
row	O
at	O
most	O
once	O
,	O
and	O
the	O
cursor	B-Language
automatically	O
moves	O
to	O
the	O
next	O
row	O
.	O
</s>
<s>
After	O
you	O
fetch	O
the	O
last	O
row	O
,	O
if	O
you	O
fetch	O
again	O
,	O
you	O
will	O
put	O
the	O
cursor	B-Language
after	O
the	O
last	O
row	O
and	O
get	O
the	O
following	O
code	O
:	O
SQLSTATE	O
02000	O
(	O
SQLCODE	O
+100	O
)	O
.	O
</s>
<s>
A	O
program	O
may	O
position	O
a	O
scrollable	O
cursor	B-Language
anywhere	O
in	O
the	O
result	B-Application
set	I-Application
using	O
the	O
FETCH	O
SQL	O
statement	O
.	O
</s>
<s>
The	O
keyword	O
SCROLL	O
must	O
be	O
specified	O
when	O
declaring	O
the	O
cursor	B-Language
.	O
</s>
<s>
The	O
default	O
is	O
NO	O
SCROLL	O
,	O
although	O
different	O
language	O
bindings	O
like	O
JDBC	B-Language
may	O
apply	O
a	O
different	O
default	O
.	O
</s>
<s>
DECLARE	O
cursor_name	O
sensitivity	O
SCROLL	O
CURSOR	B-Language
FOR	O
SELECT	O
...	O
FROM	O
...	O
</s>
<s>
The	O
target	O
position	O
for	O
a	O
scrollable	O
cursor	B-Language
can	O
be	O
specified	O
relatively	O
(	O
from	O
the	O
current	O
cursor	B-Language
position	O
)	O
or	O
absolutely	O
(	O
from	O
the	O
beginning	O
of	O
the	O
result	B-Application
set	I-Application
)	O
.	O
</s>
<s>
Scrollable	O
cursors	B-Language
can	O
potentially	O
access	O
the	O
same	O
row	O
in	O
the	O
result	B-Application
set	I-Application
multiple	O
times	O
.	O
</s>
<s>
Thus	O
,	O
data	O
modifications	O
(	O
insert	O
,	O
update	B-Language
,	O
delete	B-Language
operations	O
)	O
from	O
other	O
transactions	O
could	O
affect	O
the	O
result	B-Application
set	I-Application
.	O
</s>
<s>
A	O
cursor	B-Language
can	O
be	O
SENSITIVE	O
or	O
INSENSITIVE	O
to	O
such	O
data	O
modifications	O
.	O
</s>
<s>
A	O
sensitive	O
cursor	B-Language
picks	O
up	O
data	O
modifications	O
affecting	O
the	O
result	B-Application
set	I-Application
of	O
the	O
cursor	B-Language
,	O
and	O
an	O
insensitive	O
cursor	B-Language
does	O
not	O
.	O
</s>
<s>
Additionally	O
,	O
a	O
cursor	B-Language
may	O
be	O
INSENSITIVE	O
,	O
in	O
which	O
case	O
the	O
DBMS	B-Application
tries	O
to	O
apply	O
sensitivity	O
as	O
much	O
as	O
possible	O
.	O
</s>
<s>
Cursors	B-Language
are	O
usually	O
closed	O
automatically	O
at	O
the	O
end	O
of	O
a	O
transaction	O
,	O
i.e.	O
</s>
<s>
That	O
behavior	O
can	O
be	O
changed	O
if	O
the	O
cursor	B-Language
is	O
declared	O
using	O
the	O
WITH	O
HOLD	O
clause	O
(	O
the	O
default	O
is	O
WITHOUT	O
HOLD	O
)	O
.	O
</s>
<s>
A	O
holdable	O
cursor	B-Language
is	O
kept	O
open	O
over	O
COMMIT	O
and	O
closed	O
upon	O
ROLLBACK	O
.	O
</s>
<s>
(	O
Some	O
DBMS	B-Application
deviate	O
from	O
this	O
standard	O
behavior	O
and	O
also	O
keep	O
holdable	O
cursors	B-Language
open	O
over	O
ROLLBACK	O
.	O
)	O
</s>
<s>
DECLARE	O
cursor_name	O
CURSOR	B-Language
WITH	O
HOLD	O
FOR	O
SELECT	O
....	O
FROM	O
....	O
</s>
<s>
When	O
a	O
COMMIT	O
occurs	O
,	O
a	O
holdable	O
cursor	B-Language
is	O
positioned	O
before	O
the	O
next	O
row	O
.	O
</s>
<s>
Thus	O
,	O
a	O
positioned	O
UPDATE	B-Language
or	O
positioned	O
DELETE	B-Language
statement	O
will	O
only	O
succeed	O
after	O
a	O
FETCH	O
operation	O
occurred	O
first	O
in	O
the	O
transaction	O
.	O
</s>
<s>
Note	O
that	O
JDBC	B-Language
defines	O
cursors	B-Language
as	O
holdable	O
per	O
default	O
.	O
</s>
<s>
This	O
is	O
done	O
because	O
JDBC	B-Language
also	O
activates	O
auto-commit	O
per	O
default	O
.	O
</s>
<s>
Cursors	B-Language
can	O
not	O
only	O
be	O
used	O
to	O
fetch	O
data	O
from	O
the	O
DBMS	B-Application
into	O
an	O
application	O
but	O
also	O
to	O
identify	O
a	O
row	O
in	O
a	O
table	O
to	O
be	O
updated	O
or	O
deleted	O
.	O
</s>
<s>
The	O
SQL:2003	B-Language
standard	O
defines	O
positioned	O
update	B-Language
and	O
positioned	O
delete	B-Language
SQL	I-Language
statements	O
for	O
that	O
purpose	O
.	O
</s>
<s>
Such	O
statements	O
do	O
not	O
use	O
a	O
regular	O
WHERE	B-Language
clause	O
with	O
predicates	O
.	O
</s>
<s>
Instead	O
,	O
a	O
cursor	B-Language
identifies	O
the	O
row	O
.	O
</s>
<s>
The	O
cursor	B-Language
must	O
be	O
opened	O
and	O
already	O
positioned	O
on	O
a	O
row	O
by	O
means	O
of	O
FETCH	O
statement	O
.	O
</s>
<s>
The	O
cursor	B-Language
must	O
operate	O
on	O
an	O
updatable	O
result	B-Application
set	I-Application
in	O
order	O
to	O
successfully	O
execute	O
a	O
positioned	O
update	B-Language
or	O
delete	B-Language
statement	O
.	O
</s>
<s>
Otherwise	O
,	O
the	O
DBMS	B-Application
would	O
not	O
know	O
how	O
to	O
apply	O
the	O
data	O
changes	O
to	O
the	O
underlying	O
tables	O
referred	O
to	O
in	O
the	O
cursor	B-Language
.	O
</s>
<s>
Using	O
cursors	B-Language
in	O
distributed	O
transactions	O
(	O
X/Open	O
XA	O
Environments	O
)	O
,	O
which	O
are	O
controlled	O
using	O
a	O
transaction	O
monitor	O
,	O
is	O
no	O
different	O
from	O
cursors	B-Language
in	O
non-distributed	O
transactions	O
.	O
</s>
<s>
One	O
has	O
to	O
pay	O
attention	O
when	O
using	O
holdable	O
cursors	B-Language
,	O
however	O
.	O
</s>
<s>
Thus	O
,	O
once	O
a	O
transaction	O
has	O
been	O
ended	O
and	O
committed	O
,	O
a	O
subsequent	O
transaction	O
(	O
running	O
in	O
a	O
different	O
application	O
)	O
could	O
inherit	O
existing	O
holdable	O
cursors	B-Language
.	O
</s>
<s>
The	O
XQuery	B-Language
language	O
allows	O
cursors	B-Language
to	O
be	O
created	O
using	O
the	O
subsequence( )	O
function	O
.	O
</s>
<s>
Where	B-Language
$result	O
is	O
the	O
result	O
of	O
the	O
initial	O
XQuery	B-Language
,	O
$start	O
is	O
the	O
item	O
number	O
to	O
start	O
and	O
$	O
item-count	O
is	O
the	O
number	O
of	O
items	O
to	O
return	O
.	O
</s>
<s>
Where	B-Language
$end	O
is	O
the	O
end	O
sequence	O
.	O
</s>
<s>
The	O
following	O
information	O
may	O
vary	O
depending	O
on	O
the	O
specific	O
database	B-Application
system	I-Application
.	O
</s>
<s>
Fetching	O
a	O
row	O
from	O
the	O
cursor	B-Language
may	O
result	O
in	O
a	O
network	O
round	O
trip	O
each	O
time	O
.	O
</s>
<s>
This	O
uses	O
much	O
more	O
network	O
bandwidth	O
than	O
would	O
ordinarily	O
be	O
needed	O
for	O
the	O
execution	O
of	O
a	O
single	O
SQL	O
statement	O
like	O
DELETE	B-Language
.	O
</s>
<s>
Repeated	O
network	O
round	O
trips	O
can	O
severely	O
reduce	O
the	O
speed	O
of	O
the	O
operation	O
using	O
the	O
cursor	B-Language
.	O
</s>
<s>
Cursors	B-Language
allocate	O
resources	B-General_Concept
on	O
the	O
server	O
,	O
such	O
as	O
locks	O
,	O
packages	O
,	O
processes	O
,	O
and	O
temporary	O
storage	O
.	O
</s>
<s>
For	O
example	O
,	O
Microsoft	B-Application
SQL	I-Application
Server	I-Application
implements	O
cursors	B-Language
by	O
creating	O
a	O
temporary	O
table	O
and	O
populating	O
it	O
with	O
the	O
query	O
's	O
result	B-Application
set	I-Application
.	O
</s>
<s>
If	O
a	O
cursor	B-Language
is	O
not	O
properly	O
closed	O
(	O
deallocated	O
)	O
,	O
the	O
resources	B-General_Concept
will	O
not	O
be	O
freed	O
until	O
the	O
SQL	O
session	O
(	O
connection	O
)	O
itself	O
is	O
closed	O
.	O
</s>
<s>
This	O
wasting	O
of	O
resources	B-General_Concept
on	O
the	O
server	O
can	O
lead	O
to	O
performance	O
degradations	O
and	O
failures	O
.	O
</s>
