Structured Query Language (SQL) - Cultural View of Technology
Structured Query Language (SQL) - Cultural View of Technology Structured Query Language (SQL) - Cultural View of Technology
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]
- Page 1 and 2: Structured Query Language (SQL) SQL
- Page 3 and 4: Nested SQL 72 Object-oriented SQL 7
- Page 5 and 6: SQL 1 SQL Paradigm Multi-paradigm A
- Page 7 and 8: SQL 3 result set. SELECT * FROM Boo
- Page 9 and 10: SQL 5 • MERGE is used to combine
- Page 11 and 12: SQL 7 REVOKE SELECT, UPDATE ON My_t
- Page 13 and 14: SQL 9 2008 SQL:2008 Legalizes ORDER
- Page 15 and 16: SQL 11 [9] ANSI/ISO/IEC Internation
- Page 17 and 18: SQL:2003 13 [11] http://www.iso.org
- Page 19 and 20: Advantage Database Server 15 • Sm
- Page 21 and 22: Call Level Interface 17 Call Level
- Page 23 and 24: Check Constraint 19 Check Constrain
- Page 25 and 26: Commit (data management) 21 Commit
- Page 27 and 28: Correlated subquery 23 Correlated s
- Page 29 and 30: CUBRID 25 Linux Foundation Silver S
- Page 31 and 32: CUBRID 27 References [1] http://www
- Page 33: Cursor (databases) 29 Scrollable cu
- Page 37 and 38: Data Definition Language 33 Data De
- Page 39 and 40: Data Manipulation Language 35 Data
- Page 41 and 42: Database Console Commands (Transact
- Page 43 and 44: Database Console Commands (Transact
- Page 45 and 46: Database Console Commands (Transact
- Page 47 and 48: DbForge Studio for MySQL 43 • Dat
- Page 49 and 50: Declarative Referential Integrity 4
- Page 51 and 52: Devgems Data Modeler 47 External li
- Page 53 and 54: Embedded SQL 49 External links •
- Page 55 and 56: EnterpriseDB 51 EnterpriseDB also o
- Page 57 and 58: Epictetus Database Client 53 Extern
- Page 59 and 60: Foreign key 55 SET NULL The foreign
- Page 61 and 62: FSQL 57 Comparator for Possibility
- Page 63 and 64: Hint (SQL) 59 Hint (SQL) In various
- Page 65 and 66: HSQLDB 61 Transaction support HSQLD
- Page 67 and 68: MaxDB 63 MaxDB Stable release 7.6.0
- Page 69 and 70: Meta-SQL 65 Meta-SQL 'Meta-SQL' Use
- Page 71 and 72: MySQL Workbench 67 MySQL Migration
- Page 73 and 74: MySQL Workbench 69 [14] MySQL Workb
- Page 75 and 76: Navicat 71 Free version Navicat Lit
- Page 77 and 78: PL/pgSQL 73 PL/pgSQL PL/pgSQL (Proc
- Page 79 and 80: PL/SQL 75 User-defined exceptions a
- Page 81 and 82: PL/SQL 77 A selection of other data
- Page 83 and 84: PL/SQL 79 Array handling PL/SQL ref
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]