<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Mohamed Sharaf's Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/mosharaf/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/mosharaf/feed.xml</link>
    <description>SQL Server Senior Premier Field Engineer &amp;amp; SQL MCM. This blog is my small window to post any technical thoughts. All posts as is.</description>
    <lastBuildDate>Tue, 16 Apr 2019 05:21:55 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>New fix for SQL 2012 SP1. Fix data loss possibility</title>
      <link>https://docs.microsoft.com/archive/blogs/mosharaf/new-fix-for-sql-2012-sp1-fix-data-loss-possibility</link>
      <pubDate>Tue, 17 Jun 2014 05:51:34 GMT</pubDate>
      <dc:creator><![CDATA[Mohamed]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mosharaf/2014/06/17/new-fix-for-sql-2012-sp1-fix-data-loss-possibility/</guid>
      <description><![CDATA[On June 10, a new fix released that fixes a bug in SQL 2012 SP1. It's not included in CU#9 that's...]]></description>
      <content:encoded><![CDATA[<p style="font: 12.72px/1.6 'Segoe UI', Segoe, Tahoma, Helvetica, Arial, sans-serif; margin: 0px 0px 10px; text-align: left; color: #333333; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgba(255, 255, 255, 0.85098); -webkit-text-stroke-width: 0px;">On June 10, a new fix released that fixes a bug in SQL 2012 SP1. It's not included in CU#9 that's included in SP2 so if you choose to install it, you need to delay SP2 until the first CU#1 for SP2 relaeased.&nbsp;</p>
<p style="font: 12.72px/1.6 'Segoe UI', Segoe, Tahoma, Helvetica, Arial, sans-serif; margin: 0px 0px 10px; text-align: left; color: #333333; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgba(255, 255, 255, 0.85098); -webkit-text-stroke-width: 0px;">​The fix is very important becuase the bug is causing data loss when rebuilding indexes with some options enabled and deadlock happened during the rebuilding. The full documentation in the link below.&nbsp;</p>
<div style="font: 12.72px/normal 'Segoe UI', Segoe, Tahoma, Helvetica, Arial, sans-serif; text-align: left; color: #333333; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgba(255, 255, 255, 0.85098); -webkit-text-stroke-width: 0px;">FIX: Data loss in clustered index occurs when you run online build index in SQL Server 2012:&nbsp;</div>
<div style="font: 12.72px/normal 'Segoe UI', Segoe, Tahoma, Helvetica, Arial, sans-serif; text-align: left; color: #333333; text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgba(255, 255, 255, 0.85098); -webkit-text-stroke-width: 0px;"><a href="http://support.microsoft.com/kb/2969896">http://support.microsoft.com/kb/2969896</a> &nbsp;</div>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Index maintenance script</title>
      <link>https://docs.microsoft.com/archive/blogs/mosharaf/index-maintenance-script</link>
      <pubDate>Mon, 16 Jun 2014 17:06:00 GMT</pubDate>
      <dc:creator><![CDATA[Mohamed]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mosharaf/2014/06/16/index-maintenance-script/</guid>
      <description><![CDATA[&nbsp;
Another script that I wanted to share for so much time but didn't have the commitment to be...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;</p>
<p><span style="font-size: 12pt;">Another script that I wanted to share for so much time but didn't have the commitment to be persisted with my posts<span style="font-family: Wingdings;">J</span>. Using the built-in SQL Server <a href="http://msdn.microsoft.com/en-us/library/ms187658.aspx">maintenance plans</a> for index maintenance works well for small databases without HA solutions that are affected by the amount of transaction logs generated. When it comes to mid-size or large databases, the process of reorganizing or rebuilding the indexes has to be optimized. In this case, there has to be a script used to reorganize/rebuild only the indexes needed to be maintained. There are many factors that influence the decision like </span></p>
<ul style="margin-left: 72pt;">
<li><span style="font-size: 12pt;">Number of pages in the index: Small indexes will not cause problem because SQL will read them one time and they stay in memory </span></li>
<li><span style="font-size: 12pt;">Fragmentation percentage: small fragmentation makes more sense to have them reorganized. Big fragmentation will be rebuilt much faster than reorganization </span></li>
<li><span style="font-size: 12pt;">Whether or not will use online index rebuilding </span></li>
<li><span style="font-size: 12pt;">Whether or not sorting in the tempdb (recommended if the tempdb on a faster disk) </span></li>
<li><span style="font-size: 12pt;">Number of range scans on the index. If we have an index with mostly singleton searches then there's no need to defrag it. </span></li>
</ul>
<p>&nbsp;</p>
<p><span style="font-size: 12pt;">The script is taken care of all these. At the beginning of the script, there's a part where I set the default values for these configurable variables. </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"><span style="color: green;">--set values</span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"><span style="color: green;">--------Configuration-----------</span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"><span style="color: blue;">SET</span> @useOnlineIndexing <span style="color: gray;">=</span> 1<span style="color: gray;">;</span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"><span style="color: blue;">SET</span> @SORT_IN_TEMPDB <span style="color: gray;">=</span> <span style="color: red;">'OFF'<span style="color: gray;">;</span> </span></span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"> <span style="color: green;">--change it to ON to sort in tempdb </span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"><span style="color: blue;">SET</span> @minPagestoConsider <span style="color: gray;">=</span> 1000<span style="color: gray;">;</span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"> <span style="color: green;">--8MB indexes</span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"><span style="color: blue;">SET</span> @MDOP <span style="color: gray;">=</span> 8<span style="color: gray;">;</span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"> <span style="color: green;">--Max Degree of Parallelism. Change it to 1 to reduce the number of CPUs involved in rebuilding the index</span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"><span style="color: blue;">SET</span> @MinNumberOfScansToConsider <span style="color: gray;">=</span> 1000<span style="color: gray;">;</span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"> <span style="color: green;">--how many range scans to consider </span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"><span style="color: blue;">SET</span> @WhenPageLockOFFandReBuilding <span style="color: gray;">=</span> <span style="color: red;">'EnablePageLock'<span style="color: gray;">;</span> </span></span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"> <span style="color: green;">--either 'EnablePageLock' or 'SetMDOP1'</span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"><span style="color: green;">/* When building index online with MDOP&gt;1 &amp; Page locks disabled, would cause more fragmentation. deciding the best action either enable Page locks then disable it</span> </span></p>
<p><span style="font-family: Consolas; font-size: 14pt;"><span style="color: green;">or set the MDOP=1 */</span> </span></p>
<p><span style="color: green; font-family: Consolas; font-size: 14pt;">-------------------------------- </span></p>
<p>&nbsp;</p>
<p><span style="font-size: 12pt;">One important option is when rebuilding index online while MDOP&gt;1 and Enable Page Lock disabled, you will notice that fragmentation is increased not decreased. That's a known behavior documented on this <a href="http://support.microsoft.com/kb/2292737">KB article</a>. The script give you two options, either to Enable Page locking before the rebuilding and disable it after rebuilding or make MDOP=1. </span></p>
<p>&nbsp;</p>
<p><span style="font-size: 12pt;">The script works against one database. If you would like to run it against more than one database, use the <a href="http://msdn.microsoft.com/en-us/library/ms188714.aspx">SQLCMD variable</a> named </span><span style="color: green; font-family: Consolas; font-size: 14pt;">$(DatabaseName) </span><span style="font-size: 12pt;">to target multiple databases using SQLCMD calls in a batch file or using a SQL Job. </span></p>
<p>&nbsp;</p>
<p>The script can be <a href="https://msharafcom-public.sharepoint.com/public/Index%20maintenance_single%20database.sql">downloaded from here</a></p>
<p>&nbsp;</p>
<p><span style="color: #333333; font-family: Segoe UI; font-size: 10pt;">This blog post is cross posted on </span></p>
<p><a href="http://blogs.msdn.com/b/mosharaf/"><span style="color: #c51102; font-family: Consolas; font-size: 13pt;">http://blogs.msdn.com/b/mosharaf/</span></a></p>
<p><a href="http://www.msharaf.com/blog"><span style="color: #c51102; font-family: Consolas; font-size: 13pt;">http://www.msharaf.com/blog</span></a></p>]]></content:encoded>
    </item>
    <item>
      <title>Get Current Activity in SQL Server instance</title>
      <link>https://docs.microsoft.com/archive/blogs/mosharaf/get-current-activity-in-sql-server-instance</link>
      <pubDate>Mon, 19 May 2014 22:06:00 GMT</pubDate>
      <dc:creator><![CDATA[Mohamed]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mosharaf/2014/05/19/get-current-activity-in-sql-server-instance/</guid>
      <description><![CDATA[&nbsp;[Update: Version 2.0 uploaded with some bug fixes]
When troubleshooting customer's...]]></description>
      <content:encoded><![CDATA[<p><strong>&nbsp;<span style="color: #333333; text-transform: none; line-height: 18.9px; text-indent: 0px; letter-spacing: normal; font-family: 'Segoe UI', Segoe, Tahoma, Helvetica, Arial, sans-serif; font-size: 12.72px; font-style: normal; font-variant: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgba(255, 255, 255, 0.85098); -webkit-text-stroke-width: 0px;">[Update: Version 2.0 uploaded with some bug fixes]</span></strong></p>
<p>When troubleshooting customer's performance issue that's happening at the moment, we have many tools out of the box. The good old SQL traces that are not needed for the majority of the cases and if it's needed then my first option would ne XE if we are on 2012. Also there are many DMV that we can use and these are the building blocks that SQL Server <a href="http://msdn.microsoft.com/en-us/library/ms175518.aspx">Management Studio Activity Monitor</a> is built on. However to get the best response and the result sets that you are only interested in you need to use your own script.</p>
<p>I used to write an ad-hoc script every time I'm troubleshooting something based on the situation. There are some great scripts online that covers what I need but I wanted to have my own implementation as simple and function-based so one can use it with filters easily. If you are looking for more comprehensive script, I'd refer to <a href="http://sqlblog.com/blogs/adam_machanic/archive/2012/03/22/released-who-is-active-v11-11.aspx">Adam Machanic's whoisactive</a> stored procedure. I don't have much experience using it but he's the first one who used the idea of printing the TSQL code as XML &ndash;as far as I know- so will be easier to view in SSMS by clicking on it. I'm using the same idea as well.</p>
<p>&nbsp;</p>
<p>Eventually I wrote the function <span style="font-family: Consolas; font-size: 12pt;">dbo<span style="color: gray;">.</span>GetCurrentActivity. </span>I'm using it for few months now and if you are a customer whom I promised that it will be uploaded to my blog soon, please accept my apology and find the details below <span style="font-family: Wingdings;">J</span></p>
<p>&nbsp;</p>
<p>The function will show only the running/runnable or suspended requests. If we have a session that does nothing then it will not be seen by this function.</p>
<p>The function make it easy for these scenarios</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;-Waiting on any waiting type as it shows the waiting summary column</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;-Blocking chain as it shows the complete chain so you can get the root blocker</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;-It shows the original TSQL when having FETCH API_CURSOR operation</p>
<p>The output of the function is detailed in the table below.</p>
<p>&nbsp;</p>
<div>
<table style="border-collapse: collapse;" border="0"><colgroup><col style="width: 390px;" /><col style="width: 1134px;" /></colgroup>
<tbody valign="top">
<tr>
<td style="padding-left: 9px; padding-right: 9px; border: solid 0.5pt;">
<p>session_id SMALLINT</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: solid 0.5pt; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>The session id of the request (from sys.dm_exec_sessions)</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>status NVARCHAR(60)</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>The request status (from sys.dm_exec_requests)</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>command NVARCHAR(64)</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>The command being executed (from sys.dm_exec_requests)</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>Waiting_summary NVARCHAR(200)</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>Summary of the waiting type, resource and duration (from sys.dm_os_waiting_tasks)</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>database_name NVARCHAR(128)</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>Database name, this column will only show data if running with @lightweight parameter =0 (from dm_exec_plan_attributes)</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>SQLQuery XML</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>TSQL query from cross applying dm_exec_sql_text</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>CursorQuery XML</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>When SQLQuery column shows FETCH API_CURSOR, this column should have the original statement</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>wait_duration_ms BIGINT</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>Wait duration (from sys.dm_os_waiting_tasks)</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>blocking_session_id SMALLINT</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>The blocking session if you have a blocking chain, see blockChain column</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>BlockChain VARCHAR(200)</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>Shows the blocking chain using the format &lt;sessionID&gt;--&gt;&lt;blockedSessionID&gt;. 0 means there's no blocking</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>elapsed_time_ms BIGINT</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>Total elapsed time in ms</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>physical_reads_KB BIGINT</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>Physical reads converted to KB</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>logical_writes_KB BIGINT</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>Logical writes in KB</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>logical_reads_KB BIGINT</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>Logical reads in KB</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>granted_query_memory_KB INT</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>Granted memory in KB</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>cpu_time_ms INT</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>CPU time in ms</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>host_name NVARCHAR(256)</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>The host name (the server originating the request)</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>program_name NVARCHAR(256)</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>The program name</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>query_plan XML</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>Query plan in XML. this column will only show data if running with @lightweight parameter =0 (from dm_exec_plan_attributes)</p>
</td>
</tr>
<tr>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: solid 0.5pt; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>fn_version VARCHAR(10)</p>
</td>
<td style="padding-left: 9px; padding-right: 9px; border-top: none; border-left: none; border-bottom: solid 0.5pt; border-right: solid 0.5pt;">
<p>To keep track of the current version. If you faced any issues with the function please reply with the version number you have.</p>
</td>
</tr>
</tbody>
</table>
</div>
<p>&nbsp;</p>
<p>To setup the function, execute the script file from the location below. The function will be installed by default on the master database. Then from any other database you can call</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SELECT</span> <span style="color: gray;">*</span> <span style="color: blue;">FROM</span> <span style="color: blue;">master<span style="color: gray;">.</span>dbo<span style="color: gray;">.</span>GetCurrentActivity<span style="color: gray;">(</span>1<span style="color: gray;">); --with lightweight parameter =1, quick execution </span></span></span></p>
<p><span style="font-family: Consolas;"><span style="color: blue;">SELECT</span> <span style="color: gray;">*</span> <span style="color: blue;">FROM</span> <span style="color: blue;">master<span style="color: gray;">.</span>dbo<span style="color: gray;">.</span>GetCurrentActivity<span style="color: gray;">(</span>0<span style="color: gray;">); --with lightweight parameter =0, slower execution but more details. </span></span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>The complete <a href="https://msharafcom-public.sharepoint.com/public/GetCurrentActivity.sql">script can be found here</a></p>
<p>&nbsp;</p>
<p>This blog post is cross posted on</p>
<p><a href="http://blogs.msdn.com/b/mosharaf/"><span style="font-family: Consolas; font-size: 13pt;">http://blogs.msdn.com/b/mosharaf/</span></a></p>
<p><a href="http://www.msharaf.com/blog"><span style="font-family: Consolas; font-size: 13pt;">http://www.msharaf.com/blog</span></a></p>
<p>&nbsp;</p>
<p>Enjoy your performance troubleshooting.</p>
<p>&nbsp;</p>
<p>~Mohamed</p>]]></content:encoded>
    </item>
    <item>
      <title>Viewing encrypted data using Trace</title>
      <link>https://docs.microsoft.com/archive/blogs/mosharaf/viewing-encrypted-data-using-trace</link>
      <pubDate>Wed, 18 Dec 2013 06:59:00 GMT</pubDate>
      <dc:creator><![CDATA[Mohamed]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mosharaf/2013/12/18/viewing-encrypted-data-using-trace/</guid>
      <description><![CDATA[Today morning I got an email from a customer to review some design recommendation. In the...]]></description>
      <content:encoded><![CDATA[<p>Today morning I got an email from a customer to review some design recommendation. In the recommendation, another DBA claimed that encrypting the data by using EncryptByKey or EncryptByPassPhrase is not secured because we can trace the data and read it as plain text.</p>
<p>This is not correct and if you have the patience to go through the rest of the post you will get the proof. That goes for both profiler trace and extended events as well. However network sniffing is possible which can be prevented by SSL encryption to the data in the wire but that's outside the scope of this post.</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">USE</span> <span style="color: blue;">master<span style="color: gray;">;</span> </span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">Create</span> <span style="color: blue;">Database</span> <span style="color: teal;">TestEncryption01</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">USE</span> <span style="color: teal;">TestEncryption01<span style="color: gray;">;</span> </span></span></p>
<p><span style="color: blue; font-family: Lucida Console; font-size: 10pt;">GO </span></p>
<p>&nbsp;</p>
<p><span style="color: green; font-family: Lucida Console; font-size: 10pt;">--Create the seniorEmployees table </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">IF</span> <span style="color: gray;">EXISTS<span style="color: blue;"> <span style="color: gray;">(<span style="color: blue;">SELECT</span> <span style="color: teal;">name</span> <span style="color: blue;">from</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">tables</span> <span style="color: blue;">where</span> <span style="color: teal;">name<span style="color: gray;">=<span style="color: red;">'SeniorEmployees'<span style="color: gray;">)</span> </span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">DROP</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">SeniorEmployees</span> </span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">SeniorEmployees</span> </span></span></span></p>
<p><span style="color: blue; font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">(</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">ID</span> <span style="color: blue;">int</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">EmployeeID</span> <span style="color: blue;">varbinary<span style="color: gray;">(<span style="color: fuchsia;">MAX<span style="color: gray;">)</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> </span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">EmployeeSalary</span> <span style="color: blue;">decimal<span style="color: gray;">(</span>8<span style="color: gray;">,</span> 2<span style="color: gray;">)</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> </span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">EmployeePosition</span> <span style="color: blue;">varbinary<span style="color: gray;">(<span style="color: fuchsia;">MAX<span style="color: gray;">)</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL</span> </span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">)</span> <span style="color: blue;">ON</span> <span style="color: teal;">[PRIMARY]</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">TEXTIMAGE_ON</span> <span style="color: teal;">[PRIMARY]</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">ALTER</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">SeniorEmployees</span> <span style="color: blue;">ADD</span> <span style="color: blue;">CONSTRAINT</span> </span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">PK_SeniorEmployees</span> <span style="color: blue;">PRIMARY</span> <span style="color: blue;">KEY</span> <span style="color: blue;">CLUSTERED</span> </span></p>
<p><span style="color: blue; font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">(</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">ID</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">)</span> <span style="color: blue;">WITH<span style="color: gray;">(</span> STATISTICS_NORECOMPUTE</span> <span style="color: gray;">=</span> <span style="color: blue;">OFF<span style="color: gray;">,</span> IGNORE_DUP_KEY</span> <span style="color: gray;">=</span> <span style="color: blue;">OFF<span style="color: gray;">,</span> ALLOW_ROW_LOCKS</span> <span style="color: gray;">=</span> <span style="color: blue;">ON<span style="color: gray;">,</span> ALLOW_PAGE_LOCKS</span> <span style="color: gray;">=</span> <span style="color: blue;">ON<span style="color: gray;">)</span> ON</span> <span style="color: teal;">[PRIMARY] </span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">--CREATE database master key</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">MASTER</span> <span style="color: blue;">KEY</span> <span style="color: blue;">ENCRYPTION</span> <span style="color: blue;">BY</span> <span style="color: blue;">PASSWORD</span> <span style="color: gray;">=<span style="color: red;">'Str0ngPassw0rd!' </span></span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">--Create certificate first</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">CERTIFICATE</span> <span style="color: teal;">EmployeesCert</span> <span style="color: blue;">WITH</span> <span style="color: blue;">SUBJECT<span style="color: gray;">=<span style="color: red;">'Certificate for encrypting symmetric keys'<span style="color: gray;">; </span></span></span></span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">--check the certificate</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">SELECT</span> <span style="color: gray;">*</span> <span style="color: blue;">FROM</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">certificates </span></span></span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">--now create the symmetric key</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">SYMMETRIC</span> <span style="color: blue;">KEY</span> <span style="color: teal;">EmployeesKey</span> <span style="color: blue;">WITH</span> <span style="color: blue;">ALGORITHM</span> <span style="color: gray;">=</span> <span style="color: blue;">AES_256</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">ENCRYPTION</span> <span style="color: blue;">BY</span> <span style="color: blue;">CERTIFICATE</span> <span style="color: teal;">EmployeesCert<span style="color: gray;">; </span></span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">--Key meta data</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">SELECT</span> <span style="color: gray;">*</span> <span style="color: blue;">FROM</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">symmetric_keys</span> <span style="color: blue;">WHERE</span> <span style="color: teal;">name<span style="color: gray;">=<span style="color: red;">'EmployeesKey' </span></span></span></span></span></span></p>
<p>&nbsp;</p>
<p>Now let's stop here a moment to create a quick trace. I used a small trace definition to track the SQL:batchCompleted event, below is the output for the definition, the trace filters by the database name that we created above.</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">-- Create a Queue</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">declare</span> <span style="color: teal;">@rc</span> <span style="color: blue;">int</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">declare</span> <span style="color: teal;">@TraceID</span> <span style="color: blue;">int</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">declare</span> <span style="color: teal;">@maxfilesize</span> <span style="color: blue;">bigint</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">set</span> <span style="color: teal;">@maxfilesize</span> <span style="color: gray;">=</span> 5 </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">-- Please replace the text InsertFileNameHere, with an appropriate</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">-- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">-- will be appended to the filename automatically. If you are writing from</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">-- remote server to local drive, please use UNC path and make sure server has</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">-- write access to your network share</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">exec</span> <span style="color: teal;">@rc</span> <span style="color: gray;">=</span> <span style="color: maroon;">sp_trace_create<span style="color: blue;"> <span style="color: teal;">@TraceID</span> output<span style="color: gray;">,</span> 0<span style="color: gray;">,</span> <span style="color: red;">N'InsertFileNameHere'<span style="color: gray;">,</span> <span style="color: teal;">@maxfilesize<span style="color: gray;">,</span> <span style="color: gray;">NULL</span> </span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">if <span style="color: gray;">(<span style="color: teal;">@rc</span> !=</span> 0<span style="color: gray;">)</span> goto</span> <span style="color: teal;">error</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">-- Client side File and Table cannot be scripted</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">-- Set the events</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">declare</span> <span style="color: teal;">@on</span> <span style="color: blue;">bit</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">set</span> <span style="color: teal;">@on</span> <span style="color: gray;">=</span> 1 </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">exec</span> <span style="color: maroon;">sp_trace_setevent<span style="color: blue;"> <span style="color: teal;">@TraceID<span style="color: gray;">,</span> 12<span style="color: gray;">,</span> 1<span style="color: gray;">,</span> @on</span> </span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">exec</span> <span style="color: maroon;">sp_trace_setevent<span style="color: blue;"> <span style="color: teal;">@TraceID<span style="color: gray;">,</span> 12<span style="color: gray;">,</span> 11<span style="color: gray;">,</span> @on</span> </span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">exec</span> <span style="color: maroon;">sp_trace_setevent<span style="color: blue;"> <span style="color: teal;">@TraceID<span style="color: gray;">,</span> 12<span style="color: gray;">,</span> 12<span style="color: gray;">,</span> @on</span> </span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">exec</span> <span style="color: maroon;">sp_trace_setevent<span style="color: blue;"> <span style="color: teal;">@TraceID<span style="color: gray;">,</span> 12<span style="color: gray;">,</span> 13<span style="color: gray;">,</span> @on</span> </span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">exec</span> <span style="color: maroon;">sp_trace_setevent<span style="color: blue;"> <span style="color: teal;">@TraceID<span style="color: gray;">,</span> 12<span style="color: gray;">,</span> 16<span style="color: gray;">,</span> @on</span> </span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">exec</span> <span style="color: maroon;">sp_trace_setevent<span style="color: blue;"> <span style="color: teal;">@TraceID<span style="color: gray;">,</span> 12<span style="color: gray;">,</span> 35<span style="color: gray;">,</span> @on</span> </span></span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">-- Set the Filters</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">declare</span> <span style="color: teal;">@intfilter</span> <span style="color: blue;">int</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">declare</span> <span style="color: teal;">@bigintfilter</span> <span style="color: blue;">bigint</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">exec</span> <span style="color: maroon;">sp_trace_setfilter<span style="color: blue;"> <span style="color: teal;">@TraceID<span style="color: gray;">,</span> 35<span style="color: gray;">,</span> 0<span style="color: gray;">,</span> 6<span style="color: gray;">,</span> <span style="color: red;">N'TestEncryption01'</span> </span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">-- Set the trace status to start</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">exec</span> <span style="color: maroon;">sp_trace_setstatus<span style="color: blue;"> <span style="color: teal;">@TraceID<span style="color: gray;">,</span> 1 </span></span></span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">-- display trace id for future references</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">select</span> <span style="color: teal;">TraceID<span style="color: gray;">=<span style="color: teal;">@TraceID</span> </span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">goto</span> <span style="color: teal;">finish</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">error:</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">select</span> <span style="color: teal;">ErrorCode<span style="color: gray;">=<span style="color: teal;">@rc</span> </span></span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">finish:</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">go</span> </span></p>
<p>&nbsp;</p>
<p>Now after setup the trace and running it, let's go back to management studio to try inserting and selecting from the encrypted column using the keys</p>
<p><span style="color: green; font-family: Lucida Console; font-size: 10pt;">--now use the key to insert some records </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">OPEN</span> <span style="color: blue;">SYMMETRIC</span> <span style="color: blue;">KEY</span> <span style="color: teal;">EmployeesKey</span> <span style="color: blue;">DECRYPTION</span> <span style="color: blue;">BY</span> <span style="color: blue;">CERTIFICATE</span> <span style="color: teal;">EmployeesCert<span style="color: gray;">;</span> </span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">DECLARE</span> <span style="color: teal;">@X</span> <span style="color: blue;">INT<span style="color: gray;">=</span>1 </span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">WHILE</span> <span style="color: teal;">@X<span style="color: gray;">&lt;=</span>1000&nbsp;&nbsp;&nbsp;&nbsp; </span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">BEGIN</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">INSERT</span> <span style="color: blue;">INTO</span> <span style="color: teal;">SeniorEmployees<span style="color: gray;">(<span style="color: teal;">ID<span style="color: gray;">,<span style="color: teal;">EmployeeID<span style="color: gray;">,<span style="color: teal;">EmployeeSalary<span style="color: gray;">,<span style="color: teal;">EmployeePosition<span style="color: gray;">)</span> </span></span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">VALUES<span style="color: gray;">(<span style="color: teal;">@X<span style="color: gray;">,<span style="color: fuchsia;">ENCRYPTBYKEY<span style="color: gray;">(<span style="color: fuchsia;">KEY_GUID<span style="color: gray;">(<span style="color: red;">'EmployeesKey'<span style="color: gray;">),<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">NVARCHAR<span style="color: gray;">(</span>20<span style="color: gray;">),<span style="color: fuchsia;">CEILING<span style="color: gray;">(<span style="color: fuchsia;">RAND<span style="color: gray;">()*</span>999999<span style="color: gray;">)),</span>1<span style="color: gray;">,<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">varbinary<span style="color: gray;">,<span style="color: teal;">@X<span style="color: gray;">)),</span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: fuchsia;">CEILING<span style="color: gray;">(<span style="color: fuchsia;">RAND<span style="color: gray;">()*</span>999999<span style="color: gray;">),</span> </span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: fuchsia;">ENCRYPTBYKEY<span style="color: gray;">(<span style="color: fuchsia;">KEY_GUID<span style="color: gray;">(<span style="color: red;">'EmployeesKey'<span style="color: gray;">),<span style="color: red;">N'BOSS#'</span> +</span> <span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">NCHAR<span style="color: gray;">(</span>4<span style="color: gray;">),<span style="color: teal;">@X<span style="color: gray;">),</span>1<span style="color: gray;">,<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">varbinary<span style="color: gray;">,<span style="color: teal;">@X<span style="color: gray;">)))</span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">SET</span> <span style="color: teal;">@X<span style="color: gray;">=<span style="color: teal;">@X<span style="color: gray;">+</span>1<span style="color: gray;">;</span> </span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">END</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">CLOSE</span> <span style="color: blue;">SYMMETRIC</span> <span style="color: blue;">KEY</span> <span style="color: teal;">EmployeesKey<span style="color: gray;">; </span></span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">--Selecting WITH THE KEYS</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">OPEN</span> <span style="color: blue;">SYMMETRIC</span> <span style="color: blue;">KEY</span> <span style="color: teal;">EmployeesKey</span> <span style="color: blue;">DECRYPTION</span> <span style="color: blue;">BY</span> <span style="color: blue;">CERTIFICATE</span> <span style="color: teal;">EmployeesCert<span style="color: gray;">;</span> </span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">SELECT</span> <span style="color: teal;">ID<span style="color: gray;">,<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">NVARCHAR<span style="color: gray;">(</span>20<span style="color: gray;">),<span style="color: fuchsia;">DECRYPTBYKEY<span style="color: gray;">(<span style="color: teal;">EmployeeID<span style="color: gray;">,</span>1<span style="color: gray;">,<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">VARBINARY<span style="color: gray;">,<span style="color: teal;">ID<span style="color: gray;">)))</span> <span style="color: blue;">as</span> <span style="color: red;">'EmployeeID'<span style="color: gray;">,<span style="color: teal;">EmployeeSalary<span style="color: gray;">,</span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p><span style="color: fuchsia; font-family: Lucida Console; font-size: 10pt;">CONVERT<span style="color: gray;">(<span style="color: blue;">NVARCHAR<span style="color: gray;">(</span>20<span style="color: gray;">),<span style="color: fuchsia;">DECRYPTBYKEY<span style="color: gray;">(<span style="color: teal;">EmployeePosition<span style="color: gray;">,</span>1<span style="color: gray;">,<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">VARBINARY<span style="color: gray;">,<span style="color: teal;">ID<span style="color: gray;">)))</span> <span style="color: blue;">as</span> <span style="color: red;">'EmployeePosition'</span> </span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">FROM</span> <span style="color: teal;">SeniorEmployees</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">CLOSE</span> <span style="color: blue;">SYMMETRIC</span> <span style="color: blue;">KEY</span> <span style="color: teal;">EmployeesKey<span style="color: gray;">; </span></span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>In the trace when we open the file in the profiler here's what we will get. The statement removed for both the insert and select statement and replaced by comments for protecting the encrypted data.</p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/48/69/metablogapi/7801.121813_1506_Viewingencr1.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-48-69-metablogapi/7801.121813_5F00_1506_5F00_Viewingencr1.png" alt="" /></p>
<p>&nbsp;</p>
<p>Same goes for the EncryptByPassPhrase function as well, continue with the remaining sample while running the trace</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">/*</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">Encrypt by pass phrase. </span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">*/</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">--Create a new table</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">IF</span> <span style="color: gray;">EXISTS<span style="color: blue;"> <span style="color: gray;">(<span style="color: blue;">SELECT</span> <span style="color: teal;">name</span> <span style="color: blue;">from</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">tables</span> <span style="color: blue;">where</span> <span style="color: teal;">name<span style="color: gray;">=<span style="color: red;">'SeniorEmployees2'<span style="color: gray;">)</span> </span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">DROP</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">SeniorEmployees2</span> </span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">SeniorEmployees2</span> </span></span></span></p>
<p><span style="color: blue; font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">(</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">ID</span> <span style="color: blue;">int</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">EmployeeID</span> <span style="color: blue;">varbinary<span style="color: gray;">(<span style="color: fuchsia;">MAX<span style="color: gray;">)</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> </span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">EmployeeSalary</span> <span style="color: blue;">decimal<span style="color: gray;">(</span>8<span style="color: gray;">,</span> 2<span style="color: gray;">)</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> </span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">EmployeePosition</span> <span style="color: blue;">varbinary<span style="color: gray;">(<span style="color: fuchsia;">MAX<span style="color: gray;">)</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL</span> </span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">)</span> <span style="color: blue;">ON</span> <span style="color: teal;">[PRIMARY]</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">TEXTIMAGE_ON</span> <span style="color: teal;">[PRIMARY]</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">ALTER</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">SeniorEmployees2</span> <span style="color: blue;">ADD</span> <span style="color: blue;">CONSTRAINT</span> </span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">PK_SeniorEmployees2</span> <span style="color: blue;">PRIMARY</span> <span style="color: blue;">KEY</span> <span style="color: blue;">CLUSTERED</span> </span></p>
<p><span style="color: blue; font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">(</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">ID</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">)</span> <span style="color: blue;">WITH<span style="color: gray;">(</span> STATISTICS_NORECOMPUTE</span> <span style="color: gray;">=</span> <span style="color: blue;">OFF<span style="color: gray;">,</span> IGNORE_DUP_KEY</span> <span style="color: gray;">=</span> <span style="color: blue;">OFF<span style="color: gray;">,</span> ALLOW_ROW_LOCKS</span> <span style="color: gray;">=</span> <span style="color: blue;">ON<span style="color: gray;">,</span> ALLOW_PAGE_LOCKS</span> <span style="color: gray;">=</span> <span style="color: blue;">ON<span style="color: gray;">)</span> ON</span> <span style="color: teal;">[PRIMARY]</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">/*Insert by encryptByPassPhrase*/</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">DECLARE</span> <span style="color: teal;">@X</span> <span style="color: blue;">INT<span style="color: gray;">=</span>1 </span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">WHILE</span> <span style="color: teal;">@X<span style="color: gray;">&lt;=</span>1000&nbsp;&nbsp;&nbsp;&nbsp; </span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">BEGIN</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">INSERT</span> <span style="color: blue;">INTO</span> <span style="color: teal;">SeniorEmployees2<span style="color: gray;">(<span style="color: teal;">ID<span style="color: gray;">,<span style="color: teal;">EmployeeID<span style="color: gray;">,<span style="color: teal;">EmployeeSalary<span style="color: gray;">,<span style="color: teal;">EmployeePosition<span style="color: gray;">)</span> </span></span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">VALUES<span style="color: gray;">(<span style="color: teal;">@X<span style="color: gray;">,<span style="color: fuchsia;">EncryptByPassPhrase<span style="color: gray;">(<span style="color: red;">'This is My Pass Phrase'<span style="color: gray;">,<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">NVARCHAR<span style="color: gray;">(</span>20<span style="color: gray;">),<span style="color: fuchsia;">CEILING<span style="color: gray;">(<span style="color: fuchsia;">RAND<span style="color: gray;">()*</span>999999<span style="color: gray;">)),</span>1<span style="color: gray;">,<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">varbinary<span style="color: gray;">,<span style="color: teal;">@X<span style="color: gray;">)),</span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: fuchsia;">CEILING<span style="color: gray;">(<span style="color: fuchsia;">RAND<span style="color: gray;">()*</span>999999<span style="color: gray;">),</span> </span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: fuchsia;">EncryptByPassPhrase<span style="color: gray;">(<span style="color: red;">'This is My Pass Phrase'<span style="color: gray;">,<span style="color: red;">N'BOSS#'</span> +</span> <span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">NCHAR<span style="color: gray;">(</span>4<span style="color: gray;">),<span style="color: teal;">@X<span style="color: gray;">),</span>1<span style="color: gray;">,<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">varbinary<span style="color: gray;">,<span style="color: teal;">@X<span style="color: gray;">)))</span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">SET</span> <span style="color: teal;">@X<span style="color: gray;">=<span style="color: teal;">@X<span style="color: gray;">+</span>1<span style="color: gray;">;</span> </span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">END</span> </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">/*Select by DecryptByPassPhrase */</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">SELECT</span> <span style="color: teal;">ID<span style="color: gray;">,<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">NVARCHAR<span style="color: gray;">(</span>20<span style="color: gray;">),<span style="color: fuchsia;">DecryptByPassPhrase<span style="color: gray;">(<span style="color: red;">'This is My Pass Phrase'<span style="color: gray;">,<span style="color: teal;">EmployeeID<span style="color: gray;">,</span>1<span style="color: gray;">,<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">VARBINARY<span style="color: gray;">,<span style="color: teal;">ID<span style="color: gray;">)))</span> <span style="color: blue;">as</span> <span style="color: red;">'EmployeeID'<span style="color: gray;">,<span style="color: teal;">EmployeeSalary<span style="color: gray;">,</span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p><span style="color: fuchsia; font-family: Lucida Console; font-size: 10pt;">CONVERT<span style="color: gray;">(<span style="color: blue;">NVARCHAR<span style="color: gray;">(</span>20<span style="color: gray;">),<span style="color: fuchsia;">DecryptByPassPhrase<span style="color: gray;">(<span style="color: red;">'This is My Pass Phrase'<span style="color: gray;">,<span style="color: teal;">EmployeePosition<span style="color: gray;">,</span>1<span style="color: gray;">,<span style="color: fuchsia;">CONVERT<span style="color: gray;">(<span style="color: blue;">VARBINARY<span style="color: gray;">,<span style="color: teal;">ID<span style="color: gray;">)))</span> <span style="color: blue;">as</span> <span style="color: red;">'EmployeePosition'</span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">FROM</span> <span style="color: teal;">SeniorEmployees2 </span></span></p>
<p>&nbsp;</p>
<p>And here's the view from the trace.</p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/48/69/metablogapi/5165.121813_1506_Viewingencr2.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-48-69-metablogapi/5165.121813_5F00_1506_5F00_Viewingencr2.png" alt="" /></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/48/69/metablogapi/0474.121813_1506_Viewingencr3.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-48-69-metablogapi/0474.121813_5F00_1506_5F00_Viewingencr3.png" alt="" /></p>
<p>&nbsp;</p>
<p>And for Extended Events as well, here's a quick screenshot of the XE outoupt</p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/48/69/metablogapi/2084.121813_1506_Viewingencr4.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-48-69-metablogapi/2084.121813_5F00_1506_5F00_Viewingencr4.png" alt="" /></p>]]></content:encoded>
    </item>
    <item>
      <title>Select everything from every table in every database</title>
      <link>https://docs.microsoft.com/archive/blogs/mosharaf/select-everything-from-every-table-in-every-database</link>
      <pubDate>Wed, 23 Oct 2013 14:34:00 GMT</pubDate>
      <dc:creator><![CDATA[Mohamed]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mosharaf/2013/10/23/select-everything-from-every-table-in-every-database/</guid>
      <description><![CDATA[[Caution: This is for testing only if you want to have a cached dataset, will have severe issues in...]]></description>
      <content:encoded><![CDATA[<p><span style="color: red;"><strong>[Caution: This is for testing only if you want to have a cached dataset, will have severe issues in production servers if you just executed it for trial] </strong></span></p>
<p>&nbsp;</p>
<p>We all know the undocumented (then it's not supported) procedure that executes a code in every database which is <span style="color: maroon; font-family: Lucida Console;"><span style="font-size: 12pt;">sp_msforeachdb</span></span></p>
<p>And its twin that executes against all tables in one database which is <span style="color: maroon; font-family: Lucida Console; font-size: 12pt;">sp_msforeachtable </span>but sometimes we want to mix them together and it's challenging because the replacement character in both is "? " But there's a way to change it and I thought it's nice to share</p>
<p>&nbsp;</p>
<p>The sp_msforeachtable takes another parameter for the replacement character so you can change it</p>
<p>&nbsp;</p>
<p>So this statement will work for one database</p>
<p><span style="font-family: Lucida Console; font-size: 12pt;"><span style="color: blue;">exec</span> <span style="color: maroon;">sp_msforeachtable<span style="color: blue;"> <span style="color: red;">'select * from $'<span style="color: gray;">,</span> '$'</span> </span></span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>And this one will get you everything from ALL databases</p>
<p><span style="font-family: Lucida Console; font-size: 12pt;"><span style="color: blue;">exec</span> <span style="color: maroon;">sp_msforeachdb<span style="color: blue;"> <span style="color: red;">'USE ?;exec sp_msforeachTable ''select * from !'',''!'''<span style="color: gray;">;</span> </span></span></span></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Inequality predicates do not trigger scans</title>
      <link>https://docs.microsoft.com/archive/blogs/mosharaf/inequality-predicates-do-not-trigger-scans</link>
      <pubDate>Mon, 14 Oct 2013 04:54:00 GMT</pubDate>
      <dc:creator><![CDATA[Mohamed]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mosharaf/2013/10/14/inequality-predicates-do-not-trigger-scans/</guid>
      <description><![CDATA[I see sometimes there is an understanding that when using inequality operators in the where clause,...]]></description>
      <content:encoded><![CDATA[<p>I see sometimes there is an understanding that when using inequality operators in the where clause, this will make the query optimizer decide to do a table scan for the table or index and will not use seek operations. Sometimes this is called non-SARGable predicate. This blog post is to discuss this understanding, but if you do not have time to read it until the end then this is not true, at least for SQL Server 2008 onward.</p>
<p>For testing this, I created a sample database in a SQL 2012 instance; I used the DMF sys.dm_db_database_page_allocations just to build some data which is only available in 2012 only. You can however copy the objectsPages table to SQL 2008/R2 and you will get the same results. The code below</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">IF</span> <span style="color: fuchsia;">DB_ID<span style="color: gray;">(<span style="color: red;">'inequality'<span style="color: gray;">)</span> <span style="color: gray;">IS</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL</span> </span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">DROP</span> <span style="color: blue;">DATABASE</span> <span style="color: teal;">inequality<span style="color: gray;">;</span> </span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">DATABASE</span> <span style="color: teal;">inequality<span style="color: gray;">;</span> </span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">USE</span> <span style="color: teal;">inequality<span style="color: gray;">;</span> </span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">GO</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">IF</span> <span style="color: gray;">EXISTS(<span style="color: blue;">SELECT</span> <span style="color: teal;">name</span> <span style="color: blue;">FROM</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">tables</span> <span style="color: blue;">WHERE</span> <span style="color: teal;">name<span style="color: gray;">=<span style="color: red;">'objectsPages'<span style="color: gray;">)</span> </span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">DROP</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">objectsPages<span style="color: gray;">;</span> </span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">objectsPages</span> </span></p>
<p><span style="color: gray; font-family: Lucida Console; font-size: 10pt;">(<span style="color: fuchsia;">object_id</span> <span style="color: blue;">bigint<span style="color: gray;">,<span style="color: teal;">index_id</span> <span style="color: blue;">int<span style="color: gray;">,<span style="color: teal;">allocation_unit_id</span> <span style="color: blue;">bigint<span style="color: gray;">,<span style="color: teal;">allocation_unit_type_desc</span> <span style="color: blue;">nvarchar<span style="color: gray;">(</span>50<span style="color: gray;">),<span style="color: teal;">allocated_page_iam_page_id</span> <span style="color: blue;">bigint<span style="color: gray;">,</span> </span></span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"> <span style="color: teal;">allocated_page_page_id</span> <span style="color: blue;">bigint</span> <span style="color: blue;">PRIMARY</span> <span style="color: blue;">KEY<span style="color: gray;">,</span> <span style="color: teal;">is_allocated</span> bit<span style="color: gray;">,<span style="color: teal;">is_iam_page</span> <span style="color: blue;">bit<span style="color: gray;">,<span style="color: teal;">is_mixed_page_allocation</span> <span style="color: blue;">bit<span style="color: gray;">)</span> </span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">GO</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">INSERT</span> <span style="color: teal;">objectsPages</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">SELECT</span> <span style="color: fuchsia;">object_id<span style="color: gray;">,<span style="color: teal;">index_id<span style="color: gray;">,<span style="color: teal;">allocation_unit_id<span style="color: gray;">,<span style="color: teal;">allocation_unit_type_desc<span style="color: gray;">,</span> </span></span></span></span></span></span></span></span></p>
<p><span style="color: teal; font-family: Lucida Console; font-size: 10pt;">allocated_page_iam_page_id<span style="color: gray;">,<span style="color: teal;">allocated_page_page_id<span style="color: gray;">,<span style="color: teal;">is_allocated<span style="color: gray;">,<span style="color: teal;">is_iam_page<span style="color: gray;">,<span style="color: teal;">is_mixed_page_allocation</span> </span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"> <span style="color: blue;">FROM</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: teal;">dm_db_database_page_allocations<span style="color: gray;">(<span style="color: fuchsia;">DB_ID<span style="color: gray;">(<span style="color: red;">'AdventureWorks2012'<span style="color: gray;">),null,null,null,<span style="color: red;">'Limited'<span style="color: gray;">);</span> </span></span></span></span></span></span></span></span></span></span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">GO</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: green;">--Create indexes</span> </span></p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">INDEX</span> <span style="color: teal;">is_iam_page</span> <span style="color: blue;">ON</span> <span style="color: teal;">objectsPages<span style="color: gray;">(<span style="color: teal;">is_iam_page<span style="color: gray;">);</span> </span></span></span></span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">INDEX</span> <span style="color: teal;">is_mixed_page_allocation</span> <span style="color: blue;">ON</span> <span style="color: teal;">objectsPages<span style="color: gray;">(<span style="color: teal;">is_mixed_page_allocation<span style="color: gray;">); </span></span></span></span></span></p>
<p>&nbsp;</p>
<p>To discuss the SARGability or whether or not SQL Server will consider a predicate as searchable or not, we have to differentiate between two operations</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;-SQL Server is doing scan (table or index doesn't matter) because the Query Optimizer doesn't know how many rows will be returned</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;-SQL Server is doing scan because SQL <span style="text-decoration: underline;"><em>knows</em></span> how many rows to be returned BUT the cost of scanning is calculated to be less than the cost of seeking</p>
<p>The first case, SQL Server consider the predicate non-SARGable and the second case, SQL Server is capable of seeking but decided to do a scan. Although the output is the same (scan operation) but the reason is totally different.</p>
<p>&nbsp;</p>
<p>Now if we tried two queries like</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">SELECT</span> <span style="color: teal;">allocated_page_page_id</span> <span style="color: blue;">FROM</span> <span style="color: teal;">objectsPages</span> <span style="color: blue;">WHERE</span> <span style="color: teal;">is_iam_page</span> <span style="color: gray;">=</span> 1<span style="color: gray;">;</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">SELECT</span> <span style="color: teal;">allocated_page_page_id</span> <span style="color: blue;">FROM</span> <span style="color: teal;">objectsPages</span> <span style="color: blue;">WHERE</span> <span style="color: teal;">is_iam_page</span> <span style="color: gray;">&lt;&gt;</span> 1<span style="color: gray;">; </span></span></p>
<p>&nbsp;</p>
<p>The execution plan will be as below. Note that in both cases SQL Server used seeks not scanning, and SQL Server can tell the expected number of Rows because we have statistics created with the index and SQL can get the estimates. However SQL Server has to convert the query to be equality by using seek predicates of &lt; and &gt; instead of &lt;&gt; (not equal). You can check that in the seek predicate in the index seek. The part of the execution plan marked with the blue marker is the part where SQL Server prepare the values for &gt; and &lt;</p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/48/69/metablogapi/7142.101413_1154_Inequalityc1.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-48-69-metablogapi/7142.101413_5F00_1154_5F00_Inequalityc1.png" alt="" /></p>
<p>&nbsp;</p>
<p>But if we changed the query a little bit and instead of only selecting the clustered index key; selecting the whole table</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">SELECT</span> <span style="color: gray;">*</span> <span style="color: blue;">FROM</span> <span style="color: teal;">objectsPages</span> <span style="color: blue;">WHERE</span> <span style="color: teal;">is_iam_page</span> <span style="color: gray;">=</span> 1<span style="color: gray;">;</span> </span></p>
<p>&nbsp;</p>
<p><span style="font-family: Lucida Console; font-size: 10pt;"><span style="color: blue;">SELECT</span> <span style="color: gray;">*</span> <span style="color: blue;">FROM</span> <span style="color: teal;">objectsPages</span> <span style="color: blue;">WHERE</span> <span style="color: teal;">is_iam_page</span> <span style="color: gray;">&lt;&gt;</span> 1<span style="color: gray;">; </span></span></p>
<p>&nbsp;</p>
<p>The execution plan shows in both cases that SQL Server didn't use the predicate and decided to scan the table because the cost of scanning is estimated to be lower than the cost of index seek + key lockups operations. Whether or not we are using inequality is irrelevant in that case.</p>
<p>&nbsp;</p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/48/69/metablogapi/3365.101413_1154_Inequalityc2.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-48-69-metablogapi/3365.101413_5F00_1154_5F00_Inequalityc2.png" alt="" /></p>]]></content:encoded>
    </item>
    <item>
      <title>Lenovo W530 doesn’t duplicate screens as expected</title>
      <link>https://docs.microsoft.com/archive/blogs/mosharaf/lenovo-w530-doesnt-duplicate-screens-as-expected</link>
      <pubDate>Sat, 23 Mar 2013 09:43:09 GMT</pubDate>
      <dc:creator><![CDATA[Mohamed]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mosharaf/2013/03/23/lenovo-w530-doesnt-duplicate-screens-as-expected/</guid>
      <description><![CDATA[I recently faced this problem with my new Lenovo W530. On Windows 8 when using win+P to duplicate my...]]></description>
      <content:encoded><![CDATA[<p>I recently faced this problem with my new Lenovo W530. On Windows 8 when using win+P to duplicate my display on a projector for a demo, it doesn't work. Only extended works. 
</p><p>I found this blog post that describe the same problem and it has good solution for it. I used the solution 1. The only difference is in my NVidia control panel, it says clone instead of duplicate. I'm adding that blog post here for reminding me and to share it in case there are others facing the same problem. 
</p><p><a href="http://bretstateham.com/lenovo-w520-and-an-external-projector-with-nvidia-quadro-2000m-and-nvidia-optimus/">http://bretstateham.com/lenovo-w520-and-an-external-projector-with-nvidia-quadro-2000m-and-nvidia-optimus/</a>
	</p>]]></content:encoded>
    </item>
    <item>
      <title>New columns in exec_sessions DMV in SQL 2012</title>
      <link>https://docs.microsoft.com/archive/blogs/mosharaf/new-columns-in-exec_sessions-dmv-in-sql-2012</link>
      <pubDate>Tue, 19 Feb 2013 09:57:12 GMT</pubDate>
      <dc:creator><![CDATA[Mohamed]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mosharaf/2013/02/19/new-columns-in-exec_sessions-dmv-in-sql-2012/</guid>
      <description><![CDATA[While reproducing the issue with DBCC OpenTran I wrote about earlier this week here I found a...]]></description>
      <content:encoded><![CDATA[<p><span style="font-size:12pt">While reproducing the issue with DBCC OpenTran I wrote about earlier this week <a href="http://blogs.msdn.com/b/mosharaf/archive/2013/02/17/when-dbcc-opentran-doesn-t-list-all-open-transactions.aspx">here</a> I found a pleasant addition to the sys.dm_exec_sessions DMV. In SQL 2012 we have 3 new columns added
</span></p><p><span style="font-size:12pt"><span style="background-color:yellow">Open_transaction_count</span>: which shows the number of open transactions for the session whether or not these transactions are reading or writing. So an open transaction that you cannot see in DBCC OpenTran will be counted here. Similar to open_tran in sys.sysprocesses.
</span></p><p><span style="font-size:12pt"><span style="background-color:yellow">Database_id</span>: The long awaited column in exec_sessions is finally here. No need for sp_who2 any more to know which session connected to which database. Of course to show to database name you need to use db_name() function.
</span></p><p><span style="font-size:12pt"><span style="background-color:yellow">Authenticating_database_id</span>: This columns was added to support the <a href="http://technet.microsoft.com/en-us/library/ff929071.aspx">contained databases</a>. It contains the database_id of the database that did the authentication for the session. If it's a login then it should show the database_id for the master database (1) otherwise it will show the database_id of the contained database that has the user with password. The <a href="http://msdn.microsoft.com/en-us/library/ms176013.aspx">sys.dm_exec_sessions documentation</a> documented this column as it will have 0 when it's a login but I tested it and it shows 1. I'll try to file a documentation bug for this one. 
</span></p><p>
 </p><p><span style="font-size:12pt">~M.S. 
</span></p>]]></content:encoded>
    </item>
    <item>
      <title>When DBCC OpenTran doesn’t list all open transactions</title>
      <link>https://docs.microsoft.com/archive/blogs/mosharaf/when-dbcc-opentran-doesnt-list-all-open-transactions</link>
      <pubDate>Sun, 17 Feb 2013 18:58:00 GMT</pubDate>
      <dc:creator><![CDATA[Mohamed]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mosharaf/2013/02/17/when-dbcc-opentran-doesnt-list-all-open-transactions/</guid>
      <description><![CDATA[Recently I was troubleshooting a customer problem on SQL 2000 instance when there was a hanging...]]></description>
      <content:encoded><![CDATA[<p><span style="font-size: 13pt;">Recently I was troubleshooting a customer problem on SQL 2000 instance when there was a hanging session that locking tens of sessions on the server. The customer was suspecting that there was an open transaction for days which causes this problem but when using DBCC OpenTran it doesn't show any transaction. </span></p>
<p><span style="font-size: 13pt;">However when checking sysprocesses I see there's a session with open_tran =1. And this behavior is reproducible in SQL 2012 as well so it's not related to SQL 2000. So why DBCC OpenTran doesn't show it? </span></p>
<p><span style="font-size: 13pt;">Well, it's not clear in in the <a href="http://msdn.microsoft.com/en-us/library/ms182792.aspx">DBCC OpenTran documentation</a> but DBCC OpenTran shows the open transactions by reading the Transaction log and gets the open transactions from it. If the transaction is not listed in the Transaction Log it will not be listed by DBCC OpenTran. In this case the open transaction was a select transaction that it doesn't show up in the transaction log so the DBCC OpenTran didn't list it however you can get it using sysprocesses. Also after troubleshooting this issue I found that the select transaction is using Serializable transaction isolation level which caused lots of range locks which blocked the index maintenance job and in return blocked many other processes. </span></p>
<p><span style="font-size: 13pt;">To reproduce the problem you can do this by opening two sessions to AdventureWorks sample database </span></p>
<p><span style="font-size: 13pt;">P1 </span></p>
<p style="background: #d9d9d9;"><span style="font-family: Tahoma; font-size: 15pt;"><span style="color: blue;">Set</span> <span style="color: blue;">Transaction</span> <span style="color: blue;">Isolation</span> <span style="color: blue;">Level</span> <span style="color: blue;">Serializable<span style="color: gray;">;</span> </span></span></p>
<p style="background: #d9d9d9;"><span style="font-family: Tahoma; font-size: 15pt;"><span style="color: blue;">go</span> </span></p>
<p style="background: #d9d9d9;"><span style="font-family: Tahoma; font-size: 15pt;"><span style="color: blue;">Begin</span> <span style="color: blue;">Tran</span> </span></p>
<p style="background: #d9d9d9;"><span style="font-family: Tahoma; font-size: 15pt;"><span style="color: blue;">Select</span> <span style="color: gray;">*</span> <span style="color: blue;">from</span> <span style="color: teal;">HumanResources<span style="color: gray;">.<span style="color: teal;">Department<span style="color: gray;">; </span></span></span></span></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 13pt;">P2 </span></p>
<p style="background: #d9d9d9;"><span style="font-family: Tahoma; font-size: 15pt;"><span style="color: blue;">DBCC</span> <span style="color: teal;">OpenTran<span style="color: gray;">(<span style="color: red;">'AdventureWorks2008R2'<span style="color: gray;">);</span> <span style="color: green;">--reads from the T-Log</span> </span></span></span></span></p>
<p style="background: #d9d9d9;"><span style="font-family: Tahoma; font-size: 15pt;"><span style="color: blue;">Select</span> <span style="color: gray;">*</span> <span style="color: blue;">from</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">sysprocesses</span> <span style="color: blue;">where</span> <span style="color: teal;">open_tran<span style="color: gray;">&gt;</span>0<span style="color: gray;">; </span></span></span></span></span></p>
<p style="background: #d9d9d9;"><span style="font-family: Tahoma; font-size: 15pt;"><span style="color: blue;">select</span> <span style="color: gray;">*</span> <span style="color: blue;">from</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">dm_tran_locks<span style="color: gray;">; </span></span></span></span></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 13pt;">Also if you are using Simple recovery model you can confirm that P1 didn't leave a trace in the Transaction Log using the undocumented function fn_dblog(). </span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>SQL Server 2008 R2 Express</title>
      <link>https://docs.microsoft.com/archive/blogs/mosharaf/sql-server-2008-r2-express</link>
      <pubDate>Wed, 22 Sep 2010 19:44:00 GMT</pubDate>
      <dc:creator><![CDATA[Mohamed]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mosharaf/2010/09/22/sql-server-2008-r2-express/</guid>
      <description><![CDATA[Good news for&nbsp;independent&nbsp;developers who are using SQL Express as backend database. With...]]></description>
      <content:encoded><![CDATA[<p>Good news for&nbsp;independent&nbsp;developers who are using SQL Express as backend database. With SQL Server 2008 R2, SQL Express supports databases up to 10 GB.</p>
<p>For more information and download, <a target="_blank" href="http://www.microsoft.com/express/Database/" title="http://www.microsoft.com/express/Database/">go to product site</a></p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
  </channel>
</rss>