<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-423583499936205205</atom:id><lastBuildDate>Thu, 09 Apr 2026 23:16:06 +0000</lastBuildDate><category>SQL 2012</category><category>2008 R2</category><category>SQL</category><category>SSAS</category><category>Analysis Services</category><category>example</category><category>performance</category><category>MDX</category><category>Cube</category><category>SSMS</category><category>XMLA</category><category>measures</category><category>ROLLBACK</category><category>THIS</category><category>scope</category><category>slow running</category><category>ERROR</category><category>Process</category><category>RANK</category><category>XML</category><category>columns</category><category>execution</category><category>measure</category><category>partition</category><category>transaction</category><category>trigger</category><category>update</category><category>BIDS</category><category>Batch</category><category>CHECKSUM</category><category>COLUMNS_UPDATED</category><category>DELETE</category><category>DataMember</category><category>FOR XML</category><category>PIVOT</category><category>SQL 2016</category><category>SQL Agent</category><category>SSRS</category><category>UNPIVOT</category><category>Window Functions</category><category>aggregation</category><category>alter</category><category>calculated members</category><category>currentmember</category><category>database</category><category>default</category><category>dimension</category><category>drop</category><category>dynamic</category><category>global variables</category><category>job</category><category>multiple</category><category>optimize</category><category>order</category><category>order by</category><category>partition by</category><category>query</category><category>ranking</category><category>rowcount</category><category>schedule</category><category>schema</category><category>stored procedure</category><category>subcube</category><category>trancount</category><category>variable</category><category>15138</category><category>AFTER</category><category>AddEvent</category><category>AggregationFunction</category><category>Average</category><category>AverageOfChildren</category><category>BCP</category><category>CATCH</category><category>COMMIT</category><category>CPU</category><category>CTE</category><category>Cache</category><category>Clear Cache</category><category>Count</category><category>Create</category><category>DAX</category><category>DISTINCT</category><category>DMV</category><category>DefaultMember</category><category>ETL</category><category>ExternalCommandTimeout</category><category>GUI</category><category>HASHBYTES</category><category>Installation</category><category>MD5</category><category>Multidimensional</category><category>OBJECT_ID</category><category>OVER</category><category>PATH</category><category>PowerPivot</category><category>Process Add</category><category>Process Cube</category><category>RAISERROR</category><category>ROOT</category><category>Reporting Services</category><category>SCD</category><category>SQL Server</category><category>SSDT</category><category>SSIS</category><category>ServerTimeout</category><category>Slice</category><category>TRUNCATE</category><category>TRY</category><category>Tabular</category><category>Time</category><category>True Average</category><category>Type</category><category>Visual Studio</category><category>WAIT</category><category>WHERE</category><category>algorithm</category><category>analytic</category><category>authorisation</category><category>blank</category><category>calculated</category><category>calculation</category><category>calculations</category><category>change</category><category>child</category><category>clause</category><category>clean up</category><category>coalesce</category><category>compare</category><category>complete</category><category>concurrent</category><category>currentcube</category><category>data</category><category>data-driven</category><category>db_owner</category><category>deallocation</category><category>default measure</category><category>deleted</category><category>dependencies</category><category>dummy</category><category>duplicates</category><category>element</category><category>error handling</category><category>errors</category><category>execution plan</category><category>exists</category><category>functions</category><category>hierarchy</category><category>if</category><category>inserted</category><category>is running</category><category>is_ms_shipped</category><category>job activity</category><category>local</category><category>log</category><category>login</category><category>long running query</category><category>manual</category><category>master database</category><category>matrix</category><category>member</category><category>newid</category><category>non empty</category><category>null</category><category>nullprocessing</category><category>option</category><category>original</category><category>parallel</category><category>parameter</category><category>parameters</category><category>parent</category><category>prinipal</category><category>profiler</category><category>query plan</category><category>random</category><category>raw</category><category>recompile</category><category>remove</category><category>remove zeros</category><category>report</category><category>restore</category><category>row number</category><category>row_number</category><category>samples</category><category>score</category><category>script</category><category>sequential</category><category>serial</category><category>showplan</category><category>slowly changing dimension</category><category>sniffing</category><category>sp_settriggerorder</category><category>steps</category><category>subscription</category><category>sum</category><category>sys.schemas</category><category>sysjobs</category><category>sysobjects</category><category>system objects</category><category>table</category><category>table names</category><category>tags</category><category>threading</category><category>throw</category><category>trace</category><category>unique</category><category>uniqueidentifier</category><category>user_id</category><category>user_name</category><category>values</category><category>waiting</category><title>SQL Banana</title><description></description><link>http://sqlbanana.blogspot.com/</link><managingEditor>noreply@blogger.com (SQL Banana)</managingEditor><generator>Blogger</generator><openSearch:totalResults>37</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-6326210212127153973</guid><pubDate>Wed, 18 Jan 2017 14:28:00 +0000</pubDate><atom:updated>2017-01-18T14:30:44.853+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">compare</category><category domain="http://www.blogger.com/atom/ns#">DAX</category><category domain="http://www.blogger.com/atom/ns#">Installation</category><category domain="http://www.blogger.com/atom/ns#">MDX</category><category domain="http://www.blogger.com/atom/ns#">Multidimensional</category><category domain="http://www.blogger.com/atom/ns#">PowerPivot</category><category domain="http://www.blogger.com/atom/ns#">SQL 2016</category><category domain="http://www.blogger.com/atom/ns#">SSAS</category><category domain="http://www.blogger.com/atom/ns#">Tabular</category><title>SQL Server 2016 Analysis Servers  - PowerPivot, Tabular or Multidimensional?</title><description>&lt;div class=&quot;Normal&quot;&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;Having downloaded SQL Server 2016 Developer Edition (it’s free, click&amp;nbsp;&lt;/span&gt;&lt;u&gt;&lt;a href=&quot;http://sqlbanana.blogspot.ch/2017/01/download-sql-2016-developer-edition-for.html&quot;&gt;&lt;span class=&quot;tm8&quot;&gt;here&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&amp;nbsp;to find out how), for the first time in years I went about a full install of the latest version of Analysis Services.&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
There are three modes in which SSAS can be installed, so I thought I’d give a brief overview of each
of them:&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9TMvyKSvcipMzALg0peLgBmCKJK1hKewNrZaDpVcJUatmr95xi8lKodoRFvXwetlAr-8B3_D45N_hP2HrMBMlc5GqJAEzAsVRVoo6LX-A41_blUZH7Crt_S1cgFybE7MOrx2wxEy-ZGA/s1600/SSAS+Modes3.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;301&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9TMvyKSvcipMzALg0peLgBmCKJK1hKewNrZaDpVcJUatmr95xi8lKodoRFvXwetlAr-8B3_D45N_hP2HrMBMlc5GqJAEzAsVRVoo6LX-A41_blUZH7Crt_S1cgFybE7MOrx2wxEy-ZGA/s400/SSAS+Modes3.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;Normal&quot;&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;&lt;/span&gt;&lt;strong&gt;Multidimensional and Data Mining&lt;/strong&gt;&lt;span class=&quot;tm7&quot;&gt; (often referred to just as Multidimensional), &lt;/span&gt;&lt;strong&gt;Tabular Mode &lt;/strong&gt;&lt;span class=&quot;tm7&quot;&gt;and finally &lt;/span&gt;&lt;strong&gt;PowerPivot Mode &lt;/strong&gt;&lt;span class=&quot;tm7&quot;&gt;are the three options given during the installation process.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;Having different modes allows SSAS to offer a more customised solution to closely meet the needs of a wider range of of businesses and users. For example Multidimensional
is well suited for large scale use - many users, large volumes of data, low latency etc, while PowerPivot is aimed more at the individual user and Tabular mode could be said to sit somewhere in between.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;&lt;/span&gt;&lt;strong&gt;Multidimensional Mode&lt;/strong&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;This is the “classic” mode for Analysis Services to be installed in, available in it&#39;s current form since SQL Server 2005 and is a mature product on an enterprise scale.
If you’re already familiar with building OLAP databases then you can skip ahead to the next mode. Otherwise read on.  &lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;In Multidimensional mode cubes, measures, dimensions etc are designed in Visual Studio and deployed to the SSAS (Multidimensional Mode) server. Typically this process is a dedicated
developer set of tasks - you would not expect business users to design the OLAP database.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;Measure groups can be heavily partitioned to support very large volumes of data, only limited by the resources available. Parallel processing of partitions can be implemented to reduce
data latency and improve performance, and security can be customised right down to the cell level. A lot of power and complexity can be built into the model to handle different kinds of relationships and aggregations,  and
additional enhancements can be made using MDX. Many previous posts on this blog have been written covering some of the more advanced features of Multidimensional mode. Also note that MDX is the primary language for querying
databases designed in this mode.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;&lt;/span&gt;&lt;strong&gt;PowerPivot Mode&lt;/strong&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;To quote from MSDN:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;“An Analysis Services server in Power Pivot mode provides server hosting of Power Pivot data in a SharePoint farm”&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;So that is basically the use-case for installing SSAS in PowerPivot mode - if you intend to use PowerPivot (an Excel-addin) to create models and then want to share those models, or schedule
automated refreshes of them say to support regular reporting, then you need to have an SSAS PowerPivot instance. &amp;nbsp;In addition, this will need to be registered with an Office Online Server (a SharePoint 2016 feature, previously this was called Excel Services).&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;PowerPivot models are comparatively quick and easy to design, with far less complexity than the other two modes. Therefore power users on the business side (as opposed to developers)
could be expected to develop and deploy these models themselves.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;However, if you don&#39;t intend to distribute or perform server-style tasks (scheduling, security etc) on your models and they are really just for local use, it is unlikely you’ll
need SSAS in PowerPivot mode. &amp;nbsp;You could instead just use PowerPivot as it comes - as a downloadable Add-in for Excel.&amp;nbsp; &lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;PowerPivot is an end-user tool where users can source data from multiple sources and define their
own relationships and calculations.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;Important Note: While PowerPivot can support significant data compression, there is still a file size limitation of 2Gb, regardless of whether it is being used locally or uploaded and
managed in SharePoint.  So if you are expecting to handle large volumes of data, this might not be the best choice for you. You would do well then to consider Tabular Mode.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;Tabular Mode&lt;/strong&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;Introduced in SQL 2012, Tabular Mode has similar functionality to PowerPivot, except that is more powerful and can handle significantly larger volumes of data. While Multidimensional
mode is considered &quot;matured&quot;, the list of new and improved components in SSAS consists almost entirely of updates to Tabular mode.&amp;nbsp; Expect more features to be added in future releases.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;Tabular mode uses column storage (as opposed to row storage for Mutidimensional).  It therefore compresses data using an entirely different engine, making possible higher compression
rates. This can have significant improvement on things like distinct counts, which have often been a sore point in traditional Multidimensional models, but can be used to significant advantage in Tabular mode.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;In Tabular mode, the data is held in memory so, instead of the 2Gb hard limit of PowerPivot, you are limited only by how much memory is available to you (rather than disk space in Multidimensional Mode), and compression
rates are high. &amp;nbsp;While models are quick and fast to design the power of Tabular (and therefore complexity) is with the DAX language used to create formulas, relationships and quickly
link data together. While Multidimensional (and therefore MDX) is very fast at aggregating data but poor when it comes to accessing leaf level, DAX can handle leaf level data with relative ease and performance, but designing
aggregations and other features such as Custom Rollups are simply not possible.  Other Multidimensional features such as parallel processing are also not available with Tabular, which could impact data latency times and performance
as the database grows in size.  &lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm5 Normal&quot;&gt;
&lt;span class=&quot;tm6&quot;&gt;Note that as Tabular mode is still maturing, it can be expected that Microsoft will work to close the gap between Multidimensional and Tabular modes with each new release.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;In making the decision of which mode is best for you, you will want to consider areas such as expected data volumes, data latency/refresh times, required features, hardware resources available etc.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;tm7&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span class=&quot;tm7&quot;&gt;For a more detailed feature comparison between Multidimensional and Tabular check out the MSDN article here:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;span class=&quot;tm7&quot;&gt;&lt;/span&gt;&lt;u&gt;&lt;a href=&quot;https://msdn.microsoft.com/en-us/library/hh212940.aspx#Anchor_1&quot;&gt;&lt;span class=&quot;tm9&quot;&gt;https://msdn.microsoft.com/en-us/library/hh212940.aspx#Anchor_1&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div class=&quot;tm6 Normal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2017/01/sql-server-2016-analysis-servers.html</link><author>noreply@blogger.com (SQL Banana)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9TMvyKSvcipMzALg0peLgBmCKJK1hKewNrZaDpVcJUatmr95xi8lKodoRFvXwetlAr-8B3_D45N_hP2HrMBMlc5GqJAEzAsVRVoo6LX-A41_blUZH7Crt_S1cgFybE7MOrx2wxEy-ZGA/s72-c/SSAS+Modes3.JPG" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-1749090121692551110</guid><pubDate>Mon, 02 Jan 2017 17:16:00 +0000</pubDate><atom:updated>2017-01-02T17:23:57.312+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">samples</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2016</category><category domain="http://www.blogger.com/atom/ns#">SSAS</category><category domain="http://www.blogger.com/atom/ns#">SSDT</category><category domain="http://www.blogger.com/atom/ns#">SSMS</category><category domain="http://www.blogger.com/atom/ns#">Visual Studio</category><title>Download SQL 2016 Developer Edition for free</title><description>With the release of SQL Server 2016, folks like me get very excited to get their hands on the new software. &amp;nbsp;However, in the past it has always been quite expensive, or very time-limited for an individual to get hold of a full Developer license.&lt;br /&gt;
&lt;br /&gt;
Not so anymore - As part of the Visual Studio Dev Essentials program, a full developer license for SQL Server 2016 SP1 is now available for free. This includes all the tools such as SSMS, SSDT and a host of other products too, including Microsoft R Server and even a few quids worth of Azure is being thrown in.&lt;br /&gt;
&lt;br /&gt;
In order to access the downloads you&#39;ll need to sign up for the program first (free), and then download away:&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://www.visualstudio.com/dev-essentials/&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;https://www.visualstudio.com/dev-essentials/&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Also note that a new product such as SQL 2016 requires new sample databases - say goodbye to AdventureWorks and hello to the brand new Wide World Importers! &amp;nbsp;Download all the latest samples from github here:&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://github.com/Microsoft/sql-server-samples/tree/master/samples/databases/wide-world-importers&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;https://github.com/Microsoft/sql-server-samples/tree/master/samples/databases/wide-world-importers&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Interesting, however, is that there does not appear to be an SSAS solution build on the new samples. &amp;nbsp;I guess very little has changed is SSAS in this release, so MS felt they need not bother. &amp;nbsp;A bit irritating in my opinion, as it would be nice to have a consistent set of samples, and not have to switch back to old solutions to demo SSAS - gives the impression that SSAS is being less and less encouraged/supported. But maybe that is deliberate....&lt;br /&gt;
&lt;br /&gt;
Happy downloading, and Happy New Year!&lt;br /&gt;
&lt;br /&gt;</description><link>http://sqlbanana.blogspot.com/2017/01/download-sql-2016-developer-edition-for.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-5622126088680955166</guid><pubDate>Mon, 14 Dec 2015 14:05:00 +0000</pubDate><atom:updated>2015-12-14T14:05:31.998+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Analysis Services</category><category domain="http://www.blogger.com/atom/ns#">DMV</category><category domain="http://www.blogger.com/atom/ns#">ExternalCommandTimeout</category><category domain="http://www.blogger.com/atom/ns#">long running query</category><category domain="http://www.blogger.com/atom/ns#">MDX</category><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">ServerTimeout</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">SSAS</category><category domain="http://www.blogger.com/atom/ns#">XMLA</category><title>SSAS  - How to Terminate Long Running Queries</title><description>Despite how well you may design your SSAS database or how well you educate your users, in my experience eventually someone will issue queries that take way too much time to complete.&amp;nbsp; It&#39;s usually down to a user forgetting to apply a filter somewhere.&amp;nbsp;&amp;nbsp;They&#39;ll&amp;nbsp;likely be using a front end tool like&amp;nbsp;Excel that allows them&amp;nbsp;to easily drag and drop measures and attributes without too much thought, and may &quot;accidentally&quot; query data for all time rather than just the&amp;nbsp;desired&amp;nbsp;dates.&amp;nbsp;If the cube is partitioned by date (eg by day or month) and contains many years of data then this query could be scanning every single partition in the measure group, leaving the user to go get a coffee while waiting for the results.&lt;br /&gt;
&lt;br /&gt;
(Incidentally, if a trace reveals that more partitions than expected are being scanned than there may be an issue with the &lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2015/09/ssas-slow-running-query-unnecessary.html&quot; target=&quot;_blank&quot;&gt;Slice property&lt;/a&gt;)&lt;br /&gt;
&lt;br /&gt;
Different front end applications will handle this waiting time in different ways, but many will simply appear to&amp;nbsp;hang or freeze.&amp;nbsp; Cue a call from an agitated user: &quot;Every time&amp;nbsp;I run my query the application&amp;nbsp;dies&quot;.&amp;nbsp; This is obviously unacceptable, not to mention the less visible drain on resources it may be causing.&amp;nbsp; But what steps can we take to resolve this?&lt;br /&gt;
&lt;br /&gt;
While it&#39;s important to ensure that users are aware of certain front-end best practices of querying the cube (eg apply filters &lt;em&gt;before&amp;nbsp;&lt;/em&gt;nesting dimensions), there are a number of approaches we can take to quickly resolve the problem and stop the applications hanging.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;1. Identify and Kill the Query&lt;/strong&gt;&lt;br /&gt;
Just like a SQL relational database, SSAS also has DMVs that provide insights into server activity, including connection information.&amp;nbsp; Run the below&amp;nbsp;&lt;strong&gt;MDX &lt;/strong&gt;query:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt; mso-bidi-font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt; mso-bidi-font-family: Consolas;&quot;&gt; SESSION_SPID, &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt; mso-bidi-font-family: Consolas;&quot;&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SESSION_USER_NAME,
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt; mso-bidi-font-family: Consolas;&quot;&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SESSION_CURRENT_DATABASE,
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt; mso-bidi-font-family: Consolas;&quot;&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SESSION_LAST_COMMAND,
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt; mso-bidi-font-family: Consolas;&quot;&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SESSION_CPU_TIME_MS,
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt; mso-bidi-font-family: Consolas;&quot;&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SESSION_ELAPSED_TIME_MS
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt; mso-bidi-font-family: Consolas;&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt; mso-bidi-font-family: Consolas;&quot;&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;$system.discover_sessions&lt;/span&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
From these results, we can identify which session_id is the troublemaker.&amp;nbsp; Once identified, we can investigate further, and, if necessary, manually kill the session. &amp;nbsp;Killing the session can be done with the following XMLA command:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;Cancel&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;xmlns&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2003/engine&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;ConnectionID&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;[CID]&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;ConnectionID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;SessionID&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;[SID]&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;SessionID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;&lt;span style=&quot;mso-tab-count: 1;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;SPID&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;[SPID]&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;SPID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt; mso-layout-grid-align: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;Cancel&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;consolas&amp;quot;; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Note: this command can be used to kill the connection, the session or the SPID - delete as required.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Executing the above command will likely cause an error to be thrown in the users application and in some cases may require them to restart the program.&amp;nbsp; However in most circumstances this is preferable to leaving the application hanging indefinitely.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;strong&gt;2. Dynamically Kill Long Running Queries With Scheduled Jobs&lt;/strong&gt;&lt;/div&gt;
&lt;div&gt;
While the above method is fine if a manual intervention is required, a much slicker approach would be to automate the process.&amp;nbsp; It would be simple enough to create a SQL Agent Job or SSIS package to scan the DMVs&amp;nbsp;in order to&amp;nbsp;detect any queries running for longer than a predefined time.&amp;nbsp; The SPIDs could then be passed dynamically to the XMLA command and killed.&amp;nbsp; An advantage of this approach is that it allows a great deal of flexibility - for example if certain users or certain times year (eg month-end), or&amp;nbsp;certain databases&amp;nbsp;are known to demand the use of unavoidably&amp;nbsp;long running queries, then the cut-off time for killing these sessions could vary based on those parameters.&amp;nbsp; &lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;strong&gt;3. Dynamically Kill Long Running Queries using SSAS Server Properties&lt;/strong&gt;&lt;/div&gt;
&lt;div&gt;
By far the most common approach for handling this problem is to use the SSAS server properties.&amp;nbsp; However, I have seen many occasions in which these properties are misunderstood, which is actually what inspired this post.&amp;nbsp; These properties can be accessed either by right-clicking on the SSAS server in SSMS and selecting Properties (tick the Advance box at the bottom), or they can be accessed directly in the server file msmdsrv.ini:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikobd0ThHRnUBcpj-0njT9RwfnmeXTmqWoojhnQ-KLwrKuKXYsYU_yYmmAizzzyGcWVuWbRQUnFtGYJDdvKAEB1DcLEifIDkVNPGp3Hiq9R98qNbf8w48M4DBKE-v81k2LoV84AEI85b8/s1600/SSAS+properties.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;289&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikobd0ThHRnUBcpj-0njT9RwfnmeXTmqWoojhnQ-KLwrKuKXYsYU_yYmmAizzzyGcWVuWbRQUnFtGYJDdvKAEB1DcLEifIDkVNPGp3Hiq9R98qNbf8w48M4DBKE-v81k2LoV84AEI85b8/s320/SSAS+properties.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;strong&gt;ExternalCommandTimeout&lt;/strong&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyF_gk_mHInJ0qQhF96uH1jXn0fUNfA76yhg5AYv_SKADnbjPGcWBMXDvh-Bam71xBQnLMU34JDjnyHO_XbQ-bNYawJ6CSrKJWa3o9iZ9zNz5hzWOiYpJVJEouJkuG5sqAPQ5l5FrEVog/s1600/ExternalCommandTimeout.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;32&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyF_gk_mHInJ0qQhF96uH1jXn0fUNfA76yhg5AYv_SKADnbjPGcWBMXDvh-Bam71xBQnLMU34JDjnyHO_XbQ-bNYawJ6CSrKJWa3o9iZ9zNz5hzWOiYpJVJEouJkuG5sqAPQ5l5FrEVog/s320/ExternalCommandTimeout.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Often I have seen this setting used to handle this particular problem, however that is a common mistake.&amp;nbsp; This setting relates to OLAP processing performance, not query performance.&amp;nbsp; If a processing query, ie a query sent from SSAS to a source system in order to process dimensions or partitions, has not completed its execution on the source system by the threshold set by this property, then the cube process command will be forced to fail.&lt;/div&gt;
&lt;div&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div&gt;
&lt;strong&gt;ServerTimeout&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQPnhlI2zg5jCDw8YG070sk-3Ahoiw6BObL31aplJzpZ9xMQQZ8M1vOWamFwe-bUyoFrTmTnh-F5fySPTe46aOThyje-UpNPJRgDBoYiLuc_oe5P4cHXw-nugBUPOIJx63gar3FEWfA-Y/s1600/ServerTimeout.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;48&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQPnhlI2zg5jCDw8YG070sk-3Ahoiw6BObL31aplJzpZ9xMQQZ8M1vOWamFwe-bUyoFrTmTnh-F5fySPTe46aOThyje-UpNPJRgDBoYiLuc_oe5P4cHXw-nugBUPOIJx63gar3FEWfA-Y/s320/ServerTimeout.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div&gt;
This is the&amp;nbsp;property, towards the end of the Advanced Properties list,&amp;nbsp;that is relevant for us.&amp;nbsp; Any MDX query on any OLAP database on the server that does not complete within the time set by this property will be terminated.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
There&#39;s no flexibility with &lt;strong&gt;ServerTimeout&lt;/strong&gt; - one setting affects all MDX queries on the server regardless of origin, destination or anything else.&amp;nbsp; 99% of the time this is a sufficient and effective solution for terminating long running queries with minimal fuss.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2015/12/ssas-how-to-terminate-long-running.html</link><author>noreply@blogger.com (SQL Banana)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikobd0ThHRnUBcpj-0njT9RwfnmeXTmqWoojhnQ-KLwrKuKXYsYU_yYmmAizzzyGcWVuWbRQUnFtGYJDdvKAEB1DcLEifIDkVNPGp3Hiq9R98qNbf8w48M4DBKE-v81k2LoV84AEI85b8/s72-c/SSAS+properties.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-7648302065299192254</guid><pubDate>Wed, 16 Sep 2015 20:05:00 +0000</pubDate><atom:updated>2015-09-16T21:15:49.793+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Analysis Services</category><category domain="http://www.blogger.com/atom/ns#">MDX</category><category domain="http://www.blogger.com/atom/ns#">partition</category><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">profiler</category><category domain="http://www.blogger.com/atom/ns#">Slice</category><category domain="http://www.blogger.com/atom/ns#">slow running</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">SSAS</category><category domain="http://www.blogger.com/atom/ns#">trace</category><category domain="http://www.blogger.com/atom/ns#">XMLA</category><title>SSAS - Slow Running Query? Unnecessary Partition Scans? Solve it with a Slice!</title><description>&lt;div class=&quot;Publishwithline&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;While there are many possible reasons why an MDX
query&amp;nbsp;of an OLAP cube may not perform as well as expected - the cube dimension
hierarchies may not be optimised for best use, the MDX query itself may require
optimisation etc - an often overlooked area is the measure group partition
definitions and the partition Slice property.&lt;/span&gt;&lt;span style=&quot;font-family: Times New Roman, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&amp;nbsp;In a typical SSAS project&amp;nbsp;I would
normally design an &quot;initial&quot; partition inside SSDT (formerly BIDS):&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWFZSC2vZTwVwHxlsTNaxw1nuvSW_sgaHst448eEF69hnim0Ln5ajKnTzXSTjsWPOwAk8B6dQlMEMkXLSyZwNLSOon9oi-FkKyDqtEcg4PCIiSBrRgmMMQRGePOoY2gurN4CQQEFLFq60/s1600/SSDT+Partition+design.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;77&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWFZSC2vZTwVwHxlsTNaxw1nuvSW_sgaHst448eEF69hnim0Ln5ajKnTzXSTjsWPOwAk8B6dQlMEMkXLSyZwNLSOon9oi-FkKyDqtEcg4PCIiSBrRgmMMQRGePOoY2gurN4CQQEFLFq60/s400/SSDT+Partition+design.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;I would deploy this solution and create a
scheduled job to create additional partitions when necessary, depending
on&amp;nbsp;the partition strategy (eg every night/week/month etc).&amp;nbsp;
The&amp;nbsp;Create Partition xmla&amp;nbsp;script might look something like this:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: 13.5pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xmlns&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2003/engine&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;ParentObject&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;DatabaseID&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;MyDb&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;DatabaseID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;CubeID&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;MyDb&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;CubeID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;MeasureGroupID&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;MyMeasureGroup&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;MeasureGroupID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;ParentObject&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;ObjectDefinition&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;Partition&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xmlns:xsd&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://www.w3.org/2001/XMLSchema&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;xmlns:xsi&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://www.w3.org/2001/XMLSchema-instance&lt;/span&gt;&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xmlns:ddl2&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2003/engine/2&lt;/span&gt;&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xmlns:ddl2_2&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2003/engine/2/2&lt;/span&gt;&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xmlns:ddl100_100&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2008/engine/100/100&lt;/span&gt;&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xmlns:ddl200&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2010/engine/200&lt;/span&gt;&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xmlns:ddl200_200&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2010/engine/200/200&lt;/span&gt;&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xmlns:ddl300&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2011/engine/300&lt;/span&gt;&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xmlns:ddl300_300&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2011/engine/300/300&lt;/span&gt;&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xmlns:ddl400&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2012/engine/400&lt;/span&gt;&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xmlns:ddl400_400&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2012/engine/400/400&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;20150102&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;Name&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;20150102&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;Name&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;Source&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xsi:type&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;QueryBinding&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;DataSourceID&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;MyDs&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;DataSourceID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;QueryDefinition&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;SELECT
* FROM MyTable WHERE MyDate = 20150102&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;QueryDefinition&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;Source&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;StorageMode&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;Molap&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;StorageMode&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;ProcessingMode&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;Regular&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;ProcessingMode&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;ProactiveCaching&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;SilenceInterval&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;-PT1S&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;SilenceInterval&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;Latency&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;-PT1S&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;Latency&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;SilenceOverrideInterval&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;-PT1S&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;SilenceOverrideInterval&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;ForceRebuildInterval&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;-PT1S&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;ForceRebuildInterval&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;Source&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt;&quot;&gt;xsi:type&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;ProactiveCachingInheritedBinding&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt; /&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;ProactiveCaching&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;EstimatedRows&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;5000000&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;EstimatedRows&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;AggregationDesignID&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;AggregationDesign&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;AggregationDesignID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;Partition&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;ObjectDefinition&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Once deployed and processed, this
solution&amp;nbsp;may work well. Users find the cube responsive
and&amp;nbsp;fast.&amp;nbsp; Over time, however,&amp;nbsp;users start to complain that the
same daily query/report&amp;nbsp;that used to run almost instantaneously is now
painfully slow.&amp;nbsp; No design changes have been deployed, no
hardware&amp;nbsp;modifications have been made - nothing has been altered.&amp;nbsp; So
how is it that the same query now takes significantly longer?&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;An often overlooked area is the slice setting
for partitions. &amp;nbsp;As an example of what can happen, let&#39;s look at the
AdventureWorks cube from the SQL 2012 samples (available here&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;&lt;a href=&quot;http://msftdbprodsamples.codeplex.com/releases/view/55330&quot;&gt;http://msftdbprodsamples.codeplex.com/releases/view/55330&lt;/a&gt;).&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;A good starting point for debugging queries is
often to run a SQL Profiler trace on the SSAS database and see what&#39;s happening
when this query is executed. &amp;nbsp;Let&#39;s take a simple query:&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;[Measures].[Internet
Sales Amount]&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;0,&lt;br /&gt;
[Geography].[City].&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Members&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;1&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;[Adventure
Works]&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;[Date].[Calendar].[Date].&amp;amp;[20080607]&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;This will return the &quot;Internet Sales
Amount&quot; measure by City on 7th June 2008. &amp;nbsp;If we use the Object
Browser in Management Studio to examine the Internet Sales measure group we see
this:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXr-eM8j3rtTr0aTNhLIXe5_cZ5ByUdvPk4zyN51OYg2FQFhO67bqjXMZxC724xuEKGm8PyXT8yW9SOvLDwmTla_WZpQXnImH64aKAPwfcol5ot4ayIcojUTACkOrJHNaD0r7ffqQRm60/s1600/cube+partitions.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXr-eM8j3rtTr0aTNhLIXe5_cZ5ByUdvPk4zyN51OYg2FQFhO67bqjXMZxC724xuEKGm8PyXT8yW9SOvLDwmTla_WZpQXnImH64aKAPwfcol5ot4ayIcojUTACkOrJHNaD0r7ffqQRm60/s1600/cube+partitions.JPG&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;The measure group contains four partitions, one
for each year. &amp;nbsp;Therefore we would expect that our query above should only
hit the one single partition, Internet_Sales_2008. &amp;nbsp;Let&#39;s confirm by
viewing the SQL Profiler trace results:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilrH7u8XwW2RVy5a3Px2v49w1TlOi3nvDiTgTjnstlWvRHpbuYVHDSYnhPvvEEuvneEo5k7s80n0PeQ8bVM8-uI8TbcKxFaA2VeucDMqBo7XBSjl_pZ5LNWUfzmdsXn3gN4lILwIaT1Os/s1600/trace_partitions.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;153&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilrH7u8XwW2RVy5a3Px2v49w1TlOi3nvDiTgTjnstlWvRHpbuYVHDSYnhPvvEEuvneEo5k7s80n0PeQ8bVM8-uI8TbcKxFaA2VeucDMqBo7XBSjl_pZ5LNWUfzmdsXn3gN4lILwIaT1Os/s400/trace_partitions.JPG&quot; width=&quot;400&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;The trace shows that SSAS is actually scanning
three partitions, not one - 2005, 2006 and 2008. &amp;nbsp;But we know the result
set lies only in 2008, so why this extra scanning? &amp;nbsp;We may want to confirm
that the results are only going to be in the 2008 partition by examining the
partition query - the SQL query used as the source for the partition data.
&amp;nbsp;In this case it&#39;s been checked and they are all set up with the correct
queries and WHERE clauses.&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;The reason for the extra partition scans is down
to the Slice property of the partition not being set. &amp;nbsp;The slice property
is used by SSAS to determine which partition(s) contains the desired data.
&amp;nbsp;If SSAS knows up front where the data lies it can avoid wasting time and resources
scanning partitions that will not return any data. Let&#39;s take a look at the
properties of one of the partitions in SSMS:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJrGW1L0EX06NvQcmExoktUkv0M2UC5ocselkJA-0ibcpIldfcfouhdUfDPK5o_6MBALuZzvZ9VJBSXoNKYfbMSMwuxD7N4W3ToCOoL8D2bl3OaLnFHKDA_HXk62Liuxhcm1wOSe0ZAH0/s1600/partition+properties.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;289&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJrGW1L0EX06NvQcmExoktUkv0M2UC5ocselkJA-0ibcpIldfcfouhdUfDPK5o_6MBALuZzvZ9VJBSXoNKYfbMSMwuxD7N4W3ToCOoL8D2bl3OaLnFHKDA_HXk62Liuxhcm1wOSe0ZAH0/s320/partition+properties.jpg&quot; width=&quot;320&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;We can see here that the Slice property is
blank. &amp;nbsp;When left blank, SSAS uses it&#39;s own default method of determining
which partitions may contain the data. &amp;nbsp;While this may sometimes be
sufficient, the trace above shows it is hardly foolproof. &amp;nbsp;Far better to
actually set this property to the value we know to be valid, enabling the SSAS
engine to quickly be directed to partition containing the results.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Valid values for a slice property are an MDX
member, set or tuple. &amp;nbsp;In this example the slices should be set to the
relevant year, eg:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;[Date].[Calendar Year].&amp;amp;[2008]&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;If we now set this for each of the partitions
and rerun the same query we see the following in the trace file:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3K0hr7IFZw7gZVPOSELCColjJjq6Oo2rigJdaoGowDOtX7y69EALCdm5Yyp2F_Bllm6VJ18O8hx_le_3O2JKgCer0bQnYv2OYhdxQVmhhbhDqZ37_X5wU96BIYvcA0oc01oHQmnT7Vlk/s1600/trace_partitions_post_slice.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;127&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3K0hr7IFZw7gZVPOSELCColjJjq6Oo2rigJdaoGowDOtX7y69EALCdm5Yyp2F_Bllm6VJ18O8hx_le_3O2JKgCer0bQnYv2OYhdxQVmhhbhDqZ37_X5wU96BIYvcA0oc01oHQmnT7Vlk/s400/trace_partitions_post_slice.JPG&quot; width=&quot;400&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;The engine is going directly to the correct
partition and ignoring all others. &amp;nbsp;The result is a much faster running
query. &amp;nbsp;I have seen examples where there were 100+ partitions, all without
the slice set. &amp;nbsp;The result was that queries were taking up to 30 seconds
to complete. &amp;nbsp;On setting the slices appropriately, query time reduced to
around 1 second.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Note that we can set the slice either via
properties in SSDT at design time, in SSMS properties post deployment, or better
yet, in the original XMLA we used at the start of this post to automatically
create the partitions on a schedule:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;…&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;StorageMode&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;Molap&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;StorageMode&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;gt;&lt;br /&gt;
&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;ProcessingMode&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;Regular&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;ProcessingMode&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;gt;&lt;br /&gt;
&lt;b&gt;&amp;lt;&lt;/b&gt;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Slice&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;[Date].[Calendar Year].&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: red; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;amp;amp;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;[2008]&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Slice&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;ProactiveCaching&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;gt;….&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;So the lesson is: Always set the Slice property
:)&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2015/09/ssas-slow-running-query-unnecessary.html</link><author>noreply@blogger.com (SQL Banana)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWFZSC2vZTwVwHxlsTNaxw1nuvSW_sgaHst448eEF69hnim0Ln5ajKnTzXSTjsWPOwAk8B6dQlMEMkXLSyZwNLSOon9oi-FkKyDqtEcg4PCIiSBrRgmMMQRGePOoY2gurN4CQQEFLFq60/s72-c/SSDT+Partition+design.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-1831459460598943482</guid><pubDate>Sun, 14 Jun 2015 17:50:00 +0000</pubDate><atom:updated>2015-07-23T23:06:51.295+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">COLUMNS_UPDATED</category><category domain="http://www.blogger.com/atom/ns#">deleted</category><category domain="http://www.blogger.com/atom/ns#">exists</category><category domain="http://www.blogger.com/atom/ns#">inserted</category><category domain="http://www.blogger.com/atom/ns#">rowcount</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">trigger</category><category domain="http://www.blogger.com/atom/ns#">update</category><title>SQL Server Triggers – Which fields have been updated? – Part 2</title><description>&lt;div class=&quot;MsoNormal&quot;&gt;
In the last post (&lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2015/06/sql-2012-triggers-which-fields-have.html&quot; target=&quot;_blank&quot;&gt;SQL Server Triggers – Which fields have been updated? – Part 1&lt;/a&gt;) we explored various methods to determine
which columns in a table were being updated.&amp;nbsp;
All those methods relied on a SQL trigger being fired.&amp;nbsp; Once fired, if a specific column in the table
was updated we could carry out an appropriate action, such as inserting an
audit record into another table.&amp;nbsp;
However, what if the &lt;span style=&quot;color: magenta; font-family: Consolas; font-size: 9.5pt;&quot;&gt;UPDATE&lt;/span&gt; statement was executed against the table, but the actual
values of the fields were not changed?&amp;nbsp; Consider
the following example:&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TABLE&lt;/span&gt; MyTable&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;Col1 &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;1&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; Col2 &lt;span style=&quot;color: blue;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;10&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; Col3 &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TRIGGER&lt;/span&gt; dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;tr_MyTable
&lt;span style=&quot;color: blue;&quot;&gt;ON&lt;/span&gt; dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;MyTable&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;AFTER&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;UPDATE&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;PRINT&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;An Update has occurred&#39;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt; MyTable &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;7 &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;b&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;8 &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;c&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;9 &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;d&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;10 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: magenta; font-family: Consolas; font-size: 9.5pt;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;
MyTable&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; Col3 &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 10 &lt;span style=&quot;color: blue;&quot;&gt;WHERE&lt;/span&gt; Col2 &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;d&#39;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The &lt;span style=&quot;color: magenta; font-family: Consolas; font-size: 9.5pt;&quot;&gt;UPDATE&lt;/span&gt; statement above has overwritten Col3 with the same
value as it had before.&amp;nbsp; The table data
has not actually been changed. &amp;nbsp;Nonetheless, because an &lt;span style=&quot;color: magenta; font-family: Consolas; font-size: 9.5pt;&quot;&gt;UPDATE&lt;/span&gt; statement
has been executed, the &lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;AFTER&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: magenta;&quot;&gt;UPDATE&lt;/span&gt;&lt;/span&gt; Trigger is fired and will output the
print statement.&amp;nbsp; In fact even if the &lt;span style=&quot;color: magenta; font-family: Consolas; font-size: 9.5pt;&quot;&gt;UPDATE&lt;/span&gt; statement
had not affected a single row, the trigger would still have been fired,
outputting the print statement.&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
This is often not the desired behaviour – I don’t want to be
inserting audit records if nothing has actually changed.&amp;nbsp; Depending on what logic has been implemented
in the trigger and how often it is fired, this could have an adverse impact on
performance.&amp;nbsp; Therefore the first
improvement we can make is to ensure nothing happens if no records have been
updated.&amp;nbsp; To do this we use the &lt;span style=&quot;color: magenta; font-family: Consolas; font-size: 9.5pt;&quot;&gt;@@ROWCOUNT&lt;/span&gt;
global variable (for more on this variable see my post on &lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2013/12/sql-server-global-variables.html&quot; target=&quot;_blank&quot;&gt;SQL Server Global Variables&lt;/a&gt;):&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TRIGGER&lt;/span&gt; dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;tr_MyTable
&lt;span style=&quot;color: blue;&quot;&gt;ON&lt;/span&gt; dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;MyTable&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;AFTER&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;UPDATE&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;IF&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;@@ROWCOUNT&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 0 &lt;span style=&quot;color: blue;&quot;&gt;RETURN&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;PRINT&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;An Update has occurred&#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: green;&quot;&gt;--Perform
UPDATE logic here&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;GO&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
This trigger will now immediately return, doing
nothing, if no records have been updated. &lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
Next we need ensure we only take action if field
values have actually changed.&amp;nbsp; To do this
we can make use of the special &lt;i&gt;inserted &lt;/i&gt;and
&lt;i&gt;deleted &lt;/i&gt;tables.&amp;nbsp; These are temporary tables created and managed
automatically by SQL Server.&amp;nbsp; While we
cannot perform any DDL operations on these tables, they contain identical
columns to the original table the trigger is created on, and we can refer
directly to these columns in the code.&amp;nbsp;
When a DML trigger fires, these two tables are populated with the
deleted and inserted records from the underlying table respectively.&amp;nbsp; If records are updated then the original pre-updated
record arrives in the &lt;i&gt;deleted&lt;/i&gt; table
and the post-updated record in the &lt;i&gt;inserted
&lt;/i&gt;table.&amp;nbsp; Therefore, if we want to know
if previous values have actually been updated with new, different values we can
compare the contents of these two tables:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TRIGGER&lt;/span&gt; dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;tr_MyTable
&lt;span style=&quot;color: blue;&quot;&gt;ON&lt;/span&gt; dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;MyTable&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;AFTER&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;UPDATE&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;IF&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;@@ROWCOUNT&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 0 &lt;span style=&quot;color: blue;&quot;&gt;RETURN&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;IF&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;EXISTS(&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;*&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inserted
i &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: grey;&quot;&gt;INNER&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;JOIN&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; deleted d&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;ON&lt;/span&gt;&amp;nbsp; &amp;nbsp; i&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;Col1 &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; d&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;Col1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;WHERE&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;col2&amp;nbsp; &lt;span style=&quot;color: grey;&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; i&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;col2&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: grey;&quot;&gt;OR&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;col3 &lt;span style=&quot;color: grey;&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; i&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;col3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;BEGIN&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;PRINT&lt;/span&gt;
&lt;span style=&quot;color: red;&quot;&gt;&#39;An Update has occurred&#39;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;END&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
In the above trigger, inside the&amp;nbsp;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 12.6666669845581px;&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 12.6666669845581px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 12.6666669845581px;&quot;&gt;EXISTS&lt;/span&gt;&amp;nbsp;function,
we are joining the &lt;i&gt;inserted &lt;/i&gt;and &lt;i&gt;deleted&lt;/i&gt; tables together on Col1, the
identity column.&amp;nbsp; Because these tables
are effectively the “before” and “after” state of the data, we are only
interested where the values in the remaining fields do not match, indicating
that data has changed in those fields.&amp;nbsp;
This is logic is carried out by the &lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;WHERE&lt;/span&gt; clause.&amp;nbsp; Only if such rows exist do we then perform
the desired &lt;span style=&quot;color: magenta; font-family: Consolas; font-size: 9.5pt;&quot;&gt;UPDATE&lt;/span&gt;
logic, in this case the PRINT statement. &amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
The above trigger will now only do anything if data
has actually changed in the underlying table. &amp;nbsp;This has the potential of saving a lot of time
and unnecessary processing of data on those occasions when an &lt;span style=&quot;color: magenta; font-family: Consolas; font-size: 9.5pt;&quot;&gt;UPDATE&lt;/span&gt; has occurred
but no overall change has actually taken place.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2015/06/sql-server-triggers-which-fields-have.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-188139812571378924</guid><pubDate>Mon, 08 Jun 2015 20:44:00 +0000</pubDate><atom:updated>2015-06-14T18:52:56.587+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">columns</category><category domain="http://www.blogger.com/atom/ns#">COLUMNS_UPDATED</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">trigger</category><category domain="http://www.blogger.com/atom/ns#">update</category><title>SQL Server Triggers – Which fields have been updated? – Part 1</title><description>&lt;span style=&quot;font-family: &amp;quot;Calibri&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 1pt; mso-ansi-language: EN-GB; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: &amp;quot;Times New Roman&amp;quot;; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;&quot;&gt;&lt;w:sdtpr&gt;&lt;/w:sdtpr&gt;&lt;w:sdt docpart=&quot;32D38E75949146C4A7F979C35E239DCB&quot; id=&quot;89512082&quot; storeitemid=&quot;X_5075E326-C7C7-476E-9738-1458707A9156&quot; text=&quot;t&quot; title=&quot;Post Title&quot; xpath=&quot;/ns0:BlogPostInfo/ns0:PostTitle&quot;&gt;&lt;/w:sdt&gt;&lt;/span&gt;
  &lt;br /&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt;&quot;&gt;
&lt;div class=&quot;PadderBetweenControlandBody&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
When updating a table it can often be very useful to know not just
that an update has taken place, but exactly which columns have been updated.
For example, in an HR department you may have an employee table that contains
customer names and bank account numbers.&amp;nbsp;&amp;nbsp;If the bank account number
for an employee changes, either through a front end application, a DML query or
any other method, it should be audited – by which we mean an audit table should
be inserted with a corresponding record.&amp;nbsp;&amp;nbsp;While if an employee middle
name is changed, an audit is not required.&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Below we’ll look at a number of ways for testing for specific
column updates, and in Part 2 we’ll look at determining if values have indeed actually
changed or not – they could have been updated with the same value as before.&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;UPDATE&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: grey; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
One way of determing which column has been updated is by using&amp;nbsp;an&amp;nbsp;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;AFTER&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&amp;nbsp;&lt;/span&gt;trigger.&amp;nbsp;&amp;nbsp;Within this trigger we could use the&amp;nbsp;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;()&amp;nbsp;&lt;/span&gt;function to detect the
column that has been updated and apply the audit logic accordingly:&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; text-indent: 36.0pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;TRIGGER&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;tr_Employee&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Employee&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;AFTER&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; text-indent: 36.0pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;IF&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;BankAccountNo&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;INTO&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;EmployeeAudit&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Column1&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Column2&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;,...&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;ModifiedBy&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;ModifiedDate&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;inserted&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Column1&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;inserted&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Column2&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;,...&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;USER_NAME&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(),&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
But what happens if we have additional columns, some requiring
audit (such as SortCode, NationalInsuranceNo etc), and some not (eg marital
status)?&amp;nbsp;&amp;nbsp;Or perhaps the non-sensitive columns don’t require audit
but do require some other logic, eg an additional table to be amended in line
with this one.&amp;nbsp;&amp;nbsp;We could append multiple&amp;nbsp;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;()&amp;nbsp;&lt;/span&gt;statements:&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;BankAccountNo&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: green; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;--apply audit
logic&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;MaritalStatus&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: green; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;--apply other
logic&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: green; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;--next
IF…&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Or we could combine the groups of logic together into fewer
statements:&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;IF&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;BankAccountNo&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;OR&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;SortCode&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: green; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;--apply audit
logic&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;IF&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;MaritalStatus&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;OR&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;MiddleName&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: green; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;--apply other
logic&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;COLUMNS_UPDATED&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: grey; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
If you like working with
binary and bitwise logic,&amp;nbsp;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;COLUMNS_UPDATED&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;() &lt;/span&gt;can be used to achieve similar results and much less code.&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;COLUMNS_UPDATED&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;returns a varbinary bit pattern indicating which columns in a
table have been updated. From BOL:&lt;span style=&quot;color: grey; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN&quot;&gt;COLUMNS_UPDATED returns one or more bytes
that are ordered from left to right, with the least significant bit in each
byte being the rightmost. The rightmost bit of the leftmost byte represents the
first column in the table; the next bit to the left represents the second
column, and so on. COLUMNS_UPDATED returns multiple bytes if the table on which
the trigger is created contains more than eight columns, with the least
significant byte being the leftmost.&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
So for a simple four column
table, when&amp;nbsp;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;COLUMNS_UPDATED&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;is used in the trigger it
may return a binary value of 1010.&amp;nbsp;&amp;nbsp;This would indicate that columns
two and four (as listed in&amp;nbsp;&lt;span style=&quot;color: green; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;syscolumns&lt;/span&gt;) have been updated, but one and three have not.&amp;nbsp;&amp;nbsp;We can
therefore use the value returned by this function in our trigger to determine
if a specific combination of columns in our table have been
updated.&amp;nbsp;&amp;nbsp;However in order to do this we need to first convert the
binary number into decimal.&lt;span style=&quot;color: grey; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
As each column (right to left) represents an incremental increase
in the power of 2 (ie, right most column is 2^0, next to the left is 2^1, then
2^2), a binary value of 1010 can be translated to decimal as:&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184; width: 510px;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 155.55pt;&quot; valign=&quot;top&quot; width=&quot;207&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Column Name&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Col4&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Col3&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Col2&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Col1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 155.55pt;&quot; valign=&quot;top&quot; width=&quot;207&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Column Position&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
4&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
3&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
2&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 155.55pt;&quot; valign=&quot;top&quot; width=&quot;207&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;COLUMNS_UPDATED&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: grey; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;()&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 155.55pt;&quot; valign=&quot;top&quot; width=&quot;207&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Bin to dec conversion&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
2^3&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
2^2&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
2^1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
2^0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 155.55pt;&quot; valign=&quot;top&quot; width=&quot;207&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Decimal value&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
8&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
2&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 36.0pt;&quot;&gt;
Binary 1010&amp;nbsp;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;= &lt;/span&gt;( 0 x 2^0) + (1 x 2^1) + (0
x 2^2) + (1 x 2^3) =&amp;nbsp;&amp;nbsp;0 + 2 + 0 + 8 =&amp;nbsp;&lt;b&gt;10&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
When working with binary we
need to use SQL Server bitwise operators, in this case ‘&amp;amp;’ (Bitwise AND) to
compare two values or we may get unexpected results.&amp;nbsp;&amp;nbsp;The ‘&amp;amp;’
function looks at each bit (that is each 1/0) in the first binary number and
compares it to the corresponding bit in the second number.&amp;nbsp; If the first bit in the first number is 1 &lt;i&gt;AND&lt;/i&gt; the second bit in the second number
is 1, then the first bit of the output is 1.&amp;nbsp;
If either bits are 0 then the output is zero. Then it moves onto the
second bit in both numbers and starts again. The output number can be converted
back to decimal again.&amp;nbsp; In the first
table below, the binary number 1010 is ‘AND-ed’ with itself and the result is
shown. In the second table it is ‘AND-ed’ with a different number.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 116.85pt;&quot; valign=&quot;top&quot; width=&quot;156&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 59.95pt;&quot; valign=&quot;top&quot; width=&quot;80&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Col4&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Col3&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Col2&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.75pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Col1&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Decimal&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 116.85pt;&quot; valign=&quot;top&quot; width=&quot;156&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Binary Value 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 59.95pt;&quot; valign=&quot;top&quot; width=&quot;80&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.75pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
10&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 116.85pt;&quot; valign=&quot;top&quot; width=&quot;156&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Binary Value 2&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 59.95pt;&quot; valign=&quot;top&quot; width=&quot;80&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.75pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
10&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 116.85pt;&quot; valign=&quot;top&quot; width=&quot;156&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Output of ‘AND’&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 59.95pt;&quot; valign=&quot;top&quot; width=&quot;80&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.75pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
10&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 116.85pt;&quot; valign=&quot;top&quot; width=&quot;156&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 59.95pt;&quot; valign=&quot;top&quot; width=&quot;80&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Col4&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Col3&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Col2&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.75pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Col1&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Decimal&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 116.85pt;&quot; valign=&quot;top&quot; width=&quot;156&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Binary Value 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 59.95pt;&quot; valign=&quot;top&quot; width=&quot;80&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.75pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
10&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 116.85pt;&quot; valign=&quot;top&quot; width=&quot;156&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Binary Value 2&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 59.95pt;&quot; valign=&quot;top&quot; width=&quot;80&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.75pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
6&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 116.85pt;&quot; valign=&quot;top&quot; width=&quot;156&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Output of ‘AND’&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 59.95pt;&quot; valign=&quot;top&quot; width=&quot;80&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.8pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 63.75pt;&quot; valign=&quot;top&quot; width=&quot;85&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
2&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
The above tables show that
using the bitwise AND operator:&amp;nbsp; 10 &amp;amp;
10 = 10,&amp;nbsp; 10 &amp;amp; 6 = 2.&amp;nbsp; Therefore to we can say that in our table if &lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;COLUMNS_UPDATED&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;() &lt;/span&gt;= 10 we know that columns two and four have been updated.&lt;span style=&quot;color: grey; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
The resulting trigger for
this example would look like this:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;TRIGGER&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;tr_Employee&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Employee&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;AFTER&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;IF&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;COLUMNS_UPDATED&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&amp;amp;&amp;nbsp;10&amp;nbsp;=&amp;nbsp;10)&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: green; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;--apply audit logic&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; text-indent: 36.0pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Note that the IF statement
above will return TRUE if columns two and four&amp;nbsp;&lt;i&gt;or more&lt;/i&gt;&amp;nbsp;are
updated, not just two and four alone.&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
Advantages of this approach
are that for a very wide table, the number of IF statements, or alternatively
the number of conditions within the IF statements could be huge and time
consuming to write, while using&amp;nbsp;&lt;span style=&quot;color: magenta; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;COLUMNS_UPDATED&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: Consolas; mso-bidi-font-size: 11.0pt;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&amp;nbsp;&lt;/span&gt;reduces it to just one short line.&amp;nbsp;&amp;nbsp;However, using this
function will mean that the functionality of the code is heavily dependent on
the order of the columns in the table.&amp;nbsp;&amp;nbsp;If the table is dropped and
recreated with columns reordered, the trigger may no longer function as
expected.&amp;nbsp;&amp;nbsp;In addition, while being concise, this code is more
difficult to understand and debug.&amp;nbsp;&amp;nbsp;Therefore I would recommend the
earlier approaches unless absolutely necessary.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
In &lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2015/06/sql-server-triggers-which-fields-have.html&quot; target=&quot;_blank&quot;&gt;Part 2&lt;/a&gt; &amp;nbsp;we’ll look at how
to analyse the records that have been updated and determine if any update has
actually been made at all – an Update trigger can be fired even if no values
have actually changed.&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2015/06/sql-2012-triggers-which-fields-have.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-5876282949753529033</guid><pubDate>Sat, 25 Apr 2015 16:55:00 +0000</pubDate><atom:updated>2015-04-29T11:20:29.272+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Analysis Services</category><category domain="http://www.blogger.com/atom/ns#">blank</category><category domain="http://www.blogger.com/atom/ns#">measures</category><category domain="http://www.blogger.com/atom/ns#">non empty</category><category domain="http://www.blogger.com/atom/ns#">null</category><category domain="http://www.blogger.com/atom/ns#">nullprocessing</category><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">Process</category><category domain="http://www.blogger.com/atom/ns#">query</category><category domain="http://www.blogger.com/atom/ns#">remove zeros</category><category domain="http://www.blogger.com/atom/ns#">slow running</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">SSAS</category><title>SSAS 2012 Remove Zeros Using NullProcessing and Improve Query Times</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
In Analysis
Services optimal query response times is key to a successful project. &amp;nbsp;The
cube may contain all sorts of clever logic, processing times may be lightning
fast, but if users are having to wait on long running queries each time they
access the SSAS database they will soon tire of using it.&lt;/div&gt;
&lt;div style=&quot;margin: 0cm 0cm 0pt;&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
As fact and
dimension tables grow over time, query performance can slow down. &amp;nbsp;There
are many techniques for improving query performance - good partition design for
measure groups, implementing natural hierarchies for dimensions etc.
&amp;nbsp;However, there are also other techniques that often get overlooked.
&amp;nbsp;One such technique is to look at the source data and decide if it is really
necessary to import it into the cube. A smaller cube will generally outperform
a larger one (all other factors being equal).&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
I recently
worked on an SSAS database with a dimension that had grown to contain over 30
million members at the leaf level. &amp;nbsp; For any given day a subset of these
members would be used - around fifty thousand would actually have data in the
cube, and this was massively impacting query performance time for users –
around 3 minutes were required to complete the query.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
On examining
the data returned by the SSAS queries, most of the measure values for these
members was zero. &amp;nbsp;Only a few hundred records per day contained non-zero
values. &amp;nbsp;In the source system relational tables the values were null, but
in the cube they were displayed as zero. &amp;nbsp;As a result, to resolve the cube
queries SSAS was having to read and aggregate all these zero values when in actual
fact they were of no interest to the users. &amp;nbsp; If these zeros could be
removed we could eliminate a huge chunk of data from the cube and reduce the
overhead of processing, reading and returning all that unnecessary data. &amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
It may seem
odd that the cube would convert null values in the source tables into zero&#39;s in
the cube, but the key to this behaviour lies in the&amp;nbsp;&lt;b&gt;NullProcessing&amp;nbsp;&lt;/b&gt;property.
&amp;nbsp;As a simple example, let’s first create some dimension and fact data:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TABLE&lt;/span&gt;
dimCustomer&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;custId
&lt;span style=&quot;color: blue;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; CustName &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;25&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; CustCountry &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;25&lt;span style=&quot;color: grey;&quot;&gt;))&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TABLE&lt;/span&gt; dimDate&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;DateId &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; CalendarDate &lt;span style=&quot;color: blue;&quot;&gt;DATE&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TABLE&lt;/span&gt; factMeasures&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;dimCustId &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; dimDateId &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; Sales &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt; dimCustomer&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Dave&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;England&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 2&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Bob&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Scotland&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 3&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Jenny&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Ireland&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 4&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Jill&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Scotland&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 5&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Donny&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Ireland&#39;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;DELCARE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;
@t &lt;span style=&quot;color: blue;&quot;&gt;SMALLDATETIME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;()&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt; dimDate&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;@t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;1 &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;DATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;@t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;2 &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;DATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;3&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;@t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;3 &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;DATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;4&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;@t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;4 &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;DATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;5&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;@t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;5 &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;DATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;6&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;@t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;6 &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;DATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;7&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;@t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;7 &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;DATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt; factMeasures&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; 10 &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; 20 &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;3&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; 20 &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 2&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; 30 &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 2&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; 50 &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 3&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 3&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 3&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;3&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 3&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;4&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 4&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; 10 &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 4&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 4&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;3&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 5&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 5&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 5&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;3&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;UNION&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;ALL&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt; 5&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;12&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; @t&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;4&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 112&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;NULL&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt; factMeasures&lt;/span&gt;&lt;span style=&quot;font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;mso-no-proof: yes;&quot;&gt;&lt;!--[if gte vml 1]&gt;&lt;v:shapetype
 id=&quot;_x0000_t75&quot; coordsize=&quot;21600,21600&quot; o:spt=&quot;75&quot; o:preferrelative=&quot;t&quot;
 path=&quot;m@4@5l@4@11@9@11@9@5xe&quot; filled=&quot;f&quot; stroked=&quot;f&quot;&gt;
 &lt;v:stroke joinstyle=&quot;miter&quot;/&gt;
 &lt;v:formulas&gt;
  &lt;v:f eqn=&quot;if lineDrawn pixelLineWidth 0&quot;/&gt;
  &lt;v:f eqn=&quot;sum @0 1 0&quot;/&gt;
  &lt;v:f eqn=&quot;sum 0 0 @1&quot;/&gt;
  &lt;v:f eqn=&quot;prod @2 1 2&quot;/&gt;
  &lt;v:f eqn=&quot;prod @3 21600 pixelWidth&quot;/&gt;
  &lt;v:f eqn=&quot;prod @3 21600 pixelHeight&quot;/&gt;
  &lt;v:f eqn=&quot;sum @0 0 1&quot;/&gt;
  &lt;v:f eqn=&quot;prod @6 1 2&quot;/&gt;
  &lt;v:f eqn=&quot;prod @7 21600 pixelWidth&quot;/&gt;
  &lt;v:f eqn=&quot;sum @8 21600 0&quot;/&gt;
  &lt;v:f eqn=&quot;prod @7 21600 pixelHeight&quot;/&gt;
  &lt;v:f eqn=&quot;sum @10 21600 0&quot;/&gt;
 &lt;/v:formulas&gt;
 &lt;v:path o:extrusionok=&quot;f&quot; gradientshapeok=&quot;t&quot; o:connecttype=&quot;rect&quot;/&gt;
 &lt;o:lock v:ext=&quot;edit&quot; aspectratio=&quot;t&quot;/&gt;
