1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
|
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>SQL Server API Server Cursors</TITLE>
<LINK REL="stylesheet" TYPE="text/css" HREF="sqldoc.css">
</HEAD>
<BODY bgcolor="#FFFFFF">
<h2>System stored procedures</h2>
<p>This document contains information on undocumented stored procedures in Microsoft SQL Server.</p>
<BL>
<table>
<tr><td> </td><th align="left">Name</th><th align="left">Function</th></tr>
<tr><td> </td><td><a href="#_sp_cursor">sp_cursor</a></td><td>Update a cursor</td></tr>
<tr><td> </td><td><a href="#_sp_cursorclose">sp_cursorclose</a></td><td>close a cursor</td></tr>
<tr><td> </td><td><a href="#_sp_cursorexecute">sp_cursorexecute</a></td><td>Open a prepared cursor</td></tr>
<tr><td> </td><td><a href="#_sp_cursorfetch">sp_cursorfetch</a></td><td>Fetch rows</td></tr>
<tr><td> </td><td><a href="#_sp_cursoropen">sp_cursoropen</a></td><td>Open a cursor</td></tr>
<tr><td> </td><td><a href="#_sp_cursoroption">sp_cursoroption</a></td><td>Set cursor options</td></tr>
<tr><td> </td><td><a href="#_sp_cursorprepare">sp_cursorprepare</a></td><td>Prepare a cursor statement</td></tr>
<tr><td> </td><td><a href="#_sp_cursorprepexec">sp_cursorprepexec</a></td><td>Prepare a cursor statement and open</td></tr>
<tr><td> </td><td><a href="#_sp_cursorunprepare">sp_cursorunprepare </a></td><td>Free a prepared cursor statement</td></tr>
<tr><td> </td><td><a href="#_sp_execute">sp_execute</a></td><td>Execute a prepared statement</td></tr>
<tr><td> </td><td><a href="#_sp_prepare">sp_prepare</a></td><td>Prepare an SQL statement</td></tr>
<tr><td> </td><td><a href="#_sp_prepexec">sp_prepexec</a></td><td>Prepare and execute an SQL statement</td></tr>
<tr><td> </td><td><a href="#_sp_unprepare">sp_unprepare</a></td><td>Free a prepared statement</td></tr>
</table>
</BL>
<h2><A NAME="_sp_cursoropen"></A>sp_cursoropen</h2>
<P>Defines the attributes of an API server cursor, such as its scrolling behavior
and the statement used to build the result set on which the cursor operates,
then populates the cursor. The statement can contain embedded parameters.</P>
<h5>Syntax</h5>
<P><B>sp_cursoropen</b> [<B>@cursor =</B>] <I>cursor_handle</I> OUTPUT,<BR>
[<B>@stmt =</B>] '<I>stmt</I>'<BR>
[, [<B>@scrollopt =</B>] <I>scroll_options</I> OUTPUT]<BR>
[, [<B>@ccopt =</B>] <I>concurrency_options</I> OUTPUT]<BR>
[, [<B>@rowcount =</B>] <I>rowcount</I> OUTPUT]</P>
<P>[<BR>
{, [<B>@paramdef =</B>] N'<I>parameter_name data_type</I>
[,<i>...n</i>]' }<BR>
{, [<B>@</B><i>param1</i><B> =</B>] <I>value1</I> [,<i>...n</i>]
}</P>
<P>]</P>
<h5>Arguments</h5>
<DL>
<DT>[<B>@cursor =</B>] <I>cursor_handle</I> OUTPUT</DT>
<DD>Is the name of a declared integer variable to receive the cursor handle.
<I>cursor_handle</I> is <B>int</B>, with no default.</DD>
<DT>[<B>@stmt =</B>] '<I>stmt</I>'</DT>
<DD>
<P>Is a string containing a single SELECT statement or a single stored procedure
call. The size of the string is limited only by available database server
memory. <I>stmt</i> can contain parameters having the same form as a variable
name, for example:</P>
<P class="ex"><CODE>'SELECT * FROM Employees WHERE EmployeeID = @IDParameter'</CODE></P>
<P>Each parameter included in <i>stmt</i> must have a corresponding entry
in both the <b>@paramdef</b> parameter definition list and the parameter
values list.</P>
</DD>
<DT>[<B>@scrollopt =</B>] <I>scroll_options</I> OUTPUT</DT>
<DD> Is the cursor scroll type. <I>scroll_options</I> is <B>int</B> with a default
of 1 (keyset-driven), and can be a combination of these values (exactly one
of the first 5 must be specified).<BR>
<BR>
<table cellspacing="1" cols="2" width="446">
<TR VALIGN="top">
<TH>Value</TH>
<TH>Description</TH>
</TR>
<TR VALIGN="top">
<TD>0x0001</TD>
<TD>Keyset-driven cursor.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0002</TD>
<TD>Dynamic cursor.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0004</TD>
<TD>Forward-only cursor.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0008</TD>
<TD>Static cursor.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0010</TD>
<TD>Fast forward-only cursor.</TD>
</TR>
<TR VALIGN="top">
<TD>0x1000</TD>
<TD>Parameterized query.</TD>
</TR>
<TR VALIGN="top">
<TD>0x2000</TD>
<TD>Auto fetch.</TD>
</TR>
<TR VALIGN="top">
<TD>0x4000</TD>
<TD>Auto close.</TD>
</TR>
<TR VALIGN="top">
<TD>0x8000</TD>
<TD>Check acceptable types.</TD>
</TR>
<TR VALIGN="top">
<TD>0x10000</TD>
<TD>Keyset-driven acceptable.</TD>
</TR>
<TR VALIGN="top">
<TD>0x20000</TD>
<TD>Dynamic acceptable.</TD>
</TR>
<TR VALIGN="top">
<TD>0x40000</TD>
<TD>Forward-only acceptable.</TD>
</TR>
<TR VALIGN="top">
<TD>0x80000</TD>
<TD>Static acceptable.</TD>
</TR>
<TR VALIGN="top">
<TD>0x100000</TD>
<TD>Fast forward-only acceptable.</TD>
</TR>
</table>
<BR>
On return, <b>@scrollopt</b> contains the type of cursor actually created,
which may not match what was requested.</DD>
<DT>[<B>@ccopt =</B>] <I>concurrency_options</i> OUTPUT</DT>
<DD>Is the cursor concurrency. <I>concurrency_options</I> is <B>int</B>, with
a default of 4 (optimistic) and can be a combination of these values (exactly
one of the first 4 must be specified).<BR>
<BR>
<table cellspacing="1" cols="2" width="446">
<TR VALIGN="top">
<TH>Value</TH>
<TH>Description</TH>
</TR>
<TR VALIGN="top">
<TD>0x0001</TD>
<TD>Read-only.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0002</TD>
<TD>Scroll locks.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0004</TD>
<TD>Optimistic. Checks timestamps and, when not available, values.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0008</TD>
<TD>Optimistic. Checks values (non-text, non-image).</TD>
</TR>
<TR VALIGN="top">
<TD>0x2000</TD>
<TD>Open on any SQL.</TD>
</TR>
<TR VALIGN="top">
<TD>0x4000</TD>
<TD>Update keyset in place.</TD>
</TR>
<TR VALIGN="top">
<TD>0x10000</TD>
<TD>Read-only acceptable.</TD>
</TR>
<TR VALIGN="top">
<TD>0x20000</TD>
<TD>Locks acceptable.</TD>
</TR>
<TR VALIGN="top">
<TD>0x40000</TD>
<TD>Optimistic acceptable.</TD>
</TR>
</table>
<BR>
On return, <b>@ccopt</b> contains the type of cursor actually created, which
may not match what was requested.</DD>
<DT>[<B>@rowcount =</B>] <I>rowcount</I> OUTPUT</DT>
<DD> Is the name of a declared integer variable to receive the number of affected
rows. <I>rowcount</I> is <B>int</B> with no default value.</DD>
<DT>[<B>@paramdef =</B>] N'<i>parameter_name data_type</i> [,<i>...n</i>]'</DT>
<DD>Is one string that contains the definitions of all parameters that have
been embedded in <i>stmt</i>. Each parameter definition consists of a parameter
name and a data type. <i>n</i> is a placeholder indicating additional parameter
definitions. Every parameter specified in <i>stmt</i> must be defined in <b>@paramdef</b>.
If the Transact-SQL statement in <i>stmt</i> does not contain parameters,
<b>@paramdef</b> is not needed. The default value for this parameter is NULL.</DD>
<DT>[<b>@</b><i>param1</i><b> =</b>] <i>value1</i></DT>
<DD>Is a value for the first parameter defined in the parameter string. The
value can be a constant or a variable. There must be a parameter value supplied
for every parameter included in <i>stmt</i>. The values are not needed if
the Transact-SQL statement in <i>stmt</i> has no parameters.</DD>
<DT><i>n</i></DT>
<DD>Is a placeholder for the values of additional parameters. Values can be
only constants or variables. Values cannot be more complex expressions such
as functions, or expressions built using operators.</DD>
</DL>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>Returns the result set generated by <i>stmt</i>, but containing no rows.</P>
<h5>Remarks</h5>
<P><b>sp_cursoropen</b> is a more powerful (and programmatic) way of creating
server-side cursors on SQL Server.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Examples</h5>
<h5>A. Create a cursor for a simple SELECT statement</h5>
<P>This simple example creates a dynamic read-only cursor for a SELECT statement
with no parameters.</P>
<P class="ex"><CODE>USE pubs</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Create a dynamc read-only cursor</CODE></P>
<P class="ex"><CODE>DECLARE @cursor INT</CODE></P>
<P class="ex"><CODE>EXEC sp_cursoropen @cursor OUTPUT, N'SELECT * FROM myTable',
2, 8193</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Close the cursor</CODE></P>
<P class="ex"><CODE>EXEC sp_cursorclose @cursor</CODE></P>
<P class="ex"><CODE> </CODE></P>
<h5>B. Create a cursor for a parameterized SELECT statement</h5>
<p>This example creates a dynamic read-only cursor for a SELECT statement with
2 parameters.</p>
<p class="ex"><code>USE pubs</code></p>
<p class="ex"><code> </code></p>
<p class="ex"><code>-- Create a dynamc read-only cursor</code></p>
<p class="ex"><code>DECLARE @cursor INT</code></p>
<p class="ex"><code>EXEC sp_cursoropen @cursor OUTPUT, N'SELECT * FROM myTable
WHERE col1=@P1 AND col2 LIKE @P2', 2, 8193, N'@P1 INT, @P2 VARCHAR(255)', 10,
'%x%' </code></p>
<p class="ex"><code> </code></p>
<p class="ex"><code>-- Close the cursor</code></p>
<p class="ex"><code>EXEC sp_cursorclose @cursor</code></p>
<p class="ex"><code> </code></p>
<h5>C. Create a cursor for a stored procedure call</h5>
<p>This example creates a dynamic read-only cursor for a stored procedure with
2 parameters (the procedure must return only one result set or the cursor creation
will fail). Note that output parameters can also be used and return values retrieved
via output parameters.</p>
<p class="ex"><code>USE pubs</code></p>
<p class="ex"><code> </code></p>
<p class="ex"><code>-- Create a dynamc read-only cursor</code></p>
<p class="ex"><code>DECLARE @cursor INT</code></p>
<p class="ex"><code>DECLARE @retval INT</code></p>
<p class="ex"><code>EXEC sp_cursoropen @cursor OUTPUT, 'EXEC @P1=myProc @P2',
2, 8193, N'@P1 INT OUTPUT, @P2 INT', @retval, 1</code></p>
<p class="ex"><code> </code></p>
<p class="ex"><code>-- Close the cursor</code></p>
<p class="ex"><code>EXEC sp_cursorclose @cursor</code></p>
<p class="ex"><code> </code></p>
<h2><A NAME="_sp_cursorfetch"></A>sp_cursorfetch</h2>
<P>Fetches a row or block of rows from an API server cursor.</P>
<h5>Syntax</h5>
<P><B>sp_cursorfetch</b> [<B>@cursor =</B>] <I>cursor_handle</i><BR>
[, [<B>@fetchtype =</B>] <I>fetchtype</I>]<BR>
[, [<B>@rownum =</B>] <I>rownum</I> OUTPUT]<BR>
[, [<B>@nrows =</B>] <I>nrows</I> OUTPUT]</P>
<h5>Arguments</h5>
<DL>
<DT>[<B>@cursor =</B>] <I>cursor_handle</i></DT>
<DD>Is the cursor handle. <I>cursor_handle</I> is <B>int</B>, with no default.</DD>
<dt>[<B>@fetchtype =</B>] <I>fetchtype</i></dt>
<dd>Is the fetch type. <I>fetchtype</I> is <B>int</B>, with a default of 2 and
can have one of these values.<br>
<br>
<table cellspacing="1" cols="2" width="446">
<TR VALIGN="top">
<TH>Value</TH>
<TH>Description</TH>
</TR>
<TR VALIGN="top">
<TD>0x0001</TD>
<TD>First row.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0002</TD>
<TD>Next row.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0004</TD>
<TD>Previous row.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0008</TD>
<TD>Last row.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0010</TD>
<TD>Absolute row index.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0020</TD>
<TD>Relative row index.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0040</TD>
<TD>By value (???).</TD>
</TR>
<TR VALIGN="top">
<TD>0x0080</TD>
<TD>Refresh.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0100</TD>
<TD>Result set info.</TD>
</TR>
<TR VALIGN="top">
<TD>0x0200</TD>
<TD>Previous noadjust (?).</TD>
</TR>
<TR VALIGN="top">
<TD>0x0400</TD>
<TD>Skip update concurrency (???).</TD>
</TR>
</table>
</dd>
<dt>[<B>@rownum =</B>] <I>rownum</I> OUTPUT</dt>
<dd>Is the row number. <I>rownum</I> is <B>int</B>, with a default of NULL.</dd>
<dt>[<B>@nrows =</B>] <I>nrows</I> OUTPUT</dt>
<dd>Is the number of rows to fetch. <I>nrows</I> is <B>int</B>, with a default
of NULL (fetch all rows).</dd>
</DL>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>Returns the requested row or group of rows from the cursor.</P>
<h5>Remarks</h5>
<P>In addition to fetching rows, the 'result set info' fetch type can be used
to retrieve information about the cursor (current row in <b>@rownum</b> and
total number of rows in <b>@nrows</b>).</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
<P class="ex"><CODE>USE pubs</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Create a dynamc read-only cursor </CODE></P>
<P class="ex"><CODE>DECLARE @cursor INT</CODE></P>
<P class="ex"><CODE>EXEC sp_cursoropen @cursor OUTPUT, 'SELECT * FROM myTable',
2, 8193</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Fetch the next 3 lines</CODE></P>
<P class="ex"><CODE>EXEC sp_cursorfetch @cursor, 2, 0, 3</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Close the cursor</CODE></P>
<P class="ex"><CODE>EXEC sp_cursorclose @cursor</CODE></P>
<P class="ex"><CODE> </CODE></P>
<h2><A NAME="_sp_cursorclose"></A>sp_cursorclose</h2>
<P>Closes ande deallocates an API server cursor.</P>
<h5>Syntax</h5>
<P><B>sp_cursorclose</b> [<B>@cursor =</B>] <I>cursor_handle</i></P>
<h5>Arguments</h5>
<dl>
<dt>[<B>@cursor =</B>] <I>cursor_handle</i></dt>
<dd>Is a cursor handle obtained by calling <B>sp_cursorcreate</B>. <I>cursor_handle</I>
is <B>int</B>, with no default.</dd>
</dl>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>None.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
<P class="ex"><CODE>USE pubs</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Create a dynamic read-only cursor </CODE></P>
<P class="ex"><CODE>DECLARE @cursor INT</CODE></P>
<P class="ex"><CODE>EXEC sp_cursoropen @cursor OUTPUT, 'SELECT * FROM myTable',
2, 8193</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Close the cursor</CODE></P>
<P class="ex"><CODE>EXEC sp_cursorclose @cursor</CODE></P>
<P class="ex"><CODE> </CODE></P>
<h2><A NAME="_sp_cursoroption"></A>sp_cursoroption</h2>
<P>Sets various options for API server cursors.</P>
<h5>Syntax</h5>
<P><B>sp_cursoroption</b> [<B>@cursor =</B>] <I>cursor_handle</i>,<BR>
[<B>@code =</B>] <I>code</i>,<BR>
{ [<B>@value =</B>] <I>value</i><BR>
| [<B>@cursorname =</B>] <I>cursorname</I> }</P>
<h5>Arguments</h5>
<dl>
<dt>[<B>@cursor =</B>] <I>cursor_handle</i></dt>
<dd>Is a cursor handle obtained by calling <B>sp_cursorcreate</B>. <I>cursor_handle</I>
is <B>int</B>, with no default.</dd>
<dt>[<B>@code =</B>] <I>code</i></dt>
<dd>Is the option code. <I>code</I> is <B>int</B>, with no default and can be
one of these values.<br>
<br>
<table cellspacing="1" cols="2" width="446">
<TR VALIGN="top">
<TH>Value</TH>
<TH>Description</TH>
</TR>
<TR VALIGN="top">
<TD>1</TD>
<TD>Only return the TEXTPTR of the LOB column specified by <i>value</i></TD>
</TR>
<TR VALIGN="top">
<TD>2</TD>
<TD>Set cursor name.</TD>
</TR>
</table>
</dd>
<dt>[<B>@value =</B>] <I>value</i></dt>
<dd>Is the value of the selected option (for option 1 it's the index of the
LOB column). <I>value</I> is <B>int</B>, with no default.</dd>
<dt>[<B>@cursorname =</B>] <I>cursorname</i></dt>
<dd>Is the name for the cursor. <I>cursorname</I> is <B>sysname</B>,
with no default.</dd>
</dl>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>None.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
<P class="ex"><CODE>USE pubs</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Create a dynamc cursor </CODE></P>
<P class="ex"><CODE>DECLARE @cursor INT</CODE></P>
<P class="ex"><CODE>EXEC sp_cursoropen @cursor OUTPUT, 'SELECT * FROM myTable',
2, 8193</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Name the cursor</CODE></P>
<P class="ex"><CODE>EXEC sp_cursoroption @cursor, 2, 'myCursor'</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><code>-- Use a cursor variable to access the cursor</code></P>
<P class="ex"><CODE>DECLARE @x CURSOR</CODE></P>
<P class="ex"><CODE>EXEC sp_describe_cursor @x out, N'global', 'myCursor'</CODE></P>
<P class="ex"><CODE>FETCH NEXT FROM @x</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><code>-- Use the cursor directly by name</code></P>
<P class="ex"><code>FETCH NEXT FROM myCursor</code></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Close the cursor</CODE></P>
<P class="ex"><CODE>EXEC sp_cursorclose @cursor</CODE></P>
<P class="ex"><CODE> </CODE></P>
<h2><A NAME="_sp_cursor"></A>sp_cursor</h2>
<P>Can be used to request inserts and positioned updates or deletes on API server
cursors.</P>
<h5>Syntax</h5>
<P><B>sp_cursor</b> [<B>@cursor =</B>] <I>cursor_handle</i>,<BR>
[<B>@optype =</B>] <I>optype</i>,<BR>
[<B>@rownum =</B>] <I>rownum</i>,<BR>
[<B>@table =</B>] '<I>table</i>'<BR>
{, [<B>@</B><i>param1</i><B> =</B>] <I>value1</I> [,<i>...n</i>]
} </P>
<h5>Arguments</h5>
<dl>
<dt>[<B>@cursor =</B>] <I>cursor_handle</i></dt>
<dd>Is a cursor handle obtained by calling <B>sp_cursorcreate</B>. <I>cursor_handle</I>
is <B>int</B>, with no default.</dd>
<dt>[<B>@optype =</B>] <I>optype</i></dt>
<dd> Is a the operation to perform. <I>optype</I> is <B>int</B>, with no default
and can be one of these values.<BR>
<BR>
<table cellspacing="1" cols="2" width="446">
<TR VALIGN="top">
<TH>Value</TH>
<TH>Description</TH>
</TR>
<TR VALIGN="top">
<TD>1</TD>
<TD>Update row (?).</TD>
</TR>
<TR VALIGN="top">
<TD>4</TD>
<TD>Insert row.</TD>
</TR>
<TR VALIGN="top">
<TD>33</TD>
<TD>Update row.</TD>
</TR>
<TR VALIGN="top">
<TD>34</TD>
<TD>Delete row.</TD>
</TR>
</table>
</dd>
<dt>[<B>@rownum =</B>] <I>rownum</i></dt>
<dd>Is the number of the row to update in the fetch cache. <i>rownum</i> is
<b>int</b>, with no default.</dd>
<dt>[<B>@table =</B>] '<I>table</i>'</dt>
<dd> Is the name of the table to update (an empty character string seems to
be ok). <I>table</I> is <B>sysname</B>, with no default.</dd>
</dl>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>None.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
<P class="ex"><CODE>USE pubs</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Create a dynamc cursor </CODE></P>
<P class="ex"><CODE>DECLARE @cursor INT</CODE></P>
<P class="ex"><CODE>EXEC sp_cursoropen @cursor OUTPUT, 'SELECT * FROM myTable',
2, 8193</CODE></P>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Fetch the next 2 lines; this puts lines 1 and 2 in the
fetch buffer</CODE></P>
<P class="ex"><CODE>EXEC sp_cursorfetch @cursor, 2, 0, 2</CODE></P>
<P class="ex"><CODE> </CODE></P>
<p class="ex"><code>-- Update the second line in the fetch buffer</code></p>
<p class="ex"><code>EXEC sp_cursor @cursor, 33, 2, '', @intCol=5, @charCol='x'</code></p>
<P class="ex"><CODE> </CODE></P>
<P class="ex"><CODE>-- Close the cursor</CODE></P>
<P class="ex"><CODE>EXEC sp_cursorclose @cursor</CODE></P>
<P class="ex"><CODE> </CODE></P>
<h2><A NAME="_sp_cursorprepare"></A>sp_cursorprepare</h2>
<P>Used to prepare a parameterized cursor statement.</P>
<h5>Syntax</h5>
<P><B>sp_cursorprepare</b> [<B>@cursor =</B>] <I>statement_handle</I> OUTPUT,<BR>
[<B>@paramdef =</B>] N'<I>parameter_name data_type</I>
[,<i>...n</i>]', <BR>
[<B>@stmt =</B>] N'<I>stmt</I>',<BR>
[<B>@options =</B>] <I>options</I>, <BR>
[, [<B>@scrollopt =</B>] <I>scroll_options</I> OUTPUT]<BR>
[, [<B>@ccopt =</B>] <I>concurrency_options</I> OUTPUT]<BR>
<h5>Arguments</h5>
<dl>
<dt>[<B>@cursor =</B>] <I>statement_handle</i></dt>
<DD>Is the name of a declared integer variable to receive the statement handle.
<I>statement_handle</I> is <B>int</B>, with no default.</DD>
<DT>[<B>@paramdef =</B>] N'<i>parameter_name data_type</i> [,<i>...n</i>]'</DT>
<DD>Is one string that contains the definitions of all parameters that have
been embedded in <i>stmt</i>. Each parameter definition consists of a parameter
name and a data type. <i>n</i> is a placeholder indicating additional parameter
definitions. Every parameter specified in <i>stmt</i> must be defined in <b>@paramdef</b>. </DD>
<DT>[<B>@stmt =</B>] '<I>stmt</I>'</DT>
<DD>
<P>Is a string containing a single SELECT statement or a single stored procedure
call. The size of the string is limited only by available database server
memory. <I>stmt</i> can contain parameters having the same form as a variable
name, for example:</P>
<P class="ex"><CODE>'SELECT * FROM Employees WHERE EmployeeID = @IDParameter'</CODE></P>
<P>Each parameter included in <i>stmt</i> must have a corresponding entry
in both the <b>@paramdef</b> parameter definition list.</P>
</DD>
<dt>[<B>@options =</B>] <I>options</I></dt>
<DD>An integer value. The exact function of this parameter is unknown.
<I>options</I> is <B>int</B>, with a value of 1.</DD>
<DT>[<B>@scrollopt =</B>] <I>scroll_options</I> OUTPUT</DT>
<DD> Is the cursor scroll type. <I>scroll_options</I> is <B>int</B> with a default
of 1 (keyset-driven). See sp_cursoropen for more information.
<BR>
On return, <b>@scrollopt</b> contains the type of cursor actually created,
which may not match what was requested.</DD>
<DT>[<B>@ccopt =</B>] <I>concurrency_options</i> OUTPUT</DT>
<DD>Is the cursor concurrency. <I>concurrency_options</I> is <B>int</B>, with
a default of 4 (optimistic). See sp_cursoropen for more information.
<BR>
On return, <b>@ccopt</b> contains the type of cursor actually created, which
may not match what was requested.</DD>
</dl>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>None.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
<h2><A NAME="_sp_cursorprepexec"></A>sp_cursorprepexec</h2>
<P>Used to prepare and open a parameterized cursor statement. This command combines the
functions of the sp_cursorprepare and sp_cursorexecute procedures and is available from SQL2000 onwards.</P>
<h5>Syntax</h5>
<P><B>sp_cursorprepexec</b> [<B>@handle =</B>] <I>statement_handle</I> OUTPUT,<BR>
[<B>@cursor =</B>] <I>cursor_handle</I> OUTPUT,<BR>
[<B>@paramdef =</B>] N'<I>parameter_name data_type,</I>
[,<i>...n</i>]' <BR>
[<B>@stmt =</B>] N'<I>stmt</I>',<BR>
[, [<B>@scrollopt =</B>] <I>scroll_options</I> OUTPUT]<BR>
[, [<B>@ccopt =</B>] <I>concurrency_options</I> OUTPUT]<BR>
[, [<B>@rowcount =</B>] <I>rowcount</I> OUTPUT]</P>
<h5>Arguments</h5>
<dl>
<dt>[<B>@handle =</B>] <I>statement_handle</i></dt>
<DD>Is the name of a declared integer variable to receive the statement handle.
<I>statement_handle</I> is <B>int</B>, with no default.</DD>
<DT>[<B>@cursor =</B>] <I>cursor_handle</I> OUTPUT</DT>
<DD>Is the name of a declared integer variable to receive the cursor handle.
<I>cursor_handle</I> is <B>int</B>, with no default.</DD>
<DT>[<B>@paramdef =</B>] N'<i>parameter_name data_type</i> [,<i>...n</i>]'</DT>
<DD>Is one string that contains the definitions of all parameters that have
been embedded in <i>stmt</i>. Each parameter definition consists of a parameter
name and a data type. <i>n</i> is a placeholder indicating additional parameter
definitions. Every parameter specified in <i>stmt</i> must be defined in <b>@paramdef</b>. </DD>
<DT>[<B>@stmt =</B>] '<I>stmt</I>'</DT>
<DD>
<P>Is a string containing a single SELECT statement or a single stored procedure
call. The size of the string is limited only by available database server
memory. <I>stmt</i> can contain parameters having the same form as a variable
name, for example:</P>
<P class="ex"><CODE>'SELECT * FROM Employees WHERE EmployeeID = @IDParameter'</CODE></P>
<P>Each parameter included in <i>stmt</i> must have a corresponding entry
in both the <b>@paramdef</b> parameter definition list.</P>
</DD>
<DT>[<B>@scrollopt =</B>] <I>scroll_options</I> OUTPUT</DT>
<DD> Is the cursor scroll type. <I>scroll_options</I> is <B>int</B> with a default
of 1 (keyset-driven). See sp_cursoropen for more information.
<BR>
On return, <b>@scrollopt</b> contains the type of cursor actually created,
which may not match what was requested.</DD>
<DT>[<B>@ccopt =</B>] <I>concurrency_options</i> OUTPUT</DT>
<DD>Is the cursor concurrency. <I>concurrency_options</I> is <B>int</B>, with
a default of 4 (optimistic). See sp_cursoropen for more information.
<BR>
On return, <b>@ccopt</b> contains the type of cursor actually created, which
may not match what was requested.</DD>
<DT>[<B>@rowcount =</B>] <I>rowcount</I> OUTPUT</DT>
<DD> Is the name of a declared integer variable to receive the number of affected
rows. <I>rowcount</I> is <B>int</B> with no default value.</DD>
</dl>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>Returns the result set generated by <i>stmt</i>, but containing no rows.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
<h2><A NAME="_sp_cursorexecute"></A>sp_cursorexecute</h2>
<P>Used to execute (open) a prepared cursor statement.</P>
<h5>Syntax</h5>
<P><B>sp_cursorexecute</b> [<B>@handle =</B>] <I>statement_handle</I>,<BR>
[<B>@cursor =</B>] <I>cursor_handle</I> OUTPUT,<BR>
[, [<B>@scrollopt =</B>] <I>scroll_options</I> OUTPUT]<BR>
[, [<B>@ccopt =</B>] <I>concurrency_options</I> OUTPUT]<BR>
[, [<B>@rowcount =</B>] <I>rowcount</I> OUTPUT]<BR>
{, [<B>@</B><i>param1</i><B> =</B>] <I>value1</I> [,<i>...n</i>]
}</P>
<h5>Arguments</h5>
<dl>
<dt>[<B>@handle =</B>] <I>statement_handle</i></dt>
<DD>Is the integer value of the statement handle.
<I>statement_handle</I> is <B>int</B>, with no default.</DD>
<DT>[<B>@cursor =</B>] <I>cursor_handle</I> OUTPUT</DT>
<DD>Is the name of a declared integer variable to receive the cursor handle.
<I>cursor_handle</I> is <B>int</B>, with no default.</DD>
<DT>[<B>@scrollopt =</B>] <I>scroll_options</I> OUTPUT</DT>
<DD> Is the cursor scroll type. <I>scroll_options</I> is <B>int</B> with a default
of 1 (keyset-driven). See sp_cursoropen for more information.
<BR>
On return, <b>@scrollopt</b> contains the type of cursor actually created,
which may not match what was requested.</DD>
<DT>[<B>@ccopt =</B>] <I>concurrency_options</i> OUTPUT</DT>
<DD>Is the cursor concurrency. <I>concurrency_options</I> is <B>int</B>, with
a default of 4 (optimistic). See sp_cursoropen for more information.
<BR>
On return, <b>@ccopt</b> contains the type of cursor actually created, which
may not match what was requested.</DD>
<DT>[<B>@rowcount =</B>] <I>rowcount</I> OUTPUT</DT>
<DD> Is the name of a declared integer variable to receive the number of affected
rows. <I>rowcount</I> is <B>int</B> with no default value.</DD>
<DT>[<b>@</b><i>param1</i><b> =</b>] <i>value1</i></DT>
<DD>Is a value for the first parameter defined in the parameter string. The
value can be a constant or a variable. There must be a parameter value supplied
for every parameter included in <i>stmt</i>. The values are not needed if
the Transact-SQL statement in <i>stmt</i> has no parameters.</DD>
<DT><i>n</i></DT>
<DD>Is a placeholder for the values of additional parameters. Values can be
only constants or variables. Values cannot be more complex expressions such
as functions, or expressions built using operators.</DD>
</dl>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>Returns the result set generated by the prepared statement <i>handle</i>, but containing no rows.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
<h2><A NAME="_sp_cursorunprepare"></A>sp_cursorunprepare</h2>
<P>Used to free a prepared cursor statement.</P>
<h5>Syntax</h5>
<P><B>sp_cursorunprepare</b> [<B>@handle =</B>] <I>statement_handle</I></P>
<h5>Arguments</h5>
<dl>
<dt>[<B>@handle =</B>] <I>statement_handle</i></dt>
<DD>Is the integer value of the statement handle.
<I>statement_handle</I> is <B>int</B>, with no default.</DD>
</dl>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>None.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
<h2><A NAME="_sp_prepare"></A>sp_prepare</h2>
<P>Used to prepare a parameterized SQL statement.</P>
<h5>Syntax</h5>
<P><B>sp_prepare</b> [<B>@handle =</B>] <I>statement_handle</I> OUTPUT,<BR>
[<B>@paramdef =</B>] N'<I>parameter_name data_type</I>
[,<i>...n</i>]', <BR>
[<B>@stmt =</B>] N'<I>stmt</I>',<BR>
[<B>@flag =</B>] <I>flag</I>, <BR>
<h5>Arguments</h5>
<dl>
<dt>[<B>@handle =</B>] <I>statement_handle</i></dt>
<DD>Is the name of a declared integer variable to receive the statement handle.
<I>statement_handle</I> is <B>int</B>, with no default.</DD>
<DT>[<B>@paramdef =</B>] N'<i>parameter_name data_type</i> [,<i>...n</i>]'</DT>
<DD>Is one string that contains the definitions of all parameters that have
been embedded in <i>stmt</i>. Each parameter definition consists of a parameter
name and a data type. <i>n</i> is a placeholder indicating additional parameter
definitions. Every parameter specified in <i>stmt</i> must be defined in <b>@paramdef</b>. </DD>
<DT>[<B>@stmt =</B>] '<I>stmt</I>'</DT>
<DD>
<P>Is a string containing a single SELECT statement or a single stored procedure
call. The size of the string is limited only by available database server
memory. <I>stmt</i> can contain parameters having the same form as a variable
name, for example:</P>
<P class="ex"><CODE>'SELECT * FROM Employees WHERE EmployeeID = @IDParameter'</CODE></P>
<P>Each parameter included in <i>stmt</i> must have a corresponding entry
in both the <b>@paramdef</b> parameter definition list.</P>
</DD>
<dt>[<B>@flag =</B>] <I>flag</i></dt>
<DD>An integer value. The exact function of this flag is unknown.
<I>flag</I> is <B>int</B>, with value of 1.</DD>
</dl>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>None.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
<h2><A NAME="_sp_execute"></A>sp_execute</h2>
<P>Used to execute a prepared SQL statement.</P>
<h5>Syntax</h5>
<P><B>sp_execute</b> [<B>@handle =</B>] <I>statement_handle</I><BR>
{, [<B>@</B><i>param1</i><B> =</B>] <I>value1</I> [,<i>...n</i>]
}</P>
<h5>Arguments</h5>
<dl>
<dt>[<B>@handle =</B>] <I>statement_handle</i></dt>
<DD>Is the integer value of the statement handle.
<I>statement_handle</I> is <B>int</B>, with no default.</DD>
<DT>[<b>@</b><i>param1</i><b> =</b>] <i>value1</i></DT>
<DD>Is a value for the first parameter defined in the parameter string. The
value can be a constant or a variable. There must be a parameter value supplied
for every parameter included in prepared statement <i>handle</i>. The values are not needed if
the Transact-SQL statement in <i>stmt</i> has no parameters.</DD>
<DT><i>n</i></DT>
<DD>Is a placeholder for the values of additional parameters. Values can be
only constants or variables. Values cannot be more complex expressions such
as functions, or expressions built using operators.</DD>
</dl>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>Returns a result set if specified by the prepared statement.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
<h2><A NAME="_sp_unprepare"></A>sp_unprepare</h2>
<P>Used to free a prepared SQL statement.</P>
<h5>Syntax</h5>
<P><B>sp_unprepare</b> [<B>@handle =</B>] <I>statement_handle</I></P>
<h5>Arguments</h5>
<dl>
<dt>[<B>@handle =</B>] <I>statement_handle</i></dt>
<DD>Is the integer value of the statement handle.
<I>statement_handle</I> is <B>int</B>, with no default.</DD>
</dl>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>None.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
<h2><A NAME="_sp_prepexec"></A>sp_prepexec</h2>
<P>Used to prepare and execute a parameterized SQL statement. This command combines the
functions of the sp_prepare and sp_execute procedures and is available from SQL2000 onwards.</P>
<h5>Syntax</h5>
<P><B>sp_prepexec</b> [<B>@handle =</B>] <I>statement_handle</I> OUTPUT,<BR>
[<B>@paramdef =</B>] N'<I>parameter_name data_type,</I>
[,<i>...n</i>]' <BR>
[<B>@stmt =</B>] N'<I>stmt</I>',<BR>
{, [<B>@</B><i>param1</i><B> =</B>] <I>value1</I> [,<i>...n</i>]
}</P>
<h5>Arguments</h5>
<dl>
<dt>[<B>@handle =</B>] <I>statement_handle</i></dt>
<DD>Is the name of a declared integer variable to receive the statement handle.
<I>statement_handle</I> is <B>int</B>, with no default.</DD>
<DT>[<B>@paramdef =</B>] N'<i>parameter_name data_type</i> [,<i>...n</i>]'</DT>
<DD>Is one string that contains the definitions of all parameters that have
been embedded in <i>stmt</i>. Each parameter definition consists of a parameter
name and a data type. <i>n</i> is a placeholder indicating additional parameter
definitions. Every parameter specified in <i>stmt</i> must be defined in <b>@paramdef</b>. </DD>
<DT>[<B>@stmt =</B>] '<I>stmt</I>'</DT>
<DD>
<P>Is a string containing a valid SQL statement. The size of the string is limited only by available database server
memory. <I>stmt</i> can contain parameters having the same form as a variable
name, for example:</P>
<P class="ex"><CODE>'SELECT * FROM Employees WHERE EmployeeID = @IDParameter'</CODE></P>
<P>Each parameter included in <i>stmt</i> must have a corresponding entry
in both the <b>@paramdef</b> parameter definition list.</P>
</DD>
<DT>[<b>@</b><i>param1</i><b> =</b>] <i>value1</i></DT>
<DD>Is a value for the first parameter defined in the parameter string. The
value can be a constant or a variable. There must be a parameter value supplied
for every parameter included in <i>stmt</i>. The values are not needed if
the Transact-SQL statement in <i>stmt</i> has no parameters.</DD>
<DT><i>n</i></DT>
<DD>Is a placeholder for the values of additional parameters. Values can be
only constants or variables. Values cannot be more complex expressions such
as functions, or expressions built using operators.</DD>
</dl>
<h5>Return Code Values</h5>
<P>0 (success) or 1 (failure).</P>
<h5>Result Sets</h5>
<P>Returns a result set if specified by the prepared statement.</P>
<h5>Permissions</h5>
<P>Execute permissions default to the <b>public</b> role.</P>
<h5>Example</h5>
</BODY>
</HTML>
|