Structured Query Language (SQL) - Cultural View of Technology

Structured Query Language (SQL) - Cultural View of Technology Structured Query Language (SQL) - Cultural View of Technology

culturalview.com
from culturalview.com More from this publisher
23.10.2012 Views

Cursor (databases) 30 Positioned update/delete statements Cursors can not only be used to fetch data from the DBMS into an application but also to identify a row in a table to be updated or deleted. The SQL:2003 standard defines positioned update and positioned delete SQL statements for that purpose. Such statements do not use a regular WHERE clause with predicates. Instead, a cursor identifies the row. The cursor must be opened and positioned on a row already using the FETCH statement. UPDATE table_name SET ... WHERE CURRENT OF cursor_name DELETE FROM table_name WHERE CURRENT OF cursor_name The cursor must operate on an updatable result set in order to successfully execute a positioned update or delete statement. Otherwise, the DBMS would not know how to apply the data changes to the underlying tables referred to in the cursor. Cursors in distributed transactions Using cursors in distributed transactions (X/Open XA Environments), which are controlled using a transaction monitor, is no different than cursors in non-distributed transactions. One has to pay attention when using holdable cursors, however. Connections can be used by different applications. Thus, once a transaction has been ended and committed, a subsequent transaction (running in a different application) could inherit existing holdable cursors. Therefore, an application developer has to be aware of that situation. Cursors in XQuery The XQuery language allows cursors to be created using the subsequence() function. The format is: let $displayed-sequence := subsequence($result, $start, $item-count) Where $result is the result of the initial XQuery, $start is the item number to start and $item-count is the number of items to return. Equivalently this can also be done using a predicate: let $displayed-sequence := $result[$start to $end] Where $end is the end sequence. For complete examples see the XQuery Wikibook [1] .

Cursor (databases) 31 Disadvantages of cursors The following information may vary from database system to database system. Fetching a row from the cursor may result in a network round trip each time. This uses much more network bandwidth than would ordinarily be needed for the execution of a single SQL statement like DELETE. Repeated network round trips can severely impact the speed of the operation using the cursor. Some DBMSs try to reduce this impact by using block fetch. Block fetch implies that multiple rows are sent together from the server to the client. The client stores a whole block of rows in a local buffer and retrieves the rows from there until that buffer is exhausted. Cursors allocate resources on the server, for instance locks, packages, processes, temporary storage, etc. For example, Microsoft SQL Server implements cursors by creating a temporary table and populating it with the query's result-set. If a cursor is not properly closed (deallocated), the resources will not be freed until the SQL session (connection) itself is closed. This wasting of resources on the server can not only lead to performance degradations but also to failures. See also • Iterator References • Christopher J. Date: Database in Depth, O'Reilly & Associates, ISBN 0-596-10012-4 • Thomas M. Connolly, Carolyn E. Begg: Database Systems, Addison-Wesley, ISBN 0-321-21025-5 • Ramiz Elmasri, Shamkant B. Navathe: Fundamentals of Database Systems, Addison-Wesley, ISBN 0-201-54263-3 • Neil Matthew, Richard Stones: Beginning Databases with PostgreSQL: From Novice to Professional, Apress, ISBN 1-59059-478-9 • Thomas Kyte: Expert One-On-One: Oracle, Apress, ISBN 1-59059-525-4 • Kevin Loney: Oracle Database 10g: The Complete Reference, Oracle Press, ISBN 0-07-225351-7 External links • Cursor Optimization Tips (for MS SQL Server) [2] • Descriptions from Portland Pattern Repository [3] • PostgreSQL Documentation [4] • Berkeley DB Reference Guide: Cursor operations [5] • ResultSet in JDBC [6] • Q3SqlCursor Class Reference [7] • OCI Scrollable Cursor [8] • function oci_new_cursor [9] • MySQL's Cursor Documentation [10] • Cursors in DB2 CLI applications [11] ; Cursors in DB2 SQL stored procedures [12]

Cursor (databases) 31<br />

Disadvantages <strong>of</strong> cursors<br />

The following information may vary from database system to database system.<br />

Fetching a row from the cursor may result in a network round trip each time. This uses much more network<br />

bandwidth than would ordinarily be needed for the execution <strong>of</strong> a single <strong>SQL</strong> statement like DELETE. Repeated<br />

network round trips can severely impact the speed <strong>of</strong> the operation using the cursor. Some DBMSs try to reduce this<br />

impact by using block fetch. Block fetch implies that multiple rows are sent together from the server to the client.<br />

The client stores a whole block <strong>of</strong> rows in a local buffer and retrieves the rows from there until that buffer is<br />

exhausted.<br />

Cursors allocate resources on the server, for instance locks, packages, processes, temporary storage, etc. For<br />

example, Micros<strong>of</strong>t <strong>SQL</strong> Server implements cursors by creating a temporary table and populating it with the query's<br />

result-set. If a cursor is not properly closed (deallocated), the resources will not be freed until the <strong>SQL</strong> session<br />

(connection) itself is closed. This wasting <strong>of</strong> resources on the server can not only lead to performance degradations<br />

but also to failures.<br />

See also<br />

• Iterator<br />

References<br />

• Christopher J. Date: Database in Depth, O'Reilly & Associates, ISBN 0-596-10012-4<br />

• Thomas M. Connolly, Carolyn E. Begg: Database Systems, Addison-Wesley, ISBN 0-321-21025-5<br />

• Ramiz Elmasri, Shamkant B. Navathe: Fundamentals <strong>of</strong> Database Systems, Addison-Wesley, ISBN<br />

0-201-54263-3<br />

• Neil Matthew, Richard Stones: Beginning Databases with Postgre<strong>SQL</strong>: From Novice to Pr<strong>of</strong>essional, Apress,<br />

ISBN 1-59059-478-9<br />

• Thomas Kyte: Expert One-On-One: Oracle, Apress, ISBN 1-59059-525-4<br />

• Kevin Loney: Oracle Database 10g: The Complete Reference, Oracle Press, ISBN 0-07-225351-7<br />

External links<br />

• Cursor Optimization Tips (for MS <strong>SQL</strong> Server) [2]<br />

• Descriptions from Portland Pattern Repository [3]<br />

• Postgre<strong>SQL</strong> Documentation [4]<br />

• Berkeley DB Reference Guide: Cursor operations [5]<br />

• ResultSet in JDBC [6]<br />

• Q3SqlCursor Class Reference [7]<br />

• OCI Scrollable Cursor [8]<br />

• function oci_new_cursor [9]<br />

• My<strong>SQL</strong>'s Cursor Documentation [10]<br />

• Cursors in DB2 CLI applications [11] ; Cursors in DB2 <strong>SQL</strong> stored procedures [12]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!