&lt;/v:shapetype&gt;&lt;v:shape id=&quot;Picture_x0020_1&quot; o:spid=&quot;_x0000_i1029&quot; type=&quot;#_x0000_t75&quot;
 style=&#39;width:162.75pt;height:274.5pt;visibility:visible;mso-wrap-style:square&#39;&gt;
 &lt;v:imagedata src=&quot;file:///C:\Users\gsoudry\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png&quot;
  o:title=&quot;&quot;/&gt;
&lt;/v:shape&gt;&lt;![endif]--&gt;&lt;!--[if !vml]--&gt;&lt;!--[endif]--&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhh_mJvgEcPvDSx-dl6W7eCgKdRHwwqIG3JpI2avDVRhAmIuei85QxWbEwnjE-AhlXwG5tu_bDXUBhJwmv_23SkbsWuBWcwUFk5lPVxM6FLgkclNQDAeZ51NftpwIlWkq4UYlSR2IoTHFk/s1600/factData.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhh_mJvgEcPvDSx-dl6W7eCgKdRHwwqIG3JpI2avDVRhAmIuei85QxWbEwnjE-AhlXwG5tu_bDXUBhJwmv_23SkbsWuBWcwUFk5lPVxM6FLgkclNQDAeZ51NftpwIlWkq4UYlSR2IoTHFk/s1600/factData.JPG&quot; height=&quot;320&quot; width=&quot;189&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;br /&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
As the results show, we now have a simple schema with a fact
table containing Sales values.&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp; &lt;/span&gt;Note that
for CustID 5, Donny, all values are null.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Next let’s build a simple SQL Server Analysis Server 2012
cube on top of this schema, using SQL Server Data Tools (SSDT).&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp; &lt;/span&gt;The cube design looks like this:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;br /&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIqaNayJbmvGlhdwpCIoeGgrhGUCzcdwUJnDTdFDoiBwMuJBkGNvE3gMwVF2bH6q1bNt6k-CA7LhcsKu7shrHhuXRzg5Zmj9DJZTgoF4vwGBDeFkz9-u7uwY6o1Zej3XrC3t6azdL-ISY/s1600/Cube+Design.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIqaNayJbmvGlhdwpCIoeGgrhGUCzcdwUJnDTdFDoiBwMuJBkGNvE3gMwVF2bH6q1bNt6k-CA7LhcsKu7shrHhuXRzg5Zmj9DJZTgoF4vwGBDeFkz9-u7uwY6o1Zej3XrC3t6azdL-ISY/s1600/Cube+Design.JPG&quot; height=&quot;263&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;br /&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
After deploying and processing the cube, we’re now ready to
browse the data by customer and date:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhanToMiratMY8Ro3bHjXDryQj1mG85R6NapJH8j7hC_zobO5OSgpbgF29Tcwb3dGyhDEEeU67QHnmn8yV1ttPvGU5LuFdF2dNgUbgvGdTXOSZWc9hdp72MGkDOa_cnl5f9OhsYMHBO6jY/s1600/zero+value+measure.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhanToMiratMY8Ro3bHjXDryQj1mG85R6NapJH8j7hC_zobO5OSgpbgF29Tcwb3dGyhDEEeU67QHnmn8yV1ttPvGU5LuFdF2dNgUbgvGdTXOSZWc9hdp72MGkDOa_cnl5f9OhsYMHBO6jY/s1600/zero+value+measure.JPG&quot; height=&quot;320&quot; width=&quot;315&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;br /&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Note that Donny is listed with 4 days of data, with a sales
value of zero.&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp; &lt;/span&gt;The source system showed
nulls, but the cube has converted that to zeros, in effect creating values where none existed.&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;In our
example we would prefer that these cells in the cube are empty.&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp; There&lt;/span&gt;&amp;nbsp;may be a need to have the
record in the source system, e.g. while this measure column for this row in the source table is null, &amp;nbsp;there may be an additional measure column for which
there are valid figures for Donny. However, at present a &lt;b&gt;NON EMPTY&lt;/b&gt; query will return these zeros, when we would expect Donny not to be listed at all.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
To change this behaviour to that desired we need to examine
the &lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;NullProcessing&lt;/b&gt; property for the
measure. In SSDT open the Cube and go to the Cube Structure tab.&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp; &lt;/span&gt;In the measures list on the left select the
relevant measure and open the properties window (F4).&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp; &lt;/span&gt;At the bottom of the list of properties
expand the Source property:&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgucykt7jV8rokIrITHF-pCKWLwIKbIiGd0QMAQZTxGU3TGgt_C7TpTE8LtKrBCTv0sATLyGyVBtxyel_JML_b90S3x7cWeMo0WZDVKZGu1xSZDp7dPsyKo2afAP5eFamORShLnwWaRpkA/s1600/NullProcessingAutomatic.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgucykt7jV8rokIrITHF-pCKWLwIKbIiGd0QMAQZTxGU3TGgt_C7TpTE8LtKrBCTv0sATLyGyVBtxyel_JML_b90S3x7cWeMo0WZDVKZGu1xSZDp7dPsyKo2afAP5eFamORShLnwWaRpkA/s1600/NullProcessingAutomatic.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
By default the &lt;b&gt;NullProcessing &lt;/b&gt;is set to &lt;b&gt;Automatic&lt;/b&gt;,&amp;nbsp; which rather ambiguously means the Analysis Services server
decides what to do.&amp;nbsp; Changing the NullProcessing setting to &lt;b&gt;Preserve&lt;/b&gt;, will preserve
the nulls from the source data.&amp;nbsp; Using the
above example, setting this to Preserve and reprocessing produces the following
results for the same query as before:&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGA5Hdj7I3rzv9avmlNy7R-day7QinjlJnT3atVGrC6S0UWBYFv-rRTWMffJ-GZf6M5-Fyv2jnuVowqpCVfy172cBnxHoae0DS71baSO-X-eG6ez8V6SKSNfI_hnDvYYqTkGLUxS7-jgY/s1600/zeros+removed+from+measure.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGA5Hdj7I3rzv9avmlNy7R-day7QinjlJnT3atVGrC6S0UWBYFv-rRTWMffJ-GZf6M5-Fyv2jnuVowqpCVfy172cBnxHoae0DS71baSO-X-eG6ez8V6SKSNfI_hnDvYYqTkGLUxS7-jgY/s1600/zeros+removed+from+measure.JPG&quot; height=&quot;164&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Donny no longer appears in the list, and the results have
reduced from 48 cells to 18.&amp;nbsp; Although
not apparent on such a small dataset, in the original example of 50k records
taking 3 mins to return, applying the &lt;b&gt;NullProcessing &lt;/b&gt;changes reduced query time
to less than 5 seconds to return only a few hundred rows.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;w:sdt contentlocked=&quot;t&quot; id=&quot;89512093&quot; sdtgroup=&quot;t&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Calibri&amp;quot;,sans-serif; font-size: 1.0pt; mso-ansi-language: EN-GB; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: &amp;quot;Times New Roman&amp;quot;; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin;&quot;&gt;&lt;w:sdtpr&gt;&lt;/w:sdtpr&gt;&lt;w:sdt docpart=&quot;69D3E808D6BD413B98462283DA6EE9C2&quot; id=&quot;89512082&quot; storeitemid=&quot;X_5F329CAD-B019-4FA6-9FEF-74898909AD20&quot; text=&quot;t&quot; title=&quot;Post Title&quot; xpath=&quot;/ns0:BlogPostInfo/ns0:PostTitle&quot;&gt;&lt;/w:sdt&gt;&lt;/span&gt;
  
 &lt;/w:sdt&gt;
 
 






















































































































































