DB2 嵌入式应用中定义游标(开放平台上)
DECLARE CURSOR statement
The DECLARE CURSOR statement defines a cursor.
Invocation
Although an interactive SQL facility might provide an interface that gives the appearance of interactive execution, this statement can only be embedded within an application program. It is not an executable statement and cannot be dynamically prepared.
When invoked using the command line processor, additional options can be specified.
For more information, refer to Using command line SQL statements and XQuery statements .
Authorization
- The prepared select-statement identified by statement-name
- The specified select-statement
The privileges held by the authorization ID of the statement must include the privileges necessary to execute the select-statement. See the Authorization section in "SQL queries".
- The authorization ID of the statement is the runtime authorization ID.
- The authorization check is performed when the SELECT-statement is prepared.
- The cursor cannot be opened unless the SELECT-statement is in a prepared state.
- GROUP privileges are not checked.
- The authorization ID of the statement is the authorization ID specified during program preparation.
Syntax
.-ASENSITIVE------.
>>-DECLARE--cursor-name--+-----------------+--CURSOR--●--------->
| (1) |
'-INSENSITIVE-----' >--| holdability |--●--| returnability |--●---------------------> >--FOR--+-select-statement-+-----------------------------------><
'-statement-name---' holdability .-WITHOUT HOLD-.
|--+--------------+---------------------------------------------|
'-WITH HOLD----' returnability .-WITHOUT RETURN-------------.
|--+----------------------------+-------------------------------|
| .-TO CALLER-. |
'-WITH RETURN--+-----------+-'
'-TO CLIENT-'
- This option can be used only in the context of a compound SQL (compiled) statement
Description
- cursor-name
- Specifies the name of the cursor created when the source program is run. The name must not be the same as the name of another cursor declared in the source program. The cursor must be opened before use.
- ASENSITIVE or INSENSITIVE
- Specifies whether the cursor is asensitive or insensitive to changes.
- ASENSITIVE
- Specifies that the cursor should be as sensitive as possible to insert, update, or delete operations made to the rows underlying the result table, depending on how the select-statement is optimized. This option is the default.
- INSENSITIVE
- Specifies that the cursor does not have sensitivity to insert, update, or delete operations that are made to the rows underlying the result table. If INSENSITIVE is specified, the cursor is read-only and the result table is materialized when the cursor is opened. As a result, the size of the result table, the order of the rows, and the values for each row do not change after the cursor is opened. The SELECT statement cannot contain a FOR UPDATE clause, and the cursor cannot be used for positioned updates or deletes.
- WITHOUT HOLD or WITH HOLD
- Specifies whether or not the cursor should be prevented from being closed as a consequence of a commit operation.
- WITHOUT HOLD
- Does not prevent the cursor from being closed as a consequence of a commit operation. This is the default.
- WITH HOLD
- Maintains resources across multiple units of work. The effect of the WITH HOLD cursor attribute is as follows:
- For units of work ending with COMMIT:
- Open cursors defined WITH HOLD remain open. The cursor is positioned before the next logical row of the results table.
If a DISCONNECT statement is issued after a COMMIT statement for a connection with WITH HOLD cursors, the held cursors must be explicitly closed or the connection will be assumed to have performed work (simply by having open WITH HELD cursors even though no SQL statements were issued) and the DISCONNECT statement will fail.
- All locks are released, except locks protecting the current cursor position of open WITH HOLD cursors. The locks held include the locks on the table, and for parallel environments, the locks on rows where the cursors are currently positioned. Locks on packages and dynamic SQL sections (if any) are held.
- Valid operations on cursors defined WITH HOLD immediately following a COMMIT request are:
- FETCH: Fetches the next row of the cursor.
- CLOSE: Closes the cursor.
- UPDATE and DELETE CURRENT OF CURSOR are valid only for rows that are fetched within the same unit of work.
- LOB locators are freed.
- The set of rows modified by:
- A data change statement
- Routines that modify SQL data embedded within open WITH HOLD cursors
is committed.
- Open cursors defined WITH HOLD remain open. The cursor is positioned before the next logical row of the results table.
- For units of work ending with ROLLBACK:
- All open cursors are closed.
- All locks acquired during the unit of work are released.
- LOB locators are freed.
- For special COMMIT case:
- Packages can be recreated either explicitly, by binding the package, or implicitly, because the package has been invalidated and then dynamically recreated the first time it is referenced. All held cursors are closed during package rebind. This might result in errors during subsequent execution.
- For units of work ending with COMMIT:
- WITHOUT RETURN or WITH RETURN
- Specifies whether or not the result table of the cursor is intended to be used as a result set that will be returned from a procedure.
- WITHOUT RETURN
- Specifies that the result table of the cursor is not intended to be used as a result set that will be returned from a procedure.
- WITH RETURN
- Specifies that the result table of the cursor is intended to be used as a result set that will be returned from a procedure. WITH RETURN is relevant only if the DECLARE CURSOR statement is contained with the source code for a procedure. In other cases, the precompiler might accept the clause, but it has no effect.
Within an SQL procedure, cursors declared using the WITH RETURN clause that are still open when the SQL procedure ends, define the result sets from the SQL procedure. All other open cursors in an SQL procedure are closed when the SQL procedure ends. Within an external procedure (one not defined using LANGUAGE SQL), the default for all cursors is WITH RETURN TO CALLER. Therefore, all cursors that are open when the procedure ends will be considered result sets. Cursors that are returned from a procedure cannot be declared as scrollable cursors.
- TO CALLER
- Specifies that the cursor can return a result set to the caller. For example, if the caller is another procedure, the result set is returned to that procedure. If the caller is a client application, the result set is returned to the client application.
- TO CLIENT
- Specifies that the cursor can return a result set to the client application. This cursor is invisible to any intermediate nested procedures. If a function, method, or trigger called the procedure either directly or indirectly, result sets cannot be returned to the client and the cursor will be closed after the procedure finishes.
- select-statement
- Identifies the SELECT statement of the cursor. The select-statement must not include parameter markers, but can include references to host variables. The declarations of the host variables must precede the DECLARE CURSOR statement in the source program.
- statement-name
- The SELECT statement of the cursor is the prepared SELECT statement identified by the statement-name when the cursor is opened. The statement-name must not be identical to a statement-name specified in another DECLARE CURSOR statement of the source program.
For an explanation of prepared SELECT statements, see "PREPARE".
Notes
- A program called from another program, or from a different source file within the same program, cannot use the cursor that was opened by the calling program.
- Unnested procedures, with LANGUAGE other than SQL, will have WITH RETURN TO CALLER as the default behavior if DECLARE CURSOR is specified without a WITH RETURN clause, and the cursor is left open in the procedure. This provides compatibility with procedures from previous versions that allow procedures to return result sets to applicable client applications. To avoid this behavior, close all cursors opened in the procedure.
- If the SELECT statement of a cursor contains CURRENT DATE, CURRENT TIME, or CURRENT TIMESTAMP, all references to these special registers will yield the same respective datetime value on each FETCH. This value is determined when the cursor is opened.
- For more efficient processing of data, the database manager can block data for read-only cursors when retrieving data from a remote server. The use of the FOR UPDATE clause helps the database manager decide whether a cursor is updatable or not. Updatability is also used to determine the access path selection as well. If a cursor is not going to be used in a Positioned UPDATE or DELETE statement, it should be declared as FOR READ ONLY.
- A cursor in the open state designates a result table and a position relative to the rows of that table. The table is the result table specified by the SELECT statement of the cursor.
- A cursor is deletable if each of the following conditions is true:
- Each FROM clause of the outer fullselect identifies only one base table or deletable view (cannot identify a nested or common table expression or a nickname) without use of the OUTER clause
- The outer fullselect does not include a VALUES clause
- The outer fullselect does not include a GROUP BY clause or HAVING clause
- The outer fullselect does not include column functions in the select list
- The outer fullselect does not include SET operations (UNION, EXCEPT, or INTERSECT) with the exception of UNION ALL
- The outer fullselect does not contain a FOR SYSTEM_TIME period specification.
- The select list of the outer fullselect does not include DISTINCT
- The outer fullselect does not include an ORDER BY clause (even if the ORDER BY clause is nested in a view), and the FOR UPDATE clause has not been specified
- The select-statement does not include a FOR READ ONLY clause
- The FROM clause of the outer fullselect does not include a data-change-table-reference
- One or more of the following conditions is true:
- The FOR UPDATE clause is specified
- The cursor is statically defined, unless the STATICREADONLY bind option is YES
- The LANGLEVEL bind option is MIA or SQL92E
A column in the select list of the outer fullselect associated with a cursor is updatable if each of the following conditions is true:- The cursor is deletable
- The column resolves to a column of the base table
- The LANGLEVEL bind option is MIA, SQL92E or the select-statement includes the FOR UPDATE clause (the column must be specified explicitly or implicitly in the FOR UPDATE clause)
A cursor is read-only if it is not deletable.
A cursor is ambiguous if each of the following conditions is true:- The select-statement is dynamically prepared
- The select-statement does not include either the FOR READ ONLY clause or the FOR UPDATE clause
- The LANGLEVEL bind option is SAA1
- The cursor otherwise satisfies the conditions of a deletable cursor
An ambiguous cursor is considered read-only if the BLOCKING bind option is ALL, otherwise it is considered updatable.
- Cursors in procedures that are called by application programs written using CLI can be used to define result sets that are returned directly to the client application. Cursors in SQL procedures can also be returned to a calling SQL procedure only if they are defined using the WITH RETURN clause.
- Cursors declared in routines that are invoked directly or indirectly from a cursor declared WITH HOLD, do not inherit the WITH HOLD option. Thus, unless the cursor in the routine is explicitly defined WITH HOLD, a COMMIT in the application will close it.
Consider the following application and two UDFs:
Application: DECLARE APPCUR CURSOR WITH HOLD FOR SELECT UDF1() ...
OPEN APPCUR
FETCH APPCUR ...
COMMIT UDF1: DECLARE UDF1CUR CURSOR FOR SELECT UDF2() ...
OPEN UDF1CUR
FETCH UDF1CUR ... UDF2: DECLARE UDF2CUR CURSOR WITH HOLD FOR SELECT UDF2() ...
OPEN UDF2CUR
FETCH UDF2CUR ...After the application fetches cursor APPCUR, all three cursors are open. When the application issues the COMMIT statement, APPCUR remains open, because it was declared WITH HOLD. In UDF1, however, the cursor UDF1CUR is closed, because it was not defined with the WITH HOLD option. When the cursor UDF1CUR is closed, all routine invocations in the corresponding select-statement complete (receiving a final call, if so defined). UDF2 completes, which causes UDF2CUR to close.
Examples
EXEC SQL DECLARE C1 CURSOR FOR
SELECT DEPTNO, DEPTNAME, MGRNO
FROM DEPARTMENT
WHERE ADMRDEPT = 'A00';
EXEC SQL DECLARE C2 CURSOR FOR
SELECT E.WEEKLYPAY
FROM NEW TABLE
(INSERT INTO EMPLOYEE
(EMPNO, FIRSTNME, MIDINIT, LASTNAME, EDLEVEL, SALARY)
VALUES('000420', 'Peter', 'U', 'Bender', 16, 31842) AS E;
DB2 嵌入式应用中定义游标(开放平台上)的更多相关文章
- 国内物联网平台(8):中移物联网开放平台OneNet
国内物联网平台(8)——中移物联网开放平台OneNet 马智 平台定位 OneNET是中移物联网有限公司搭建的开放.共赢设备云平台,为各种跨平台物联网应用.行业解决方案,提供简便的云端接入.存储.计算 ...
- 国内物联网平台初探(八):中移物联网开放平台OneNet
平台定位 OneNET是中移物联网有限公司搭建的开放.共赢设备云平台,为各种跨平台物联网应用.行业解决方案,提供简便的云端接入.存储.计算和展现,快速打造物联网产品应用,降低开发成本. IoT Paa ...
- shared SDK 微信开放平台遇到的问题
shared sdk是用于集成到app中,方便快速社交化分享的组件,其使用方法比较简单,参考官网的快速集成步骤就能搞定.稍微麻烦一点的就是需要到各个开放平台去注册你的APP. 在各个开放平台注册好之后 ...
- 微信开放平台开发——网页微信扫码登录(OAuth2.0)
1.OAuth2.0 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. 允许用户提供 ...
- phonegap与微信开放平台接口整合
在开发phonegap应用的过程中有个需求需要将应用的消息推送到微信上.于是我自己写了一个微信的phonegap插件,并成功整合进了应用. 插件地址:https://github.com/ajccom ...
- OAuth简介及sina微博开放平台
OAuth简介及sina微博开放平台 2010-10-26 13:15:25 标签:新浪 sina 微博 OAuth 开放平台 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者 ...
- php微信开放平台--第三方网页微信扫码登录(OAuth2.0)
第一.OAuth2.0 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. 允许用户提 ...
- 微信开放平台,微信登陆第三方网站 提示redirect_uri 参数错误
在微信开放平台上我填写的回调域是:bbs.qiaoshisui.com/LoginApi/WeiXinCallBack,我构造的链接是:https://open.weixin.qq.com/conne ...
- windows平台是上的sublime编辑远程linux平台上的文件
sublime是个跨平台的强大的代码编辑工具,不多说. 想使用sublime完毕linux平台下django网站的代码编辑工作以提高效率(原来使用linux下的vim效率较低,适合编辑一些小脚本). ...
随机推荐
- mysql workbench建表时PK,NN,UQ,BIN,UN,ZF,AI
1. [intrinsic column flags] (基本字段类型标识) - PK: primary key (column is part of a pk) 主键 - NN: not null ...
- web.xml 模板和Servlet版本
最近没事干,写自己小项目(项目周期无限长.开发效率无限低)的时候,遇到web.xml的dtd声明不正确,这里罗列下从Eclipse里新建项目时,自动生成的web.xml,供以后遇到类似问题的时候进行参 ...
- hiho #1326 : 有序01字符串
#1326 : 有序01字符串 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于一个01字符串,你每次可以将一个0修改成1,或者将一个1修改成0.那么,你最少需要修改 ...
- 极客DIY:如何用Siri与树莓派“交互”
苹果在2014年推出的HomeKit智能家居平台的确给人眼前一亮的感觉.随着时间的推移,国外的黑客对HomeKit该逆向的逆向,结果也都汇总到了git.本着折腾到死的极客心态,从网上淘了一块树莓派进行 ...
- BZOJ 1019: [SHOI2008]汉诺塔
Description 一个汉诺塔,给出了移动的优先顺序,问从A移到按照规则移到另一个柱子上的最少步数. 规则:小的在大的上面,每次不能移动上一次移动的,选择可行的优先级最高的. Sol DP. 倒着 ...
- time()函数,dirname(__FILE__) 的使用总结
time()函数将返回从1970-1-1 0:0:0到当前时间的秒数.(整型) dirname(__FILE__) php中定义了一个很有用的常数,即 __file__ 这个内定常数是当前php程序的 ...
- Java对象访问 类的静态变量
Java类的静态变量用对象和类名都能访问,一般用类名,但如果用对象来访问静态变量呢,有何种效果? 测试一下: package JavaTest; public class test{ public s ...
- MySQL一次插入多行数据
CREATE TABLE `viewhistory` ( `viewid` ) NOT NULL AUTO_INCREMENT, `uid` ) NOT NULL, `video` ) NOT NUL ...
- git config proxy
$ export http_proxy=http://proxy.ip.ad.ress:portnumber/ $ export https_proxy=http://proxy.ip.ad.ress ...
- neutron 同一虚拟网卡的多个IP设置
neutron port-update <端口ID> --fixed-ip subnet_id=<子网ID/子网名>,ip_address=<IP地址> --fix ...