<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">博客园_SQL Server</title><subtitle type="text"/><id>http://feed.cnblogs.com/blog/u/25473/rss</id><updated>2012-05-31T02:32:50Z</updated><author><name>nzperfect</name><uri>http://www.cnblogs.com/nzperfect/</uri></author><generator>feed.cnblogs.com</generator><link rel="alternate" type="text/html" href="http://www.cnblogs.com/nzperfect/"/><link rel="self" type="application/atom+xml" href="http://feed.cnblogs.com/blog/u/25473/rss"/><entry><id>http://www.cnblogs.com/nzperfect/archive/2012/04/24/2467689.html</id><title type="text">MEMORYCLERK_SQLOPTIMIZER占用内存过高带来的性能问题</title><summary type="text">记录一下之前遇到过两次服务器由于MEMORYCLERK_SQLOPTIMIZER占用内存过高带来的性能问题。症状：SQL Server突然变的特别慢，无论是读还是写。检查：如下图，可以看到当前正在进行的进程，等待类型是RESOURCE_SEMAPHORE，初步判断是内存不足造成；接下来我们查看服务器内存配置：可以看到SQL可用内存为29GB，实用29GB，没有问题，再继续，可以看到Single_page占到了27GB左右，这表明DataPage只占到了2GB，明显存在问题；再接下，我们查看SinglePage内存占用情况，发现MEMORYCLERK_SQLOPTIMIZER占用了27GB左右；</summary><published>2012-04-24T03:08:00Z</published><updated>2012-04-24T03:08:00Z</updated><author><name>nzperfect</name><uri>http://www.cnblogs.com/nzperfect/</uri></author><link rel="alternate" href="http://www.cnblogs.com/nzperfect/archive/2012/04/24/2467689.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/nzperfect/archive/2012/04/24/2467689.html"/><content type="html">&lt;p&gt;记录一下之前遇到过两次服务器由于MEMORYCLERK_SQLOPTIMIZER占用内存过高带来的性能问题。&lt;br /&gt;&lt;br /&gt;症状：&lt;br /&gt;SQL Server突然变的特别慢，无论是读还是写。&lt;br /&gt;&lt;br /&gt;检查：&lt;br /&gt;如下图，可以看到当前正在进行的进程，等待类型是RESOURCE_SEMAPHORE，初步判断是内存不足造成；&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012050213081565.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;接下来我们查看服务器内存配置：&lt;br /&gt;可以看到SQL可用内存为29GB，实用29GB，没有问题，再继续，可以看到Single_page占到了27GB左右，这表明DataPage只占到了2GB，明显存在问题；&lt;br /&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012042410513818.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;再接下，我们查看SinglePage内存占用情况，发现MEMORYCLERK_SQLOPTIMIZER占用了27GB左右；&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012042410514812.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;可以基本确认是由于MEMORYCLERK_SQLOPTIMIZER占用内存太多造成。&lt;br /&gt;临时解决：重启SQLService.&lt;br /&gt;原因猜测应该是由于提交了大批量的SQL造成，重现做要时间，暂未找到具体SQL.&lt;/p&gt;&lt;img src="http://www.cnblogs.com/nzperfect/aggbug/2467689.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/nzperfect/archive/2012/04/24/2467689.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/nzperfect/archive/2012/03/21/2409975.html</id><title type="text">DBMIRROR_DBM_EVENT</title><summary type="text">原文：http://blogs.msdn.com/b/grahamk/archive/2011/01/10/sql-server-blocking-caused-by-database-mirroring-wait-type-dbmirror-dbm-event.aspxI was looking at a problem this morning about long blocking chains caused by the wait type DBMIRROR_DBM_EVENT, and it makes an interesting case study about database</summary><published>2012-03-21T07:23:00Z</published><updated>2012-03-21T07:23:00Z</updated><author><name>nzperfect</name><uri>http://www.cnblogs.com/nzperfect/</uri></author><link rel="alternate" href="http://www.cnblogs.com/nzperfect/archive/2012/03/21/2409975.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/nzperfect/archive/2012/03/21/2409975.html"/><content type="html">&lt;p&gt;原文：&lt;a href="http://blogs.msdn.com/b/grahamk/archive/2011/01/10/sql-server-blocking-caused-by-database-mirroring-wait-type-dbmirror-dbm-event.aspx" target="_blank"&gt;http://blogs.msdn.com/b/grahamk/archive/2011/01/10/sql-server-blocking-caused-by-database-mirroring-wait-type-dbmirror-dbm-event.aspx&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;I was looking at a problem this morning about long blocking chains caused by the wait type DBMIRROR_DBM_EVENT, and it makes an interesting case study about database mirroring, blocking, undocumented wait types and general troubleshooting techniques for database mirroring. The information following relates ONLY to SQL 2005, SQL 2008 and SQL 2008 R2. However since the undocumented wait type is not supported, it is not guaranteed to be relevant to future versions higher than SQL 2008 R2 and considering the enhancements coming in this area of functionality, it may well be completely different.&lt;/p&gt;&lt;p&gt;First off some thoughts about looking at root cause analysis of database mirroring problems. We see quite a few of these and often people do not have the appropriate data with which is make a full analysis. Typical examples being unexpected failovers, slow performance and sometimes blocking. Obviously the more data you have the easier, but some of key parts for looking at DBM problems historically are this:&lt;/p&gt;&lt;p&gt;The perfmon counters in the SQLServer:Database Mirroring and SQLServer:Service Broker objects.&lt;/p&gt;&lt;p&gt;Default profiler trace files (people often forget these but they have very useful DBM state change audit information in them)&lt;/p&gt;&lt;p&gt;SQL Server Error Logs&lt;/p&gt;&lt;p&gt;windows event logs&lt;/p&gt;&lt;p&gt;backup of msdb database (this contains useful meta data such as the size of every log backup and the DBM monitor meta data if you've enabled it)&lt;/p&gt;&lt;p&gt;memory dumps (if any were produced)&lt;/p&gt;&lt;p&gt;Obviously if you've also proactively collected MPSReports or SQLDIAG as well, then this will be no bad thing. Back to my specific scenario.....&lt;/p&gt;&lt;p&gt;In this example the symptom was that a huge blocking chain built on a high volume OLTP database where all the head blockers were waiting on DBMIRROR_DBM_EVENT. Here is the sequence of events I went through:&lt;/p&gt;&lt;p&gt;1. Review the blocking chain itself - ho help here as all we can see is that we're waiting on DBMIRROR_DBM_EVENT&lt;/p&gt;&lt;p&gt;2. Review the source for the undocumented wait type. Obviously you can't do this outside of MS, but I can say that at the time of writing this wait type represents the wait used when the principal is waiting for the mirror to harden an LSN, meaning that the transaction it's part of cannot commit. This immediately points quite specifically to the problem that the principal cannot commit transactions as it's waiting on the mirror. Now we need to investigate why the mirror is not committing transactions or why the principal doesn't know whether it is.&lt;/p&gt;&lt;p&gt;3. Review the msdb system tables&lt;/p&gt;&lt;p&gt;(a) Look at the [backupset] table to see if the size of the logs produced at the time of the problem are significantly higher then normal. If they were exceptionally large it may be that the mirror was flooded with transactions and could simply not keep up with the volume. This is why books online will tell you sometimes to disable mirroring if you need to do an exceptionally large logged operation such as an index rebuild. (reference for why this is at &lt;a href="http://technet.microsoft.com/en-us/library/cc917681.aspx"&gt;http://technet.microsoft.com/en-us/library/cc917681.aspx&lt;/a&gt;). Here i used the following TSQL&lt;/p&gt;&lt;p&gt;SELECT backup_set_id,backup_start_date,database_name,has_bulk_logged_data,backup_size / 1000 &lt;br /&gt;FROM [backupset] &lt;br /&gt;where backup_start_date between '2011-01-05 14:00:00' and '2011-01-05 19:30:00' &lt;br /&gt;go&lt;/p&gt;&lt;p&gt;select round((AVG(backup_size)/1000),0) &lt;br /&gt;FROM [backupset] &lt;br /&gt;where database_name = 'mydatabase'&lt;/p&gt;&lt;p&gt;(b) secondly I looked at the data in the tables [dbm_monitor_data]. The key here is to locate the timeframe in which we had a problem and then see if we were significant experiencing changes in any of the following:&lt;/p&gt;&lt;p&gt;log_flush_rate &lt;br /&gt;send_queue_size &lt;br /&gt;send_rate &lt;br /&gt;redo_queue_size &lt;br /&gt;redo_rate&lt;/p&gt;&lt;p&gt;These are all indicators similar to part (a) in that they might show a component or piece of architecture that wasn't responding. For example if the send_queue suddenly starts to grow but the re_do queue doesn't grow, then it would imply that the the principal cannot send the log records to the mirror so you'd want to look at connectivity maybe, or the service broker queues dealing with the actual transmissions.&lt;/p&gt;&lt;p&gt;In this particular scenario we noted that all the counters appeared to have strange values, in that there were log backups going on of normal sizes, but there were no status changes, 0 send queue, 0 redo queue, a flat send rate and a flat redo rate. This is very strange as it implies that the DBM Monitor could not record any values from anywhere over the problem period.&lt;/p&gt;&lt;p&gt;4. Review the SQL Server error logs. In this case there were no errors or information messages whatsoever, but in other scenarios such as this, it&amp;rsquo;s very common for errors in the 1400 range to be reported, examples of which you can find in other places in my other mirroring blogs, such as this &lt;a href="http://blogs.msdn.com/b/grahamk/archive/2008/05/09/1413-error-when-starting-database-mirroring-how-many-virtual-log-files-is-too-many.aspx"&gt;Error 1413 example&lt;/a&gt;&lt;/p&gt;&lt;p&gt;5. Review the default trace files &amp;ndash; in this scenario I was not provided the default traces, however they are fantastic sources of DBM problem information, as they record state change events on all the partners.This is documented here:&lt;/p&gt;&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms191502%28v=SQL.100%29.aspx"&gt;Database Mirroring State Change Event Class&lt;/a&gt;&lt;/p&gt;&lt;p&gt;This often gives you a great picture of scenarios such as when network connectivity failed between one or all of the partners and then what the state of the partnership became afterwards.&lt;/p&gt;&lt;p&gt;CONCLUSIONS:&lt;/p&gt;&lt;p&gt;In this particular scenario I&amp;rsquo;m currently missing 2 key points of data, but that apart I can still make a reasonable hypothesis on the above information. We certainly can say that the blocking was caused by the fact that DBM was enabled to the due the blockers all waiting on the DBMIRROR_DBM_EVENT wait type. Since we know we didn&amp;rsquo;t flood the mirror with a large logged operation and that this deployment normally runs happily in this mode, we can exclude unusual large operations. This means that we have 2 potential candidates at this stage:&lt;/p&gt;&lt;p&gt;1. Hardware problems on the connectivity between some or all of the partners.&lt;/p&gt;&lt;p&gt;2. CPU exhaustion on the mirror server &amp;ndash; simply unable to keep up with redos &amp;ndash; the CPU exhaustion could itself be from a process outside of SQL Server or outside of the this mirror partnership.&lt;/p&gt;&lt;p&gt;3. A problem with the mirroring code itself (we&amp;rsquo;d really need some memory dumps to confirm this though).&lt;/p&gt;&lt;p&gt;Based upon experience I&amp;rsquo;d suspect 1 or 2, but I always keep an open mind about 3 as well, we&amp;rsquo;re trying to collect some more data now to look at this problem in more detail.&lt;/p&gt;&lt;p&gt;As a final note though I noted some similar scenarios on the forums, and one particular one of note where the poster said that everything worked fine until they enabled mirroring, at which point the system ground to a halt with this wait type. This is far more likely to be a non-optimal configuration where some section of the architecture can simply not keep up with the volume of requests required by the partnership, and is a perfect example of why one must benchmark mirror deployments very carefully. In that scenario you should review this KB first : &lt;a title="http://support.microsoft.com/kb/2001270" href="http://support.microsoft.com/kb/2001270"&gt;http://support.microsoft.com/kb/2001270&lt;/a&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/nzperfect/aggbug/2409975.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/nzperfect/archive/2012/03/21/2409975.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/nzperfect/archive/2012/03/12/2392013.html</id><title type="text">SQL Server 2008 Uninstall - Restart Computer Failure</title><summary type="text">Today, I uninstalled the SQL Server 2008 on Windows Server 2003 and encountered an issue with "Restart computer", But restart computer many times, it was still this error.Finally, I got it from "http://ntsblog.homedev.com.au/index.php/2010/05/05/sql-server-2008-uninstall-restart-compu</summary><published>2012-03-12T08:30:00Z</published><updated>2012-03-12T08:30:00Z</updated><author><name>nzperfect</name><uri>http://www.cnblogs.com/nzperfect/</uri></author><link rel="alternate" href="http://www.cnblogs.com/nzperfect/archive/2012/03/12/2392013.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/nzperfect/archive/2012/03/12/2392013.html"/><content type="html">&lt;p&gt;Today, I uninstalled the SQL Server 2008 on Windows Server 2003 and encountered an issue with "Restart computer", But restart computer many times, it was still this error.&lt;br /&gt;Finally, I got it from "&lt;a href="http://ntsblog.homedev.com.au/index.php/2010/05/05/sql-server-2008-uninstall-restart-computer-failure/" target="_blank"&gt;http://ntsblog.homedev.com.au/index.php/2010/05/05/sql-server-2008-uninstall-restart-computer-failure/&lt;/a&gt;"&lt;/p&gt;&lt;p&gt;To resolve that need to do below steps:&lt;br /&gt;1.Leave the "Setup Support Rule" UI open if you are already in there&lt;br /&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012031216094937.png" alt="" /&gt;&lt;br /&gt;2.Delete "PendingFileRenameOperations&amp;rdquo;" from key&amp;rdquo;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager&amp;rdquo;&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012031216100072.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;3.Reclick "Re-run".&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now It does work well.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/nzperfect/aggbug/2392013.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/nzperfect/archive/2012/03/12/2392013.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/nzperfect/archive/2012/03/08/2384783.html</id><title type="text">SQL Server 2008 install SP3后，发布数据库log变得越来越大</title><summary type="text">起因：前一段时间在为一组sql server 2008 failover cluster安装sp3，之前是sq1.windows server 2003 enterprise 64bit + sql server 2008 enterprise 64bitfailover cluster 是active/acitve 双实例问题：在两个节点都install sp3后，发现有一个sql server实例在切换时（其实也就是在restart sql server service)，用SSMS连接时会显示“服务器处于脚本升级模式。此时只有管理员可以进行连接”，大概几分钟，之后就正常了，当时就并未重视</summary><published>2012-03-08T02:15:00Z</published><updated>2012-03-08T02:15:00Z</updated><author><name>nzperfect</name><uri>http://www.cnblogs.com/nzperfect/</uri></author><link rel="alternate" href="http://www.cnblogs.com/nzperfect/archive/2012/03/08/2384783.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/nzperfect/archive/2012/03/08/2384783.html"/><content type="html">&lt;p&gt;&lt;strong&gt;起因：&lt;/strong&gt;&lt;br /&gt;前一段时间在为一组sql server 2008 failover cluster安装sp3，之前是sq1.&lt;br /&gt;windows server 2003 enterprise 64bit + sql server 2008 enterprise 64bit&lt;br /&gt;failover cluster 是active/acitve 双实例&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;问题：&lt;/strong&gt;&lt;br /&gt;在两个节点都install sp3后，发现有一个sql server实例在切换时（其实也就是在restart sql server service)，用SSMS连接时会显示&amp;ldquo;服务器处于脚本升级模式。此时只有管理员可以进行连接&amp;rdquo;，大概几分钟，之后就正常了，当时就并未重视。过了段时间，在例行维护切换时，问题重现，此时意识到之前安装sp3的时候应该存在问题。最近发现该实例上一个发布的库，log文件增长到了近30GB，于是考虑收缩该Log文件。但在收缩时，发现尽管不停的备份log，仍然无法收缩Log文件。&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;检查：&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;通过查看该库的log_reuse_wait_desc，发现值为ACTIVE_TRANSACTION&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012030810430084.png" alt="" /&gt;&lt;br /&gt;&lt;br /&gt;于是查看虚拟日志文件使用情况，发现有2000多个虚拟日志文件都为活动状态&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012030810430836.png" alt="" /&gt;&lt;br /&gt;&lt;br /&gt;查看活动的事务信息，发现一个名为tran_sp_mscreate_peer_tables自从2012.02.02 晚10点43起一起到现在，而这个时间是上次切换该cluster时间;&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012030810431626.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;近一步验证，transaction_type=1标明是写操作；&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012030810432724.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;从sql server errorlog内发现&lt;span style="color: #ff0000;"&gt;Error executing sp_vupgrade_replication&lt;/span&gt;.&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012030810433472.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;推断应该是在生升发布订阅相关对象时失败，导致事务尽管已被发布到分发，但无法标记为已分发，也就始终无法截断它们并重用。&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;解决：&lt;/strong&gt;&lt;br /&gt;目前google到的方法大概是：&lt;br /&gt;1.disabled sql agent service&lt;br /&gt;2.stop sql server service and sql agent service&lt;br /&gt;3.only start sql server service&lt;br /&gt;4.execute sp_vupgrade_replication&lt;br /&gt;5.restart sql server service&lt;br /&gt;6.enable and start sql agent service.&lt;/p&gt;&lt;p&gt;在操作过程中，sql server 处于脚本升级模式，不可以连接。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;2012.03.09补充：经邮件咨询微软支持组，得到官方解决方法：&lt;br /&gt;&lt;strong&gt;官方解决方案：&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://support.microsoft.com/kb/2509302"&gt;http://support.microsoft.com/kb/2509302&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;相关讨论：&lt;a href="http://social.msdn.microsoft.com/Forums/en-US/sqlsetupandupgrade/thread/b77799d2-a931-467a-928d-49426d83d5ce/" target="_blank"&gt;http://social.msdn.microsoft.com/Forums/en-US/sqlsetupandupgrade/thread/b77799d2-a931-467a-928d-49426d83d5ce/&lt;/a&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/nzperfect/aggbug/2384783.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/nzperfect/archive/2012/03/08/2384783.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/nzperfect/archive/2012/03/07/2383437.html</id><title type="text">SQL Server 2012 正式发布</title><summary type="text">SQL Server 2012 正式发布 2012.3.7pls refer:https://www.microsoft.com/sqlserver/en/us/default.aspxhttp://blogs.technet.com/b/dataplatforminsider/archive/2012/03/06/sql-server-2012-released-to-manufacturing.aspxhttps://www.microsoft.com/sqlserver/en/us/get-sql-server/try-it.aspx下载sql server 2012</summary><published>2012-03-07T06:02:00Z</published><updated>2012-03-07T06:02:00Z</updated><author><name>nzperfect</name><uri>http://www.cnblogs.com/nzperfect/</uri></author><link rel="alternate" href="http://www.cnblogs.com/nzperfect/archive/2012/03/07/2383437.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/nzperfect/archive/2012/03/07/2383437.html"/><content type="html">&lt;p&gt;SQL Server 2012 正式发布 2012.3.7&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012030713573637.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012030713575278.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012030713580343.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;pls refer:&lt;br /&gt;&lt;a href="https://www.microsoft.com/sqlserver/en/us/default.aspx" target="_blank"&gt;https://www.microsoft.com/sqlserver/en/us/default.aspx&lt;/a&gt;&lt;br /&gt;&lt;a href="http://blogs.technet.com/b/dataplatforminsider/archive/2012/03/06/sql-server-2012-released-to-manufacturing.aspx" target="_blank"&gt;http://blogs.technet.com/b/dataplatforminsider/archive/2012/03/06/sql-server-2012-released-to-manufacturing.aspx&lt;/a&gt;&lt;br /&gt;&lt;a href="https://www.microsoft.com/sqlserver/en/us/get-sql-server/try-it.aspx" target="_blank"&gt;https://www.microsoft.com/sqlserver/en/us/get-sql-server/try-it.aspx&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=29066" target="_blank"&gt;&lt;br /&gt;下载sql server 2012&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/nzperfect/aggbug/2383437.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/nzperfect/archive/2012/03/07/2383437.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/nzperfect/archive/2012/02/21/2360887.html</id><title type="text">[分享]系统crash后SQL Server 在recovery时的rollback机制</title><summary type="text">来自：msdn论坛贴子创建一个测试表TESTTABLE,并且插入如下两行数据:insertinto TESTTABLE values (1, 'aaaa')insertinto TESTTABLE values (2, 'bbbb')然后做如下的操作。update TESTTABLEset myName = 'cccc' where myID = 2为了更加直观化，我们可以用一些日志工具如ApexSQL Log (从http://www.apexsql.com/ ) 可以下载来观察一下日志的结构。这时候，我们可以看到，update的语句包含了ol</summary><published>2012-02-21T02:16:00Z</published><updated>2012-02-21T02:16:00Z</updated><author><name>nzperfect</name><uri>http://www.cnblogs.com/nzperfect/</uri></author><link rel="alternate" href="http://www.cnblogs.com/nzperfect/archive/2012/02/21/2360887.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/nzperfect/archive/2012/02/21/2360887.html"/><content type="html">&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;来自：&lt;a href="http://social.microsoft.com/Forums/zh-CN/sqlserverzhchs/thread/4e685bd8-6d0b-4bd5-a317-52ebb85a9078" target="_blank"&gt;msdn论坛贴子&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;创建一个测试表&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;TESTTABLE,&lt;/span&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;并且插入如下两行数据&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;insert&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;into&lt;/span&gt; TESTTABLE &lt;span style="color: blue;"&gt;values &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;1&lt;span style="color: gray;"&gt;,&lt;/span&gt; &lt;span style="color: red;"&gt;'aaaa'&lt;/span&gt;&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;insert&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;into&lt;/span&gt; TESTTABLE &lt;span style="color: blue;"&gt;values &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;2&lt;span style="color: gray;"&gt;,&lt;/span&gt; &lt;span style="color: red;"&gt;'bbbb'&lt;/span&gt;&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: 宋体; color: gray; font-size: 10pt;" lang="ZH-CN"&gt;然后做如下的操作。&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: 'Courier New'; color: blue; font-size: 10pt;"&gt;update&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt; TESTTABLE&lt;span style="color: blue;"&gt;set&lt;/span&gt; myName &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: red;"&gt;'cccc'&lt;/span&gt; &lt;span style="color: blue;"&gt;where&lt;/span&gt; myID &lt;span style="color: gray;"&gt;=&lt;/span&gt; 2&lt;br /&gt;&lt;br /&gt;为了更加直观化，我们可以用一些日志工具如&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;ApexSQL Log (&lt;/span&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;从&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;&lt;a title="http://www.apexsql.com/CTRL + 单击以下链接" href="http://www.apexsql.com/"&gt;http://www.apexsql.com/&lt;/a&gt; ) &lt;/span&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;可以下载来观察一下日志的结构。&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;这时候，我们可以看到，&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;update&lt;/span&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;的语句包含了&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;oldValue&lt;/span&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;和&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;newValue&lt;/span&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;，所以，在做&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;rollback&lt;/span&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;的时候，因为有了&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;oldValue&lt;/span&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;，所以所谓的恢复就是把&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;value&lt;/span&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;改回为&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;oldValue&lt;/span&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;即可&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012022110121840.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;同时，我们也可以看到相应的&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt;UNDO Script&lt;/span&gt;.&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012022110130246.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: 宋体; color: #1f497d;" lang="ZH-CN"&gt;和&lt;/span&gt;&lt;span style="font-family: 'Calibri','sans-serif'; color: #1f497d;"&gt; REDO script:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/21914/2012022110132149.png" alt="" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #1f497d;"&gt;只是记录oldValue和newValue，而不是记录那个值对应的整个oldpage跟newpage。&lt;/span&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/nzperfect/aggbug/2360887.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/nzperfect/archive/2012/02/21/2360887.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/nzperfect/archive/2012/02/17/2355716.html</id><title type="text">4.0 Query Parameterization</title><summary type="text">原文：http://blogs.msdn.com/b/sqlprogrammability/archive/2007/01/11/4-0-query-parameterization.aspxUsing parameters or parameter markers in queries increases the ability of SQL Server to reuse compiled plans. There are two places where parameterization of queries can be done: on the client side applica</summary><published>2012-02-17T06:11:00Z</published><updated>2012-02-17T06:11:00Z</updated><author><name>nzperfect</name><uri>http://www.cnblogs.com/nzperfect/</uri></author><link rel="alternate" href="http://www.cnblogs.com/nzperfect/archive/2012/02/17/2355716.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/nzperfect/archive/2012/02/17/2355716.html"/><content type="html">&lt;p&gt;原文：&lt;a href="http://blogs.msdn.com/b/sqlprogrammability/archive/2007/01/11/4-0-query-parameterization.aspx" target="_blank"&gt;http://blogs.msdn.com/b/sqlprogrammability/archive/2007/01/11/4-0-query-parameterization.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Using parameters or parameter markers in queries increases the ability of SQL Server to reuse compiled plans. There are two places where parameterization of queries can be done: on the client side application (or mid tier) or on the server side. &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;4.1 Client Side Parameterization&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Client side parameterization (via ADO.NET, ODBC, or sp_executesql) is the recommended approach for query parameterization. Based on the degree of re-execution of queries (once versus multiple executions), client applications can decide to parameterize queries by specifying parameter holders, their data types and lengths in the queries. Therefore between query executions the only thing that changes is the actual value of parameters. This allows for re-use of one parameterized compiled plan. The server will not attempt to parameterize these queries further. It is important to parameterize queries completely and correctly in the client code in order to get the full benefits of parameterized queries. Consider the example below where the client submits a parameterized query using sp_executesql:&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;declare&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; @param_value &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; @sqlstring &lt;span style="color: blue;"&gt;nvarchar&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;500&lt;span style="color: gray;"&gt;),&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;@param_definition &lt;span style="color: blue;"&gt;nvarchar&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;500&lt;span style="color: gray;"&gt;),&lt;/span&gt; @col2 &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;set&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; @sqlstring &lt;span style="color: gray;"&gt;=&lt;/span&gt; N&lt;span style="color: red;"&gt;'select @col2_out = t1.col2 from t1 join t2 on t1.col1 = t2.col1 where t2.col2 = @param'&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;set&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; @param_definition &lt;span style="color: gray;"&gt;=&lt;/span&gt; N&lt;span style="color: red;"&gt;'@param int, @col2_out int output'&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;set&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; @param_value &lt;span style="color: gray;"&gt;=&lt;/span&gt; 5&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;exec&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; &lt;span style="color: maroon;"&gt;sp_executesql&lt;/span&gt; @sqlstring&lt;span style="color: gray;"&gt;,&lt;/span&gt; @param_definition&lt;span style="color: gray;"&gt;,&lt;/span&gt; @param &lt;span style="color: gray;"&gt;=&lt;/span&gt; @param_value&lt;span style="color: gray;"&gt;,&lt;/span&gt; @col2_out &lt;span style="color: gray;"&gt;=&lt;/span&gt; @col2 &lt;span style="color: blue;"&gt;output&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;set&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; @param_value &lt;span style="color: gray;"&gt;=&lt;/span&gt; 10&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;exec&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; &lt;span style="color: maroon;"&gt;sp_executesql&lt;/span&gt; @sqlstring&lt;span style="color: gray;"&gt;,&lt;/span&gt; @param_definition&lt;span style="color: gray;"&gt;,&lt;/span&gt; @param &lt;span style="color: gray;"&gt;=&lt;/span&gt; @param_value&lt;span style="color: gray;"&gt;,&lt;/span&gt; @col2_out &lt;span style="color: gray;"&gt;=&lt;/span&gt; @col2 &lt;span style="color: blue;"&gt;output&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;The adhoc query in this case is fully parameterized, and for different values of the parameter the same compiled plan is re-used which gives significant performance gains. The compiled plan is as below:&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;table id="table1" class="MsoNormalTable" style="border-width: medium; border-style: none; border-color: -moz-use-text-color; border-collapse: collapse; width: 403px;" border="1" cellspacing="0" cellpadding="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt; border-style: solid; border-color: silver; width: 94px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Text&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: silver silver silver -moz-use-text-color; width: 88px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Plan_handle &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: silver silver silver -moz-use-text-color; width: 96px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Cacheobjtype&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: silver silver silver -moz-use-text-color; width: 64px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Objtype&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color silver silver; width: 94px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;(@param int, @col2_out int output)select @col2_out = t1.col2 from t1 join t2 on t1.col1 = t2.col1 where t2.col2 = @param&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 88px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;0x06000100&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;6639AD2BB8&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;4181040000&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;0000000000&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;0000000000&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 96px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Compiled Plan&amp;nbsp; &lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 64px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Prepared&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;The next section describes the best practices for query parameterization and highlights how several common omissions in client side parameterization can manifest as performance degradations. &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;4.2 Server Side Parameterization: Simple Parameterization&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;If the query submitted for execution has no parameters specified, then SQL server attempts to parameterize this query. This process is called simple or auto parameterization. The parameterization of queries is done on the server side and is on by default. As the name simple parameterization suggests, only a relatively small set of queries do get parameterized using simple parameterization. Consider the following example:&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;select&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; col2 &lt;span style="color: blue;"&gt;from&lt;/span&gt; t1 &lt;span style="color: blue;"&gt;where&lt;/span&gt; col1 &lt;span style="color: gray;"&gt;&amp;gt;&lt;/span&gt; 5&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;go&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;select&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; col2 &lt;span style="color: blue;"&gt;from&lt;/span&gt; t1 &lt;span style="color: blue;"&gt;where&lt;/span&gt; col1 &lt;span style="color: gray;"&gt;&amp;gt;&lt;/span&gt; 6&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;go&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;The 2 queries only differ in the literal values in the where clause. When the first query is submitted for execution, SQL Server parameterizes the first and the entries in the cache look like:&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;table id="table2" class="MsoNormalTable" style="border-width: medium; border-style: none; border-color: -moz-use-text-color; border-collapse: collapse; width: 431px;" border="1" cellspacing="0" cellpadding="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt; border-style: solid; border-color: silver; width: 122px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Text&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: silver silver silver -moz-use-text-color; width: 88px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Plan_handle &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: silver silver silver -moz-use-text-color; width: 96px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Cacheobjtype&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: silver silver silver -moz-use-text-color; width: 64px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Objtype&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color silver silver; width: 122px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;select col2 from t1 where col1 &amp;gt; 5&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 88px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;0x06000100&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;87711124B8&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;614C040000&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;0000000000&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;0000000000&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 96px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Compiled Plan&amp;nbsp; &lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 64px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Adhoc&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color silver silver; width: 122px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;(@1 tinyint)SELECT [col2] FROM [t1] WHERE [col1]&amp;gt;@1&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 88px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;0x06000100&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;533AC103B8&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;81CF030000&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;0000000000&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;0000000000&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 96px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Compiled Plan&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 64px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Prepared&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;The second row contains what we call a parameterized query and has objtype of prepared. The parameterized (or prepared) query is the actual compiled plan for the query with the literal values replaced with parameters. The first row contains what we call the shell query and has objtype adhoc. As the name suggests, this entry just saves the exact adhoc batch text in cache and points to the parameterized query plan (the prepared compiled plan). In other words the shell query has a dummy compiled plan which just points to the parameterized compiled plan. The shell query is relatively small in size compared to the parameterized (or prepared query). &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;There are benefits in caching the shell query: If the same query were to be re-executed, then we would compute the hash value of the sql text of the query and find an exact match in the cache i.e. the shell query. Since this shell query points to the compiled plan, the compiled plan is executed and we are done. If we had not cached this shell query and if the same query was re-executed then the steps followed would be slightly different: first we would compute the hash of the sql text of the query and not find an exact match in the cache. Next, the query is auto-parameterized. Now for this auto-parameterized query we will search the cache and find an exact match in the cache avoiding the need to go to the query optimizer. Finally we execute this compiled plan and are done. Clearly there are performance gains from caching the shell query, especially for applications that re-execute the same query with the same literal values as well. Note that we do not cache insert shell queries because the probability of re-using the exact same adhoc query is low. &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;SQL Server attempts to auto-parameterize queries and sends the auto-parameterized query to the query processor. Now the query processor decides if it can generate a potentially better plan given the constant values instead of the auto-parameterized query. If the query processor cannot find a better plan with the constant value plugged in, it means that the parameterized plan can be used across different parameter values. This parameterization is considered &amp;lsquo;safe&amp;rsquo;. In this case we cache both the parameterized and the shell query. However if the query processor can generate a better plan with the literal values, then this parameterization is considered &amp;lsquo;unsafe&amp;rsquo;. The query is parameterized and the shell query is cached. The parameterized query is not cached and is not visible through any of the plan cache DMV&amp;rsquo;s. The shell query points to the parameterized query and this is the only way to get to the parameterized query. In other words, unless the exact same query (and parameter value) is re-executed there is no chance of re-using this &amp;lsquo;unsafe&amp;rsquo; parameterized query. Queries that already have explicitly defined parameters are not auto-parameterized. &amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;It is important to note that SQL Server will not even attempt to auto-parameterize certain types of queries. A non-exhaustive list of such queries with certain constructs is as below:&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;1.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;BULK INSERT&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;2.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;IN Expressions (Ex. IN (1, 2, 3))&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;3.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;UPDATE with SET clause that has variables&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;4.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;SELECT with UNION &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;5.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;SELECT with INTO&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;6.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;SELECT/UPDATE with FOR BROWSE&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;7.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;SELECT/UPDATE/DELETE with OPTION &amp;lt;query hints&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;8.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;SELECT with DISTINCT&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;9.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;SELECT/UPDATE/DELETE/INSERT with TOP&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;10.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;WAITFOR statements&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;11.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;SELECT with GROUP BY, HAVING, COMPUTE&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;12.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;DELETE/UPDATE with FROM CLAUSE&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;13.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;full text predicates&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;14.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Sub queries&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;15.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;FROM Clause of a SELECT statement has table valued method or full text table or OPENROWSET or OPENXML or OPENQUERY or OPENDATASOURCE&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;16.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Comparison predicate of the form EXPR &amp;lt;&amp;gt; a non null constant&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.25in;"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;17.&lt;span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: Times New Roman; font-variant: normal;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Queries with JOINS&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;A general rule of thumb would be that the server auto-parameterizes a query when only one feasible plan for that query is possible. It follows that when auto-parameterization is done, no cost based query optimization is performed by the query optimizer. &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;4.3 Server Side Parameterization: Forced Parameterization&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Clearly since simple parameterization encompasses a small subset of query types, a more powerful option called forced parameterization is available in SQL Server 2005. There are two levels of granularity with which forced parameterization can be enabled: for the entire database or at the query level. &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Between the 2 options of database and query level granularity, query level parameterization is the recommended approach. With this alternative, the benefits of parameterization are retained for specific queries and any potential performance problems that may emerge from applying it at the database level are eliminated. Forced parameterization at the query level can be specified using plan guides. Consider the example below:&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;select&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; t1&lt;span style="color: gray;"&gt;.&lt;/span&gt;col2&lt;span style="color: gray;"&gt;,&lt;/span&gt; t2&lt;span style="color: gray;"&gt;.&lt;/span&gt;col2 &lt;span style="color: blue;"&gt;from&lt;/span&gt; t1 &lt;span style="color: gray;"&gt;join&lt;/span&gt; t2 &lt;span style="color: blue;"&gt;on&lt;/span&gt; t1&lt;span style="color: gray;"&gt;.&lt;/span&gt;col1 &lt;span style="color: gray;"&gt;=&lt;/span&gt; t2&lt;span style="color: gray;"&gt;.&lt;/span&gt;col1 &lt;span style="color: gray;"&gt;and&lt;/span&gt; t1&lt;span style="color: gray;"&gt;.&lt;/span&gt;col2 &lt;span style="color: gray;"&gt;&amp;gt;&lt;/span&gt; 5&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;go&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;This query does not get parameterized when the database parameterized option is set to simple. Now let us create a plan guide for queries of this template that differ only in the where clause parameter value:&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;declare&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; @stmt &lt;span style="color: blue;"&gt;nvarchar&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia;"&gt;max&lt;/span&gt;&lt;span style="color: gray;"&gt;),&lt;/span&gt; @params &lt;span style="color: blue;"&gt;nvarchar&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia;"&gt;max&lt;/span&gt;&lt;span style="color: gray;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;exec&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; sp_get_query_template N&lt;span style="color: red;"&gt;'select t1.col2, t2.col2 from t1 join t2 on t1.col1 = t2.col1 and t1.col2 &amp;gt; 5'&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; @stmt &lt;span style="color: blue;"&gt;output&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; @params &lt;span style="color: blue;"&gt;output&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;exec&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; sp_create_plan_guide N&lt;span style="color: red;"&gt;'JoinGuide'&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; @stmt&lt;span style="color: gray;"&gt;,&lt;/span&gt; N&lt;span style="color: red;"&gt;'Template'&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; &lt;span style="color: gray;"&gt;NULL,&lt;/span&gt; @params&lt;span style="color: gray;"&gt;,&lt;/span&gt; N&lt;span style="color: red;"&gt;'OPTION(PARAMETERIZATION FORCED)'&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;go&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;select&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; t1&lt;span style="color: gray;"&gt;.&lt;/span&gt;col2&lt;span style="color: gray;"&gt;,&lt;/span&gt; t2&lt;span style="color: gray;"&gt;.&lt;/span&gt;col2 &lt;span style="color: blue;"&gt;from&lt;/span&gt; t1 &lt;span style="color: gray;"&gt;join&lt;/span&gt; t2 &lt;span style="color: blue;"&gt;on&lt;/span&gt; t1&lt;span style="color: gray;"&gt;.&lt;/span&gt;col1 &lt;span style="color: gray;"&gt;=&lt;/span&gt; t2&lt;span style="color: gray;"&gt;.&lt;/span&gt;col1 &lt;span style="color: gray;"&gt;and&lt;/span&gt; t1&lt;span style="color: gray;"&gt;.&lt;/span&gt;col2 &lt;span style="color: gray;"&gt;&amp;gt;&lt;/span&gt; 15&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;go&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Now if we look at the cached plans we have the parameterized and the shell queries cached.&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;table id="table3" class="MsoNormalTable" style="border-width: medium; border-style: none; border-color: -moz-use-text-color; border-collapse: collapse; width: 442px;" border="1" cellspacing="0" cellpadding="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt; border-style: solid; border-color: silver; width: 116px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Text&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: silver silver silver -moz-use-text-color; width: 102px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Plan_handle &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: silver silver silver -moz-use-text-color; width: 99px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Cacheobjtype&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: silver silver silver -moz-use-text-color; width: 64px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Objtype&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color silver silver; width: 116px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;select t1.col2, t2.col2 from t1 join t2 on t1.col1 = t2.col1 and t1.col2 &amp;gt; 15&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 102px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;0x060001009&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;DE67C01B8C1&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;B0040000000&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;00000000000&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;000000&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 99px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Compiled Plan&amp;nbsp; &lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 64px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Adhoc&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt; border-style: none solid solid; border-color: -moz-use-text-color silver silver; width: 116px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;(@0 int)select t1 . col2 , t2 . col2 from t1 join t2 on t1 . col1 = t2 . col1 and t1 . col2 &amp;gt; @0&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 102px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;0x060001009&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;647E335B881&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;70040000000&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;00000000000&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;000000&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 99px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Compiled Plan&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style="border-right: 1pt solid silver; padding: 0in 5.4pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color silver silver -moz-use-text-color; width: 64px;" valign="top"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;Prepared&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms189854.aspx"&gt;BOL&lt;/a&gt; has more information on plan guides.&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Next let us consider the database level option. Forced parameterization at the database level can be enabled using the alter database DDL: &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;alter database &amp;lt;database_name&amp;gt; set parameterization forced&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;go&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;For some databases with a high volume of queries it may significantly reduce the frequency of query compilations. In general, a broad class of DML queries are parameterized and literal values are replaced with parameters during query compilation. &lt;a style="color: blue; text-decoration: underline;" href="http://msdn2.microsoft.com/en-us/library/ms175037.aspx"&gt;BOL&lt;/a&gt; article &amp;lsquo;Forced Parameterization&amp;rsquo; has a list of query clauses and in literals in query constructs that will not be forced parameterized. &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;There are some factors to consider before turning on forced parameterization for a database. Examine these factors carefully along with query level forced parameterization options before turning on forced parameterization at the database level. Turning on (or off) forced parameterization flushes the plan cache of all entries, except those that were then compiling or executing. Plans that were then compiling or executing are parameterized the next time the query is executed. The database parameterization setting is preserved when re-attaching or restoring a database. Distributed queries may also be parameterized as long these queries are executing in a database where forced parameterization is enabled. Since parameterization replaces the literal values in the query with parameters, the query optimizer may very well choose a sub optimal plan. Specifically, the query optimizer is less likely to match the query to an index, indexed view or a computed column. It is also less likely to choose optimal plans for partitioned tables and distributed partitioned views. Thus forced parameterization can have some serious performance ramifications and should be used with caution.&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Note that it is possible to turn on forced parameterization at the database level and then use plan guides to specify simple parameterization for specific queries that suffer from poor performance due to forced parameterization. However the reverse approach i.e. specifying forced parameterization for specific queries, with the database parameterization option simple is what is recommended. &lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Using forced parameterization the performance problems associated with not re-using cached plans seems to have to been solved largely. However in some cases it can give rise to another set of problems emerging from parameter sniffing. Parameter sniffing can be explained as follows: If the parameter values are specified during the first execution of a query then the query optimizer attempts to pick the best plan based on the current parameter values provided. Parameter values are sniffed for stored procedure executions, queries submitted for sp_executesql, and prepared queries. While in most cases this works well, in some cases where the optimal plan for a query varies greatly with the range of parameter values, there can be some performance degradation. This is especially true if the first set of parameter values are atypical, because it causes a sub-optimal plan (for other parameter values) to be inserted into cache and re-used as well. Consider the following example: Sales table has three columns: transaction id, country id, and total_amount. We also know that majority of the sales occur in country with id = 1, and country with id = 2 has far fewer rows in the sales table. If we had a stored procedure that reported sales by country id as below:&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;create&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; &lt;span style="color: blue;"&gt;proc&lt;/span&gt; sales_report @country_id &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;as&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;select&lt;/span&gt; &lt;span style="color: gray;"&gt;*&lt;/span&gt; &lt;span style="color: blue;"&gt;from&lt;/span&gt; sales &lt;span style="color: blue;"&gt;where&lt;/span&gt; country_id &lt;span style="color: gray;"&gt;=&lt;/span&gt; @country_id&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;end&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;go&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;If we now executed the stored proc with country id 2 first, then the query_plan can be obtained by:&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;select&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; &lt;span style="color: blue;"&gt;text&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; query_plan&lt;span style="color: gray;"&gt;,&lt;/span&gt; cacheobjtype&lt;span style="color: gray;"&gt;,&lt;/span&gt; objtype&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;from&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; &lt;span style="color: green;"&gt;sys.dm_exec_cached_plans&lt;/span&gt; cp &lt;span style="color: gray;"&gt;cross&lt;/span&gt; &lt;span style="color: gray;"&gt;apply&lt;/span&gt; sys.dm_exec_sql_text&lt;span style="color: gray;"&gt;(&lt;/span&gt;plan_handle&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: gray; font-family: Courier New;"&gt;outer&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; &lt;span style="color: gray;"&gt;apply&lt;/span&gt; sys.dm_exec_query_plan&lt;span style="color: gray;"&gt;(&lt;/span&gt;plan_handle&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;where&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; cacheobjtype &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: red;"&gt;'Compiled Plan'&lt;/span&gt; &lt;span style="color: gray;"&gt;and&lt;/span&gt; objtype &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: red;"&gt;'Proc'&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: gray; font-family: Courier New;"&gt;and&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; &lt;span style="color: blue;"&gt;text&lt;/span&gt; &lt;span style="color: gray;"&gt;not&lt;/span&gt; &lt;span style="color: gray;"&gt;like&lt;/span&gt; &lt;span style="color: red;"&gt;'%select text%'&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;go&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;The query plan shows that the query optimizer does an index seek when we execute the procedure with country id 2 first, where as the query optimizer does a clustered index scan with country id 1 on first execution of the stored procedure. This difference in plan for the stored procedure execution comes from parameter sniffing and cause poor query execution time for more frequently executed parameter values if the first parameter was not a representative value. Or if the parameterized plan removed from cache due a cache flush and the next stored procedure execution was with atypical value. In such cases one way to bypass parameter sniffing would be to modify the stored procedure as follows using local variables:&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;create&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; &lt;span style="color: blue;"&gt;proc&lt;/span&gt; sales_report @country_id &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; @country_id_default &lt;span style="color: blue;"&gt;int&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; 1&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;as&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt; &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;set&lt;/span&gt; @country_id_default &lt;span style="color: gray;"&gt;=&lt;/span&gt; @country_id&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;select&lt;/span&gt; &lt;span style="color: gray;"&gt;*&lt;/span&gt; &lt;span style="color: blue;"&gt;from&lt;/span&gt; sales &lt;span style="color: blue;"&gt;where&lt;/span&gt; country_id &lt;span style="color: gray;"&gt;=&lt;/span&gt; @country_id_default&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: Courier New;"&gt;end&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Courier New;"&gt;go&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 10pt; font-family: Arial;"&gt;Now irrespective of what order the parameters are supplied, the query optimizer does a clustered index scan. This is because the optimizer sniffs the @country_id_default variable which is always set to the more commonly used value (in this case 1), leading to the same plan irrespective of what parameter we pass to the procedure. Notice that the parameter we pass to the stored procedure is not part of the select statement where clause. Also note that the set statement cannot affect the compiled plan chosen by the query optimizer because it is evaluated only at execution time. There are other options like creating the procedure with the &amp;lsquo;with recompile&amp;rsquo; option. More details are available in this &lt;a style="color: blue; text-decoration: underline;" href="http://www.microsoft.com/technet/prodtechnol/sql/2005/recomp.mspx"&gt;white paper&lt;/a&gt; and in this &lt;a href="http://blogs.msdn.com/queryoptteam/archive/2006/03/31/565991.aspx"&gt;blog&lt;/a&gt; post. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/nzperfect/aggbug/2355716.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/nzperfect/archive/2012/02/17/2355716.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/nzperfect/archive/2012/02/03/2337328.html</id><title type="text">日志文件如何影响我数据库的启动</title><summary type="text">原文：http://blogs.msdn.com/b/apgcdsd/archive/2011/12/30/10251946.aspx截取一段，备查日志文件如何影响我数据库的启动？无论你的SQL Server启动，或者你将某数据库重备份中恢复，或者其他的一些情况，总之在你的数据库能够被正常使用之前，你的数据库都会进入 Recovery的状态。http://msdn.microsoft.com/en-us/library/ms190442.aspx 如果这一个步失败，那么你的数据库就会进入Suspect状态而无法正常使用。在一些特殊情况下，这个Recovery所花费的时间会很长。在数据库进入On</summary><published>2012-02-03T08:56:00Z</published><updated>2012-02-03T08:56:00Z</updated><author><name>nzperfect</name><uri>http://www.cnblogs.com/nzperfect/</uri></author><link rel="alternate" href="http://www.cnblogs.com/nzperfect/archive/2012/02/03/2337328.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/nzperfect/archive/2012/02/03/2337328.html"/><content type="html">&lt;p&gt;原文：&lt;a href="http://blogs.msdn.com/b/apgcdsd/archive/2011/12/30/10251946.aspx" target="_blank"&gt;http://blogs.msdn.com/b/apgcdsd/archive/2011/12/30/10251946.aspx&lt;/a&gt;&lt;/p&gt;&lt;p&gt;截取一段，备查&lt;br /&gt;&lt;span style="font-size: 16px;"&gt;&lt;strong&gt;日志文件如何影响我数据库的启动？&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;无论你的SQL Server启动，或者你将某数据库重备份中恢复，或者其他的一些情况，总之在你的数据库能够被正常使用之前，你的数据库都会进入 Recovery的状态。&lt;a href="http://msdn.microsoft.com/en-us/library/ms190442.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms190442.aspx&lt;/a&gt; 如果这一个步失败，那么你的数据库就会进入Suspect状态而无法正常使用。在一些特殊情况下，这个Recovery所花费的时间会很长。在数据库进入Online状态之前，我们都不能认为SQL Server可以被正常使用了。&lt;/p&gt;&lt;p&gt;那么在Recovery中做了什么呢？&lt;/p&gt;&lt;p&gt;我们可以在你的SSMS中运行如下语句。在运行之前，你可以先按下Ctrl+T来把结果转化成Plain Text格式。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;sp_readerrorlog会返回自从最近一次SQL Server服务启动，或者Error Log被回收开始的SQL Server的Error Log信息。这里有两个词汇可能会让你感到疑惑。&lt;em&gt;首先，这里的&lt;/em&gt;&lt;em&gt;Log&lt;/em&gt;&lt;em&gt;和我们上文所说的日志文件（&lt;/em&gt;&lt;em&gt;Transaction Log&lt;/em&gt;&lt;em&gt;）中的&lt;/em&gt;&lt;em&gt;Log&lt;/em&gt;&lt;em&gt;不是一件事情。其次，所谓的&lt;/em&gt;&lt;em&gt;Error Log&lt;/em&gt;&lt;em&gt;，并不是说这里出现了&lt;/em&gt;&lt;em&gt;Error&lt;/em&gt;&lt;em&gt;。&lt;/em&gt;SQL Server使用这个Error Log记录了很多诊断信息。所以，你可以认为这只是一个普通的记录了很多SQL Server相关信息的日志文件。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;在结果返回中，你可能会找到上面的这些信息。引号中为在你当前数据库实例下的各个数据库名。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;以数据库&amp;lsquo;CaseLync&amp;rsquo;为例。由于Error Log会记录当前SQL Server实例下的所有数据库的相关信息，所以你可能会看到和这个数据库相关的日志被顺序地分布在日志的不同地方。在上图中，第一和第二行提及两个词：rolled forward和rolled back。这两个词和我们本文中所提及的Transaction Log息息相关。&lt;/p&gt;&lt;p&gt;在前文中，我们提到过数据修改的过程：先同步的被写入Transaction Log，然后再Checkpoint发生的时候被同步到Data File里。那么，如果我的事物（Transaction）没有被提交（Commit）或者回滚（Rollback），那么这个数据修改是否也在日志文件（Transaction Log）还是在数据文件里（Data File）？&lt;/p&gt;&lt;p&gt;答案是：都在！&lt;/p&gt;&lt;p&gt;SQL Server在记录数据改变时，并不会区分该语句是否有显示的进行事物操作（Begin Transaction，Commit / Rollback Transaction）, 或者该事务是否有完成。SQL Server会忠实地记录所有的修改操作。而Begin Transaction和Commit / Rollback Transaction本身也是日志文件需要记录的操作之一。&lt;/p&gt;&lt;p&gt;由于对于事物日志的修改要先于对于数据文件的修改，所以当你的数据库处于Recovery的状态时，那么Transaction Log&lt;em&gt;就会从最近的一个&lt;/em&gt;&lt;em&gt;Checkpoint&lt;/em&gt;点开始做如下操作：&lt;/p&gt;&lt;ol&gt;&lt;li&gt;如果该操作已在在Transaction Log中，而不在Data File之中，并且如果它使用显式的Transaction操作符而且处于Commit状态，则会发生一次Rolled Forward操作，将该操作同步到Data File之中。&lt;/li&gt;&lt;li&gt;如果该操作已在在Transaction Log中，而不在Data File之中，并且如果它使用显式的Transaction操作符而且处于Rollback Transaction状态，则会发生一次Rolled back操作，将Data File之中的相关数据修改回滚到Transaction发生之前。&lt;/li&gt;&lt;li&gt;如果该操作已在在Transaction Log中，而不在Data File之中，并且如果它使用显式的Transaction在日志文件中即没有Commit操作，也没有Rollback操作，则会发生一次Rolled back操作，将Data File之中的相关数据修改回滚到Transaction发生之前。&lt;/li&gt;&lt;li&gt;如果该操作已在在Transaction Log中，而不在Data File之中，并且如果它没有使用显式的Transaction操作符（Begin Transaction）, 则被认为是一个Rolled Forward操作。&lt;/li&gt;&lt;li&gt;如上步骤都完成后，SQL Server会对该数据库做一个Checkpoint的标识，并写入Transaction Log，表示Data File和Transaction Log已经同步。这表明了数据库的Recovery完成。数据库将进入Online状态，并被正常使用。如果你的数据库在最近的一次Checkpoint到现今的修改操作足够多，在Error Log中也会看到SQL Server用百分比标识Recovery完成的进展。&lt;/li&gt;&lt;/ol&gt;&lt;img src="http://www.cnblogs.com/nzperfect/aggbug/2337328.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/nzperfect/archive/2012/02/03/2337328.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/nzperfect/archive/2012/02/01/2334594.html</id><title type="text">How can I create a dump of SQL Server?</title><summary type="text">原文：http://blogs.msdn.com/b/askjay/archive/2010/02/05/how-can-i-create-a-dump-of-sql-server.aspxYou can create a memory dump of the SQL Server process space in several ways. There are many external tools that can help you accomplish this such as userdump.exe, debugdiag.exe, and ADPlus.exe. In this po</summary><published>2012-02-01T06:58:00Z</published><updated>2012-02-01T06:58:00Z</updated><author><name>nzperfect</name><uri>http://www.cnblogs.com/nzperfect/</uri></author><link rel="alternate" href="http://www.cnblogs.com/nzperfect/archive/2012/02/01/2334594.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/nzperfect/archive/2012/02/01/2334594.html"/><content type="html">&lt;p&gt;原文：http://blogs.msdn.com/b/askjay/archive/2010/02/05/how-can-i-create-a-dump-of-sql-server.aspx&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;You can create a memory dump of the SQL Server process space in several ways.&amp;nbsp; There are many external tools that can help you accomplish this such as userdump.exe, debugdiag.exe, and ADPlus.exe.&amp;nbsp; In this post, I&amp;rsquo;ll cover 3 common ways to accomplish this for SQL Server:&amp;nbsp; The most common way (sqldumper), using the debugger, and three methods from within SQL Server itself.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;First, two of these methods will require that you get the process id (PID) of SQL Server.&amp;nbsp;&amp;nbsp; Here are a few simple ways to do that:&lt;/span&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;1.&amp;nbsp; From a command window, execute --&amp;gt;&lt;strong&gt;&amp;nbsp;&lt;em&gt;tasklist | find /i &amp;ldquo;sqlservr&amp;rdquo;&lt;/em&gt;&lt;/strong&gt; or from the Debugging tools directory execute &amp;ndash;&amp;gt;&lt;strong&gt; &lt;em&gt;tlist | find /i &amp;ldquo;sqlservr&amp;rdquo;&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;2.&amp;nbsp; Open task manager, and find sqlservr.exe and get the PID from the PID column.&amp;nbsp; If you don&amp;rsquo;t see this column, then select View &amp;ndash;&amp;gt; Select Columns to add it.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;3.&amp;nbsp; In a query window inside of SSMS on the instance you wish to dump, execute &amp;ndash;&amp;gt; &amp;ldquo;SELECT SERVERPROPERTY(&amp;lsquo;PROCESSID&amp;rsquo;)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;4.&amp;nbsp; Get the process ID from the SQL Server Errorlog.&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;Now that you have the PID, you can use one of the following ways to create the dump:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: xx-small;"&gt;&lt;strong&gt;&lt;span style="text-decoration: underline;"&gt;&lt;span style="font-size: small;"&gt;Method 1 (Using SqlDumper)&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;The &lt;span style="text-decoration: underline;"&gt;most common way&lt;/span&gt; (and the way used internally by SQL Server) is to run SqlDumper.exe.&amp;nbsp;&amp;nbsp; You will find SqlDumper.exe in the following directory for a default installation of SQL Server:&lt;/span&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;C:\Program Files\Microsoft SQL Server\100\Shared&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;The syntax of SqlDumper is as follows:&lt;/span&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;strong&gt;SqlDumper&lt;/strong&gt; &amp;lt;process id (PID)&amp;gt; &amp;lt;thread id (TID)&amp;gt; &amp;lt;Flags:Minidump Flags&amp;gt; &amp;lt;SQLInfoPtr&amp;gt; &amp;lt;Dump Directory&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;more parameters can be seen with SqlDumper /?&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;The common flags are as follows:&lt;/span&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;&lt;strong&gt;0x0120&lt;/strong&gt; &amp;ndash; Minidump (this is a dump of just the stacks and loaded modules &amp;ndash; this is the smallest of the dump types &amp;ndash; this is normally the type of dump created automatically by SQL Server during AVs and other exceptions) &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;0x01100&lt;/strong&gt; &amp;ndash; Full Dump (this dumps the entire process space &amp;ndash; this can be very large on a 64 bit system or any system with a large amount of memory assigned to SQL Server) &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;0x8100&lt;/strong&gt; &amp;ndash; Filtered Dump (a filtered dump is a dump of all of Stolen Memory plus certain areas of the buffer pool)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;NOTICE:&amp;nbsp; SQLDumper can be used to dump ANY user mode process &amp;ndash; not just SQL Server.&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;Some examples for SQLServer running under SPID 4024 to c:\temp:&lt;/span&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;Minidump:&amp;nbsp; sqldumper 4024 0 0x0120 0 c:\temp &lt;br /&gt;Full Dump:&amp;nbsp; sqldumper 4024 0 0x01100 0 c:\temp &lt;br /&gt;Filtered Dump:&amp;nbsp; sqldumper 4024 0 0x8100 0 c:\temp&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;The dump file will have the naming convention of:&amp;nbsp;&amp;nbsp; SQLDmpr####.mdmp (i.e.&amp;nbsp; SQLDmpr0001.mdmp)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: xx-small;"&gt;&lt;strong&gt;&lt;span style="text-decoration: underline;"&gt;&lt;span style="font-size: small;"&gt;Method 2 (Using a debugger)&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;To dump SQL Server from WINDBG or other debugger, attach the debugger to the process using the PID:&lt;/span&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/askjay/WindowsLiveWriter/HowcanIcreateadumpofSQLServer_398/image_2.png"&gt;&lt;span style="font-size: small;"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" src="http://blogs.msdn.com/blogfiles/askjay/WindowsLiveWriter/HowcanIcreateadumpofSQLServer_398/image_thumb.png" alt="image" width="431" height="533" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;Once connected, just use the .dump command which has the following syntax:&lt;/span&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;Options are: &lt;br /&gt;&amp;nbsp; /a - Create dumps for all processes (requires -u) &lt;br /&gt;&amp;nbsp; /b[a] - Package dump in a CAB and delete dump &lt;br /&gt;&amp;nbsp; /c &amp;lt;comment&amp;gt; - Add a comment (not supported in all formats) &lt;br /&gt;&amp;nbsp; /j &amp;lt;addr&amp;gt; - Provide a JIT_DEBUG_INFO address &lt;br /&gt;&amp;nbsp; /f - Create a legacy style full dump &lt;br /&gt;&amp;nbsp; /m[acdfFhiprRtuw] - Create a minidump (default) &lt;br /&gt;&amp;nbsp; /o - Overwrite any existing file &lt;br /&gt;&amp;nbsp; /u - Append unique identifier to dump name&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;&amp;ldquo;.dump /ma&amp;rdquo; is the recommend method of creating a complete memory dump of a user mode process. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;So, to create a complete memory dump of the SQL Server to c:\temp, execute the following:&lt;/span&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;.dump /ma c:\temp\sqldump.dmp&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span style="font-size: xx-small;"&gt;&lt;strong&gt;&lt;span style="text-decoration: underline;"&gt;&lt;span style="font-size: small;"&gt;Method 3 (From within SQL Server)&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;From inside SQL Server, you can create a dump using two different methods.&amp;nbsp; First, to create a manual dump immediately, use the following undocumented command:&lt;/span&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-size: small;"&gt;DBCC STACKDUMP&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;This will create a memory dump in the LOG directory of your SQL Server instance installation.&amp;nbsp; To enable this method to create a FULL DUMP, you must turn on trace flags 2544 and 2546:&lt;/span&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-size: small;"&gt;dbcc traceon(2544, -1) &lt;br /&gt;go &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-size: small;"&gt;dbcc traceon(2546, -1) &lt;br /&gt;go &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-size: small;"&gt;dbcc stackdump&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;To create only a minidump, enable trace flag 2546.&amp;nbsp; To create a full-filtered dump, use trace flag 2551.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;You can use the undocumented DBCC DUMPTRIGGER command to enable SQL Server to create a dump on the occurrence of an error.&amp;nbsp; You can use the following to enable a full dump on error 802 (There is insufficient memory available in the buffer pool):&lt;/span&gt;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-size: small;"&gt;-- turn on TFs for full dump &lt;br /&gt;dbcc traceon(2544, -1) &lt;br /&gt;go &lt;br /&gt;dbcc traceon(2546, -1) &lt;br /&gt;go &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-size: small;"&gt;-- set DUMP TRIGGER for exception 802 &lt;br /&gt;dbcc dumptrigger('set', 802) &lt;br /&gt;go &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-size: small;"&gt;-- view exceptions set for DUMP TRIGGER &lt;br /&gt;dbcc traceon(3604, -1) &lt;br /&gt;go &lt;br /&gt;dbcc dumptrigger('display') &lt;br /&gt;go &lt;br /&gt;dbcc traceoff(3604, -1) &lt;br /&gt;go &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-size: small;"&gt;-- Turn off dumptrigger for exception 802 &lt;br /&gt;dbcc dumptrigger('clear', 802) &lt;br /&gt;go&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;Finally, you can also use the &amp;ndash;y parameter on SQL Server startup to achieve the same functionality as SQL Server&amp;rsquo;s DBCC DUMPTRIGGER.&amp;nbsp;&amp;nbsp; For more information refer to:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;a title="http://blogs.msdn.com/psssql/archive/2008/01/10/how-it-works-sql-server-engine-error-messages.aspx" href="http://blogs.msdn.com/psssql/archive/2008/01/10/how-it-works-sql-server-engine-error-messages.aspx"&gt;&lt;span style="font-size: small;"&gt;http://blogs.msdn.com/psssql/archive/2008/01/10/how-it-works-sql-server-engine-error-messages.aspx&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;One method not covered in this post is to create the dump from within Task Manager as seen here:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/askjay/WindowsLiveWriter/HowcanIcreateadumpofSQLServer_398/image_4.png"&gt;&lt;span style="font-size: small;"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" src="http://blogs.msdn.com/blogfiles/askjay/WindowsLiveWriter/HowcanIcreateadumpofSQLServer_398/image_thumb_1.png" alt="image" width="695" height="312" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;I am not a big fan of this method since you have little control over it.&amp;nbsp; It will create a full dump inside your user profile by default.&amp;nbsp;&amp;nbsp; It is also only available on Windows 2008 and Windows 2008 R2 (or Vista and Windows 7).&amp;nbsp; However, it is yet another way to dump the SQL Server process.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: small;"&gt;- Jay&lt;/span&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/nzperfect/aggbug/2334594.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/nzperfect/archive/2012/02/01/2334594.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/nzperfect/archive/2011/12/01/2270427.html</id><title type="text">在发布订阅中增加rowversion数据类型字段引发错误(or bug)</title><summary type="text">考虑这样的场景，现在每天要同步更新的数据，为了记录哪些数据发生过变更，需要对表添加一个rowversion列来标识被更新过。恰好这个表比较大，而且有发布订阅。由于该字段对于订阅表来讲，无实际意义，而且添加一个有默认值的不可为空的8字节字段，会产生数据空间分配，或许带来大量的页拆分，对IO压力会加大，所以考虑将此表的发布项暂时改为不支持架构复制，等添加完该字段后，再将其改为支持架构复制。下面来做这个测试：1.下面是一个正常的事务发布，将DB_1中的T_1发布到DB_2的T_1表中。2.修改发布项DBrepTEST关于架构复制的选项，设置为不支持DECLARE @publication AS sy</summary><published>2011-12-01T05:19:00Z</published><updated>2011-12-01T05:19:00Z</updated><author><name>nzperfect</name><uri>http://www.cnblogs.com/nzperfect/</uri></author><link rel="alternate" href="http://www.cnblogs.com/nzperfect/archive/2011/12/01/2270427.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/nzperfect/archive/2011/12/01/2270427.html"/><content type="html">&lt;p&gt;考虑这样的场景，现在每天要同步更新的数据，为了记录哪些数据发生过变更，需要对表添加一个rowversion列来标识被更新过。恰好这个表比较大，而且有发布订阅。由于该字段对于订阅表来讲，无实际意义，而且添加一个有默认值的不可为空的8字节字段，会产生数据空间分配，或许带来大量的页拆分，对IO压力会加大，所以考虑将此表的发布项暂时改为不支持架构复制，等添加完该字段后，再将其改为支持架构复制。&lt;/p&gt;&lt;p&gt;下面来做这个测试：&lt;/p&gt;&lt;p&gt;1.下面是一个正常的事务发布，将DB_1中的T_1发布到DB_2的T_1表中。&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2011/21914/2011120113490287.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;2.修改发布项DBrepTEST关于架构复制的选项，设置为不支持&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;DECLARE&lt;/span&gt; &lt;span style="color: #008000;"&gt;@publication&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;AS&lt;/span&gt; sysname&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;SET&lt;/span&gt; &lt;span style="color: #008000;"&gt;@publication&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;DBrepTEST&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;USE&lt;/span&gt; DB_1&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;EXEC&lt;/span&gt; sp_changepublication &lt;br /&gt;  &lt;span style="color: #008000;"&gt;@publication&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #008000;"&gt;@publication&lt;/span&gt;, &lt;br /&gt;  &lt;span style="color: #008000;"&gt;@property&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;replicate_ddl&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;, &lt;br /&gt;  &lt;span style="color: #008000;"&gt;@value&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;GO&lt;/span&gt;&lt;/div&gt;&lt;p&gt;3.然后，添加一个字段，类型为rowversion&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;use&lt;/span&gt; DB_1&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;alter&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;table&lt;/span&gt; t_1 &lt;span style="color: #0000ff;"&gt;add&lt;/span&gt; rv rowversion&lt;/div&gt;&lt;p&gt;4.经过观察，发布订阅一切正常，该字段并未被创建在订阅库DB_2上&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2011/21914/2011120113491897.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;5.我们在另外一个进程开始一个sql，向表DB_1.dbo.T_1 insert data.&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;while&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #808080;"&gt;=&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold;"&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;begin&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;waitfor&lt;/span&gt; delay &lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;00:00:01&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;insert&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;into&lt;/span&gt; DB_1.dbo.T_1(name)&lt;span style="color: #0000ff;"&gt;select&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;a&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;end&lt;/span&gt;&lt;/div&gt;&lt;p&gt;6.发现数据已同步到DB_2.dbo.T_1，但rv字段并未被同步，现在一切正常&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2011/21914/2011120113492965.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;7.现在将发布项DBrepTEST修改回支持架构复制&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;DECLARE&lt;/span&gt; &lt;span style="color: #008000;"&gt;@publication&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;AS&lt;/span&gt; sysname&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;SET&lt;/span&gt; &lt;span style="color: #008000;"&gt;@publication&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;DBrepTEST&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;USE&lt;/span&gt; DB_1&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;EXEC&lt;/span&gt; sp_changepublication &lt;br /&gt;  &lt;span style="color: #008000;"&gt;@publication&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #008000;"&gt;@publication&lt;/span&gt;, &lt;br /&gt;  &lt;span style="color: #008000;"&gt;@property&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;replicate_ddl&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;, &lt;br /&gt;  &lt;span style="color: #008000;"&gt;@value&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;GO&lt;/span&gt;&lt;/div&gt;&lt;p&gt;8.然后，此时向表DB_1.dbo.T_1新加一个int类型字段&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;use&lt;/span&gt; DB_1&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;go&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;alter&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;table&lt;/span&gt; t_1 &lt;span style="color: #0000ff;"&gt;add&lt;/span&gt; age &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;&lt;/div&gt;&lt;p&gt;9.此时发布订阅出错&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2011/21914/2011120113494430.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2011/21914/2011120113495426.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2011/21914/2011120113500415.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;出错文字为：&lt;br /&gt;Command attempted:&lt;br /&gt;if @@trancount &amp;gt; 0 rollback tran&lt;br /&gt;(Transaction sequence number: &lt;strong&gt;0x000000580000013C000100000000&lt;/strong&gt;, Command ID: 6)&lt;br /&gt;&lt;br /&gt;Error messages:&lt;br /&gt;A DDL change has been replicated. (Source: MSSQL_REPL, Error number: MSSQL_REPL27332)&lt;br /&gt;Get help: http://help/MSSQL_REPL27332&lt;br /&gt;&lt;strong&gt;列名 'rv' 无效&lt;/strong&gt;。 (Source: MSSQLServer, Error number: 207)&lt;br /&gt;Get help: http://help/207&lt;br /&gt;10.由上面的信息，可以看出此时是由于在订阅库上找不到rv这个列造成的，为了确定这一点，我们看下这个事务执行的是什么&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;use&lt;/span&gt; distribution&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;go&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;EXEC&lt;/span&gt; sp_browsereplcmds &lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;0x000000580000013C000100000000&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;0x000000580000013C000100000000&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;/div&gt;&lt;p&gt;11.由10的结果中的command列，得到如下需要应用到订阅库的t-sql&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;ALTER&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;TABLE&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;T_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;add&lt;/span&gt; age &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;object_id&lt;/span&gt;(N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;[dbo].[sp_MSins_dboT_1]&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;, &lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;P&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;) &lt;span style="color: #808080;"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;0&lt;/span&gt;      &lt;span style="color: #0000ff;"&gt;drop&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;proc&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSins_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;object_id&lt;/span&gt;(N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo.MSreplication_objects&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;) &lt;span style="color: #0000ff;"&gt;is&lt;/span&gt; &lt;span style="color: #808080;"&gt;not&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;null&lt;/span&gt;      &lt;span style="color: #0000ff;"&gt;delete&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;from&lt;/span&gt; dbo.MSreplication_objects &lt;span style="color: #0000ff;"&gt;where&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;object_name&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSins_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt; &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;create&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;procedure&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSins_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c1&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c2&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;varchar&lt;/span&gt;(&lt;span style="color: #800000; font-weight: bold;"&gt;20&lt;/span&gt;), &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c3&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff;"&gt;as&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff;"&gt;begin&lt;/span&gt;   &lt;br /&gt; &lt;span style="color: #0000ff;"&gt;insert&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;into&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;T_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;( &lt;br /&gt;  &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;id&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;, &lt;br /&gt;  &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;name&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;, &lt;br /&gt;  &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;rv&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;, &lt;br /&gt;  &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;age&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;br /&gt; ) &lt;span style="color: #0000ff;"&gt;values&lt;/span&gt; ( &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c1&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c2&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;default&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c3&lt;/span&gt; )  &lt;br /&gt;&lt;span style="color: #0000ff;"&gt;end&lt;/span&gt;   &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;columnproperty&lt;/span&gt;(&lt;span style="color: #ff00ff;"&gt;object_id&lt;/span&gt;(N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo.MSreplication_objects&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;), N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;article&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;, &lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;AllowsNull&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;) &lt;span style="color: #0000ff;"&gt;is&lt;/span&gt; &lt;span style="color: #808080;"&gt;not&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;null&lt;/span&gt;  &lt;span style="color: #0000ff;"&gt;exec&lt;/span&gt; sp_executesql       &lt;span style="color: #008000;"&gt;@statement&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt;           N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;insert dbo.MSreplication_objects (object_name, publisher, publisher_db, publication, article, object_type)               values           (@object_name, @publisher, @publisher_db, @publication, @article, &lt;/span&gt;&lt;span style="color: #ff0000;"&gt;''&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;P&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;''&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;)&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@parameters&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt;           N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;@object_name sysname, @publisher sysname, @publisher_db sysname, @publication sysname, @article sysname&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@object_n&lt;/span&gt;&lt;br /&gt;ame &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSins_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@publisher&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;mySrv\SQL2008&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@publisher_db&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;DB_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@publication&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;DBrepTEST&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@article&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;T_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt; &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style="color: #008080;"&gt;--&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;object_id&lt;/span&gt;(N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;[dbo].[sp_MSdel_dboT_1]&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;, &lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;P&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;) &lt;span style="color: #808080;"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;0&lt;/span&gt;      &lt;span style="color: #0000ff;"&gt;drop&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;proc&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSdel_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;object_id&lt;/span&gt;(N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo.MSreplication_objects&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;) &lt;span style="color: #0000ff;"&gt;is&lt;/span&gt; &lt;span style="color: #808080;"&gt;not&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;null&lt;/span&gt;      &lt;span style="color: #0000ff;"&gt;delete&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;from&lt;/span&gt; dbo.MSreplication_objects &lt;span style="color: #0000ff;"&gt;where&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;object_name&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSdel_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt; &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;create&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;procedure&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSdel_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@pkc1&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;  &lt;span style="color: #0000ff;"&gt;as&lt;/span&gt;  &lt;span style="color: #0000ff;"&gt;begin&lt;/span&gt;   &lt;br /&gt; &lt;span style="color: #0000ff;"&gt;delete&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;T_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;  &lt;span style="color: #0000ff;"&gt;where&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;id&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #008000;"&gt;@pkc1&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold;"&gt;@@rowcount&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;0&lt;/span&gt;      &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold;"&gt;@@microsoftversion&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold;"&gt;0x07320000&lt;/span&gt;          &lt;span style="color: #0000ff;"&gt;exec&lt;/span&gt; sp_MSreplraiserror &lt;span style="color: #800000; font-weight: bold;"&gt;20598&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff;"&gt;end&lt;/span&gt;   &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;columnproperty&lt;/span&gt;(&lt;span style="color: #ff00ff;"&gt;object_id&lt;/span&gt;(N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo.MSreplication_objects&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;), N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;article&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;, &lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;AllowsNull&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;) &lt;span style="color: #0000ff;"&gt;is&lt;/span&gt; &lt;span style="color: #808080;"&gt;not&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;null&lt;/span&gt;  &lt;span style="color: #0000ff;"&gt;exec&lt;/span&gt; sp_executesql       &lt;span style="color: #008000;"&gt;@statement&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt;           N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;insert dbo.MSreplication_objects (object_name, publisher, publisher_db, publication, article, object_type)               values           (@object_name, @publisher, @publisher_db, @publication, @article, &lt;/span&gt;&lt;span style="color: #ff0000;"&gt;''&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;P&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;''&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;)&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@parameters&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt;           N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;@object_name sysname, @publisher sysname, @publisher_db sysname, @publication sysname, @article sysname&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@object_n&lt;/span&gt;&lt;br /&gt;ame &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSdel_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@publisher&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;mySrv\SQL2008&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@publisher_db&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;DB_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@publication&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;DBrepTEST&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@article&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;T_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt; &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style="color: #008080;"&gt;--&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;object_id&lt;/span&gt;(N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;[dbo].[sp_MSupd_dboT_1]&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;, &lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;P&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;) &lt;span style="color: #808080;"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;0&lt;/span&gt;      &lt;span style="color: #0000ff;"&gt;drop&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;proc&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSupd_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;object_id&lt;/span&gt;(N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo.MSreplication_objects&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;) &lt;span style="color: #0000ff;"&gt;is&lt;/span&gt; &lt;span style="color: #808080;"&gt;not&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;null&lt;/span&gt;      &lt;span style="color: #0000ff;"&gt;delete&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;from&lt;/span&gt; dbo.MSreplication_objects &lt;span style="color: #0000ff;"&gt;where&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;object_name&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSupd_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt; &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;create&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;procedure&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSupd_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c1&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c2&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;varchar&lt;/span&gt;(&lt;span style="color: #800000; font-weight: bold;"&gt;20&lt;/span&gt;) &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c3&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@pkc1&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;NULL&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@bitmap&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;binary&lt;/span&gt;(&lt;span style="color: #800000; font-weight: bold;"&gt;1&lt;/span&gt;)  &lt;span style="color: #0000ff;"&gt;as&lt;/span&gt;  &lt;span style="color: #0000ff;"&gt;begin&lt;/span&gt;   &lt;br /&gt;&lt;span style="color: #0000ff;"&gt;update&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;T_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;set&lt;/span&gt; &lt;br /&gt;    &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;name&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;case&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;substring&lt;/span&gt;(&lt;span style="color: #008000;"&gt;@bitmap&lt;/span&gt;,&lt;span style="color: #800000; font-weight: bold;"&gt;1&lt;/span&gt;,&lt;span style="color: #800000; font-weight: bold;"&gt;1&lt;/span&gt;) &lt;span style="color: #808080;"&gt;&amp;amp;&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;2&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;when&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;2&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;then&lt;/span&gt; &lt;span style="color: #008000;"&gt;@c2&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;else&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;name&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;end&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;age&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;case&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;substring&lt;/span&gt;(&lt;span style="color: #008000;"&gt;@bitmap&lt;/span&gt;,&lt;span style="color: #800000; font-weight: bold;"&gt;1&lt;/span&gt;,&lt;span style="color: #800000; font-weight: bold;"&gt;1&lt;/span&gt;) &lt;span style="color: #808080;"&gt;&amp;amp;&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;4&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;when&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;4&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;then&lt;/span&gt; &lt;span style="color: #008000;"&gt;@c3&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;else&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;age&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;end&lt;/span&gt;  &lt;span style="color: #0000ff;"&gt;where&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;id&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #008000;"&gt;@pkc1&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold;"&gt;@@rowcount&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold;"&gt;0&lt;/span&gt;      &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold;"&gt;@@microsoftversion&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold;"&gt;0x07320000&lt;/span&gt;          &lt;span style="color: #0000ff;"&gt;exec&lt;/span&gt; sp_MSreplraiserror &lt;span style="color: #800000; font-weight: bold;"&gt;20598&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff;"&gt;end&lt;/span&gt;  &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; &lt;span style="color: #ff00ff;"&gt;columnproperty&lt;/span&gt;(&lt;span style="color: #ff00ff;"&gt;object_id&lt;/span&gt;(N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo.MSreplication_objects&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;), N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;article&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;, &lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;AllowsNull&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;) &lt;span style="color: #0000ff;"&gt;is&lt;/span&gt; &lt;span style="color: #808080;"&gt;not&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;null&lt;/span&gt;  &lt;span style="color: #0000ff;"&gt;exec&lt;/span&gt; sp_executesql       &lt;span style="color: #008000;"&gt;@statement&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt;           N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;insert dbo.MSreplication_objects (object_name, publisher, publisher_db, publication, article, object_type)               values           (@object_name, @publisher, @publisher_db, @publication, @article, &lt;/span&gt;&lt;span style="color: #ff0000;"&gt;''&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;P&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;''&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;)&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@parameters&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt;           N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;@object_name sysname, @publisher sysname, @publisher_db sysname, @publication sysname, @article sysname&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@object_n&lt;/span&gt;&lt;br /&gt;ame &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSupd_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@publisher&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;mySrv\SQL2008&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@publisher_db&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;DB_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@publication&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;DBrepTEST&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;,      &lt;span style="color: #008000;"&gt;@article&lt;/span&gt; &lt;span style="color: #808080;"&gt;=&lt;/span&gt; N&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;T_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;'&lt;/span&gt; &lt;br /&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt; &lt;span style="color: #008080;"&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style="color: #008080;"&gt;--&lt;/span&gt;&lt;/div&gt;&lt;p&gt;可以看到对于添加字段，会重新创建对表操作的三个proc，本例中，insert这个proc会将rv加入进来，并且值为default,如下所示，但由于该列并不存在于订阅中，所以就会报错了，由此造成分发中断，后续事务无法应用到订阅端。&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;create&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;procedure&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;sp_MSins_dboT_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c1&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c2&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;varchar&lt;/span&gt;(&lt;span style="color: #800000; font-weight: bold;"&gt;20&lt;/span&gt;), &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c3&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff;"&gt;as&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff;"&gt;begin&lt;/span&gt;   &lt;br /&gt; &lt;span style="color: #0000ff;"&gt;insert&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;into&lt;/span&gt; &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;dbo&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;.&lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;T_1&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;( &lt;br /&gt;  &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;id&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;, &lt;br /&gt;  &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;name&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;, &lt;br /&gt;  &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;rv&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt;, &lt;br /&gt;  &lt;span style="color: #ff0000;"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;age&lt;/span&gt;&lt;span style="color: #ff0000;"&gt;]&lt;/span&gt; &lt;br /&gt; ) &lt;span style="color: #0000ff;"&gt;values&lt;/span&gt; ( &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c1&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c2&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #0000ff;"&gt;default&lt;/span&gt;, &lt;br /&gt;    &lt;span style="color: #008000;"&gt;@c3&lt;/span&gt; )  &lt;/div&gt;&lt;p&gt;12.虽然可以通过在订阅端手工端建一个rv列，将其设置为可为null的&lt;span&gt;&lt;span class="input"&gt;varbinary(8)&lt;/span&gt;&lt;/span&gt;类型，但这个列对于订阅并没有实际意义。&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;use&lt;/span&gt; DB_2&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;go&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff;"&gt;alter&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;table&lt;/span&gt; t_1 &lt;span style="color: #0000ff;"&gt;add&lt;/span&gt; rv &lt;span style="color: #0000ff;"&gt;varbinary&lt;/span&gt;(&lt;span style="color: #800000; font-weight: bold;"&gt;8&lt;/span&gt;) &lt;/div&gt;&lt;p&gt;&lt;br /&gt;总结，经过测试，发现当添加其它类型字段，并设置为有默认值且not null时，不会有此错误出现，目前发现仅是rowversion类型列，不知是否为SQL Server bug.&lt;br /&gt;测试环境：windows server 2003 enterprise 32bit sql server 2008 sp2 standard 32bit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/nzperfect/aggbug/2270427.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/nzperfect/archive/2011/12/01/2270427.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry></feed>
