<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sybase数据库技术，数据库恢复专家 &#187; for read only</title>
	<atom:link href="http://www.dbainfo.net/tag/for-read-only/feed" rel="self" type="application/rss+xml" />
	<link>https://www.dbainfo.net</link>
	<description>提供Sybase ASE及Sybase SQL Anywhere数据库修复服务，电话：13811580958(微信)，QQ：289965371！We have many years of experience in recovering data from damanged Sybase devices. Contact us by Phone: +86 13811580958 Wechat: 13811580958 Email: 289965371@qq.com</description>
	<lastBuildDate>Sat, 14 Jun 2025 16:28:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>游标可更新与for read only/for update</title>
		<link>https://www.dbainfo.net/cursor_updatable_for_read_only_for_update.htm</link>
		<comments>https://www.dbainfo.net/cursor_updatable_for_read_only_for_update.htm#comments</comments>
		<pubDate>Mon, 09 Jun 2014 06:30:30 +0000</pubDate>
		<dc:creator>dbainfo</dc:creator>
				<category><![CDATA[Sybase ASE]]></category>
		<category><![CDATA[for read only]]></category>

		<guid isPermaLink="false">http://www.dbainfo.net/%e6%b8%b8%e6%a0%87%e5%8f%af%e6%9b%b4%e6%96%b0%e4%b8%8efor-read-onlyfor-update.htm</guid>
		<description><![CDATA[在定义游标时不指定for update 或 for read only，ASE会检查以了解游标是否可更新； 如果游标查询语句中包含order by子句，则ASE会将游标定义为只读；其它情况下定义为可更新游标； 如果不涉及更新或删除表数据的话，建议在游标定义中加上for read only选项，这样ASE将游标定义为只读； &#160; 表customer在c_custkey列上建有唯一索引，查询表的前10行内容（所有字段拼接成一个字符串）， 如果定义游标为for read only： &#160;&#160;&#160;&#160;declare cur_hash cursor for select top 10 convert(varchar,c_custkey)+coalesce( nullif(isnull(c_name,&#39;&#39;), &#39;&#39;) , &#39; &#39;)+coalesce( nullif(isnull(c_address,&#39;&#39;),&#39;&#39;) , &#39; &#39;)+convert(varchar,c_nationkey)+coalesce( nullif(isnull(c_phone,&#39;&#39;),&#39;&#39;) , &#39; &#39;)+convert(varchar,c_acctbal)+coalesce( nullif(isnull(c_mktsegment,&#39;&#39;),&#39;&#39;) , &#39; &#39;)+coalesce( nullif(isnull(c_comment,&#39;&#39;),&#39;&#39; ) , &#39; &#39;) from customer for read only 则使用表扫描返回表的前10行数据； 如果定义游标为for update或者不指定for read [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color:black; font-family:微软雅黑; font-size:10pt">在定义游标时不指定for update 或 for read only，ASE会检查以了解游标是否可更新； </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">如果游标查询语句中包含order by子句，则ASE会将游标定义为只读；其它情况下定义为可更新游标； </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">如果不涉及更新或删除表数据的话，建议在游标定义中加上for read only选项，这样ASE将游标定义为只读； </span></p>
<p>&nbsp;</p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">表customer在c_custkey列上建有唯一索引，查询表的前10行内容（所有字段拼接成一个字符串）， </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">如果定义游标为for read only： </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">&nbsp;&nbsp;&nbsp;&nbsp;declare cur_hash cursor for select top 10 <span style="color:blue">convert(varchar,c_custkey)+coalesce( nullif(isnull(c_name,&#39;&#39;), &#39;&#39;) , &#39; &#39;)+coalesce( nullif(isnull(c_address,&#39;&#39;),&#39;&#39;) , &#39; &#39;)+convert(varchar,c_nationkey)+coalesce( nullif(isnull(c_phone,&#39;&#39;),&#39;&#39;) , &#39; &#39;)+convert(varchar,c_acctbal)+coalesce( nullif(isnull(c_mktsegment,&#39;&#39;),&#39;&#39;) , &#39; &#39;)+coalesce( nullif(isnull(c_comment,&#39;&#39;),&#39;&#39; ) , &#39; &#39;) <span style="color:black">from customer <span style="color:red">for read only </span></span></span></span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">则使用表扫描返回表的前10行数据； <span id="more-2477"></span></span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">如果定义游标为for update或者不指定for read only/for update： </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">则ASE会使用c_custkey列上的唯一索引扫描表数据，然后返回按照c_custkey键顺序的前10行数据； </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">对于该查询语句：select top 10 ... from customer，即使不指定for update，ASE也会将游标定义为可更新； </span></p>
<p>&nbsp;</p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">如果没有指定 for update 子句， ASE将选择任何唯一索引；如果指定表列没有唯一索引的情况下，它也可以使用其它索引或表扫描。 </span></p>
<p><span style="color:red; font-family:微软雅黑; font-size:10pt">但如果指定了 for update 子句， ASE必须使用为一个或多个列定义的唯一索引扫描基表。如果不存在，则返回一个错误。 </span></p>
<p>&nbsp;</p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">可以通过抽象计划来查看游标扫描表数据的时候是否使用了唯一索引： </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">查看在存储过程外声明的游标信息： </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">&nbsp;&nbsp;&nbsp;&nbsp;sp_cursorinfo null, <em>cursor_name </em></span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">查看在存储过程<strong>内</strong>声明的游标信息： </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">&nbsp;&nbsp;&nbsp;&nbsp;在存储过程内加入： exec sp_cursorinfo </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">存储过程sp_cursorinfo除了显示游标的状态信息外，还显示游标查询语句的抽象计划信息。 </span></p>
<p>&nbsp;</p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">游标的状态不同输出也会不同，一般会输出： </span></p>
<p style="margin-left: 11pt"><span style="color:black; font-family:微软雅黑; font-size:10pt">1、游标定义、编译、扫描数据所使用的隔离级别； </span></p>
<p style="margin-left: 11pt"><span style="color:black; font-family:微软雅黑; font-size:10pt">2、游标是否可滚动； </span></p>
<p style="margin-left: 11pt"><span style="color:black; font-family:微软雅黑; font-size:10pt">3、游标当前位置； </span></p>
<p style="margin-left: 11pt"><span style="color:black; font-family:微软雅黑; font-size:10pt">4、游标已经读取/更新/删除的行数； </span></p>
<p style="margin-left: 11pt"><span style="color:black; font-family:微软雅黑; font-size:10pt">5、游标在事务提交或回滚后是否继续保持打开状态； </span></p>
<p style="margin-left: 11pt"><span style="color:black; font-family:微软雅黑; font-size:10pt">6、每次提取的行数； </span></p>
<p style="margin-left: 11pt"><span style="color:black; font-family:微软雅黑; font-size:10pt">7、游标是只读还是可更新的； </span></p>
<p style="margin-left: 11pt"><span style="color:black; font-family:微软雅黑; font-size:10pt">8、游标所消耗的内存数量； </span></p>
<p style="margin-left: 11pt"><span style="color:black; font-family:微软雅黑; font-size:10pt">9、游标返回的结果集的列数； </span></p>
<p style="margin-left: 11pt"><span style="color:black; font-family:微软雅黑; font-size:10pt">10、游标结果集的列信息（列类型、列宽度、是否可更新）； </span></p>
<p>&nbsp;</p>
<p><span style="color:black; font-family:微软雅黑">关于选项：set close on endtran </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">如果提交或回退游标的当前事务，默认情况下游标是保持打开状态；输出：The cursor will remain open when a transaction is committed or rolled back. </span></p>
<p><span style="color:black; font-family:微软雅黑; font-size:10pt">如果设置set close on endtran on则提交或回退游标的当前事务时游标会关闭；输出：The cursor will be closed when a transaction is committed or rolled back. </span></p>
<div style="clear: both; margin: 10px 0pt; border: 1px dashed rgb(153, 153, 153); font-size: 12px; padding: 5px 10px;">
<li>本文链接地址：<a href="https://www.dbainfo.net/cursor_updatable_for_read_only_for_update.htm">https://www.dbainfo.net/cursor_updatable_for_read_only_for_update.htm</a>；</li>
<li>本文为dbainfo个人原创，请在尊重作者劳动成果的前提下进行转载；</li>
<li>转载务必注明原始出处 : <a href="https://www.dbainfo.net/">Sybase数据库技术，数据库恢复专家</a>；</li>
<li>对《<a href="https://www.dbainfo.net/cursor_updatable_for_read_only_for_update.htm">游标可更新与for read only/for update</a>》有何疑问或见解，请在本文下方发表；</li>
<li>对网站还有其他问题或建议，请提交在<a href="https://www.dbainfo.net/messages" target="_blank">留言板</a>，谢谢！</li>
</div>
<h2  class="related_post_title">随机日志</h2><ul class="related_post"><li>2010-05-31 -- <a href="https://www.dbainfo.net/oracle-split-page-by-rownum.htm" title="Oracle中实现分页的方法">Oracle中实现分页的方法</a> (0)</li><li>2015-03-08 -- <a href="https://www.dbainfo.net/sap-asa-cr-number-6.htm" title="SAP SQL Anywhere的所有已知BUG列表（6）">SAP SQL Anywhere的所有已知BUG列表（6）</a> (0)</li><li>2015-09-02 -- <a href="https://www.dbainfo.net/sap-ase-manual-index2.htm" title="SAP ASE 15.7 ESD#2中文版官方手册目录列表（2）">SAP ASE 15.7 ESD#2中文版官方手册目录列表（2）</a> (0)</li><li>2015-03-08 -- <a href="https://www.dbainfo.net/sap-ase-cr-number-3.htm" title="SAP Adaptive Server Enterprise的所有已知BUG列表（3）">SAP Adaptive Server Enterprise的所有已知BUG列表（3）</a> (0)</li><li>2015-03-08 -- <a href="https://www.dbainfo.net/sap-ase-cr-number-8.htm" title="SAP Adaptive Server Enterprise的所有已知BUG列表（8）">SAP Adaptive Server Enterprise的所有已知BUG列表（8）</a> (0)</li><li>2015-03-08 -- <a href="https://www.dbainfo.net/sap-pb-cr-number-1.htm" title="SAP PB Enterprise的所有已知BUG列表（1）">SAP PB Enterprise的所有已知BUG列表（1）</a> (0)</li><li>2009-11-18 -- <a href="https://www.dbainfo.net/script-backup-db-many-times.htm" title="实现连续N次备份数据库的脚本">实现连续N次备份数据库的脚本</a> (0)</li><li>2010-02-22 -- <a href="https://www.dbainfo.net/sql-server-patch-version-mapping-table.htm" title="SQL Server补丁版本的检查[转]">SQL Server补丁版本的检查[转]</a> (0)</li><li>2010-08-09 -- <a href="https://www.dbainfo.net/sqlserver2008-backup-database-with-compression-options.htm" title="sql server 2008中的备份压缩特性">sql server 2008中的备份压缩特性</a> (0)</li><li>2011-06-19 -- <a href="https://www.dbainfo.net/news-sybase-chinaccs-joint-venture.htm" title="中国通信服务与SYBASE 成立合资公司">中国通信服务与SYBASE 成立合资公司</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>https://www.dbainfo.net/cursor_updatable_for_read_only_for_update.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