&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2015/04/ssas-2012-remove-zeros-using.html</link><author>noreply@blogger.com (SQL Banana)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhh_mJvgEcPvDSx-dl6W7eCgKdRHwwqIG3JpI2avDVRhAmIuei85QxWbEwnjE-AhlXwG5tu_bDXUBhJwmv_23SkbsWuBWcwUFk5lPVxM6FLgkclNQDAeZ51NftpwIlWkq4UYlSR2IoTHFk/s72-c/factData.JPG" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-7072092681373077106</guid><pubDate>Tue, 27 Jan 2015 13:42:00 +0000</pubDate><atom:updated>2015-01-27T13:50:17.885+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">AddEvent</category><category domain="http://www.blogger.com/atom/ns#">data-driven</category><category domain="http://www.blogger.com/atom/ns#">example</category><category domain="http://www.blogger.com/atom/ns#">execution</category><category domain="http://www.blogger.com/atom/ns#">job</category><category domain="http://www.blogger.com/atom/ns#">manual</category><category domain="http://www.blogger.com/atom/ns#">schedule</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">SQL Agent</category><category domain="http://www.blogger.com/atom/ns#">SQL Server</category><category domain="http://www.blogger.com/atom/ns#">SSMS</category><category domain="http://www.blogger.com/atom/ns#">SSRS</category><category domain="http://www.blogger.com/atom/ns#">stored procedure</category><category domain="http://www.blogger.com/atom/ns#">subscription</category><title>Manually Execute SSRS 2012 Subscriptions</title><description>&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
By far the easiest place to setup a SQL
Server 2012 SSRS subscription for a report is through the Reporting
Services Report Manager.  A simple GUI guides users through the
process of selecting a subscription type, where to save or send the output and on what
schedule to execute.  For example, you can
specify the report to execute every morning at 9am and email the
report as a pdf to a set of users, or to save the output file to a
fileshare.  
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
Now suppose you don’t want this
report subscription to email or save the file on an automated
schedule, but rather to be manually/actively triggered.  Perhaps you
want to test the subscription is working or you want it to be
triggered by a third party application. There’s more than one way
of doing this, and here I will cover two methods:&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;_GoBack&quot;&gt;&lt;/a&gt;&lt;b&gt;Method 1 –
SQL Agent Job&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
When a subscription is created in the
SSRS Reports Manager, a corresponding SQL agent job is also created
on the SQL server.  Unfortunately there is no way to control the name
of the job and, once created, it cannot be modified without affecting
the subscription.  The name of the job will be an UID similar to the
highlighted item below:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWt0GXaUr_QFI98sBN9ebZJAXqIZ25KtnLYLiv9jkCJojRQYNgmWZL4Od2ynLUmsstMQxHIaA1dzlMlQQfsEV9EjEKr8kRhjeABS95RGB8AQ43bDNvcekhMERWb6t8fLxHXZ597iCoDKU/s1600/pic1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWt0GXaUr_QFI98sBN9ebZJAXqIZ25KtnLYLiv9jkCJojRQYNgmWZL4Od2ynLUmsstMQxHIaA1dzlMlQQfsEV9EjEKr8kRhjeABS95RGB8AQ43bDNvcekhMERWb6t8fLxHXZ597iCoDKU/s1600/pic1.png&quot; height=&quot;111&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
Right-click on this job and select
“Start Job at step…” to run it.  This will cause the
subscription to execute, and you will see in the SQL Server Reporting
Services Report Manager that the Last Run field for the subscription
has been updated.  This is the simplest approach and is particularly
useful for testing the item:&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2rP_ZjUfdyJUtxRBOBc5Qz371xSkd9zHW2L8-j06Pv1qxk_CJNPSWmuzuQvhpsa_aRdw6Sh4FL35qguqJkAoFj3lB26fMTwXPfVQQ9OHIkgDRd7RPSVh6iuBC8K1X9OhA_fM2aaivOII/s1600/pic2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2rP_ZjUfdyJUtxRBOBc5Qz371xSkd9zHW2L8-j06Pv1qxk_CJNPSWmuzuQvhpsa_aRdw6Sh4FL35qguqJkAoFj3lB26fMTwXPfVQQ9OHIkgDRd7RPSVh6iuBC8K1X9OhA_fM2aaivOII/s1600/pic2.png&quot; height=&quot;75&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
Alternatively, a job request can be
made using SQL code:&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;EXEC&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;dbo&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;sp_start_job&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;N&#39;AAB7E0BD-8089-4330-A3D6-8B95ACD90132&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
This will have exactly the same effect
as right-clicking to execute the job.&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
The drawback to this approach is that
SQL Agent job execution requests for a single job do not queue or run
in parallel.  If the job is already running then the subsequent job
execution request will fail:&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: #ff3333;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Request
to run job AAB7E0BD-8089-4330-A3D6-8B95ACD90132 refused because the
job already has a pending request&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: #ff3333;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
Therefore if multiple users are going
to be issuing requests then this may not be the best approach.&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Method 2 – Stored Procedure&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
If you open the
SQL Agent Job used for the subscription and examine the step that has
been defined, you will see that it is executing a stored procedure:&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU9UFa_hY8fkj95Vil_5jvzqBRQO5A5bCjUBS1cEgsOI0ai0R8FjUExZh7LIcvcQwCOpimo78NfMPzM_PLLv3EKQ9dUhFlY3VSdqPxaS7QwMD05BUsaxd_PLNeHbVTbhq1KgmUYn92fqw/s1600/pic3.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU9UFa_hY8fkj95Vil_5jvzqBRQO5A5bCjUBS1cEgsOI0ai0R8FjUExZh7LIcvcQwCOpimo78NfMPzM_PLLv3EKQ9dUhFlY3VSdqPxaS7QwMD05BUsaxd_PLNeHbVTbhq1KgmUYn92fqw/s1600/pic3.png&quot; height=&quot;288&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
The AddEvent
stored procedure is being called:&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;exec&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;[ReportServer]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;dbo&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;AddEvent&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;@EventType&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;TimedSubscription&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;@EventData&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;a7fd8d5f-0af9-49ec-ad8e-b273d44f1bb0&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
This stored
procedure inserts a record in the ReportServer.dbo.Events table.  The
Events table is polled every few seconds by the server and any
entries in this table will trigger the subscription to be executed. 
Note that the @EventData variable value is the same as the SQL Agent
Job name. 
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
This same SQL
stored procedure can in fact be run manually against the SQL Server
&lt;b&gt;msdb &lt;/b&gt;database directly and will have exactly the same effect – it
will trigger the subscription to execute.  However, as we are no
longer using the SQL Agent to call the procedure, we can actually run
this code multiple times as quickly as we like.  Each time it will
insert a record into the events table, and every entry will trigger a
new subscription execution, even if it is the same subscription. 
Effectively, subscription requests will be now be queued, and run
sequentially.  
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
If a subscription
execution request needs to be made actively, or occur multiple times
in a short space of time, and each request needs to be completed,
then this method is a good approach to handling the scenario.&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2015/01/manually-execute-ssrs-2012-subscriptions.html</link><author>noreply@blogger.com (SQL Banana)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWt0GXaUr_QFI98sBN9ebZJAXqIZ25KtnLYLiv9jkCJojRQYNgmWZL4Od2ynLUmsstMQxHIaA1dzlMlQQfsEV9EjEKr8kRhjeABS95RGB8AQ43bDNvcekhMERWb6t8fLxHXZ597iCoDKU/s72-c/pic1.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-974462838594654481</guid><pubDate>Tue, 30 Dec 2014 13:05:00 +0000</pubDate><atom:updated>2015-01-15T13:09:49.917+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">concurrent</category><category domain="http://www.blogger.com/atom/ns#">example</category><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">sequential</category><category domain="http://www.blogger.com/atom/ns#">slow running</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">stored procedure</category><category domain="http://www.blogger.com/atom/ns#">threading</category><category domain="http://www.blogger.com/atom/ns#">waiting</category><title>How to Execute SQL Script Asynchronously</title><description>&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;SQL code generally runs sequentially. &amp;nbsp;One line of code
must complete before the next line is executed. &amp;nbsp;We can redirect the path
that is followed through the code using conditional logic or GOTO statements,
but ultimately we cannot progress to the next step until the previous one has
completed.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;However, there are situations where we may want the code to
continue to step 2 without waiting for a particularly time-consuming step 1 to
complete. &amp;nbsp;If step 1 and step 2 do not impact each other then it might be beneficial
to have the SQL execute asynchronously or concurrently, saving overall
execution time.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;For an example let&#39;s create a table to insert data into for
step 1, and a stored procedure that will insert data into it:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 5.0pt; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;TABLE&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;[dbo]&lt;/span&gt;.&lt;span style=&quot;color: teal;&quot;&gt;[InsertSomething]&lt;/span&gt;(&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;id&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;IDENTITY&lt;/span&gt;(1,1)&amp;nbsp;NOT&amp;nbsp;NULL,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;Col1&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;(32)&amp;nbsp;NULL,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;InsertedDate&lt;/span&gt;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;DATETIME&lt;/span&gt;&amp;nbsp;NULL&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;)&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;GO&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 5.0pt; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;PROC&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;DoSomething&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@Data&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;(32)&amp;nbsp;=&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS&lt;br /&gt;
INSERT&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;InsertSomething&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;(&lt;span style=&quot;color: teal;&quot;&gt;Col1&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;InsertedDate&lt;/span&gt;)&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;(&lt;span style=&quot;color: teal;&quot;&gt;@Data&lt;/span&gt;&amp;nbsp;+&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39; - Start&#39;&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;())&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;WAITFOR&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;DELAY&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;00:00:10:00&#39;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;InsertSomething&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;(&lt;span style=&quot;color: teal;&quot;&gt;Col1&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;InsertedDate&lt;/span&gt;)&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;(&lt;span style=&quot;color: teal;&quot;&gt;@Data&lt;/span&gt;&amp;nbsp;+&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39; - End&#39;&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;())&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;GO&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;inherit&amp;quot;,serif; font-size: 12.0pt; mso-bidi-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;The stored proc
DoSomething will insert two rows into the table InsertSomething. &amp;nbsp;After
the first row is inserted there will be a delay of ten seconds before the
second row is inserted. &amp;nbsp;Therefore the time for the procedure to complete
is approximately ten seconds, which in this example represents a long running
piece of code.&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 5.0pt; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: green; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;EXEC&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;DoSomething&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Test 1&#39;&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;InsertSomething&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;(&lt;span style=&quot;color: teal;&quot;&gt;Col1&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;InsertedDate&lt;/span&gt;)&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;(&lt;span style=&quot;color: red;&quot;&gt;&#39;Test 2&#39;&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;())&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;inherit&amp;quot;,serif; font-size: 12.0pt; mso-bidi-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;*&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;InsertSomething&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 10.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Results&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;id&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Col1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
InsertedDate&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;-----------
-------------------------------- -----------------------&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Test 1 -
Start&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
2014-12-30 12:38:40.023&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Test 1 -
End&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
2014-12-30 12:38:50.030&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 5.0pt; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Test
2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
2014-12-30 12:38:50.037&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;inherit&amp;quot;,serif; font-size: 12.0pt; mso-bidi-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;We can see from the timestamps that the &quot;Test 2&quot; record was inserted only after the stored procedure had completed inserting
the Test 1 records. IE the sql script is executing sequentially, as expected.&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;inherit&amp;quot;,serif; font-size: 12.0pt; mso-bidi-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;In the above example
we would prefer the &quot;Test 2&quot; insert statement not to have to wait for the
stored procedure to complete. We would like the two sql commands to run
concurrently. &amp;nbsp;To do this we need to create a second stored procedure to
call the first via a new thread:&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;PROC&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;[dbo]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;[AsyncInsert]&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@Data&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;32&lt;span style=&quot;color: grey;&quot;&gt;)&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;DECLARE&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@rc&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;br /&gt;
DECLARE&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@object&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;br /&gt;
DECLARE&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@osql_cmd&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;1000&lt;span style=&quot;color: grey;&quot;&gt;)&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@ServerName&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;256&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: magenta;&quot;&gt;@@SERVERNAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;DECLARE&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@ExecStmt&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;&quot;EXEC
DoSomething @Data=&#39;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;@Data&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&#39;&quot;&#39;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;EXEC&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@rc&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: maroon;&quot;&gt;sp_oacreate&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;wscript.shell&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@object&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;OUT&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;SET&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@osql_cmd&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;osql -E -dMyDB -S&#39;&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;@ServerName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39; -Q&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@ExecStmt&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;EXEC&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@rc&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;sp_oamethod&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;@object&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;run&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;null,&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;@osql_cmd&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;EXEC&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: maroon;&quot;&gt;sp_oadestroy&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;@object&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;GO&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;inherit&amp;quot;,serif; font-size: 12.0pt; mso-bidi-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;inherit&amp;quot;,serif; font-size: 12.0pt; mso-bidi-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Test the new
procedure by running the following code:&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;EXEC&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;[AsyncInsert]&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;@Data&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;Test 1 Async&#39;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;WAITFOR&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;DELAY&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;00:00:01:00&#39;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;INSERT&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;InsertSomething&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;Col1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;InsertedDate&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Test 2 Async&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;())&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;WAITFOR&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;DELAY&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;00:00:10:00&#39;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;SELECT&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;*&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: teal;&quot;&gt;InsertSomething&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;Results&lt;br /&gt;
id&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Col1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
InsertedDate&lt;br /&gt;
----------- -------------------------------- -----------------------&lt;br /&gt;
1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Test 1 -
Start&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
2014-12-30 12:38:40.023&lt;br /&gt;
2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Test 1 -
End&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
2014-12-30 12:38:50.030&lt;br /&gt;
3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Test
2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
2014-12-30 12:38:50.037&lt;br /&gt;
4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Test 1 Async -
Start&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
2014-12-30 12:39:01.967&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Test 2
Async&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
2014-12-30 12:39:02.930&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Test 1 Async -
End&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
2014-12-30 12:39:11.987&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;In the results above we can see that for the Async tests the
timestamps show that the Test 2 insert &amp;nbsp;occurred while the
stored procedure (Test 1) was still running &amp;nbsp;- ie after the &quot;Test 1
Async - Start&quot; insert command completed but before the &quot;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: 16px;&quot;&gt;Test 1 Async -&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: 16px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif; font-size: 12pt;&quot;&gt;End&quot; insert was
executed.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;,serif; font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;We have generated a new thread to execute the stored
procedure that does not require us to wait for it to complete before
continuing. &amp;nbsp;Therefore the SQL script is now effectively running
asynchronously.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2014/12/how-to-execute-sql-script-asynchronously.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-5724645679468301448</guid><pubDate>Mon, 17 Nov 2014 01:11:00 +0000</pubDate><atom:updated>2015-01-15T13:12:36.207+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">clause</category><category domain="http://www.blogger.com/atom/ns#">columns</category><category domain="http://www.blogger.com/atom/ns#">example</category><category domain="http://www.blogger.com/atom/ns#">matrix</category><category domain="http://www.blogger.com/atom/ns#">measures</category><category domain="http://www.blogger.com/atom/ns#">multiple</category><category domain="http://www.blogger.com/atom/ns#">PIVOT</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">table</category><category domain="http://www.blogger.com/atom/ns#">UNPIVOT</category><category domain="http://www.blogger.com/atom/ns#">WHERE</category><title>SQL UNPIVOT on Multiple Columns</title><description>&lt;div class=&quot;tr_bq&quot; style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Some time ago I posted about using the &lt;span style=&quot;color: #999999;&quot;&gt;UNPIVOT &lt;/span&gt;function in a stored procedure &lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2012/12/reporting-services-report-how-to-handle.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. &amp;nbsp;Recently I came across an interesting problem that required further use of the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #999999;&quot;&gt;UNPIVOT&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;command.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tr_bq&quot; style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;tr_bq&quot; style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Suppose you have a number of columns in a wide table that you would like to unpivot into a single column and multiple rows. An example might be when there are many measure columns and you would like to narrow the table by combining all the measure columns into a single column. This would be a fairly standard use of the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #999999;&quot;&gt;UNPIVOT&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;command that was covered in the earlier post.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
Now suppose that there are two sets of measures that you would like kept as separate columns, &amp;nbsp;Instead of a single MeasureName and MeasureValue column, you would like 2 of each. &amp;nbsp;Or perhaps a simpler example might not be measure, but dimensional data. &amp;nbsp;Let&#39;s look at CRM system data:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;CREATE&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;TABLE&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Company&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Id&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;INT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;CompanyName&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;VARCHAR&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;500&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;VARCHAR&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;500&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;VARCHAR&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;500&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Email1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;VARCHAR&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;500&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Email2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;VARCHAR&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;500&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;GO&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt; &lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;INSERT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;INTO&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Company&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;FastCarsCo&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;Mr
Purple&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;Mr
Orange&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;purple@fcc.com&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;orange@fcc.com&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;UNION&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ALL&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;TastyCakeShop&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;Mr
Brown&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;Mr
White&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;brown@tcs.com&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;orange@tcs.com&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;UNION&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ALL&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;KidsToys&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;Mr
Pink&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;Mr
Black&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;pink@kt.com&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;black@kt.com&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;UNION&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ALL&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;FruitStall&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;Mr
Red&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;Mr
Blue&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;red@fs.com&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;blue@fs.com&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;GO&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt; &lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Id&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;CompanyName&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;contact1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;contact2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;email1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;email2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Company&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;GO&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB1CZutl4_tUSgS3EfZHxsJ1MyQh2A9V6GValYD2Le6_JNvSjOiMrNZnPbGq8XY8LP5R5ozlIAVhGM9rvUKDy5iE7wm3S2rf5YBL5NZuaymoDiav_zmYPcPJS1b_DodJAxEu_d3zB1Sjg/s1600/sample+data.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB1CZutl4_tUSgS3EfZHxsJ1MyQh2A9V6GValYD2Le6_JNvSjOiMrNZnPbGq8XY8LP5R5ozlIAVhGM9rvUKDy5iE7wm3S2rf5YBL5NZuaymoDiav_zmYPcPJS1b_DodJAxEu_d3zB1Sjg/s1600/sample+data.JPG&quot; /&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
In this example we have a company table with two contacts for each company, and an email address for each contact. &amp;nbsp;Instead of two columns for contact names and two for email addresses, we would like to reduce this to one of each: CompanyName, ContactName, EmailAddress. &amp;nbsp;First let&#39;s try and reduce the contact1 and contact2 columns to one:&lt;br /&gt;
&lt;blockquote style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ID&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;CompanyName&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ContactName&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ID&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;CompanyName&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Company&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;src&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;UNPIVOT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ContactName&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;FOR&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;IN&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;pvt&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsNJJ03nPXFaOvrFLF7TQEm0nW9iUWz3YrSgLAUOOrIpSwzx3rVyJqYIxxFnPWavMLzQS9RRjRDYpEpehv9MhPLvRBG4mESHf3riwNKoeog8Wh4zlfNb6NxKib95YU_UUDUDqJmZuh164/s1600/sample+data.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsNJJ03nPXFaOvrFLF7TQEm0nW9iUWz3YrSgLAUOOrIpSwzx3rVyJqYIxxFnPWavMLzQS9RRjRDYpEpehv9MhPLvRBG4mESHf3riwNKoeog8Wh4zlfNb6NxKib95YU_UUDUDqJmZuh164/s1600/sample+data.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
We have now replaced the contact1 and contact2 fields with a single ContactName field. &amp;nbsp;I have added the new Contact column to indicate the source of the data. &amp;nbsp;This can be removed from the select list if it is not required.&lt;br /&gt;
&lt;br /&gt;
Next we need to perform a similar unpivot on the email columns. &amp;nbsp;This is actually easier that you might think. &amp;nbsp;Rather than nesting queries, or complex CTEs, we need only append our first query with another unpivot statement:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&lt;/span&gt;
&lt;span style=&quot;color: teal;&quot;&gt;Id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;CompanyName&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ContactName&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;EmailAddress&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Id&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;CompanyName&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Email1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Email2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Company&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;src&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;UNPIVOT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ContactName&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;FOR&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;IN&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;pvt1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;UNPIVOT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;EmailAddress&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;For&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Email&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;IN&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Email1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Email2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;pvt2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;WHERE&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;RIGHT(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Contact&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;RIGHT(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;Email&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixbr8pkywEzFwhgj1J90G7FGHVs8TzjnDRc3xvYJaFn-IlpAq84rufF1d7qzgvSbZywBncT1bwy_KiDSGD5jtxrvfl9euS0D6fDJTqO98EsiWhQAbZ4dmn3b2z5kAieeRsQIsg6hPWWcg/s1600/sample+data.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixbr8pkywEzFwhgj1J90G7FGHVs8TzjnDRc3xvYJaFn-IlpAq84rufF1d7qzgvSbZywBncT1bwy_KiDSGD5jtxrvfl9euS0D6fDJTqO98EsiWhQAbZ4dmn3b2z5kAieeRsQIsg6hPWWcg/s1600/sample+data.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
The &lt;span style=&quot;color: blue;&quot;&gt;WHERE &lt;/span&gt;clause in the above query is very important. &amp;nbsp;Without it, we haven&#39;t told SQL Server how the second set of unpivoted data matches the first. &amp;nbsp;Without the&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;WHERE&amp;nbsp;&lt;/span&gt;, it will do a cross join, resulting&amp;nbsp;in Mr Purple occurring twice; once with the correct email address, and once with the address of Mr Orange, his colleague. &lt;br /&gt;
&lt;br /&gt;
In the&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;WHERE&amp;nbsp;&lt;/span&gt;clause we state that the new columns we have created, Contact (selected) and Email (not&amp;nbsp;selected) should be joined on the last character. Remember that these two columns are populated with the names of the original fields, ie Contact1, Contact2, and Email1, Email2. &amp;nbsp;The two columns therefore align as Contact1 → Email1 (1=1) and Contact2 → Email2 (2=2). &amp;nbsp;Hence it is important that the names of the original columns in our source Company table contain text such that they can be joined together in the WHERE clause at the end of the query.&lt;br /&gt;
&lt;br /&gt;
More details on the &lt;span style=&quot;color: #999999;&quot;&gt;PIVOT / UNPIVOT&lt;/span&gt; command can be found here:&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx&quot; target=&quot;_blank&quot;&gt;http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx&lt;/a&gt;&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2014/11/sql-unpivot-on-multiple-columns.html</link><author>noreply@blogger.com (SQL Banana)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB1CZutl4_tUSgS3EfZHxsJ1MyQh2A9V6GValYD2Le6_JNvSjOiMrNZnPbGq8XY8LP5R5ozlIAVhGM9rvUKDy5iE7wm3S2rf5YBL5NZuaymoDiav_zmYPcPJS1b_DodJAxEu_d3zB1Sjg/s72-c/sample+data.JPG" height="72" width="72"/><thr:total>22</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-469400611151614332</guid><pubDate>Wed, 24 Sep 2014 13:29:00 +0000</pubDate><atom:updated>2015-01-15T13:14:41.766+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">CATCH</category><category domain="http://www.blogger.com/atom/ns#">error handling</category><category domain="http://www.blogger.com/atom/ns#">errors</category><category domain="http://www.blogger.com/atom/ns#">example</category><category domain="http://www.blogger.com/atom/ns#">global variables</category><category domain="http://www.blogger.com/atom/ns#">RAISERROR</category><category domain="http://www.blogger.com/atom/ns#">ROLLBACK</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">throw</category><category domain="http://www.blogger.com/atom/ns#">trancount</category><category domain="http://www.blogger.com/atom/ns#">transaction</category><category domain="http://www.blogger.com/atom/ns#">TRY</category><title>SQL Server Error Handling</title><description>&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
Good error handling can be critical to
writing robust code and ensuring predictable behavior should things
go wrong. We always want to be certain that our database will be left in
a clean and desirable state, which can be challenging if, say, a stored
procedure errors part way through a series of inter-dependent steps.&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
As a basic starting point for basic handling of problem scenarios it&#39;s good
practice to contain code within transactions:&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRAN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: green;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;--DO
SOMETIHNG &lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;COMMIT&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
If multiple steps are contained within
the transaction, and any one should fail, then all the steps will be
rolled back, leaving the database in the same state it was before the transaction began.&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
With SQL 2005, &lt;span style=&quot;color: #0000cc;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRY..CATCH&lt;/span&gt;&lt;/span&gt;
blocks were introduced.  Using this feature we can greatly enhance
the error handling.  We can now “try” to do our data manipulation
and should any step fail, we can “catch” the error and perform further &quot;clean up&quot; steps as a result.&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
A simple example might be to open a
transaction in the &lt;span style=&quot;color: #0000cc;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRY&lt;/span&gt;&lt;/span&gt;
block, roll it back in the &lt;span style=&quot;color: #0000cc;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CATCH&lt;/span&gt;&lt;/span&gt;,
else commit it:&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRY&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRAN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;--DO
SOMETHING&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;END&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRY&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CATCH&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;IF&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;@@TRANCOUNT&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ROLLBACK&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;END&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CATCH&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;IF&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;@@TRANCOUNT&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;COMMIT&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
For more details on useful global variables such as &lt;span style=&quot;color: #ff00cc;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;@@TRANCOUNT&lt;/span&gt;&lt;/span&gt;
see my post on &lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2013/12/sql-server-global-variables.html&quot; target=&quot;_blank&quot;&gt;SQL Server Global Variables.&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
The above code allows us to cleanly
handle error scenarios and greatly improve the robustness of our code
and therefore our database.  However, the above example does not
actually let us know any details about the error that has occurred. 
To do that, we need to capture the error information: 
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRY&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
10&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;END&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRY&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CATCH&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;
   &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ERROR_MESSAGE&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
ErrorMessage&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;
   &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ERROR_NUMBER&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
ErrorNumber,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;
  &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ERROR_SEVERITY&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
ErrorSeverity,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;
   &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ERROR_STATE&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
ErrorState,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;
 &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ERROR_LINE&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
ErrorLine&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;
 &lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;END&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CATCH&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
In the above script I have forced a
“divide by zero” error in the &lt;span style=&quot;color: #0000cc;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRY&lt;/span&gt;&lt;/span&gt;
block.  When this error is thrown, the code will jump into the &lt;span style=&quot;color: #0000cc;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CATCH&lt;/span&gt;&lt;/span&gt;
block and execute the select statement:&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-size: 8pt;&quot;&gt;ErrorMessage
                         ErrorNumber ErrorSeverity ErrorState 
ErrorLine&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-size: 8pt;&quot;&gt;-------------------------------------
----------- ------------- ----------- -----------&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-size: 8pt;&quot;&gt;Divide
by zero error encountered.     8134        16            1          
3&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
Note that the actual error has not been
thrown, and the code has completed successfully without any
problems.  We are simply displaying the results of the various error
functions.&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
We can make this even more powerful if
we use the &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;RAISERROR&lt;/span&gt;&lt;/span&gt;
function.  This will allow us to actually throw an error message back
to the calling application.  The advantage this gives us is that we
can capture the error from the &lt;span style=&quot;color: #0000cc;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRY&lt;/span&gt;&lt;/span&gt;,
perform remedial action in the &lt;span style=&quot;color: #0000cc;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CATCH&lt;/span&gt;&lt;/span&gt;,
and then let the calling application know that there was an error we
had to clean up:&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
@ErrorMsg &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;VARCHAR&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;500&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRY&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRAN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
10&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;END&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;TRY&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CATCH&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
@ErrorMsg &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&#39;Error:
&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ERROR_MESSAGE&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CHAR&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;13&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&#39;Severity:
&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CAST&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ERROR_SEVERITY&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;VARCHAR&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;100&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CHAR&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;13&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&#39;State:
&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CAST&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ERROR_STATE&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;VARCHAR&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;100&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CHAR&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;13&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&#39;Number:
&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CAST&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ERROR_NUMBER&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;VARCHAR&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;100&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CHAR&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;13&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&#39;Line:
&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CAST&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ERROR_LINE&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;VARCHAR&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;100&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;IF&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;@@TRANCOUNT&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;ROLLBACK&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;RAISERROR&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;@ErrorMsg&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
16&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;10&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;END&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CATCH&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;IF&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;@@TRANCOUNT&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span lang=&quot;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;COMMIT&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 2.5cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 1.25cm;&quot;&gt;
&lt;span style=&quot;font-size: 8pt;&quot;&gt;-----------&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 1.25cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 1.25cm;&quot;&gt;
&lt;span style=&quot;font-size: 8pt;&quot;&gt;(0
row(s) affected)&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 1.25cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 1.25cm;&quot;&gt;
&lt;span style=&quot;color: #ff3333;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Msg
50000, Level 16, State 10, Line 21&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 1.25cm;&quot;&gt;
&lt;span style=&quot;color: #ff3333;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Error:
Divide by zero error encountered.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 1.25cm;&quot;&gt;
&lt;span style=&quot;color: #ff3333;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Severity:
16&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 1.25cm;&quot;&gt;
&lt;span style=&quot;color: #ff3333;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;State:
1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div lang=&quot;&quot; style=&quot;margin-bottom: 0cm; margin-left: 1.25cm;&quot;&gt;
&lt;span style=&quot;color: #ff3333;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Number:
8134&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm; margin-left: 1.25cm;&quot;&gt;
&lt;span style=&quot;color: #ff3333;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Line:
5&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
The result is that the error is
captured and handled - the transaction is rolled back. Using the
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;RAISERROR&lt;/span&gt;&lt;/span&gt; function,
however, we have build our own custom error message to return to the
calling application.  In the example above, this message lets us know
even the exact line that caused the error.  This message can be
enriched to include as much information as you wish, which can
greatly ease debugging of the problem.&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
(It&#39;s worth noting that the severity of
the error I have set to 16.  Different severity numbers can have
different effects in different places, and typically 11-18 are the
range for user defined errors. See the MSDN link at the end of this
article for more details.)&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
Finally, &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;RAISERROR&lt;/span&gt;&lt;/span&gt;
can be used to throw msg_id&#39;s, and this is the  more standard SQL
behaviour.  The list of standard error messages that can be thrown
are contained in the &lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;messages&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
table. You can insert your own (with an
id &amp;gt;50000) and then use &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;RAISERROR&lt;/span&gt;&lt;/span&gt;
to throw these when required.&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
Error handling can be used to powerful
effect using the techniques described.  If you have any techniques
you use or ideas on how to improve on the methods described here,
please feel free to share them.&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;MSDN&lt;/span&gt; - &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;RAISERROR&lt;/span&gt;&lt;/span&gt;:&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms178592.aspx&quot;&gt;http://msdn.microsoft.com/en-us/library/ms178592.aspx&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2014/09/sql-server-error-handling.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-1359834018410400523</guid><pubDate>Wed, 30 Jul 2014 17:06:00 +0000</pubDate><atom:updated>2015-01-15T13:17:03.644+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">algorithm</category><category domain="http://www.blogger.com/atom/ns#">CHECKSUM</category><category domain="http://www.blogger.com/atom/ns#">ETL</category><category domain="http://www.blogger.com/atom/ns#">example</category><category domain="http://www.blogger.com/atom/ns#">FOR XML</category><category domain="http://www.blogger.com/atom/ns#">HASHBYTES</category><category domain="http://www.blogger.com/atom/ns#">MD5</category><category domain="http://www.blogger.com/atom/ns#">raw</category><category domain="http://www.blogger.com/atom/ns#">SCD</category><category domain="http://www.blogger.com/atom/ns#">slowly changing dimension</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">XML</category><title>CheckSum, HashBytes and Slowly Changing Dimensions</title><description>A recent requirement for a DW was to implement a Type 2 Slowly Changing Dimension across all attributes in the dimension. &amp;nbsp;Unfortunately, the dimension had many, many attributes (30+), which meant that a comparison of all attribute values would be needed for each new record that arrived.&lt;br /&gt;
&lt;br /&gt;
To recap:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Type 2 Slowly Changing Dimension (SCD2)&lt;/b&gt;&lt;br /&gt;
SCDs are methodologies for tracking historical changes to a data row in a dimension table over time. While SCD1 is just an overwrite when there is a change (no history), SCD2 involves entering a new record into the table with the new changed data. &amp;nbsp;A new surrogate key is generated for the new record, while maintaining the natural key to allow the new record to be linked to the old one. &amp;nbsp;Typically, there would also be either an IsCurrent flag, to identify which is the current version of the data, and/or Start and End Date columns to indicate the order and time span of the changes:&lt;br /&gt;
&lt;br /&gt;
&lt;table class=&quot;wikitable&quot; style=&quot;background-color: #f9f9f9; border-collapse: collapse; border: 1px solid rgb(170, 170, 170); color: black; font-family: sans-serif; font-size: 14px; line-height: 22.399999618530273px; margin: 1em 0px; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th style=&quot;background-color: #f2f2f2; border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;Surrogate_Key&lt;/th&gt;&lt;th style=&quot;background-color: #f2f2f2; border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;Natural_Key&lt;/th&gt;&lt;th style=&quot;background-color: #f2f2f2; border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;CustName&lt;/th&gt;&lt;th style=&quot;background-color: #f2f2f2; border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;Country&lt;/th&gt;&lt;th style=&quot;background-color: #f2f2f2; border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;Start_Date&lt;/th&gt;&lt;th style=&quot;background-color: #f2f2f2; border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;End_Date&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;100&lt;/td&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;375&lt;/td&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;Guy&lt;/td&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;Scotland&lt;/td&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;01-Jan-2002&lt;/td&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;31-Jul-2005&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;101&lt;/td&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;375&lt;/td&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;Guy&lt;/td&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;England&lt;/td&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;01-Aug-2005&lt;/td&gt;&lt;td style=&quot;border: 1px solid rgb(170, 170, 170); padding: 0.2em;&quot;&gt;31-Dec-9999&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;div&gt;
In the above example the Country has changed and so a new record with the new country and new surrogate key has been inserted. &amp;nbsp;As each new row arrives, it&#39;s country is checked against the existing records country to see if it has changed or not.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
This is straightforward if there is only the one attribute being tracked. &amp;nbsp;A simple string comparison will quickly identify the change. &amp;nbsp;However, if there are many attributes being compared then the query doing this comparison can become quite long and perform poorly, &amp;nbsp;particularly if you have large numbers of records arriving.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
In order to solve this problem it was decided to &lt;i&gt;hashcode &lt;/i&gt;the relevant (tracked) columns together and save the output to a new column. &amp;nbsp;If we hashcode the incoming records on the same columns as well, then all we have to do is compare the two hash columns to see if there the record has changed.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
There are a number of ways to do this:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CHECKSUM()&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
From BOL:&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;color: #2a2a2a; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 18px;&quot;&gt;&amp;nbsp;CHECKSUM applied over any two lists of expressions returns the same value if the corresponding elements of the two lists have the same type and are equal when compared using the equals (=) operator...... If one of the values in the expression list changes, the checksum of the list also generally changes. However, there is a small chance that the checksum will not change. For this reason, &lt;i&gt;we do not recommend using CHECKSUM&lt;/i&gt; to detect whether values have changed, unless your application can tolerate occasionally missing a change.&lt;/span&gt;&lt;/blockquote&gt;
In fact a quick test using&amp;nbsp;&lt;span style=&quot;color: magenta;&quot;&gt;CHECKSUM() &lt;/span&gt;to hashcode 100,000 GUIDs resulted in up to 3 repeated hashcodes (&amp;nbsp;&quot;collisions&quot;). &amp;nbsp;The &lt;span style=&quot;color: magenta;&quot;&gt;CHECKSUM &lt;/span&gt;is very fast, but this rate of collision is not tolerable in this scenario.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: magenta;&quot;&gt;HASHBYTES()&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;HASHBYTES()&amp;nbsp;&lt;/span&gt;performs a similar function but the rate of collision is much much lower. It&#39;s not zero, but to date I have never been able to generate a collision.&lt;br /&gt;
To use this function, an algorithm needs to be specified. &lt;a href=&quot;http://blog.kejser.org/2011/11/06/exploring-hash-functions-in-sql-server/&quot; target=&quot;_blank&quot;&gt;Thomas Kesjer&lt;/a&gt; has done a detailed comparison of the various algorithms&#39; performance, and produced this graph:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-qMKw6iajMRefbPXTk-hHSAGySz83KM4B9w9pIYL5cmwW1hnuj9ALWXn6k78KBun8lCb8v17UAOkdX2Uiug3brCLljvUAE0AywsvSWHVtGSE-Es4V5CyWXVKbrs1oHtASEi5Kf-YoRZ4/s1600/hashcode.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-qMKw6iajMRefbPXTk-hHSAGySz83KM4B9w9pIYL5cmwW1hnuj9ALWXn6k78KBun8lCb8v17UAOkdX2Uiug3brCLljvUAE0AywsvSWHVtGSE-Es4V5CyWXVKbrs1oHtASEi5Kf-YoRZ4/s1600/hashcode.JPG&quot; height=&quot;245&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
The performance of the &lt;span style=&quot;color: magenta;&quot;&gt;HASHBYTES &lt;/span&gt;algorithms are largely similar (poorer than CHECKSUM), with the exception of MD2, which is much slower. &amp;nbsp;Typically I use MD5. &amp;nbsp;The other point to note is that&amp;nbsp;&lt;span style=&quot;color: magenta;&quot;&gt;HASHBYTES&amp;nbsp;&lt;/span&gt;requires a&amp;nbsp;single string parameter to be passed, not multiple columns eg:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 36.0pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: magenta;&quot;&gt;HASHBYTES&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;MD5&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; Country&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt; DummyTable&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;br /&gt;
In order to hashcode multiple columns together we can either concatenate them together as strings, or an even neater way would be to convert it to an XML string using &lt;span style=&quot;color: blue;&quot;&gt;FOR XML&lt;/span&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;text-indent: 36.0pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: magenta;&quot;&gt;HASHBYTES&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;MD5&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&lt;/span&gt; CustName&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;
Country &lt;span style=&quot;color: blue;&quot;&gt;FOR&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;XML&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;RAW&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;br /&gt;
Using this technique we can hashcode all the existing records in the table, and as each new record arrives we can look at its hashcode and compare it (joining on the Natural Key). &amp;nbsp;If the code is different then the record has changed, and is inserted. If not, it is ignored.&lt;br /&gt;
&lt;br /&gt;
This should be much simpler to maintain and perform much better than doing individual comparisons of the all the changing fields.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://sqlbanana.blogspot.com/2014/07/checksum-hashbytes-and-slowly-changing.html</link><author>noreply@blogger.com (SQL Banana)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-qMKw6iajMRefbPXTk-hHSAGySz83KM4B9w9pIYL5cmwW1hnuj9ALWXn6k78KBun8lCb8v17UAOkdX2Uiug3brCLljvUAE0AywsvSWHVtGSE-Es4V5CyWXVKbrs1oHtASEi5Kf-YoRZ4/s72-c/hashcode.JPG" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-4414217279057338361</guid><pubDate>Tue, 08 Jul 2014 12:20:00 +0000</pubDate><atom:updated>2015-01-15T13:21:03.580+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">clean up</category><category domain="http://www.blogger.com/atom/ns#">DELETE</category><category domain="http://www.blogger.com/atom/ns#">dependencies</category><category domain="http://www.blogger.com/atom/ns#">drop</category><category domain="http://www.blogger.com/atom/ns#">example</category><category domain="http://www.blogger.com/atom/ns#">is_ms_shipped</category><category domain="http://www.blogger.com/atom/ns#">master database</category><category domain="http://www.blogger.com/atom/ns#">original</category><category domain="http://www.blogger.com/atom/ns#">restore</category><category domain="http://www.blogger.com/atom/ns#">schema</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">sysobjects</category><category domain="http://www.blogger.com/atom/ns#">system objects</category><title>SQL to Drop All Non-System Objects From Master DB </title><description>&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;We&#39;ve all been there - well I have anyway. You&#39;ve just created your script to generate all the required objects for your database. &amp;nbsp;You run the script in the new environment. &amp;nbsp;But wait, you forgot to set the database! &amp;nbsp;All the objects have been created in the master db instead!&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;The number of times this has happened to me and to others I have worked with is infuriating. &amp;nbsp;I&#39;ve sat there watching people spend ages deleting one by one all the db objects using Management Studio, stumbling when there are dependencies, and generally wasting a lot if time.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;To solve this problem I have put together a short script to generate all the required drop statements. Fortunately, the sys.objects table contains a handy &quot;is_ms_shipped&quot; column that makes life much simpler:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: red; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&#39;IF&amp;nbsp; EXISTS (SELECT *
FROM sys.objects WHERE object_id = OBJECT_ID(N&#39;&#39;[dbo].[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;Name&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;]&#39;&#39;))&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;CHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;10&lt;span style=&quot;color: grey;&quot;&gt;)+&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;DROP &#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;CASE&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;WHEN&lt;/span&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;type&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;U&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;TABLE &#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;WHEN&lt;/span&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;type&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;P&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;PROCEDURE &#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;WHEN&lt;/span&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;type&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;FN&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;FUNCTION &#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;WHEN&lt;/span&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;type&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;V&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;VIEW &#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; Name &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;CHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;10&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;GO&#39;&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;CHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;10&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;Master&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;objects&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; is_ms_shipped &lt;span style=&quot;color: grey;&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;AND&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TYPE&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;IN&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;U&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;P&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;FN&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;V&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;ORDER&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;BY&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;CASE&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;WHEN&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;type&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;P&#39;&lt;/span&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;WHEN&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;type&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;V&#39;&lt;/span&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; 2&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;WHEN&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;type&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;FN&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; 3&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;WHEN&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;type&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;U&#39;&lt;/span&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; 4&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;background: white; margin-left: 36.0pt; mso-background-themecolor: background1;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;This script will generate all the DROP statements for the user-defined objects in the master db. &amp;nbsp;These need to be copied into a new window (easiest if you have been outputted the results to text rather than to grid in SSMS) and executed.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;If there are any foreign key constraints, however, the above script will generate an error. &amp;nbsp;To handle this either execute all the drop statements over and over until the errors cease (each subsequent execution of the drop statements will delete additional tables as each foreign key table is dropped), or, prior to executing the above script, execute the following:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;ALTER TABLE [&#39;&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;SCHEMA_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;schema_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;].[&#39;&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;fk&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;parent_object_ID&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;] DROP CONSTRAINT &#39;&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;
fk&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;name&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;CHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;10&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;GO&#39;&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;CHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;10&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;foreign_keys&lt;/span&gt; fk&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; is_ms_shipped &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 0&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
This will generate the necessary statements that need to be run in order to drop all the constraints. Copy the output to a new query window and execute.&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2014/07/sql-to-drop-all-non-system-objects-from.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-168400012011075544</guid><pubDate>Tue, 27 May 2014 13:49:00 +0000</pubDate><atom:updated>2015-01-15T13:25:52.282+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">example</category><category domain="http://www.blogger.com/atom/ns#">execution</category><category domain="http://www.blogger.com/atom/ns#">execution plan</category><category domain="http://www.blogger.com/atom/ns#">local</category><category domain="http://www.blogger.com/atom/ns#">optimize</category><category domain="http://www.blogger.com/atom/ns#">option</category><category domain="http://www.blogger.com/atom/ns#">parameters</category><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">query plan</category><category domain="http://www.blogger.com/atom/ns#">recompile</category><category domain="http://www.blogger.com/atom/ns#">rowcount</category><category domain="http://www.blogger.com/atom/ns#">showplan</category><category domain="http://www.blogger.com/atom/ns#">slow running</category><category domain="http://www.blogger.com/atom/ns#">sniffing</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">variable</category><category domain="http://www.blogger.com/atom/ns#">XML</category><title>SQL Server Parameter Sniffing - Slow Running Queries</title><description>&lt;span class=&quot;&quot;&gt;Parameter sniffing is a problem that can occasionally creep into code, just when everything seems fine. &lt;/span&gt;A stored procedure that yesterday ran nice and fast, today is taking much longer. The next day it is fast again. Nothing has changed on the server / db. It can be difficult to track down why.&lt;br /&gt;
&lt;br /&gt;
The answer could be Parameter Sniffing.&lt;br /&gt;
&lt;br /&gt;
When a stored procedure is compiled, it compiles using ( or &quot;sniffing&quot;) the parameter values set ​​at the time of the invocation. It uses those parameter values ​​to determine the optimal execution plan for the proc. &lt;span class=&quot;&quot;&gt;Simply put, if you later call the proc with a different parameter value, it will still use the execution plan determined by the first set of values. &lt;/span&gt;It will do this regardless of whether a better execution plan would have been more appropriate for the new value.&lt;br /&gt;
&lt;br /&gt;
For example, if I have a table containing all the values ​​from 1 to 100, and then 100 rows with a value 999:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: green; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;/ * Create table * /&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;CREATE &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TABLE&lt;/span&gt; DummyData &lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; col1 &lt;span style=&quot;color: blue;&quot;&gt;INT &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;​​)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;@Counter &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: green; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;/ * Insert data 1-100 * /&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;@Counter &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 0&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;WHILE &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;@Counter &lt;span style=&quot;color: grey;&quot;&gt;&amp;lt;&lt;/span&gt; 100&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;INSERT &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt; DummyData &lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; col1 &lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VALUES &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; @Counter &lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;@Counter &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; @Counter &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: green; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&lt;span class=&quot;&quot;&gt;/ * Insert 100 data rows with value 999 * /&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;@Counter &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 0&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;WHILE &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;@Counter &lt;span style=&quot;color: grey;&quot;&gt;&amp;lt;&lt;/span&gt; 100&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;INSERT &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt; DummyData &lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; col1 &lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VALUES &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; 999 &lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;@Counter &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; @Counter &lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-left: 36.0pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Next create a procedure to select values from this table:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;CREATE &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;PROC&lt;/span&gt; SelectDummyData &lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; @DummyValue &lt;span style=&quot;color: blue;&quot;&gt;INT &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;* &lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt;   
DummyData &lt;span style=&quot;color: blue;&quot;&gt;WHERE&lt;/span&gt; col1 &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;   
@DummyValue&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-left: 36.0pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
Now generate the Execution Plan for this procedure, passing a parameter of 999, and look at the &quot;estimated number of rows&quot;:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;EXEC &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SelectDummyData &lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;@DummyValue &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 999&lt;span style=&quot;color: blue;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDJJm1dceTbVXseyi__w_GtcRAXalMUn-6CRMT3uPfbM6STA6oL3LxkpaQ36kL4EJ0IL-zAgHycQWd_AGQw9-B-J5JarUMaRbHo3onHKhywKORi_qm-EhhQAWtVZ_YUK_01GFGlBRsPUk/s1600/dummydata+999.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDJJm1dceTbVXseyi__w_GtcRAXalMUn-6CRMT3uPfbM6STA6oL3LxkpaQ36kL4EJ0IL-zAgHycQWd_AGQw9-B-J5JarUMaRbHo3onHKhywKORi_qm-EhhQAWtVZ_YUK_01GFGlBRsPUk/s1600/dummydata+999.JPG&quot; height=&quot;169&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
The row count is 100, which is what we would expect. But re-run the execute statement, this time with @DummyValue = 5, and the Estimated Number of Rows is still 100, when we would expect it to be 1.&lt;br /&gt;
&lt;br /&gt;
The reason it is still expecting 100 rows is because the execution plan was determined when the proc was first compiled, when we used a value of 999. &amp;nbsp;It is still using the same execution plan, and therefore the same number of rows is expected, regardless of the input value supplied.&lt;br /&gt;
&lt;br /&gt;
The impact of this oddity in this example is negligible, but for a complex query it can result in significantly slower performance.&lt;br /&gt;
&lt;br /&gt;
If your query is complex it might be easier to generate the execution plan in XML ( &lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;SET &lt;/span&gt; &lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;SHOWPLAN_XML &lt;/span&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;) and then search the XML for something like &quot;ParameterCompiledValue =&quot;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;To avoid parameter sniffing issues from occurring there are a number of options that can be considered:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;b&gt;1. Recompile the stored proc every time it is executed:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;EXEC &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SelectDummyData &lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;@DummyValue &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;   &amp;nbsp;1&lt;br /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;WITH &lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;RECOMPILE&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/blockquote&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;background: white; mso-background-themecolor: background1;&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;This will ensure that the proc uses the supplied&amp;nbsp;&lt;/span&gt; parameters&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;to build the best execution plan every time, but the overhead is the constant recompilation. This may be acceptable if there is sufficient gain in query performance.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Generating the execution plan on the above script, using various parameters, shows the correct number of rows being estimated each time.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;If the parameter sniffing &amp;nbsp;relates to a set of queries in particular, then&amp;nbsp;&lt;/span&gt; recompilation &lt;span style=&quot;font-family: inherit;&quot;&gt;can be specified individually for queries:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;* &lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt;   
DummyData &lt;span style=&quot;color: blue;&quot;&gt;WHERE&lt;/span&gt; col1 &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;   
@DummyValue&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-left: 36.0pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;OPTION &lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;( &lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;RECOMPILE &lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;b&gt;2. Optimize the query/ies for a specific parameter value:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;ALTER &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;PROC&lt;/span&gt; SelectDummyData &lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; @DummyValue &lt;span style=&quot;color: blue;&quot;&gt;INT &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;* &lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt;   
DummyData &lt;span style=&quot;color: blue;&quot;&gt;WHERE&lt;/span&gt; col1 &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;   
@DummyValue&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;OPTION &lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;( &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;optimize &lt;span style=&quot;color: blue;&quot;&gt;FOR &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; @DummyValue &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 1 &lt;span style=&quot;color: grey;&quot;&gt;))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;background: white; margin-left: 36.0pt; mso-background-themecolor: background1;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
With this option we can force the query plan to be based on the same known value every time it is compiled (eg During maintenance tasks). This option may work if we know the optimal value for our system. The downside, however, is that it may still perform poorly for other values, but that may well be tolerable.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;3. Avoid sniffing altogether, by using a local variable inside the proc:&lt;/b&gt;&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot; style=&quot;background: white; mso-background-themecolor: background1;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;ALTER &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;PROC&lt;/span&gt;   
SelectDummyData &lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt; @DummyValue
   &lt;span style=&quot;color: blue;&quot;&gt;INT &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;br /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;BEGIN &lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;@LocalDummyValue &lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;@LocalDummyValue &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; @DummyValue&lt;br /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;* &lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt; DummyData &lt;span style=&quot;color: blue;&quot;&gt;WHERE&lt;/span&gt;   
col1 &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; @LocalDummyValue&lt;br /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;END&amp;nbsp;&lt;/span&gt;&lt;/blockquote&gt;
&lt;br /&gt;
Here there is no possibility of parameter sniffing, and the execution plan will be based on the statistics. Note, however, that the plan will be built using s&lt;span style=&quot;font-family: inherit;&quot;&gt;tatistic densities &lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;instead &lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;of &lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;statistic histograms, which are less accurate. Therefore it is not guaranteed to be the best plan for all possible values.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
Parameter sniffing has the potential to cause queries to run with widely varying performance. In order to mitigate this problem consider modifying the code based on the options presented above. Hopefully this will help keep the queries and stored procedures performing well wherever they are used.&lt;br /&gt;
&lt;br /&gt;
More useful info on this topic is available here:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;background: white; mso-background-themecolor: background1;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Degremont Michel&lt;/span&gt;&lt;span style=&quot;background: #CED5DB; color: blue; font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;a href=&quot;http://blogs.technet.com/b/mdegre/archive/2012/03/19/what-is-parameter-sniffing.aspx&quot;&gt;http://blogs.technet.com/b/mdegre/archive/2012/03/19/what-is-parameter-sniffing.aspx&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Turgay Sahtiyan&lt;br /&gt;
&lt;a href=&quot;http://blogs.msdn.com/b/turgays/archive/2013/09/10/parameter-sniffing-problem-and-workarounds.aspx&quot;&gt;http://blogs.msdn.com/b/turgays/archive/2013/09/10/parameter-sniffing-problem-and-workarounds.aspx&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://sqlbanana.blogspot.com/2014/05/sql-server-parameter-sniffing-slow.html</link><author>noreply@blogger.com (SQL Banana)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDJJm1dceTbVXseyi__w_GtcRAXalMUn-6CRMT3uPfbM6STA6oL3LxkpaQ36kL4EJ0IL-zAgHycQWd_AGQw9-B-J5JarUMaRbHo3onHKhywKORi_qm-EhhQAWtVZ_YUK_01GFGlBRsPUk/s72-c/dummydata+999.JPG" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-9068362943226492685</guid><pubDate>Mon, 28 Apr 2014 15:15:00 +0000</pubDate><atom:updated>2015-01-15T13:20:30.391+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">complete</category><category domain="http://www.blogger.com/atom/ns#">dynamic</category><category domain="http://www.blogger.com/atom/ns#">example</category><category domain="http://www.blogger.com/atom/ns#">execution</category><category domain="http://www.blogger.com/atom/ns#">GUI</category><category domain="http://www.blogger.com/atom/ns#">is running</category><category domain="http://www.blogger.com/atom/ns#">job</category><category domain="http://www.blogger.com/atom/ns#">job activity</category><category domain="http://www.blogger.com/atom/ns#">schedule</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">SQL Agent</category><category domain="http://www.blogger.com/atom/ns#">steps</category><category domain="http://www.blogger.com/atom/ns#">sysjobs</category><category domain="http://www.blogger.com/atom/ns#">WAIT</category><title>SQL Server: Dynamically Check if SQL Agent Job is Running </title><description>&lt;div class=&quot;tr_bq&quot;&gt;
It can be helpful to have a SQL script to determine if a SQL Agent job is currently running or not. In SSMS this check can be done using the tools and GUIs provided, but it may be necessary to embed this check in script. &amp;nbsp;For example you may have a series of scripts, some scheduled through jobs and some triggered by, say, a button click on an interface. &amp;nbsp;If you need to ensure that the button job does not run at the same time as the Agent Job, you&#39;ll need to dynamically check the agent job status. Here&#39;s how to do it.&lt;/div&gt;
&lt;br /&gt;
First let&#39;s create a simple SQL Agent job with two steps:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1RFO6Ub47ef4ohwq47V8bs9niO3Hp4KNAe_JlkQSQ64y4zgfIAy2-5t41_mWaH9mjdIJ_TP2N0H1Jl5ysY0XnoQQqLyxOIr9Z2LTqcth8-hg143BGs3Kw5wrsKI_OkjdGpGE62fe0CGk/s1600/WAIT.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1RFO6Ub47ef4ohwq47V8bs9niO3Hp4KNAe_JlkQSQ64y4zgfIAy2-5t41_mWaH9mjdIJ_TP2N0H1Jl5ysY0XnoQQqLyxOIr9Z2LTqcth8-hg143BGs3Kw5wrsKI_OkjdGpGE62fe0CGk/s1600/WAIT.JPG&quot; height=&quot;196&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
In the above image I have created a new job called TestDelay, containing two steps, WAIT 1 and WAIT 2. &amp;nbsp;The first step simply waits for 20 seconds before completing successfully, while the second step waits for a further 5 seconds before doing the same.&lt;br /&gt;
&lt;br /&gt;
The information relating to this job and it&#39;s execution is contained in the following tables in the msdb database:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;dbo.sysjobs&lt;br /&gt;dbo.sysjobsteps&lt;br /&gt;dbo.sysjobactivity&lt;/span&gt;&lt;/blockquote&gt;
&lt;br /&gt;
From the first table we can find out the job_id, which is key for joining the 3 tables together:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; job_id &lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt; msdb&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;sysjobs &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; name &lt;span style=&quot;color: grey;&quot;&gt;LIKE&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;TestDelay&#39;&lt;/span&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;br /&gt;
Then from the second table we can find out &quot;step&quot; information:&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; step_id&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; step_name&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; subsystem &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; msdb&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;sysjobsteps &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; job_id &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&lt;/span&gt; job_id &lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt;
MSDB&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;sysjobs
&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;WHERE&lt;/span&gt; name &lt;span style=&quot;color: grey;&quot;&gt;LIKE&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;TestDelay&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;step_id &amp;nbsp; &amp;nbsp; step_name &amp;nbsp; &amp;nbsp; subsystem&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;----------- ---------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WAIT 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TSQL&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WAIT 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TSQL&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;(2 row(s) affected)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Here we can see the two steps in our job and their step_ids. Using the third table we can run the following query for our job:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; start_execution_date&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;last_executed_step_id&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;last_executed_step_date &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;msdb&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;sysjobactivity a&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;INNER&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;JOIN&lt;/span&gt;&amp;nbsp;msdb&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;sysjobs
j&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none; text-indent: 36.0pt;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &amp;nbsp;&amp;nbsp;j&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;job_id &lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; a&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;job_id&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;j&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;name &lt;span style=&quot;color: grey;&quot;&gt;LIKE&lt;/span&gt;
&lt;span style=&quot;color: red;&quot;&gt;&#39;TestDelay&#39;&lt;/span&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;start_execution_date &amp;nbsp; &amp;nbsp;last_executed_step_id last_executed_step_date&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;----------------------- --------------------- -----------------------&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;2014-04-28 15:20:23.000 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2014-04-28 15:20:43.000&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;(1 row(s) affected)&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
We can see that on it&#39;s most recent execution, the job finished it&#39;s second and final step at 15:20:43. If the job were still running and had not completed even the first step yet, &amp;nbsp;then the&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt; last_executed_step_id&lt;/span&gt; and&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt; last_executed_step_date&lt;/span&gt; fields would be null for this row.&lt;br /&gt;
If, however, the first step has completed but the second has not, the two fields will be populated but the step_id will be 1, not 2. &lt;br /&gt;
&lt;br /&gt;
So to know if our job is still running we really need to combine these three tables into a single query, checking whether the last_execution_step_id field is null, and if not, if it is at it&#39;s maximum value or not.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;DISTINCT&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; start_execution_date&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; last_executed_step_id&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; last_executed_step_date&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;CASE&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;WHEN&lt;/span&gt; &amp;nbsp;last_executed_step_id
&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;step_id&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;Job Complete&#39;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;ELSE&lt;/span&gt;
&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&#39;Job Still
Running&#39;&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; result&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; msdb&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;sysjobactivity a&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;INNER&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;JOIN&lt;/span&gt;&amp;nbsp; msdb&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;sysjobsteps
s &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;ON&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;job_id&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;s&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;job_id &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;INNER&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;JOIN&lt;/span&gt;&amp;nbsp; msdb&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;dbo&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;sysjobs
j &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;ON&lt;/span&gt;
&amp;nbsp;&amp;nbsp; j&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;job_id
&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; a&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;job_id &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; j&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;name &lt;span style=&quot;color: grey;&quot;&gt;LIKE&lt;/span&gt;
&lt;span style=&quot;color: red;&quot;&gt;&#39;TestDelay&#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;GROUP&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;BY&lt;/span&gt; &amp;nbsp;&amp;nbsp; start_execution_date&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; last_executed_step_id&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-no-proof: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; last_executed_step_date&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;br /&gt;
In the above query, the &quot;result&quot; field will say either &quot;Job Still Running&quot; or &quot;Job Complete&quot; depending on the logic stated above. &amp;nbsp;Alternatively the script could be rewritten in the form of a stored procedure that accepts a parameter containing the job name and returns a 1/0 depending on whether it is running or not.&lt;br /&gt;
&lt;br /&gt;
It is worth examining the rest of the fields in these three tables as they can offer additional options for extracting information about the jobs set up on the server.</description><link>http://sqlbanana.blogspot.com/2014/04/sql-server-check-if-sql-agent-job-is.html</link><author>noreply@blogger.com (SQL Banana)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1RFO6Ub47ef4ohwq47V8bs9niO3Hp4KNAe_JlkQSQ64y4zgfIAy2-5t41_mWaH9mjdIJ_TP2N0H1Jl5ysY0XnoQQqLyxOIr9Z2LTqcth8-hg143BGs3Kw5wrsKI_OkjdGpGE62fe0CGk/s72-c/WAIT.JPG" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-8320794894601085329</guid><pubDate>Thu, 27 Mar 2014 11:55:00 +0000</pubDate><atom:updated>2015-01-15T13:26:58.397+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">DISTINCT</category><category domain="http://www.blogger.com/atom/ns#">duplicates</category><category domain="http://www.blogger.com/atom/ns#">example</category><category domain="http://www.blogger.com/atom/ns#">optimize</category><category domain="http://www.blogger.com/atom/ns#">order</category><category domain="http://www.blogger.com/atom/ns#">order by</category><category domain="http://www.blogger.com/atom/ns#">partition by</category><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">RANK</category><category domain="http://www.blogger.com/atom/ns#">ranking</category><category domain="http://www.blogger.com/atom/ns#">remove</category><category domain="http://www.blogger.com/atom/ns#">score</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">Window Functions</category><title>Using Rank() to Remove Duplicates </title><description>&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;In a recent post on&amp;nbsp;&lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2013/11/sql-server-window-functions.html&quot; target=&quot;_blank&quot;&gt;SQL Server Window Functions&lt;/a&gt;&amp;nbsp;I looked at &quot;scoring&quot; rows of data based on the value of specified columns.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;The same approach can be used to help cleanse data prior to, or as part of ETL processes. &amp;nbsp;Data issues should be corrected at source, but in the real world this isn&#39;t always possible. &amp;nbsp;The source may be external, there may be issues of data ownership, or there simply may not be time to carry out the process.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;It often falls to the SQL Developer to handle these issues. Here I will look at handling the issue of two records in a table for the same entity but with slightly different spelling (I refer to this as &amp;nbsp;&quot;duplicate&quot; records, though the records are not exact copies).&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;I&#39;ve encountered this problem several times at different organisations. &amp;nbsp;If the two records were identical, it would simply be a case of using a &lt;span style=&quot;color: blue;&quot;&gt;SELECT DISTINCT.&lt;/span&gt; You might also consider imposing a &lt;b&gt;unique constraint&lt;/b&gt; to ensure there are no actual duplicates.&lt;span style=&quot;color: blue;&quot;&gt; &amp;nbsp;&lt;/span&gt;But the spelling difference means that they are regarded as two completely different entities by the code.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;When presenting this to business users, the response has often been &quot;Just take the first one&quot;. &amp;nbsp;But how do you actually just take the first one? And first one based on what exactly?&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;First let&#39;s create some data: &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;TABLE&lt;/span&gt; Stones&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;1&lt;span style=&quot;color: grey;&quot;&gt;),&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;FirstName&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;10&lt;span style=&quot;color: grey;&quot;&gt;),&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;LastName&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;20&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt;&amp;nbsp;Stones&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;FirstName&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; LastName&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Mick&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Jagger&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;Stones&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;FirstName&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; LastName&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Michael&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Jagger&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;Stones&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;FirstName&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; LastName&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Kieth&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Richards&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;Stones&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;FirstName&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; LastName&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Ronnie&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Wood&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;INTO&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;Stones&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;FirstName&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; LastName&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Charlie&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Watts&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;SELECT * FROM&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;Stones&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FirstName &amp;nbsp;LastName&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;----------- ---------- --------------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Mick &amp;nbsp; &amp;nbsp; &amp;nbsp; Jagger&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Michael &amp;nbsp; &amp;nbsp;Jagger&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Kieth &amp;nbsp; &amp;nbsp; &amp;nbsp;Richards&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Ronnie &amp;nbsp; &amp;nbsp; Wood&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Charlie &amp;nbsp; &amp;nbsp;Watts&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New; font-size: x-small;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;(5 row(s) affected)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Note IDs 1 and 2 for Mick and Michael Jagger. &amp;nbsp;These are actually the same person, and we only want to pick up the first one. &amp;nbsp;We only want 4 rows in our result.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
We can use the &lt;span style=&quot;color: magenta;&quot;&gt;RANK&lt;/span&gt;() function to now score these records, partitioning by LastName (see &lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2013/11/sql-server-window-functions.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&amp;nbsp;for more on how to do this):&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; font-size: 10pt; line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;&amp;nbsp; SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;&amp;nbsp;ID&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FirstName&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;LastName&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: magenta; font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;RANK&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;OVER&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;PARTITION&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;BY&lt;/span&gt;&amp;nbsp;LastName&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;ORDER&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;BY&lt;/span&gt;&amp;nbsp;ID &lt;span style=&quot;color: blue;&quot;&gt;ASC&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt;&amp;nbsp;Score&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;&amp;nbsp; FROM&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;Stones&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ORDER BY&lt;/span&gt; 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FirstName &amp;nbsp;LastName &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Score&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;----------- ---------- -------------------- --------------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Mick &amp;nbsp; &amp;nbsp; &amp;nbsp; Jagger &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Michael &amp;nbsp; &amp;nbsp;Jagger &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Kieth &amp;nbsp; &amp;nbsp; &amp;nbsp;Richards &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Ronnie &amp;nbsp; &amp;nbsp; Wood &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Charlie &amp;nbsp; &amp;nbsp;Watts &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;(5 row(s) affected)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;I have ordered by ID, on the assumption that &quot;Take the first one&quot; means the first one to enter the table (the seeding of the table I have therefore used as the indicator of &quot;first&quot;. If there are timestamps, they can be used instead.)&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;Examining the results of the query above shows that we now only require records with a score of 1. &amp;nbsp;It is then quite a simple process to use the above code in a&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;sub-query&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&amp;nbsp;with a &lt;span style=&quot;color: blue;&quot;&gt;WHERE&lt;/span&gt; clause to restrict to the rows we want:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt; &lt;span style=&quot;color: grey;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&amp;nbsp; SELECT&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt; ID&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FirstName&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;LastName&lt;span style=&quot;color: grey;&quot;&gt;,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: magenta; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;RANK&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;OVER &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;PARTITION&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;BY&lt;/span&gt; LastName
&lt;span style=&quot;color: blue;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;BY&lt;/span&gt;
ID A&lt;span style=&quot;color: blue;&quot;&gt;SC&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; Score&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&amp;nbsp; FROM&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 15.333333015441895px;&quot;&gt;Stones&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: grey; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;a&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt; Score&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt; 1&lt;/span&gt;&lt;span lang=&quot;EN&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FirstName &amp;nbsp;LastName &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Score&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;----------- ---------- -------------------- --------------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Mick &amp;nbsp; &amp;nbsp; &amp;nbsp; Jagger &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Kieth &amp;nbsp; &amp;nbsp; &amp;nbsp;Richards &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Charlie &amp;nbsp; &amp;nbsp;Watts &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Ronnie &amp;nbsp; &amp;nbsp; Wood &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;(4 row(s) affected)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Courier New; font-size: x-small;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;line-height: 15.333333015441895px;&quot;&gt;We are now left only with the four rows we want. All duplicates have been excluded.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2014/03/using-rank-to-remove-duplicates.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-1559523915947654112</guid><pubDate>Wed, 05 Mar 2014 17:44:00 +0000</pubDate><atom:updated>2015-01-15T16:14:51.219+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">15138</category><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">alter</category><category domain="http://www.blogger.com/atom/ns#">authorisation</category><category domain="http://www.blogger.com/atom/ns#">database</category><category domain="http://www.blogger.com/atom/ns#">db_owner</category><category domain="http://www.blogger.com/atom/ns#">drop</category><category domain="http://www.blogger.com/atom/ns#">ERROR</category><category domain="http://www.blogger.com/atom/ns#">login</category><category domain="http://www.blogger.com/atom/ns#">prinipal</category><category domain="http://www.blogger.com/atom/ns#">schema</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">SSMS</category><category domain="http://www.blogger.com/atom/ns#">sys.schemas</category><category domain="http://www.blogger.com/atom/ns#">user_id</category><category domain="http://www.blogger.com/atom/ns#">user_name</category><title>The database principal owns a schema in the database, and cannot be dropped (Error: 15138)</title><description>On a recent audit of a database I noticed that there were a lot of logins for a database that were not required. &amp;nbsp;I set about removing the logins one by one, but soon hit the following error:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #2a2a2a; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; line-height: 18.165599822998047px;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;Error: 15138 The database principal owns a schema in the database, and cannot be dropped.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Well this threw me a bit, and I could not figure out what was going on and why, this one particular login could not be removed. &amp;nbsp;As the message indicates, this account owns a schema on the DB. So I opened the user properties from SSMS and sure enough, the user &amp;nbsp;in question owned three schemas: db_datareader, db_datawriter, db_owner. &amp;nbsp;However, SSMS will not simply allow you to unselect the checkboxes here, so it is still not so easy to remove this link and allow the user to be deleted!&lt;br /&gt;
&lt;br /&gt;
The schemas owned by the user can also be displayed by running the following query, within the context of the database under scrutiny:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;SELECT&amp;nbsp;&lt;/span&gt;s&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;name&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;schemas&lt;/span&gt;&amp;nbsp;s&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;WHERE&lt;/span&gt;&amp;nbsp;s&lt;span style=&quot;color: grey;&quot;&gt;.&lt;/span&gt;principal_id&amp;nbsp;&lt;span style=&quot;color: grey;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: magenta;&quot;&gt;USER_ID&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;UserInQuestion&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
For a more complete picture of all schemas and users associated with the database:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;SELECT&amp;nbsp;&lt;/span&gt;*, &lt;span style=&quot;color: magenta;&quot;&gt;USER_NAME&lt;/span&gt;(principal_id) &lt;span style=&quot;color: blue;&quot;&gt;as &lt;/span&gt;username&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;FROM&lt;/span&gt; &lt;span style=&quot;color: #274e13;&quot;&gt;sys.schemas;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In order to remove this user we have to first transfer ownership of these schemas to a different user. &amp;nbsp;Typically this would be dbo. &amp;nbsp;This has to be done once for each schema owned, using the&lt;span style=&quot;color: blue;&quot;&gt; ALTER AUTHORISATION ON SCHEMA&lt;/span&gt; command:&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ALTER AUTHORISATION ON SCHEMA&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;::&lt;/span&gt;db_datareader&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;TO&lt;/span&gt;&amp;nbsp;dbo&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ALTER AUTHORISATION ON SCHEMA&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;::&lt;/span&gt;db_datawriter&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;TO&lt;/span&gt;&amp;nbsp;dbo&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ALTER AUTHORISATION ON SCHEMA&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;::&lt;/span&gt;db_owner&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;TO&lt;/span&gt;&amp;nbsp;dbo&lt;br /&gt;
&lt;br /&gt;
(note the double colon!)&lt;br /&gt;
&lt;br /&gt;
Re-run the previous scripts to ensure that the user in question no longer owns any schemas. &amp;nbsp;Now if we again try the delete again, this user will be removed successfully.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://sqlbanana.blogspot.com/2014/03/the-database-principal-owns-schema-in.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-3797262764224382699</guid><pubDate>Mon, 03 Feb 2014 12:46:00 +0000</pubDate><atom:updated>2015-01-15T16:15:10.748+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">CHECKSUM</category><category domain="http://www.blogger.com/atom/ns#">data</category><category domain="http://www.blogger.com/atom/ns#">default</category><category domain="http://www.blogger.com/atom/ns#">dummy</category><category domain="http://www.blogger.com/atom/ns#">newid</category><category domain="http://www.blogger.com/atom/ns#">random</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">unique</category><category domain="http://www.blogger.com/atom/ns#">uniqueidentifier</category><category domain="http://www.blogger.com/atom/ns#">values</category><title>Random Data Using SQL Default Values</title><description>Wow, I decided to take a bit of time off over the winter and the next thing I know it&#39;s been two months since the last &amp;nbsp;&lt;a href=&quot;http://sqlbanana.blogspot.co.uk/&quot; target=&quot;_blank&quot;&gt;SQL Banana&lt;/a&gt;&amp;nbsp;update!&lt;br /&gt;
&lt;br /&gt;
While faffing around over the last few weeks I came across a few code snippets I found quite useful. &amp;nbsp;Here&#39;s the first :)&lt;br /&gt;
&lt;br /&gt;
Often I need to fill a table with some random dummy data. &amp;nbsp;Lot&#39;s of people have different ways of doing this, and I don&#39;t want to get into a discussion of what &quot;random&quot;&amp;nbsp;actually&amp;nbsp;means when working with computers, but 99% of the time this approach works just fine for me.&lt;br /&gt;
&lt;br /&gt;
First create your table. &amp;nbsp;Set a default constraint to use the &lt;span style=&quot;color: magenta;&quot;&gt;NEWID&lt;/span&gt;&lt;span style=&quot;color: #999999;&quot;&gt;()&lt;/span&gt; function - this will generate a &lt;b&gt;uniqueidentifier&lt;/b&gt; value. &amp;nbsp;The value will be unique every time it is generated. &amp;nbsp;Then use the &lt;span style=&quot;color: magenta;&quot;&gt;CHECKSUM&lt;/span&gt;&lt;span style=&quot;color: #999999;&quot;&gt;() &lt;/span&gt;function around it to compute it&#39;s hash value, &amp;nbsp;for our purposes effectively generating an &lt;b&gt;int&amp;nbsp;&lt;/b&gt;based on the &lt;b&gt;uniqueidentifier &lt;/b&gt;value.&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;CREATE TABLE&lt;/span&gt; dbo.Dummy&lt;br /&gt;
(&lt;br /&gt;
Id&lt;span style=&quot;color: blue;&quot;&gt; INT IDENTITY&lt;/span&gt; (1,1) &lt;span style=&quot;color: blue;&quot;&gt;PRIMARY KEY&lt;/span&gt;,&lt;br /&gt;
IntData&lt;span style=&quot;color: blue;&quot;&gt; INT CONSTRAINT &lt;/span&gt;DummyDefault &lt;span style=&quot;color: blue;&quot;&gt;DEFAULT&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CHECKSUM&lt;/span&gt;&lt;span style=&quot;color: #999999;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;NEWID&lt;/span&gt;&lt;span style=&quot;color: #999999;&quot;&gt;())&lt;/span&gt;)&lt;/blockquote&gt;
Next, insert as much row data as required:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;INSERT INTO&lt;/span&gt; dbo.Dummy&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;DEFAULT VALUES&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;GO&lt;/span&gt; 500&lt;/blockquote&gt;
This will run the insert 500 times, each time the identity column will increment, and a random integer will be inserted into the IntData field.&lt;br /&gt;
&lt;br /&gt;
If you require &lt;b&gt;varchar &lt;/b&gt;data then simply adjust the default constraint as required. EG:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
CharData &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;(&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;) &lt;span style=&quot;color: blue;&quot;&gt;CONSTRAINT&lt;/span&gt; TestCharDefault &lt;span style=&quot;color: blue;&quot;&gt;DEFAULT&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;(&lt;span style=&quot;color: magenta;&quot;&gt;NEWID&lt;/span&gt;() as &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;(&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;/span&gt;))&amp;nbsp;&lt;/blockquote&gt;
And that&#39;s it. Dummy data is now ready for use.&lt;br /&gt;
&lt;br /&gt;</description><link>http://sqlbanana.blogspot.com/2014/02/random-data-using-sql-default-values.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-6424260406899271361</guid><pubDate>Mon, 02 Dec 2013 19:27:00 +0000</pubDate><atom:updated>2015-01-15T16:14:33.746+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">CPU</category><category domain="http://www.blogger.com/atom/ns#">ERROR</category><category domain="http://www.blogger.com/atom/ns#">global variables</category><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">RANK</category><category domain="http://www.blogger.com/atom/ns#">ROLLBACK</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">trancount</category><category domain="http://www.blogger.com/atom/ns#">transaction</category><title>SQL Server Global Variables: @@Rowcount, @@Error and more</title><description>There are lots of SQL Server global variables (prefixed with @@), but I thought I&#39;d list a few that I have found particularly useful in the past. &amp;nbsp;I often find I need to make use of them to gather data when I first go on client sites:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span lang=&quot;en&quot;&gt;@@ROWCOUNT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Stores the number of rows affected by the last command. &amp;nbsp;I find myself using this variable all the time for logging. &amp;nbsp;For example, if I have a stored procedure inserting/updating/deleting data from a table, I use this variable to store the results of each of those commands in a log table. &amp;nbsp;This makes debugging much easier and any spikes or drops in records can be captured and easily viewed. &amp;nbsp;A word of warning though - this variable only stores the rowcount for the last command. &amp;nbsp;If you find that you are not storing the correct number, it is likely you have another line of code executing before you are reading this variable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span lang=&quot;en&quot;&gt;@@ERROR&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Stores the error code for the immediately previous command. &amp;nbsp;I use this most often in conjunction with the&amp;nbsp;&lt;span style=&quot;color: magenta; font-family: Consolas, sans-serif; line-height: 100%;&quot;&gt;@@ROWCOUNT&lt;/span&gt;&amp;nbsp;variable, and store them together for logging/debugging purposes. &amp;nbsp;Note that if you want to log both of them&amp;nbsp;for the same line of code, you need to make sure to write them both to the output in a single command. If you don&#39;t do this, then whichever variable&#39;s value is written second, will contain the result from the writing of the first variable, and not the actual insert/update/delete that is of concern. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span lang=&quot;en&quot;&gt;@@SPID&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Stores the session ID. &amp;nbsp;Again, my advice would be to log it with the previous two values. &amp;nbsp;If you have a number of sessions performing a range of activities, the log table will be confusing without being able to identify which sessions are doing what.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span lang=&quot;en&quot;&gt;@@TRANCOUNT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Stores the number of currently open transactions. &amp;nbsp;I often perform a check using this variable in my &lt;span style=&quot;color: blue;&quot;&gt;CATCH &lt;/span&gt;block to determine if we have hit an error inside a transaction and left it open :&amp;nbsp;&lt;span style=&quot;color: blue; font-family: Consolas, sans-serif; line-height: 100%;&quot;&gt;IF
&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: Consolas, sans-serif; line-height: 100%;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta; font-family: Consolas, sans-serif; line-height: 100%;&quot;&gt;@@TRANCOUNT&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif; line-height: 100%;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: Consolas, sans-serif; line-height: 100%;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif; line-height: 100%;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: Consolas, sans-serif; line-height: 100%;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif; line-height: 100%;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: Consolas, sans-serif; line-height: 100%;&quot;&gt;ROLLBACK&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;@@CPU_BUSY&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;@@IDLE&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;@@IO_BUSY&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
These ones are useful for gathering stats on the SQL box. &amp;nbsp;They store the number of &quot;ticks&quot; that the CPU has been busy doing SQL Server activities, the amount of ticks SQL Server has been idle and the number of ticks SQL Server has spent doing IO. &amp;nbsp;All are measured since the last time SQL Server was started.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span lang=&quot;en&quot;&gt;@@TIMETICKS&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
The number of microseconds per tick - this helps to transform the previous stats into understandable time :)&lt;br /&gt;
&lt;br /&gt;
There are many other global variables available in SQL Server, but this is simply meant to be a list of the ones I find myself using most often.</description><link>http://sqlbanana.blogspot.com/2013/12/sql-server-global-variables.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-5998545976209335670</guid><pubDate>Thu, 07 Nov 2013 18:00:00 +0000</pubDate><atom:updated>2015-01-15T16:16:25.300+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">analytic</category><category domain="http://www.blogger.com/atom/ns#">CTE</category><category domain="http://www.blogger.com/atom/ns#">example</category><category domain="http://www.blogger.com/atom/ns#">functions</category><category domain="http://www.blogger.com/atom/ns#">order by</category><category domain="http://www.blogger.com/atom/ns#">OVER</category><category domain="http://www.blogger.com/atom/ns#">partition by</category><category domain="http://www.blogger.com/atom/ns#">RANK</category><category domain="http://www.blogger.com/atom/ns#">ranking</category><category domain="http://www.blogger.com/atom/ns#">row number</category><category domain="http://www.blogger.com/atom/ns#">row_number</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">Window Functions</category><title>SQL Server Window Functions: RANK, DENSE_RANK, NTILE, ROW_NUMBER</title><description>Recently I have been finding it more and more useful to work with Window functions. These are sometimes referred to Analytic functions or more commonly Ranking functions:&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;RANK&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;DENSE_RANK&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;NTILE&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;ROW_NUMBER&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
These functions essentially &quot;score&quot; each row that is returned by the select statement. &amp;nbsp;So for example if we look at the Sales.SalesTerritory table in AdventureWorks and run the following query:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;t&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;Name&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;t&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;CountryRegionCode&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;t&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;[Group]&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;t&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;SalesYTD,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;RANK&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;OVER
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;ORDER&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;BY&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;SalesYTD&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal; font-family: Consolas, sans-serif;&quot;&gt;Ranked&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;sales&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;SalesTerritory&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
This results in the final column contain the &quot;rank&quot; of the record (ascending order), ie if it is the second lowest SalesYTD this column will contain a two.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
These functions become much more powerful, however, when using the optional &lt;span style=&quot;color: blue;&quot;&gt;PARTITION BY &lt;/span&gt;clause that can be stipulated. &amp;nbsp;Using this clause we can &quot;subrank&quot; within partitions:&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;t&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;Name&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;t&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;CountryRegionCode&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;t&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;[Group]&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;t&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;SalesYTD&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt; 
   &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;RANK&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;OVER
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;PARTITION&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;BY&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;[Group]&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;ORDER&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;BY&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;SalesYTD&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;SubRank&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;RANK&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;OVER
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;ORDER&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;BY&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;SalesYTD&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;OverallRank&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;sales&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;SalesTerritory&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
The results of this query display each record, ranked within its Group (Europe, North America, Pacific):
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;Name
                                              CountryRegionCode Group &amp;nbsp; &amp;nbsp;SalesYTD &amp;nbsp; &amp;nbsp; &amp;nbsp;SubRank              OverallRank&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;--------------------------------------------------
----------------- ---&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;Northeast &amp;nbsp; &amp;nbsp; &amp;nbsp;US                North
America                                      2402176.8476 &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;Southeast &amp;nbsp; &amp;nbsp; &amp;nbsp;US                North
America                                      2538667.2515 &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;Central &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;US                North
America                                      3072175.118 &amp;nbsp; 3&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;3&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;Germany &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DE                Europe &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3805202.3478 &amp;nbsp;1&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;4&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;France &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FR                Europe &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4772398.3078 &amp;nbsp;2&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;5&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;United
Kingdom                                     GB                Europe &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5012905.3656 &amp;nbsp;3&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;6&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;Australia &amp;nbsp; &amp;nbsp; &amp;nbsp;AU                Pacific &amp;nbsp; &amp;nbsp; &amp;nbsp; 5977814.9154 &amp;nbsp;1&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;7&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;Canada &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; CA                North
America                                      6771829.1376 &amp;nbsp;4&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;8&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;Northwest &amp;nbsp; &amp;nbsp; &amp;nbsp;US                North
America                                      7887186.7882 &amp;nbsp;5&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;9&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;Southwest &amp;nbsp; &amp;nbsp; &amp;nbsp;US                North
America                                      10510853.8739         6&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;10&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;(10
row(s) affected)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
This makes it very easy for us to produce reports that return, say, only the top performers in each region.&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
These functions avoid the need to write subqueries or CTEs to produce their results and are therefore highly efficient. &amp;nbsp;More details on these four functions can be found on technet&amp;nbsp;&lt;a href=&quot;http://technet.microsoft.com/en-us/library/ms189798.aspx&quot;&gt;here&lt;/a&gt;.&lt;/div&gt;
&lt;br /&gt;
In the next post we&#39;ll look at additional lesser known window functions within SQL Server.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://sqlbanana.blogspot.com/2013/11/sql-server-window-functions.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-3366412552375285291</guid><pubDate>Fri, 25 Oct 2013 16:20:00 +0000</pubDate><atom:updated>2015-01-15T16:18:24.591+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">COMMIT</category><category domain="http://www.blogger.com/atom/ns#">deallocation</category><category domain="http://www.blogger.com/atom/ns#">DELETE</category><category domain="http://www.blogger.com/atom/ns#">example</category><category domain="http://www.blogger.com/atom/ns#">log</category><category domain="http://www.blogger.com/atom/ns#">ROLLBACK</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">transaction</category><category domain="http://www.blogger.com/atom/ns#">TRUNCATE</category><title>Truncate vs Delete - Myth vs Reality</title><description>I have been asked many times (often in interviews) the following question:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
Q. What is the difference between Truncate and Delete in SQL Server?&lt;/blockquote&gt;
The answer that, in my experience, is most expected and most often given is that Delete allows you roll back the data you have wiped, while Truncate does not - Truncate is irreversible. &amp;nbsp;A quick search on google gives a number of posts that say as much.&lt;br /&gt;
&lt;br /&gt;
Simple. &amp;nbsp;But wrong! &amp;nbsp;Just try this simple test. &lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Insert records into a table.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Open a transaction.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Truncate the table.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;View the contents of the table.&lt;/li&gt;
&lt;li&gt;Rollback the transaction.&lt;/li&gt;
&lt;li&gt;View the contents of the table.&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&amp;nbsp;The code below is a very simple example:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;CREATE&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;TABLE&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;MyTable&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;col1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;CHAR&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;col2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;INT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;GO&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;INSERT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;INTO&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;MyTable&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;col1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;col2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
1 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;UNION&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ALL&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;b&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
2 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;UNION&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ALL&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&#39;c&#39;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
3 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;GO&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;*&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;MyTable&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;--(3
row(s) affected) &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;TRAN&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;TRUNCATE&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;TABLE&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;MyTable&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;GO&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;*&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;MyTable&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;--(0
row(s) affected) &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;ROLLBACK&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;*&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;MyTable&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;
 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&lt;span style=&quot;font-family: Consolas, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;--(3
row(s) affected) &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/blockquote&gt;
Before the transaction is rolled back, there are no rows in the table, but after the rollback all three records have returned, despite having used Truncate to remove them. &amp;nbsp;Clearly Truncate &lt;i&gt;is&lt;/i&gt;&amp;nbsp;reversible!&lt;br /&gt;
&lt;br /&gt;
So what actually is the difference between Truncate and Delete? &amp;nbsp;There are a number of general differences, but the relevant one here is the following:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: #fcfcfc; border: 0px; line-height: 24px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;When rows are DELETE&#39;d the operation is logged for each row removed.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;TRUNCATE logs only the deallocation of the data pages rather than the rows themselves, making it a much faster operation. &amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #fcfcfc; border: 0px; line-height: 24px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;There are many other differences related to permission levels, seeding, triggers, contraints etc. That&#39;s for a future post. &amp;nbsp;For now, the point to note is that TRUNCATE can indeed be rolled back, and the myth has been busted!&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2013/10/truncate-vs-delete-myth-vs-reality.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-2671584498529726097</guid><pubDate>Tue, 08 Oct 2013 16:48:00 +0000</pubDate><atom:updated>2015-01-15T16:22:22.180+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">AggregationFunction</category><category domain="http://www.blogger.com/atom/ns#">Analysis Services</category><category domain="http://www.blogger.com/atom/ns#">Average</category><category domain="http://www.blogger.com/atom/ns#">AverageOfChildren</category><category domain="http://www.blogger.com/atom/ns#">calculated</category><category domain="http://www.blogger.com/atom/ns#">Count</category><category domain="http://www.blogger.com/atom/ns#">Cube</category><category domain="http://www.blogger.com/atom/ns#">MDX</category><category domain="http://www.blogger.com/atom/ns#">measure</category><category domain="http://www.blogger.com/atom/ns#">member</category><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">SSAS</category><category domain="http://www.blogger.com/atom/ns#">sum</category><category domain="http://www.blogger.com/atom/ns#">Time</category><category domain="http://www.blogger.com/atom/ns#">True Average</category><category domain="http://www.blogger.com/atom/ns#">Type</category><title>SSAS: MDX  True Average VS AverageOfChildren</title><description>Recently&amp;nbsp;I was tasked with&amp;nbsp;working on some slow performing calculations&amp;nbsp;in a cube.&amp;nbsp; In the Calculation script a developer had used&amp;nbsp;the&amp;nbsp;&lt;span style=&quot;color: purple;&quot;&gt;DECENDANTS &lt;/span&gt;function, with the&amp;nbsp;&lt;span style=&quot;color: blue;&quot;&gt;LEAVES&lt;/span&gt; flag to navigate all the way down to the bottom of several&amp;nbsp;dimensions&#39; hierarchies.&amp;nbsp;He had&amp;nbsp;then used &lt;span style=&quot;color: purple;&quot;&gt;AVG&lt;/span&gt; across the returned set for a specified measure, effectively aggregating all the way back up again.&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;
All the developer actually wanted to do was to have a measure that returned the average value for the current member selection.&amp;nbsp; By going down to the leaf level and then backup, however, the cube is having to scan through a potentially huge volume of data to&amp;nbsp;produce the answer. Inevitably this will result in degraded query performance for users.&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;
In fact this can be a lot simpler to do than&amp;nbsp;people realise.&amp;nbsp; True, SSAS 2008R2 &lt;em&gt;still&lt;/em&gt; does not&amp;nbsp;supply an inbuilt function to do this, which surprises many new users. One day maybe&amp;nbsp;it will,&amp;nbsp;&amp;nbsp;but in the meantime here is how I do it.&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;
First, it should be pointed out that there is a certain &quot;type&quot; of averaging that SSAS does support out of the&amp;nbsp;box.&amp;nbsp; If you&amp;nbsp;look at the options in the &lt;strong&gt;AggregationFunction&lt;/strong&gt; property for a measure you will see that, although by default&amp;nbsp;set to&amp;nbsp;&lt;strong&gt;Sum&lt;/strong&gt;, there is also one option called &lt;strong&gt;AverageOfChildren&lt;/strong&gt;. This&amp;nbsp;name is very misleading.&amp;nbsp; It is &lt;em&gt;not&lt;/em&gt; a true average as&amp;nbsp;I would understand.&amp;nbsp; It is actually an average only across the Time dimension.&amp;nbsp;&amp;nbsp;If you have two dates selected and are looking at the Sales Value measure&amp;nbsp;you will see it averaged across those two days.&amp;nbsp; However, if you are looking at a single day, but across two sales areas, you will &lt;em&gt;not &lt;/em&gt;see it averaged across those areas, but&amp;nbsp;summed.&amp;nbsp; Confusing, but that is&amp;nbsp;just what it does. Note that to enable this&amp;nbsp;correctly&amp;nbsp;your time dimension needs to have its &lt;strong&gt;Type&lt;/strong&gt; property set to &lt;strong&gt;Time&lt;/strong&gt;.&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;
If, however, you want to do a true average, you will need to create a calculated member in the calculations tab of your cube.&amp;nbsp; The calculation is very simple, for what is an average? It is the sum of the values, divided by the number of values.&amp;nbsp; The sum we have as the base measure from the fact table, say Sales Value.&amp;nbsp;When we create a new measure group in the cube it automatically generates a new measure suffixed &quot;Count&quot;.&amp;nbsp; This measure is none other than the count of values (or fact table rows) in the measure group. &lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;
Therefore the average calculation is the one measure divided by the other:&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;MEMBER&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;CURRENTCUBE&lt;/span&gt;.[Measures].AvgSales&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; Measures.Sales / [Measures].[SalesFactCount]&lt;span style=&quot;font-size: 10pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Since both measures are essentially straight from the fact table, their values are stored during processing time, and benefit from aggregation design too.&amp;nbsp; As a result the AvgSales measure ought to perform very fast.&lt;o:p&gt;&lt;/o:p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin: 0cm 0cm 0pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://sqlbanana.blogspot.com/2013/10/ssas-mdx-average-vs-averageofchildren.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-3039664159644389745</guid><pubDate>Fri, 27 Sep 2013 15:39:00 +0000</pubDate><atom:updated>2015-01-15T16:22:42.200+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">Analysis Services</category><category domain="http://www.blogger.com/atom/ns#">Cache</category><category domain="http://www.blogger.com/atom/ns#">Clear Cache</category><category domain="http://www.blogger.com/atom/ns#">Cube</category><category domain="http://www.blogger.com/atom/ns#">database</category><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">query</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">SSAS</category><category domain="http://www.blogger.com/atom/ns#">SSMS</category><category domain="http://www.blogger.com/atom/ns#">XMLA</category><title>SSAS 2008: Clear Cache Command</title><description>When testing query perfornance on an SSAS database it&#39;s important to know if you are starting from a warm or clear cache. &amp;nbsp;The simplest way to clear the cache is through an XMLA command, run from SSMS:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt; 
&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;ClearCache&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2003/engine&lt;/span&gt;&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&amp;lt;&lt;span style=&quot;color: #a31515;&quot;&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;Object&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&amp;lt;&lt;span style=&quot;color: #a31515;&quot;&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;DatabaseID&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;MyDatabase&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;DatabaseID&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&amp;lt;/&lt;span style=&quot;color: #a31515;&quot;&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;Object&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&amp;lt;/&lt;span style=&quot;color: #a31515;&quot;&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;ClearCache&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;This clears the cache for the entire database. You can also clear the cache just for a specific cube:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;ClearCache&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;http://schemas.microsoft.com/analysisservices/2003/engine&lt;/span&gt;&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&amp;lt;&lt;span style=&quot;color: #a31515;&quot;&gt;Object&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;
&amp;lt;&lt;span style=&quot;color: #a31515;&quot;&gt;DatabaseID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;MyDatabase&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;DatabaseID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;
&amp;lt;&lt;span style=&quot;color: #a31515;&quot;&gt;CubeID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;MyCube&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;CubeID&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;
&amp;lt;/&lt;span style=&quot;color: #a31515;&quot;&gt;Object&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;
&amp;lt;/&lt;span style=&quot;color: #a31515;&quot;&gt;ClearCache&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;</description><link>http://sqlbanana.blogspot.com/2013/09/ssas-2008-clear-cache-command.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-5420568996011196714</guid><pubDate>Mon, 09 Sep 2013 11:13:00 +0000</pubDate><atom:updated>2015-01-15T16:24:23.329+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">aggregation</category><category domain="http://www.blogger.com/atom/ns#">Analysis Services</category><category domain="http://www.blogger.com/atom/ns#">Cube</category><category domain="http://www.blogger.com/atom/ns#">currentmember</category><category domain="http://www.blogger.com/atom/ns#">DataMember</category><category domain="http://www.blogger.com/atom/ns#">MDX</category><category domain="http://www.blogger.com/atom/ns#">measures</category><category domain="http://www.blogger.com/atom/ns#">performance</category><category domain="http://www.blogger.com/atom/ns#">scope</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">SSAS</category><category domain="http://www.blogger.com/atom/ns#">THIS</category><title>SSAS: Performance of MDX SCOPE() statement</title><description>Further to my recent posts on using &lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2013/08/analysis-services-mdx-scope-statement.html&quot;&gt;SCOPE() statements in a cube&lt;/a&gt; and &lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2013/08/ssas-2008r2-scope-and-assignments-set.html&quot;&gt;potential issues in using the SCOPE() statement&lt;/a&gt;, I came across another issue last week.&lt;br /&gt;
&lt;br /&gt;
In&amp;nbsp;a cube&amp;nbsp;I was developing&amp;nbsp;I had used a&amp;nbsp;SCOPE() statement&amp;nbsp;to ensure that&amp;nbsp;when a particular measure was selected, the dimension&amp;nbsp;member used&amp;nbsp;(for&amp;nbsp;one particular dimension) would be the datamember,&amp;nbsp;so that the measure value displayed would be the &quot;fact table&quot; value and not the aggregated&amp;nbsp;value of the children. (for more on using the DATAMEMBER function see&amp;nbsp;&lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2013/08/ssas-datamember-parent-member-should.html&quot;&gt;this post&lt;/a&gt;).&lt;br /&gt;
&lt;br /&gt;
In the calculation tab of the cube editor I wrote the following:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SCOPE&lt;/span&gt;&lt;/span&gt;([Measures].[MyMeasure]); &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;THIS&lt;/span&gt;&lt;/span&gt;=[Portfolio].[Portfolio Name].&lt;span style=&quot;color: maroon;&quot;&gt;&lt;span style=&quot;color: maroon;&quot;&gt;CURRENTMEMBER&lt;/span&gt;&lt;/span&gt;.&lt;span style=&quot;color: maroon;&quot;&gt;&lt;span style=&quot;color: maroon;&quot;&gt;DATAMEMBER&lt;/span&gt;&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;END&lt;/span&gt;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SCOPE&lt;/span&gt;&lt;/span&gt;; &lt;br /&gt;
&lt;br /&gt;
However, in user testing accross large volumes of data this measure performed very slowly, taking over a minute to return results.&lt;br /&gt;
&lt;br /&gt;
I tried experimenting with different approaches, and found that the desired results could be achieved without using the scope statement.&amp;nbsp; A calculated measure can be used, that contains the datamember function in its logic:&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;CREATE&lt;/span&gt;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;MEMBER&lt;/span&gt;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;CURRENTCUBE&lt;/span&gt;&lt;/span&gt;.[Measures].[MyMeasure2]&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt;&lt;/span&gt; ([Measures].[MyMeasure,[Portfolio].[Portfolio Name].&lt;span style=&quot;color: maroon;&quot;&gt;&lt;span style=&quot;color: maroon;&quot;&gt;DATAMEMBER&lt;/span&gt;&lt;/span&gt;), &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;VISIBLE&lt;/span&gt;&lt;/span&gt; = 1 ; &lt;br /&gt;
&lt;br /&gt;
This measure produced the same result set but surprisingly the response time reduced to around ten seconds.&lt;br /&gt;
&lt;br /&gt;
If anyone has&amp;nbsp;had a similar experience and has&amp;nbsp;explanation why using the &lt;span style=&quot;color: maroon;&quot;&gt;DATAMEMBER &lt;/span&gt;function in a calculated measure rather than a &lt;span style=&quot;color: blue;&quot;&gt;SCOPE&lt;/span&gt; statement should perform faster please leave a comment and let me know!</description><link>http://sqlbanana.blogspot.com/2013/09/ssas-performance-of-mdx-scope-statement.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-423583499936205205.post-6940489202276337248</guid><pubDate>Thu, 29 Aug 2013 14:35:00 +0000</pubDate><atom:updated>2015-01-15T16:27:13.455+00:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">2008 R2</category><category domain="http://www.blogger.com/atom/ns#">Analysis Services</category><category domain="http://www.blogger.com/atom/ns#">calculated members</category><category domain="http://www.blogger.com/atom/ns#">Cube</category><category domain="http://www.blogger.com/atom/ns#">ERROR</category><category domain="http://www.blogger.com/atom/ns#">MDX</category><category domain="http://www.blogger.com/atom/ns#">measures</category><category domain="http://www.blogger.com/atom/ns#">scope</category><category domain="http://www.blogger.com/atom/ns#">SQL 2012</category><category domain="http://www.blogger.com/atom/ns#">SSAS</category><category domain="http://www.blogger.com/atom/ns#">subcube</category><category domain="http://www.blogger.com/atom/ns#">THIS</category><title>SSAS 2008R2 - Scope and Assignments - &amp;quot;A set has been encountered that cannot contain calculated members.&amp;quot;</title><description>In my previous post I discussed &lt;a href=&quot;http://sqlbanana.blogspot.co.uk/2013/08/analysis-services-mdx-scope-statement.html&quot;&gt;using the SCOPE statement in a cube&lt;/a&gt;.&amp;nbsp;&amp;nbsp;&amp;nbsp;I use it quite extensively in my designs and&amp;nbsp;last week I came accross an unexpected issue with it.&lt;br /&gt;
&lt;br /&gt;
I have a cube containing many measures for which&amp;nbsp;I require different functionality under certain conditions.&amp;nbsp; Those conditions&amp;nbsp;can actually be&amp;nbsp;described in subcubes, and therefore the SCOPE statement addresses this requirement.&lt;br /&gt;
&lt;br /&gt;
The functionality I want is the same for several measures and therefore I put them together in the same SCOPE block:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SCOPE&lt;/span&gt;([Measures].[Area1],[Measures].[Area2]); &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;THIS&lt;/span&gt;&lt;/span&gt;=(0); &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;END&lt;/span&gt;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SCOPE&lt;/span&gt;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
Where Area1 and Area2 are both measures defined in the cube.&amp;nbsp; The above scope would set them both to zero, but&amp;nbsp;this is just for illustration.&lt;br /&gt;
&lt;br /&gt;
The issue arises if the 2 measures above, Area1 and Area2, are actually calculated measures. If that is the case then when we deploy this MDX we get the following&amp;nbsp;error:&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&quot;A set has been encountered that cannot contain calculated members.&quot;&lt;br /&gt;
&lt;br /&gt;
This can be confusing as the error message does not actually identify the SCOPE as the source of the issue.&amp;nbsp; But you can be fairly sure that that is where the problem is.&lt;br /&gt;
&lt;br /&gt;
For some reason using&amp;nbsp;more than one calculated measure in a single scope statment is a problem for SSAS.&amp;nbsp; If you&amp;nbsp;have very few relevant measures then you could split them into&amp;nbsp;individual scope statments, but this would be laborious if you have several. The solution is to use an assignment script instead.&amp;nbsp;This is as simple as:&lt;br /&gt;
&lt;br /&gt;
({[Measures].[Area1],[Measures].[Area2]}) = 0;&lt;br /&gt;
&lt;br /&gt;
Type this in the calculation script and, hey presto, the error disappears and the MDX works as expected.&lt;br /&gt;
&lt;br /&gt;
Chris Webb also covers this &lt;a href=&quot;http://cwebbbi.wordpress.com/2007/08/30/scope-and-calculated-members/&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://sqlbanana.blogspot.com/2013/08/ssas-2008r2-scope-and-assignments-set.html</link><author>noreply@blogger.com (SQL Banana)</author><thr:total>0</thr:total></item></channel></rss>