<?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-07-22 -- <a href="https://www.dbainfo.net/one-problem-about-sqlserver-backup-strategy.htm" title="关于sqlserver2005备份策略的一个问题">关于sqlserver2005备份策略的一个问题</a> (0)</li><li>2010-11-10 -- <a href="https://www.dbainfo.net/database-operation-by-sybase-central.htm" title="利用Sybase Central简单操作Sybase ASE数据库">利用Sybase Central简单操作Sybase ASE数据库</a> (4)</li><li>2010-09-28 -- <a href="https://www.dbainfo.net/view-table-column-properties-ddl.htm" title="查看表中列属性的SQL语句">查看表中列属性的SQL语句</a> (0)</li><li>2014-12-09 -- <a href="https://www.dbainfo.net/delete-duplicate-row-by-ignore_dup_row_option.htm" title="ASE使用with ignore_dup_row删除重复数据">ASE使用with ignore_dup_row删除重复数据</a> (1)</li><li>2011-08-23 -- <a href="https://www.dbainfo.net/nfs-mount-failed-reason-given-by-server-permission-denied.htm" title="NFS mount failed, reason given by server: Permission denied">NFS mount failed, reason given by server: Permission denied</a> (5)</li><li>2015-03-07 -- <a href="https://www.dbainfo.net/sap-rs-cr-number-2.htm" title="SAP Replication Server的所有已知BUG列表（2）">SAP Replication Server的所有已知BUG列表（2）</a> (0)</li><li>2012-06-11 -- <a href="https://www.dbainfo.net/i-have-the-tool-can-recover-from-asa-db-file.htm" title="研发成功了从Sybase SQL Anywhere的DB文件上恢复数据的工具">研发成功了从Sybase SQL Anywhere的DB文件上恢复数据的工具</a> (3)</li><li>2010-08-17 -- <a href="https://www.dbainfo.net/sybase-ase15-compute-table-space-by-proxy-procedure-proxy_table.htm" title="ASE15.0中利用代理表实现统计用户表存储空间大小的功能 ">ASE15.0中利用代理表实现统计用户表存储空间大小的功能 </a> (1)</li><li>2011-05-18 -- <a href="https://www.dbainfo.net/sap-erp-run-at-sybase-ase.htm" title="SAP ERP系统能够跑在ASE系统上了">SAP ERP系统能够跑在ASE系统上了</a> (0)</li><li>2012-01-17 -- <a href="https://www.dbainfo.net/sqlserver-2005-failover-cluster3.htm" title="在VM Server中配置SQL Server 2005故障转移群集(3)—安装实例">在VM Server中配置SQL Server 2005故障转移群集(3)—安装实例</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>
