<?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>I&#039;m CaLendarW Blog &#187; mssql</title>
	<atom:link href="http://wongkalun.idv.hk/tag/mssql/feed/" rel="self" type="application/rss+xml" />
	<link>http://wongkalun.idv.hk</link>
	<description>Put the technology in correct places</description>
	<lastBuildDate>Sun, 18 Apr 2010 16:19:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>MsSQL &#8211; Identity Generator</title>
		<link>http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/</link>
		<comments>http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 15:24:39 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[mssql]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=629</guid>
		<description><![CDATA[MsSQL 內既 Identity Generator, 雖然被發現當 Table 儲存超過一百萬行時, 由 SELECT @@Identity 或者 SELECT SCOPE_IDENTITY() 會存在 Return 值錯誤既問題, 但經過呢個幾月既開發都未出現問題. 係 MsSQL 入面, 新增 Record 時主要提取 Primary Key 既方法主要有 @@IDENTITY, SCOPE_IDENTY() 同 IDENT_CURRENT(&#8216;table_name&#8217;) 三種: @@IDENTITY 使用 @@IDENTITY 會 Return 當前 Session 任何 Table 最後生產的 Primary Key, 如果 Insert statement 運行後如果有任何 Trigger 中會 Insert 在其他 Table 的話, @@IDENTITY 就會變得不準確. [...]]]></description>
			<content:encoded><![CDATA[<p>MsSQL 內既 Identity Generator, 雖然被發現當 Table 儲存超過一百萬行時, 由 SELECT @@Identity 或者 SELECT SCOPE_IDENTITY() 會存在 Return 值錯誤既問題, 但經過呢個幾月既開發都未出現問題.</p>
<p>係 MsSQL 入面, 新增 Record 時主要提取 Primary Key 既方法主要有 @@IDENTITY, SCOPE_IDENTY() 同 IDENT_CURRENT(&#8216;table_name&#8217;) 三種:</p>
<p><strong>@@IDENTITY</strong><br />
使用 @@IDENTITY 會 Return 當前 Session 任何 Table 最後生產的 Primary Key, 如果 Insert statement 運行後如果有任何 Trigger 中會 Insert 在其他 Table 的話, @@IDENTITY 就會變得不準確.</p>
<p><strong>SCOPE_IDENTY()</strong><br />
使用 SCOPE_IDENTY() 會 Return 當前 Scope 內確實 Insert 左既 Primary Key, 就算有任何 Trigger 在背後運行都不會有任何影響, 係一個最好既選擇.  不過經由 MS Connect 入面既 <a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=328811">Feedback</a> 顯示, Table 被新增至一百萬行後, Return Value 有可以會籨 1 開始數起, 這問題由 MS 回應既答案中回答到已增加到它們的 Bug tracking database 中.</p>
<p><strong>IDENT_CURRENT(&#8216;table_name&#8217;)</strong><br />
而 IDENT_CURRENT(&#8216;table_name&#8217;) 會 Return 指定 Table 最後生產出來的 Identity, 由於它不會理會任何 Scope 或者 Session, 當有多於一個 Scope 或者 Session 運行時便有機會出現不準確既問題.</p>
<p>因此, 在以上三個尋找 IDENTITY 既方法中, 並沒有一個能在任何環境下百分百準確既方法, 所以在 MsSQL 使用 Identity Generator 便會有潛在問題既可能性, 因此在 deploy 前應該對 Database 作詳細測試, 已確定當前既環境設定正確.</p>
<p>再者, Identity Generator 亦開始慢慢地被其他類型既方案取代, 例如 <a href="http://en.wikipedia.org/wiki/Globally_Unique_Identifier">GUID</a>, Hi/Lo or UUID 等等.</p>
<p>正如之前 <a href="http://wongkalun.idv.hk/2009/06/15/software-engineering-does-not-equal-computer-science/">CS != SE</a> 文章內所講, Technology 其實永遠唔會有錯, 錯就只會錯在未能正確使用它們既人, 而軟件其中一個用途就係用來避免人們使用犯錯, 所以在程式篇寫時應該以不同既設定技巧及文檔來避免人們使用錯誤, 那軟件才容易正確地被使用!!</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/&amp;title=MsSQL+-+Identity+Generator" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/&amp;title=MsSQL+-+Identity+Generator" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/&amp;title=MsSQL+-+Identity+Generator&amp;desc=MsSQL%20%E5%85%A7%E6%97%A2%20Identity%20Generator%2C%20%E9%9B%96%E7%84%B6%E8%A2%AB%E7%99%BC%E7%8F%BE%E7%95%B6%20Table%20%E5%84%B2%E5%AD%98%E8%B6%85%E9%81%8E%E4%B8%80%E7%99%BE%E8%90%AC%E8%A1%8C%E6%99%82%2C%20%E7%94%B1%20SELECT%20%40%40Identity%20%E6%88%96%E8%80%85%20SELECT%20SCOPE_IDENTITY%28%29%20%E6%9C%83%E5%AD%98%E5%9C%A8%20Return%20%E5%80%BC%E9%8C%AF%E8%AA%A4%E6%97%A2%E5%95%8F%E9%A1%8C%2C%20%E4%BD%86%E7%B6%93%E9%81%8E%E5%91%A2%E5%80%8B%E5%B9%BE%E6%9C%88%E6%97%A2%E9%96%8B%E7%99%BC%E9%83%BD%E6%9C%AA%E5%87%BA%E7%8F%BE%E5%95%8F%E9%A1%8C.%0D%0A%0D%0A%E4%BF%82%20MsSQL%20%E5%85%A5%E9%9D%A2%2C%20%E6%96%B0%E5%A2%9E%20Record%20%E6%99%82%E4%B8%BB%E8%A6%81%E6%8F%90%E5%8F%96%20Primary%20Key%20%E6%97%A2%E6%96%B9%E6%B3%95%E4" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/&amp;bm_description=MsSQL+-+Identity+Generator&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/&amp;title=MsSQL+-+Identity+Generator" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/&amp;title=MsSQL+-+Identity+Generator" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/&amp;title=MsSQL+-+Identity+Generator" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=MsSQL+-+Identity+Generator+-+http://bit.ly/bfdfwd&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_629_permalink = 'http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/';
			dtsv.dtse_post_629_title = 'MsSQL – Identity Generator';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2009/07/07/mssql-identity-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Avoid identity generator when possible</title>
		<link>http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/</link>
		<comments>http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 15:50:11 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[diary]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[mssql]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=440</guid>
		<description><![CDATA[係 Ayende 個 Blog 度見到呢個 post, 入面講述應該避免使用 identity. 由 Dave 於上年二月回報 M$ 既一個 feedback 中提及, 只要新增多個一百萬行數據, 個 identity 就會被 reset 做 1. 雖然我未測試過係咪真係有呢個情況, 但該文章中已經有五個人証明左問題既存在, 而我計算過, 如果在同一個 table 入面, 一日新增五百行數據的話, 只要五年多既時間便會去到一百萬行. 五年, 雖然我以前跟開既項目都有三年一次既資料清理, 但亦有機會有每日多於五百行或者五年不清理既情況發生. 以我在網上既搜尋下, 而家外國都開始使用 Guid, 除了同 database 有關既連結外, 我見過 M$ 既 WF Example 入面已經全部使用了 Guid, 我諗有時間既情況下都要了解一下 Guid 既好處同壞處. Subscribe to the comments for this [...]]]></description>
			<content:encoded><![CDATA[<p>係 Ayende 個 Blog 度見到呢個 <a href="http://ayende.com/Blog/archive/2009/03/20/nhibernate-avoid-identity-generator-when-possible.aspx">post</a>, 入面講述應該避免使用 identity.<br />
<span id="more-440"></span><br />
由 Dave 於上年二月回報 M$ 既一個 <a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=328811">feedback</a> 中提及, 只要新增多個一百萬行數據, 個 identity 就會被 reset 做 1.</p>
<p>雖然我未測試過係咪真係有呢個情況, 但該文章中已經有<strong>五個人</strong>証明左問題既存在, 而我計算過, 如果在同一個 table 入面, 一日新增五百行數據的話, 只要五年多既時間便會去到一百萬行.  五年, 雖然我以前跟開既項目都有三年一次既資料清理, 但亦有機會有每日多於五百行或者五年不清理既情況發生.</p>
<p>以我在網上既搜尋下, 而家外國都開始使用 <a href="http://en.wikipedia.org/wiki/Globally_Unique_Identifier">Guid</a>, 除了同 database 有關既連結外, 我見過 M$ 既 <a href="http://msdn.microsoft.com/en-us/library/ms741723.aspx">WF Example</a> 入面已經全部使用了 <a href="http://msdn.microsoft.com/en-us/library/system.guid.aspx">Guid</a>, 我諗有時間既情況下都要了解一下 Guid 既好處同壞處.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/&amp;title=Avoid+identity+generator+when+possible" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/&amp;title=Avoid+identity+generator+when+possible" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/&amp;title=Avoid+identity+generator+when+possible&amp;desc=%E4%BF%82%20Ayende%20%E5%80%8B%20Blog%20%E5%BA%A6%E8%A6%8B%E5%88%B0%E5%91%A2%E5%80%8B%20post%2C%20%E5%85%A5%E9%9D%A2%E8%AC%9B%E8%BF%B0%E6%87%89%E8%A9%B2%E9%81%BF%E5%85%8D%E4%BD%BF%E7%94%A8%20identity.%0D%0A%0D%0A%E7%94%B1%20Dave%20%E6%96%BC%E4%B8%8A%E5%B9%B4%E4%BA%8C%E6%9C%88%E5%9B%9E%E5%A0%B1%20M%24%20%E6%97%A2%E4%B8%80%E5%80%8B%20feedback%20%E4%B8%AD%E6%8F%90%E5%8F%8A%2C%20%E5%8F%AA%E8%A6%81%E6%96%B0%E5%A2%9E%E5%A4%9A%E5%80%8B%E4%B8%80%E7%99%BE%E8%90%AC%E8%A1%8C%E6%95%B8%E6%93%9A%2C%20%E5%80%8B%20identity%20%E5%B0%B1%E6%9C%83%E8%A2%AB%20reset%20%E5%81%9A%201.%0D%0A%0D%0A%E9%9B%96%E7%84%B6%E6%88%91%E6%9C%AA%E6%B8%AC%E8%A9%A6%E9%81%8E%E4%BF%82%E5%92%AA%E7%9C%9F%E4%BF%82%E6%9C%89%E5%91%A2%E5%80%8B%E6%83%85%E6%B3%81%2C%20%E4%BD%86%E8%A9%B2%E6%96%87%E7%AB%A0%E4%B8%AD%E5%B7%B2%E7%B6%93%E6%9C" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/&amp;bm_description=Avoid+identity+generator+when+possible&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/&amp;title=Avoid+identity+generator+when+possible" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/&amp;title=Avoid+identity+generator+when+possible" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/&amp;title=Avoid+identity+generator+when+possible" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Avoid+identity+generator+when+possible+-+http://bit.ly/aNH6NX&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_440_permalink = 'http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/';
			dtsv.dtse_post_440_title = 'Avoid identity generator when possible';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2009/04/15/avoid-identity-generator-when-possible/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
