<?xml version="1.0" encoding="iso-8859-1"?><!-- generator="b2evolution/4.0.3" -->
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Data Management - Author(s): Eli Weinstock-Herman (tarwn)</title>
		<link>http://blogs.lessthandot.com/index.php/DataMgmt/</link>
		<atom:link rel="self" type="application/rss+xml" href="http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2" />
		<description></description>
		<language>en-GB</language>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		<admin:generatorAgent rdf:resource="http://b2evolution.net/?v=4.0.3"/>
		<ttl>60</ttl>
				<item>
			<title>Deploying Database Changes with PowerShell</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/deploying-database-changes-with-powershell</link>
			<pubDate>Fri, 17 May 2013 07:36:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">Database Programming</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">2190@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Recently, while working on a personal project, I found myself needing a lightweight way to deploy database changes to multiple environments. In the past I have used a wide range of methods, ranging from applying the changes manually to applying changes via a diff tool (SQL Compare), to automatically applying manually created change scripts, to automatically applying diff scripts that were automatically generated, to working directly in production..er, pretend you didn&#039;t see that one. &lt;/p&gt;

&lt;h2&gt;Why not _________ tool?&lt;/h2&gt;

&lt;p&gt;There are a lot of tools out there to handle database deployments, but this is a small project that I am building incrementally as a minimum viable product. Rather than tie up a bunch of time researching and experimenting with database deployment tools early on, I decided to do something simple that would work for the time being and free me up to work on the actual product. &lt;/p&gt;

&lt;p&gt;What I want from the deployment is to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Spend as little time on this as possible&lt;/li&gt;
&lt;li&gt;Work against SQL Azure and a local 2008 R2 Server&lt;/li&gt;
&lt;li&gt;Call it from TeamCity for a local server or a remote one&lt;/li&gt;
&lt;li&gt;Produce readable output for TeamCity logs&lt;/li&gt;
&lt;li&gt;Create the databases and users from the ground up&lt;/li&gt;
&lt;li&gt;Include randomly generated data&lt;/li&gt;
&lt;li&gt;Manage scripts for 2 independent databases in the same build&lt;/li&gt;
&lt;li&gt;Allow real SQL (I&#039;m not scared of SQL and I don&#039;t want to learn a code abstraction just to deploy changes)&lt;/li&gt;
&lt;li&gt;Not worry about rollbacks. I am deploying small changes and if something breaks I&#039;ll be charging forward&lt;/li&gt;
&lt;li&gt;Not expose credentials, as the code will be visible to the public&lt;/li&gt;
&lt;li&gt;Be replaceable. I might replace it with a tool one day, so keep the deployment logic separate from the application&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Looking back at this list and what I eventually created, I probably could have used something like &lt;a href=&quot;https://github.com/brunomlopes/dbdeploy.net&quot; &quot;dbdploy on github&quot;&gt;DBDeploy&lt;/a&gt;. The scripts I created ended up taking a very similar approach.&lt;/p&gt;

&lt;h2&gt;The Deployment Scripts&lt;/h2&gt;

&lt;p&gt;My deployment consists of 4 PowerShell scripts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;ApplyDatabaseUpdates.ps1&lt;/strong&gt; - Responsible for generically applying changes from a folder to a specified database&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;UpdateCoreDatabase.ps1&lt;/strong&gt; - Responsible for the application&#039;s Core Database, calls ApplyDatabaseUpdates&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;UpdateSampleDatabase.ps1&lt;/strong&gt; - Response for the application&#039;s Sample Database, calls ApplyDatabaseUpdates&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RunLocally.ps1&lt;/strong&gt; - Executes the two Update scripts against the local database on my development machine(s)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This project has been spread out over 6 months, intermixed with life, other projects, blog posts, etc. Along the way I also upgraded my local development machine to SQL Server 2012 but my main test database server is on 2008 still and my release environment is Azure Database/SQL Azure/(whatever the name is this week).&lt;/p&gt;

&lt;h3&gt;ApplyDatabaseUpdates.ps1&lt;/h3&gt;
&lt;p&gt;The purpose of the ApplyDatabaseUpdates script is to apply all of the scripts in a specified folder to the specified server. To do this it creates a tracking table on the target database, then iterates through the contents of the folder, building a script containing any files that were not previously logged in the tracking table.&lt;/p&gt;

&lt;div style=&quot;text-align:center; color: #666666;&quot;&gt;
   &lt;img src=&quot;http://tiernok.com/LTDBlog/DatabaseDeployment/SQLScripts.png&quot; alt=&quot;Core DB Scripts folder&quot; /&gt;&lt;br /&gt;
   Core Database Scripts Folder
&lt;/div&gt;

&lt;p&gt;The deployment script wraps the contents of each script file in an EXECUTE statement, followed by an INSERT to add it to the tracking table for the database.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;a href=&quot;https://github.com/tarwn/SQLisHard/blob/master/Database/ApplyDatabaseUpdates.ps1&quot; title=&quot;View on github&quot;&gt;ApplyDatabaseUpdates.ps1&lt;/a&gt;&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;powershell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;powershell&quot; id=&quot;cb96929&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;function ApplyDatabaseUpdates&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; &amp;nbsp; param (&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [parameter(Mandatory=$true)]&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [string]&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $UpdatesFolder,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [parameter(Mandatory=$true)]&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [string]&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $Server,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [parameter(Mandatory=$true)]&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [string]&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $Database,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [parameter(Mandatory=$true)]&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [string]&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $AdminUserName,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [parameter(Mandatory=$true)]&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [string]&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $AdminPassword&lt;br /&gt;&amp;nbsp; &amp;nbsp; )&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; $path = (Get-Location).Path&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; # For SQL 2008 - load the modules&lt;br /&gt;&amp;nbsp; &amp;nbsp; try{ &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if ( (Get-PSSnapin -Name SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue) -eq $null -and (Get-PSSnapin -Registered -Name SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue) -ne $null){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Add-PSSnapin SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Add-PSSnapin SqlServerProviderSnapin100 -ErrorAction SilentlyContinue&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; catch{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Error &amp;quot;Powershell Script error: $_&amp;quot; -EA Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; #updates tracking&lt;br /&gt;&amp;nbsp; &amp;nbsp; try{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Creating Update Tracking Table If Not Exists&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Invoke-Sqlcmd -Query &amp;quot;IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = &#039;UpdateTracking&#039;) CREATE TABLE UpdateTracking (UpdateTrackingKey int IDENTITY(1,1) PRIMARY KEY, Name varchar(255) NOT NULL, Applied DateTime NOT NULL);&amp;quot; -ServerInstance &amp;quot;$Server&amp;quot; -Username &amp;quot;$AdminUserName&amp;quot; -Password &amp;quot;$AdminPassword&amp;quot; -Database &amp;quot;$Database&amp;quot; -ErrorAction Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Done&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; catch{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Error &amp;quot;Powershell Script error: $_&amp;quot; -EA Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; #database updates&lt;br /&gt;&amp;nbsp; &amp;nbsp; $outputPath = &amp;quot;$path\UpdatesBatch.sql&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; $stream = [System.IO.StreamWriter] &amp;quot;$outputPath&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; $fileUpdates = Get-ChildItem &amp;quot;$UpdatesFolder&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; $datestamp = $(get-date -f &amp;quot;yyyy-MM-dd HH:mm&amp;quot;)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;/* SQL Core Updates - Updated $datestamp */&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;BEGIN TRANSACTION&amp;quot;)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; foreach($file in $fileUpdates)&lt;br /&gt;&amp;nbsp; &amp;nbsp; {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $name = ($file.Name)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $namewe = ([System.IO.Path]::GetFileNameWithoutExtension($name))&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;/* File: $name */&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;IF NOT EXISTS (SELECT 1 FROM UpdateTracking WHERE Name = &#039;$namewe&#039;)&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;BEGIN&amp;quot;)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;`tPrint &#039;Applying Update: $namewe&#039;&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;`tEXEC(&#039;&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (Get-Content &amp;quot;$UpdatesFolder\$name&amp;quot;) | % {$_ -replace &amp;quot;&#039;&amp;quot;, &amp;quot;&#039;&#039;&amp;quot;} | % {$stream.WriteLine(&amp;quot;`t`t$_&amp;quot;)}&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;`t&#039;);&amp;quot;)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;`tINSERT INTO UpdateTracking(Name, Applied) SELECT &#039;$namewe&#039;, GETUTCDATE();&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;END&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; $stream.WriteLine(&amp;quot;COMMIT TRANSACTION&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; $stream.Close()&lt;br /&gt;&amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Update Script Created.&amp;quot;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Running updates...&amp;quot;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Invoke-SqlCmd -InputFile &amp;quot;$outputPath&amp;quot; -ServerInstance &amp;quot;$Server&amp;quot; -Username &amp;quot;$AdminUserName&amp;quot; -Password &amp;quot;$AdminPassword&amp;quot; -Database &amp;quot;$Database&amp;quot; -Verbose -ErrorAction Stop&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Remove-Item &amp;quot;$outputPath&amp;quot;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Updates completed.&amp;quot;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb34988&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;i style=&quot;display: block; padding: 1em; margin: 1em; background-color: #eeeeee&quot;&gt;Note: this has only been run in the context of my personal project. That means don&#039;t copy, paste, and run it immediately against your production environment. Running stuff blindly from the internet is known as both a bad idea and a career limiting maneuver.&lt;/i&gt;&lt;/p&gt;

&lt;h3&gt;Update____Database.ps1&lt;/h3&gt;

&lt;p&gt;My application has two databases which it will access via different accounts. I want the ability to rebuild these databases from scratch as well as manage their credentials from an external system (in this case, TeamCity). If the worst should happen and these databases are compromised or overwritten in some fashion, I want to be able to recreate them with new credentials, account names, the works. &lt;/p&gt;

&lt;p&gt;To make life more difficult, many of these commands have to be executed individually in order to work with Azure Databases.&lt;/p&gt;

&lt;p&gt;Both scripts detect if their specified database exists and, if not, create them. The UpdateSampleDatabase is capable of recreating the database in Azure, provided some extra options are passed in to it (the Core Database script is missing this bit, unfortunately):&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Excerpt from &lt;a href=&quot;https://github.com/tarwn/SQLisHard/blob/master/Database/UpdateSampleDatabase.ps1&quot; title=&quot;View on github&quot;&gt;UpdateSampleDatabase.ps1&lt;/a&gt;:&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;powershell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;powershell&quot; id=&quot;cb61050&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;# ...&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; #database&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Checking database exists...&amp;quot;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $result = Invoke-Sqlcmd -Query &amp;quot;SELECT [name] FROM [sys].[databases] WHERE [name] = N&#039;$database&#039;&amp;quot; -ServerInstance &amp;quot;$Server&amp;quot; -Username &amp;quot;$AdminUserName&amp;quot; -Password &amp;quot;$AdminPassword&amp;quot; -Database &amp;quot;master&amp;quot; -ErrorAction Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if($result.name){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Database already exists&amp;quot;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Creating Database: $database&amp;quot;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Invoke-Sqlcmd -Query &amp;quot;CREATE DATABASE $database&amp;quot; -ServerInstance &amp;quot;$Server&amp;quot; -Username &amp;quot;$AdminUserName&amp;quot; -Password &amp;quot;$AdminPassword&amp;quot; -Database &amp;quot;master&amp;quot; -ErrorAction Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Invoke-Sqlcmd -Query &amp;quot;ALTER DATABASE $database SET RECOVERY SIMPLE&amp;quot; -ServerInstance &amp;quot;$Server&amp;quot; -Username &amp;quot;$AdminUserName&amp;quot; -Password &amp;quot;$AdminPassword&amp;quot; -Database &amp;quot;master&amp;quot; -ErrorAction Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Created.&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; # ...&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb85330&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;They also generate the users specified by the build server (which will also be dynamically added into the relevant web.config files for the website):&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Excerpt from &lt;a href=&quot;https://github.com/tarwn/SQLisHard/blob/master/Database/UpdateCoreDatabase.ps1&quot; title=&quot;View on github&quot;&gt;UpdateCoreDatabase.ps1&lt;/a&gt;:&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;powershell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;powershell&quot; id=&quot;cb32078&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;# ...&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; #user&lt;br /&gt;&amp;nbsp; &amp;nbsp; try{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Creating User: $NewUserName&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $result = Invoke-Sqlcmd -Query &amp;quot;SELECT [name] FROM sys.sql_logins WHERE name = &#039;$NewUserName&#039;&amp;quot; -ServerInstance &amp;quot;$Server&amp;quot; -Username &amp;quot;$AdminUserName&amp;quot; -Password &amp;quot;$AdminPassword&amp;quot; -Database &amp;quot;master&amp;quot; -ErrorAction Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if($result.name){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Login already exists&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Creating login...&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Invoke-Sqlcmd -Query &amp;quot;CREATE LOGIN $NewUserName WITH PASSWORD = &#039;$NewPassword&#039;&amp;quot; -ServerInstance &amp;quot;$Server&amp;quot; -Username &amp;quot;$AdminUserName&amp;quot; -Password &amp;quot;$AdminPassword&amp;quot; -Database &amp;quot;master&amp;quot; -ErrorAction Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Login Created.&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $result = Invoke-Sqlcmd -Query &amp;quot;SELECT [name] FROM sys.sysusers WHERE name = &#039;$NewUserName&#039;&amp;quot; -ServerInstance &amp;quot;$Server&amp;quot; -Username &amp;quot;$AdminUserName&amp;quot; -Password &amp;quot;$AdminPassword&amp;quot; -Database &amp;quot;$Database&amp;quot; -ErrorAction Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if($result.name){&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;User already exists&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Creating user...&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Invoke-Sqlcmd -Query &amp;quot;CREATE USER $NewUserName FOR LOGIN $NewUserName WITH DEFAULT_SCHEMA = dbo&amp;quot; -ServerInstance &amp;quot;$Server&amp;quot; -Username &amp;quot;$AdminUserName&amp;quot; -Password &amp;quot;$AdminPassword&amp;quot; -Database &amp;quot;$Database&amp;quot; -ErrorAction Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Invoke-Sqlcmd -Query &amp;quot;EXEC sp_addrolemember &#039;db_datareader&#039;,&#039;$NewUserName&#039;; EXEC sp_addrolemember &#039;db_datawriter&#039;,&#039;$NewUserName&#039;&amp;quot; -ServerInstance &amp;quot;$Server&amp;quot; -Username &amp;quot;$AdminUserName&amp;quot; -Password &amp;quot;$AdminPassword&amp;quot; -Database &amp;quot;$Database&amp;quot; -ErrorAction Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;User Created.&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; catch{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Error &amp;quot;Powershell Script error: $_&amp;quot; -EA Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; # ...&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb82431&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once the database and users are created, the SampleDatabase script produces a replacement for one of it&#039;s script files that will contain some randomized data. The original file is a placeholder and produces an error if it hasn&#039;t been replaced.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Excerpt from &lt;a href=&quot;https://github.com/tarwn/SQLisHard/blob/master/Database/UpdateSampleDatabase.ps1&quot; title=&quot;View on github&quot;&gt;UpdateSampleDatabase.ps1&lt;/a&gt;:&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;powershell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;powershell&quot; id=&quot;cb67763&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;# ...&lt;br /&gt;&amp;nbsp;&lt;br /&gt;# ---------------------------------- Content Generation ---------------------------------------------&lt;br /&gt;# Scripts to generate content dynamically and update the appropriate update script&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; #generate customers table content&lt;br /&gt;&amp;nbsp; &amp;nbsp; $CustomersContentPath = &amp;quot;$UpdatesFolder\0002_CustomersData.sql&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; try{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Host &amp;quot;Generating content script for dbo.Customers&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $girlsnames = (&amp;quot;&amp;lt;ns&amp;gt;&amp;lt;n&amp;gt;&amp;quot; + [string]::Join(&amp;quot;&amp;lt;/n&amp;gt;&amp;lt;n&amp;gt;&amp;quot;,(Get-Content &amp;quot;$path\Data\girlsforenames.txt&amp;quot;)) + &amp;quot;&amp;lt;/n&amp;gt;&amp;lt;/ns&amp;gt;&amp;quot;).Replace(&amp;quot;&#039;&amp;quot;,&amp;quot;&#039;&#039;&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $boysnames = &amp;nbsp;(&amp;quot;&amp;lt;ns&amp;gt;&amp;lt;n&amp;gt;&amp;quot; + [string]::Join(&amp;quot;&amp;lt;/n&amp;gt;&amp;lt;n&amp;gt;&amp;quot;,(Get-Content &amp;quot;$path\Data\boysforenames.txt&amp;quot;)) + &amp;quot;&amp;lt;/n&amp;gt;&amp;lt;/ns&amp;gt;&amp;quot;).Replace(&amp;quot;&#039;&amp;quot;,&amp;quot;&#039;&#039;&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $lastnames = &amp;nbsp;(&amp;quot;&amp;lt;ns&amp;gt;&amp;lt;n&amp;gt;&amp;quot; + [string]::Join(&amp;quot;&amp;lt;/n&amp;gt;&amp;lt;n&amp;gt;&amp;quot;,(Get-Content &amp;quot;$path\Data\surnames.txt&amp;quot;)) + &amp;quot;&amp;lt;/n&amp;gt;&amp;lt;/ns&amp;gt;&amp;quot;).Replace(&amp;quot;&#039;&amp;quot;,&amp;quot;&#039;&#039;&amp;quot;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (Get-Content &amp;quot;$path\Data\BulkImportNames.AzureFriendly.sql&amp;quot;) `&lt;br /&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; | % {$_ -replace &amp;quot;{{GIRLSNAMES}}&amp;quot;, $girlsnames} `&lt;br /&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; | % {$_ -replace &amp;quot;{{BOYSNAMES}}&amp;quot;, $boysnames} `&lt;br /&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; | % {$_ -replace &amp;quot;{{LASTNAMES}}&amp;quot;, $lastnames} `&lt;br /&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; | Set-Content -path &amp;quot;$CustomersContentPath&amp;quot;&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; catch{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Write-Error &amp;quot;Powershell Script error: $_&amp;quot; -EA Stop&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; # ...&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb72553&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This generated data is a necessary part of the application that I initially replaced on each deployment, but now only use on new database deployments. Keeping it random and replaceable prevents the application code from making any assumptions about the data in this table.&lt;/p&gt;

&lt;p&gt;The final step for both scripts is to run the ApplyDatabaseUpdates function on their respective folders and databases.&lt;/p&gt;

&lt;h3&gt;RunLocally.sample.ps1&lt;/h3&gt;
&lt;p&gt;The RunLocally.sample.ps1 script will bring a local development environment all the way up to the latest version without requiring me to type out a long series of arguments and credentials. It is basically just a list of hardcoded variables and then calls to the two Update______Database.ps1 scripts. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;RunLocally.sample.ps1:&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;powershell&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;powershell&quot; id=&quot;cb82246&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;# 1) Copy this file to RunLocally.ps1&lt;br /&gt;# 2) Open RunLocally.ps1 + substitute meaningful values for the variables below (update web.config connection strings also)&lt;br /&gt;# 3) [Cross your fingers and] Run it &lt;br /&gt;&amp;nbsp;&lt;br /&gt;$DbServer = &amp;quot;localhost&amp;quot;&lt;br /&gt;$DbAdminUsername = &amp;quot;admin&amp;quot;&lt;br /&gt;$DbAdminPassword = &amp;quot;password&amp;quot;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;$DbSampleDatabase = &amp;quot;SampleDB&amp;quot;&lt;br /&gt;$DbSampleReadUsername = &amp;quot;readuser&amp;quot;&lt;br /&gt;$DbSampleReadPassword = &amp;quot;password&amp;quot;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;$DbCoreDatabase = &amp;quot;CoreDB&amp;quot;&lt;br /&gt;$DbCoreUsername = &amp;quot;coreuser&amp;quot;&lt;br /&gt;$DbCorePassword = &amp;quot;password&amp;quot;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;.\UpdateSampleDatabase.ps1 -s $DbServer -d $DbSampleDatabase -nu $DbSampleReadUsername -np $DbSampleReadPassword -au $DbAdminUsername -ap $DbAdminPassword -DeleteGeneratedContentAfter $true&lt;br /&gt;&amp;nbsp;&lt;br /&gt;.\UpdateCoreDatabase.ps1 -s $DbServer -d $DbCoreDatabase -nu $DbCoreUsername -np $DbCorePassword -au $DbAdminUsername -ap $DbAdminPassword&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb55160&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The reason it is a sample file is because the real one is going to be different for my desktop and laptop and I knew if they were under source control I would constantly be accidentally committing them and having to change back and forth as I switched systems. To use it, I create a copy of the sample file, rename it to RunLocally.ps1 (which is ignored via the .gitignore for the project) and fill in the real values.&lt;/p&gt;

&lt;h2&gt;Future Plans&lt;/h2&gt;
&lt;p&gt;I&#039;m not a fan of the libraries that try to abstract away the SQL in SQL deployments. I know SQL and don&#039;t need to spend the time learning a library abstraction that, at best, can offer me no better control and ease of use then raw SQL. Many developers feel less than confident in their SQL skills and an abstracted library seems to reduce the need to learn SQL, but in reality the less you know about SQL the harder it will be to determine if the abstraction is doing what you think it is doing, and doing it in a safe and performant manner (and frequently the answer is &quot;it&#039;s not&quot;).&lt;/p&gt;

&lt;p&gt;This set of scripts evolved as a I built the project. In the future I&#039;ll probably switch over to something like DbDeploy, as I mentioned earlier. Having the scripts in file system folders makes it easy to glance over the list to see what has changed, makes them easily accessible in my git repositories, can be copied and pasted (or opened and saved) directly from SSMS, and requires no extra tooling.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;The scripts above are part of the deployment process for a personal project I have been working on called  &lt;a href=&quot;http://SQLisHard.com&quot;&gt;SQLisHard.com&lt;/a&gt;. Launching in the next few weeks (quietly), the site is designed to help people interactively learn or improve their SQL skills and help build the knowledge and confidence that comes from writing and solving real SQL problems.&lt;/i&gt;&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/deploying-database-changes-with-powershell&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Recently, while working on a personal project, I found myself needing a lightweight way to deploy database changes to multiple environments. In the past I have used a wide range of methods, ranging from applying the changes manually to applying changes via a diff tool (SQL Compare), to automatically applying manually created change scripts, to automatically applying diff scripts that were automatically generated, to working directly in production..er, pretend you didn't see that one. </p>

<h2>Why not _________ tool?</h2>

<p>There are a lot of tools out there to handle database deployments, but this is a small project that I am building incrementally as a minimum viable product. Rather than tie up a bunch of time researching and experimenting with database deployment tools early on, I decided to do something simple that would work for the time being and free me up to work on the actual product. </p>

<p>What I want from the deployment is to:</p>

<ol>
<li>Spend as little time on this as possible</li>
<li>Work against SQL Azure and a local 2008 R2 Server</li>
<li>Call it from TeamCity for a local server or a remote one</li>
<li>Produce readable output for TeamCity logs</li>
<li>Create the databases and users from the ground up</li>
<li>Include randomly generated data</li>
<li>Manage scripts for 2 independent databases in the same build</li>
<li>Allow real SQL (I'm not scared of SQL and I don't want to learn a code abstraction just to deploy changes)</li>
<li>Not worry about rollbacks. I am deploying small changes and if something breaks I'll be charging forward</li>
<li>Not expose credentials, as the code will be visible to the public</li>
<li>Be replaceable. I might replace it with a tool one day, so keep the deployment logic separate from the application</li>
</ol>

<p>Looking back at this list and what I eventually created, I probably could have used something like <a href="https://github.com/brunomlopes/dbdeploy.net" "dbdploy on github">DBDeploy</a>. The scripts I created ended up taking a very similar approach.</p>

<h2>The Deployment Scripts</h2>

<p>My deployment consists of 4 PowerShell scripts:</p>

<ul>
<li><strong>ApplyDatabaseUpdates.ps1</strong> - Responsible for generically applying changes from a folder to a specified database</li>
<li><strong>UpdateCoreDatabase.ps1</strong> - Responsible for the application's Core Database, calls ApplyDatabaseUpdates</li>
<li><strong>UpdateSampleDatabase.ps1</strong> - Response for the application's Sample Database, calls ApplyDatabaseUpdates</li>
<li><strong>RunLocally.ps1</strong> - Executes the two Update scripts against the local database on my development machine(s)</li>
</ul>

<p>This project has been spread out over 6 months, intermixed with life, other projects, blog posts, etc. Along the way I also upgraded my local development machine to SQL Server 2012 but my main test database server is on 2008 still and my release environment is Azure Database/SQL Azure/(whatever the name is this week).</p>

<h3>ApplyDatabaseUpdates.ps1</h3>
<p>The purpose of the ApplyDatabaseUpdates script is to apply all of the scripts in a specified folder to the specified server. To do this it creates a tracking table on the target database, then iterates through the contents of the folder, building a script containing any files that were not previously logged in the tracking table.</p>

<div style="text-align:center; color: #666666;">
   <img src="http://tiernok.com/LTDBlog/DatabaseDeployment/SQLScripts.png" alt="Core DB Scripts folder" /><br />
   Core Database Scripts Folder
</div>

<p>The deployment script wraps the contents of each script file in an EXECUTE statement, followed by an INSERT to add it to the tracking table for the database.</p>

<p><b><a href="https://github.com/tarwn/SQLisHard/blob/master/Database/ApplyDatabaseUpdates.ps1" title="View on github">ApplyDatabaseUpdates.ps1</a></b></p>
<div class="codebox"><div class="codeheader"><span>powershell</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb83093'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb83093','cb20041'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="powershell" id="cb83093" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">function ApplyDatabaseUpdates</li><li style="" class="li2">{</li><li style="" class="li1">&nbsp; &nbsp; param (</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; [parameter(Mandatory=$true)]</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; [string]</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $UpdatesFolder,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; [parameter(Mandatory=$true)]</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; [string]</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; $Server,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; [parameter(Mandatory=$true)]</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; [string]</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $Database,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; [parameter(Mandatory=$true)]</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; [string]</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; $AdminUserName,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; [parameter(Mandatory=$true)]</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; [string]</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $AdminPassword</li><li style="" class="li1">&nbsp; &nbsp; )</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; $path = (Get-Location).Path</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; # For SQL 2008 - load the modules</li><li style="" class="li2">&nbsp; &nbsp; try{ &nbsp; &nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; if ( (Get-PSSnapin -Name SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue) -eq $null -and (Get-PSSnapin -Registered -Name SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue) -ne $null){</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Add-PSSnapin SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Add-PSSnapin SqlServerProviderSnapin100 -ErrorAction SilentlyContinue</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; }</li><li style="" class="li1">&nbsp; &nbsp; }</li><li style="" class="li2">&nbsp; &nbsp; catch{</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Write-Error &quot;Powershell Script error: $_&quot; -EA Stop</li><li style="" class="li2">&nbsp; &nbsp; }</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; #updates tracking</li><li style="" class="li1">&nbsp; &nbsp; try{</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Creating Update Tracking Table If Not Exists&quot;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Invoke-Sqlcmd -Query &quot;IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'UpdateTracking') CREATE TABLE UpdateTracking (UpdateTrackingKey int IDENTITY(1,1) PRIMARY KEY, Name varchar(255) NOT NULL, Applied DateTime NOT NULL);&quot; -ServerInstance &quot;$Server&quot; -Username &quot;$AdminUserName&quot; -Password &quot;$AdminPassword&quot; -Database &quot;$Database&quot; -ErrorAction Stop</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Done&quot;</li><li style="" class="li1">&nbsp; &nbsp; }</li><li style="" class="li2">&nbsp; &nbsp; catch{</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Write-Error &quot;Powershell Script error: $_&quot; -EA Stop</li><li style="" class="li2">&nbsp; &nbsp; }</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; #database updates</li><li style="" class="li1">&nbsp; &nbsp; $outputPath = &quot;$path\UpdatesBatch.sql&quot;</li><li style="" class="li2">&nbsp; &nbsp; $stream = [System.IO.StreamWriter] &quot;$outputPath&quot;</li><li style="" class="li1">&nbsp; &nbsp; $fileUpdates = Get-ChildItem &quot;$UpdatesFolder&quot;</li><li style="" class="li2">&nbsp; &nbsp; $datestamp = $(get-date -f &quot;yyyy-MM-dd HH:mm&quot;)</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; $stream.WriteLine(&quot;/* SQL Core Updates - Updated $datestamp */&quot;)</li><li style="" class="li1">&nbsp; &nbsp; $stream.WriteLine(&quot;BEGIN TRANSACTION&quot;)</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; foreach($file in $fileUpdates)</li><li style="" class="li2">&nbsp; &nbsp; {</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; $name = ($file.Name)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $namewe = ([System.IO.Path]::GetFileNameWithoutExtension($name))</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $stream.WriteLine(&quot;&quot;)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; $stream.WriteLine(&quot;/* File: $name */&quot;)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $stream.WriteLine(&quot;IF NOT EXISTS (SELECT 1 FROM UpdateTracking WHERE Name = '$namewe')&quot;)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; $stream.WriteLine(&quot;BEGIN&quot;)</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; $stream.WriteLine(&quot;`tPrint 'Applying Update: $namewe'&quot;)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $stream.WriteLine(&quot;`tEXEC('&quot;)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; (Get-Content &quot;$UpdatesFolder\$name&quot;) | % {$_ -replace &quot;'&quot;, &quot;''&quot;} | % {$stream.WriteLine(&quot;`t`t$_&quot;)}</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $stream.WriteLine(&quot;`t');&quot;)</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $stream.WriteLine(&quot;`tINSERT INTO UpdateTracking(Name, Applied) SELECT '$namewe', GETUTCDATE();&quot;)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; $stream.WriteLine(&quot;END&quot;)</li><li style="" class="li2">&nbsp; &nbsp; }</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; $stream.WriteLine(&quot;COMMIT TRANSACTION&quot;)</li><li style="" class="li1">&nbsp; &nbsp; $stream.Close()</li><li style="" class="li2">&nbsp; &nbsp; Write-Host &quot;Update Script Created.&quot;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; Write-Host &quot;Running updates...&quot;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; Invoke-SqlCmd -InputFile &quot;$outputPath&quot; -ServerInstance &quot;$Server&quot; -Username &quot;$AdminUserName&quot; -Password &quot;$AdminPassword&quot; -Database &quot;$Database&quot; -Verbose -ErrorAction Stop</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; Remove-Item &quot;$outputPath&quot;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; Write-Host &quot;Updates completed.&quot;</li><li style="" class="li1">}</li></ol></div><div id="cb20041" style="display: none; color: red;"></div></div></div>

<p><i style="display: block; padding: 1em; margin: 1em; background-color: #eeeeee">Note: this has only been run in the context of my personal project. That means don't copy, paste, and run it immediately against your production environment. Running stuff blindly from the internet is known as both a bad idea and a career limiting maneuver.</i></p>

<h3>Update____Database.ps1</h3>

<p>My application has two databases which it will access via different accounts. I want the ability to rebuild these databases from scratch as well as manage their credentials from an external system (in this case, TeamCity). If the worst should happen and these databases are compromised or overwritten in some fashion, I want to be able to recreate them with new credentials, account names, the works. </p>

<p>To make life more difficult, many of these commands have to be executed individually in order to work with Azure Databases.</p>

<p>Both scripts detect if their specified database exists and, if not, create them. The UpdateSampleDatabase is capable of recreating the database in Azure, provided some extra options are passed in to it (the Core Database script is missing this bit, unfortunately):</p>

<p><b>Excerpt from <a href="https://github.com/tarwn/SQLisHard/blob/master/Database/UpdateSampleDatabase.ps1" title="View on github">UpdateSampleDatabase.ps1</a>:</b></p>
<div class="codebox"><div class="codeheader"><span>powershell</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb73140'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb73140','cb41728'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="powershell" id="cb73140" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"># ...</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; #database</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Checking database exists...&quot;;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; $result = Invoke-Sqlcmd -Query &quot;SELECT [name] FROM [sys].[databases] WHERE [name] = N'$database'&quot; -ServerInstance &quot;$Server&quot; -Username &quot;$AdminUserName&quot; -Password &quot;$AdminPassword&quot; -Database &quot;master&quot; -ErrorAction Stop</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; if($result.name){</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Database already exists&quot;;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; }</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; else{</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Creating Database: $database&quot;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Invoke-Sqlcmd -Query &quot;CREATE DATABASE $database&quot; -ServerInstance &quot;$Server&quot; -Username &quot;$AdminUserName&quot; -Password &quot;$AdminPassword&quot; -Database &quot;master&quot; -ErrorAction Stop</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Invoke-Sqlcmd -Query &quot;ALTER DATABASE $database SET RECOVERY SIMPLE&quot; -ServerInstance &quot;$Server&quot; -Username &quot;$AdminUserName&quot; -Password &quot;$AdminPassword&quot; -Database &quot;master&quot; -ErrorAction Stop</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Created.&quot;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; }</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; # ...</li></ol></div><div id="cb41728" style="display: none; color: red;"></div></div></div>

<p>They also generate the users specified by the build server (which will also be dynamically added into the relevant web.config files for the website):</p>

<p><b>Excerpt from <a href="https://github.com/tarwn/SQLisHard/blob/master/Database/UpdateCoreDatabase.ps1" title="View on github">UpdateCoreDatabase.ps1</a>:</b></p>
<div class="codebox"><div class="codeheader"><span>powershell</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb33937'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb33937','cb67502'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="powershell" id="cb33937" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"># ...</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; #user</li><li style="" class="li2">&nbsp; &nbsp; try{</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Creating User: $NewUserName&quot;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $result = Invoke-Sqlcmd -Query &quot;SELECT [name] FROM sys.sql_logins WHERE name = '$NewUserName'&quot; -ServerInstance &quot;$Server&quot; -Username &quot;$AdminUserName&quot; -Password &quot;$AdminPassword&quot; -Database &quot;master&quot; -ErrorAction Stop</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; if($result.name){</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Login already exists&quot;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; }</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; else{</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Creating login...&quot;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Invoke-Sqlcmd -Query &quot;CREATE LOGIN $NewUserName WITH PASSWORD = '$NewPassword'&quot; -ServerInstance &quot;$Server&quot; -Username &quot;$AdminUserName&quot; -Password &quot;$AdminPassword&quot; -Database &quot;master&quot; -ErrorAction Stop</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Login Created.&quot;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; }</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $result = Invoke-Sqlcmd -Query &quot;SELECT [name] FROM sys.sysusers WHERE name = '$NewUserName'&quot; -ServerInstance &quot;$Server&quot; -Username &quot;$AdminUserName&quot; -Password &quot;$AdminPassword&quot; -Database &quot;$Database&quot; -ErrorAction Stop</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; if($result.name){</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;User already exists&quot;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; }</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; else{</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Creating user...&quot;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Invoke-Sqlcmd -Query &quot;CREATE USER $NewUserName FOR LOGIN $NewUserName WITH DEFAULT_SCHEMA = dbo&quot; -ServerInstance &quot;$Server&quot; -Username &quot;$AdminUserName&quot; -Password &quot;$AdminPassword&quot; -Database &quot;$Database&quot; -ErrorAction Stop</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Invoke-Sqlcmd -Query &quot;EXEC sp_addrolemember 'db_datareader','$NewUserName'; EXEC sp_addrolemember 'db_datawriter','$NewUserName'&quot; -ServerInstance &quot;$Server&quot; -Username &quot;$AdminUserName&quot; -Password &quot;$AdminPassword&quot; -Database &quot;$Database&quot; -ErrorAction Stop</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;User Created.&quot;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; }</li><li style="" class="li2">&nbsp; &nbsp; }</li><li style="" class="li1">&nbsp; &nbsp; catch{</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; Write-Error &quot;Powershell Script error: $_&quot; -EA Stop</li><li style="" class="li1">&nbsp; &nbsp; }</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; # ...</li></ol></div><div id="cb67502" style="display: none; color: red;"></div></div></div>

<p>Once the database and users are created, the SampleDatabase script produces a replacement for one of it's script files that will contain some randomized data. The original file is a placeholder and produces an error if it hasn't been replaced.</p>

<p><b>Excerpt from <a href="https://github.com/tarwn/SQLisHard/blob/master/Database/UpdateSampleDatabase.ps1" title="View on github">UpdateSampleDatabase.ps1</a>:</b></p>
<div class="codebox"><div class="codeheader"><span>powershell</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb2286'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb2286','cb30596'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="powershell" id="cb2286" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"># ...</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"># ---------------------------------- Content Generation ---------------------------------------------</li><li style="" class="li2"># Scripts to generate content dynamically and update the appropriate update script</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; #generate customers table content</li><li style="" class="li1">&nbsp; &nbsp; $CustomersContentPath = &quot;$UpdatesFolder\0002_CustomersData.sql&quot;</li><li style="" class="li2">&nbsp; &nbsp; try{</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Write-Host &quot;Generating content script for dbo.Customers&quot;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $girlsnames = (&quot;&lt;ns&gt;&lt;n&gt;&quot; + [string]::Join(&quot;&lt;/n&gt;&lt;n&gt;&quot;,(Get-Content &quot;$path\Data\girlsforenames.txt&quot;)) + &quot;&lt;/n&gt;&lt;/ns&gt;&quot;).Replace(&quot;'&quot;,&quot;''&quot;)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; $boysnames = &nbsp;(&quot;&lt;ns&gt;&lt;n&gt;&quot; + [string]::Join(&quot;&lt;/n&gt;&lt;n&gt;&quot;,(Get-Content &quot;$path\Data\boysforenames.txt&quot;)) + &quot;&lt;/n&gt;&lt;/ns&gt;&quot;).Replace(&quot;'&quot;,&quot;''&quot;)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $lastnames = &nbsp;(&quot;&lt;ns&gt;&lt;n&gt;&quot; + [string]::Join(&quot;&lt;/n&gt;&lt;n&gt;&quot;,(Get-Content &quot;$path\Data\surnames.txt&quot;)) + &quot;&lt;/n&gt;&lt;/ns&gt;&quot;).Replace(&quot;'&quot;,&quot;''&quot;)</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; (Get-Content &quot;$path\Data\BulkImportNames.AzureFriendly.sql&quot;) `</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | % {$_ -replace &quot;{{GIRLSNAMES}}&quot;, $girlsnames} `</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | % {$_ -replace &quot;{{BOYSNAMES}}&quot;, $boysnames} `</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | % {$_ -replace &quot;{{LASTNAMES}}&quot;, $lastnames} `</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Set-Content -path &quot;$CustomersContentPath&quot;</li><li style="" class="li1">&nbsp; &nbsp; }</li><li style="" class="li2">&nbsp; &nbsp; catch{</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Write-Error &quot;Powershell Script error: $_&quot; -EA Stop</li><li style="" class="li2">&nbsp; &nbsp; }</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; # ...</li></ol></div><div id="cb30596" style="display: none; color: red;"></div></div></div>

<p>This generated data is a necessary part of the application that I initially replaced on each deployment, but now only use on new database deployments. Keeping it random and replaceable prevents the application code from making any assumptions about the data in this table.</p>

<p>The final step for both scripts is to run the ApplyDatabaseUpdates function on their respective folders and databases.</p>

<h3>RunLocally.sample.ps1</h3>
<p>The RunLocally.sample.ps1 script will bring a local development environment all the way up to the latest version without requiring me to type out a long series of arguments and credentials. It is basically just a list of hardcoded variables and then calls to the two Update______Database.ps1 scripts. </p>

<p><b>RunLocally.sample.ps1:</b></p>
<div class="codebox"><div class="codeheader"><span>powershell</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb49070'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb49070','cb27948'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="powershell" id="cb49070" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"># 1) Copy this file to RunLocally.ps1</li><li style="" class="li2"># 2) Open RunLocally.ps1 + substitute meaningful values for the variables below (update web.config connection strings also)</li><li style="" class="li1"># 3) [Cross your fingers and] Run it </li><li style="" class="li2">&nbsp;</li><li style="" class="li1">$DbServer = &quot;localhost&quot;</li><li style="" class="li2">$DbAdminUsername = &quot;admin&quot;</li><li style="" class="li1">$DbAdminPassword = &quot;password&quot;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">$DbSampleDatabase = &quot;SampleDB&quot;</li><li style="" class="li2">$DbSampleReadUsername = &quot;readuser&quot;</li><li style="" class="li1">$DbSampleReadPassword = &quot;password&quot;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">$DbCoreDatabase = &quot;CoreDB&quot;</li><li style="" class="li2">$DbCoreUsername = &quot;coreuser&quot;</li><li style="" class="li1">$DbCorePassword = &quot;password&quot;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">.\UpdateSampleDatabase.ps1 -s $DbServer -d $DbSampleDatabase -nu $DbSampleReadUsername -np $DbSampleReadPassword -au $DbAdminUsername -ap $DbAdminPassword -DeleteGeneratedContentAfter $true</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">.\UpdateCoreDatabase.ps1 -s $DbServer -d $DbCoreDatabase -nu $DbCoreUsername -np $DbCorePassword -au $DbAdminUsername -ap $DbAdminPassword</li></ol></div><div id="cb27948" style="display: none; color: red;"></div></div></div>

<p>The reason it is a sample file is because the real one is going to be different for my desktop and laptop and I knew if they were under source control I would constantly be accidentally committing them and having to change back and forth as I switched systems. To use it, I create a copy of the sample file, rename it to RunLocally.ps1 (which is ignored via the .gitignore for the project) and fill in the real values.</p>

<h2>Future Plans</h2>
<p>I'm not a fan of the libraries that try to abstract away the SQL in SQL deployments. I know SQL and don't need to spend the time learning a library abstraction that, at best, can offer me no better control and ease of use then raw SQL. Many developers feel less than confident in their SQL skills and an abstracted library seems to reduce the need to learn SQL, but in reality the less you know about SQL the harder it will be to determine if the abstraction is doing what you think it is doing, and doing it in a safe and performant manner (and frequently the answer is "it's not").</p>

<p>This set of scripts evolved as a I built the project. In the future I'll probably switch over to something like DbDeploy, as I mentioned earlier. Having the scripts in file system folders makes it easy to glance over the list to see what has changed, makes them easily accessible in my git repositories, can be copied and pasted (or opened and saved) directly from SSMS, and requires no extra tooling.</p>

<p><i>The scripts above are part of the deployment process for a personal project I have been working on called  <a href="http://SQLisHard.com">SQLisHard.com</a>. Launching in the next few weeks (quietly), the site is designed to help people interactively learn or improve their SQL skills and help build the knowledge and confidence that comes from writing and solving real SQL problems.</i></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/deploying-database-changes-with-powershell">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/deploying-database-changes-with-powershell#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=2190</wfw:commentRss>
		</item>
				<item>
			<title>T-SQL Tuesday #31 - Logging Simple Things</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/t-sql-tuesday-31-logging</link>
			<pubDate>Tue, 12 Jun 2012 12:14:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">Microsoft SQL Server Admin</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">1752@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;&lt;img src=&quot;http://sqlblog.com/files/folders/30073/download.aspx&quot; style=&quot;float: right; margin: 8px&quot; alt=&quot;T-SQL Tuesday&quot; /&gt; How big are your core databases right now? Do you know how they got that way? Is that normal? These questions are impossible to answer just by looking at the database options dialog in SSMS. They are also questions I&#039;ve had to try to answer in several different environments, because without logging we not only didn&#039;t know what normal growth looked like for our system, we didn&#039;t know what tables and indexes were driving that growth. &lt;/p&gt;

&lt;p&gt;The answer lies in a very simple query and a little logging.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Aaron Nelson (&lt;a href=&quot;https://twitter.com/#!/sqlvariant&quot; title=&quot;@SQLVariant on twitter&quot; alt=&quot;@sqlvariant on twitter&quot;&gt;twitter&lt;/a&gt;|&lt;a href=&quot;http://sqlvariant.com/&quot; title=&quot;Aaron&#039;s Blog&quot;&gt;blog&lt;/a&gt;) is hosting &lt;a href=&quot;http://sqlvariant.com/2012/06/t-sql-tuesday-31-logging/&quot; title=&quot;T-SQL Tuesday #31 - Logging&quot;&gt;T-SQL Tuesday&lt;/a&gt; this month with the topic of &quot;Logging&quot;. Check out &lt;a href=&quot;http://sqlvariant.com/2012/06/t-sql-tuesday-31-logging/&quot; title=&quot;T-SQL Tuesday #31 - Logging&quot;&gt;his post&lt;/a&gt; for a list of other T-SQL Tuesday posts this month and congratulate him on speaking at TechEd on Tuesday morning (he&#039;s denied being the loud powershell fan in the keynote, btw).&lt;/i&gt;&lt;/p&gt;

&lt;h2&gt;The Setup&lt;/h2&gt;
&lt;p&gt;Monitoring the sizes at the index and table level provides raw material for looking several different statistics. Luckily this is fairly easy to do for on premises and SQL Azure databases. &lt;/p&gt;

&lt;p&gt;&lt;i&gt;For the purpose of these examples I&#039;m going to be logging the database growth in the same database. Obviously this won&#039;t work for every environment and adds an extra variable to our growth statistics.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;First up, let&#039;s create a table to store the raw data in:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb95441&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;DatabaseSizeLog&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Id&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL &lt;span style=&quot;color: #0000FF;&quot;&gt;PRIMARY&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;KEY&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; LogTime&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DateTime2&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; DatabaseId&amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; ObjectId&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; TableName&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; IndexId&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; IndexName&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; AllocatedBytes&amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; UsedBytes&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; UnusedBytes&amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;RowCount&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb23255&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that I am capturing both ids and names for the table and index. We could JOIN to get the names, but being able to run a quick one line statement to look at key information is worth the slightly higher storage cost. I&#039;ve also used an integer IDENTITY column as the required clustered index for a SQL Azure database. A better option would be LogTime, DatabaseId, and IndexId in the order that makes the most sense for how you intend to look at the data.&lt;/p&gt;

&lt;p&gt;Once we have the raw table, we need the query to capture the data:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb72710&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;DatabaseSizeLog&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;LogTime, DatabaseId, ObjectId, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TableName, IndexId, IndexName, AllocatedBytes, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; UsedBytes, UnusedBytes, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;RowCount&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;GetDate&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; , &lt;span style=&quot;color: #FF00FF;&quot;&gt;DB_ID&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; , o.&lt;span style=&quot;color: #FF00FF;&quot;&gt;object_id&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; TableId&lt;br /&gt;&amp;nbsp; &amp;nbsp; , o.&lt;span style=&quot;color: #202020;&quot;&gt;name&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; , ps.&lt;span style=&quot;color: #202020;&quot;&gt;index_id&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;IndexId&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; , &lt;span style=&quot;color: #0000FF;&quot;&gt;CASE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHEN&lt;/span&gt; ps.&lt;span style=&quot;color: #202020;&quot;&gt;index_id&lt;/span&gt; &amp;gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;THEN&lt;/span&gt; i.&lt;span style=&quot;color: #202020;&quot;&gt;name&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ELSE&lt;/span&gt; NULL &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; IndexName&lt;br /&gt;&amp;nbsp; &amp;nbsp; , &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ps.&lt;span style=&quot;color: #202020;&quot;&gt;reserved_page_count&lt;/span&gt; * &lt;span style=&quot;color: #000;&quot;&gt;8192&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;18&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; Allocated&lt;br /&gt;&amp;nbsp; &amp;nbsp; , &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ps.&lt;span style=&quot;color: #202020;&quot;&gt;used_page_count&lt;/span&gt; * &lt;span style=&quot;color: #000;&quot;&gt;8192&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;18&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Used&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; , &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ps.&lt;span style=&quot;color: #202020;&quot;&gt;reserved_page_count&lt;/span&gt; - used_page_count&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; * &lt;span style=&quot;color: #000;&quot;&gt;8192&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;18&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; Unused&lt;br /&gt;&amp;nbsp; &amp;nbsp; , ps.&lt;span style=&quot;color: #202020;&quot;&gt;row_count&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;quot;RowCount&amp;quot;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_db_partition_stats&lt;/span&gt; ps&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INNER&lt;/span&gt; JOIN &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;objects&lt;/span&gt; o &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; ps.&lt;span style=&quot;color: #FF00FF;&quot;&gt;object_id&lt;/span&gt; = o.&lt;span style=&quot;color: #FF00FF;&quot;&gt;object_id&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INNER&lt;/span&gt; JOIN &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;indexes&lt;/span&gt; i &lt;span style=&quot;color: #0000FF;&quot;&gt;on&lt;/span&gt; i.&lt;span style=&quot;color: #202020;&quot;&gt;index_id&lt;/span&gt; = ps.&lt;span style=&quot;color: #202020;&quot;&gt;index_id&lt;/span&gt; AND i.&lt;span style=&quot;color: #FF00FF;&quot;&gt;object_id&lt;/span&gt; = ps.&lt;span style=&quot;color: #FF00FF;&quot;&gt;object_id&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; o.&lt;span style=&quot;color: #0000FF;&quot;&gt;type&lt;/span&gt; NOT IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;S&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;IT&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb62239&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This should be placed in a scheduled job in SQL Agent for an on-premises database, or tied into a scheduled task system (perhaps via a worker role) for a SQL Azure database.&lt;/p&gt;

&lt;p&gt;Now that we have some data and we&#039;re logging it at some regular interval, lets start getting some use out of it.&lt;/p&gt;

&lt;h2&gt;Basic Information&lt;/h2&gt;
&lt;p&gt;Once we have some data in the table, we can build some information gathering queries to learn more about our data growth rates. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;What are our largest tables right now?&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb55389&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @TargetTime &lt;span style=&quot;color: #0000FF;&quot;&gt;DateTime&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; @TargetTime = LogTime &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;DatabaseSizeLog&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; LogTime &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; TableName, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;quot;Allocated (MB)&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;AllocatedBytes&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;18&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1048576&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;quot;Used in MB&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;UsedBytes&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;18&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1048576&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;quot;Unused in MB&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;UnusedBytes&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;18&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1048576&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;quot;Row Count&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;MIN&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;RowCount&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;DatabaseSizeLog&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; LogTime = @TargetTime&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GROUP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; TableName&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;AllocatedBytes&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb34849&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;What percentage of our largest tables is indexes?&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb64536&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @TargetTime &lt;span style=&quot;color: #0000FF;&quot;&gt;DateTime&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; @TargetTime = LogTime &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;DatabaseSizeLog&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; LogTime &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; TableName, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;quot;Total Allocated (MB)&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;AllocatedBytes&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;18&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1048576&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;quot;Percent For Indexes&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CASE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHEN&lt;/span&gt; IndexName &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NOT NULL &lt;span style=&quot;color: #0000FF;&quot;&gt;THEN&lt;/span&gt; AllocatedBytes &lt;span style=&quot;color: #0000FF;&quot;&gt;ELSE&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;18&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;AllocatedBytes&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;18&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;quot;Row Count&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;MIN&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;RowCount&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;DatabaseSizeLog&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; LogTime = @TargetTime&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GROUP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; TableName&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;AllocatedBytes&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb35229&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Which tables are growing the quickest?&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb56348&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; LogHistory &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; Id, LogTime, DatabaseId, ObjectId, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TableName, IndexId, IndexName, AllocatedBytes, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; UsedBytes, UnusedBytes, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;RowCount&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF00FF;&quot;&gt;ROW_NUMBER&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OVER&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;PARTITION&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; ObjectId, IndexId &lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; LogTime &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; PastTimeNumber&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;DatabaseSizeLog&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; LH_NOW.&lt;span style=&quot;color: #202020;&quot;&gt;TableName&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PercentGrowth = &lt;span style=&quot;color: #0000FF;&quot;&gt;CASE&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHEN&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;LH_THEN.&lt;span style=&quot;color: #202020;&quot;&gt;AllocatedBytes&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt; AND &lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;LH_NOW.&lt;span style=&quot;color: #202020;&quot;&gt;AllocatedBytes&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHEN&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;LH_THEN.&lt;span style=&quot;color: #202020;&quot;&gt;AllocatedBytes&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ELSE&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; - &lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;LH_NOW.&lt;span style=&quot;color: #202020;&quot;&gt;AllocatedBytes&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;LH_THEN.&lt;span style=&quot;color: #202020;&quot;&gt;AllocatedBytes&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;18&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BytesPerHour = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;LH_NOW.&lt;span style=&quot;color: #202020;&quot;&gt;AllocatedBytes&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; - &lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;LH_THEN.&lt;span style=&quot;color: #202020;&quot;&gt;AllocatedBytes&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;minute&lt;/span&gt;, LH_THEN.&lt;span style=&quot;color: #202020;&quot;&gt;LogTime&lt;/span&gt;, LH_NOW.&lt;span style=&quot;color: #202020;&quot;&gt;LogTime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;60.0&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; LogHistory LH_NOW&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INNER&lt;/span&gt; JOIN LogHistory LH_THEN &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; LH_NOW.&lt;span style=&quot;color: #202020;&quot;&gt;ObjectId&lt;/span&gt; = LH_THEN.&lt;span style=&quot;color: #202020;&quot;&gt;ObjectId&lt;/span&gt; AND LH_NOW.&lt;span style=&quot;color: #202020;&quot;&gt;IndexId&lt;/span&gt; = LH_THEN.&lt;span style=&quot;color: #202020;&quot;&gt;IndexId&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; LH_NOW.&lt;span style=&quot;color: #202020;&quot;&gt;PastTimeNumber&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; AND LH_THEN.&lt;span style=&quot;color: #202020;&quot;&gt;PastTimeNumber&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;3&lt;/span&gt;&amp;nbsp; &lt;span style=&quot;color: #00AF00;&quot;&gt;-- tweak this to widen or narrow the comparison time&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GROUP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; LH_Now.&lt;span style=&quot;color: #202020;&quot;&gt;TableName&lt;/span&gt;, LH_NOW.&lt;span style=&quot;color: #202020;&quot;&gt;LogTime&lt;/span&gt;, LH_THEN.&lt;span style=&quot;color: #202020;&quot;&gt;LogTime&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;LH_NOW.&lt;span style=&quot;color: #202020;&quot;&gt;AllocatedBytes&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; - &lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;LH_THEN.&lt;span style=&quot;color: #202020;&quot;&gt;AllocatedBytes&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb14050&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;Monitoring&lt;/h2&gt;
&lt;p&gt;Besides being able to look at some general statistics, if we have a system that can monitor the result of a SQL query, we have a number of options we can start monitoring against:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Database size&lt;/b&gt;&lt;br /&gt;
Most recent total allocated size of the database (in M&lt;img src=&quot;http://blogs.lessthandot.com/rsc/smilies/icon_cool.gif&quot; title=&quot;B)&quot; alt=&quot;B)&quot; class=&quot;middle&quot; width=&quot;15&quot; height=&quot;15&quot; /&gt;.&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb10653&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;AllocatedBytes&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1048576.0&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;DatabaseSizeLog&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GROUP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; LogTime&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; LogTime &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb11120&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Hourly database growth for the last two entries&lt;/b&gt;&lt;br /&gt;
Hourly growth in MB/hour for the last two entries&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb6506&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; LastTwoEntries &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SizeInMB = &lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;AllocatedBytes&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1048576.0&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; LogTime&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;DatabaseSizeLog&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;GROUP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; LogTime&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; LogTime &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;LTE1.&lt;span style=&quot;color: #202020;&quot;&gt;SizeInMB&lt;/span&gt; - LTE2.&lt;span style=&quot;color: #202020;&quot;&gt;SizeInMB&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEDIFF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;minute&lt;/span&gt;, LTE1.&lt;span style=&quot;color: #202020;&quot;&gt;LogTime&lt;/span&gt;, LTE2.&lt;span style=&quot;color: #202020;&quot;&gt;LogTime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;60.0&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; LastTwoEntries LTE1&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INNER&lt;/span&gt; JOIN LastTwoEntries LTE2 &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; LTE1.&lt;span style=&quot;color: #202020;&quot;&gt;LogTime&lt;/span&gt; &amp;gt; LTE2.&lt;span style=&quot;color: #202020;&quot;&gt;LogTime&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb19145&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Wait, you deleted how many records?!?&lt;/b&gt;&lt;br /&gt;
This lists all tables that have shrunk by more than 10% since the previous log entry.&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb66940&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; Entries &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &amp;nbsp;ObjectId,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TableName,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SizeInMB = AllocatedBytes / &lt;span style=&quot;color: #000;&quot;&gt;1048576.0&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; LogTime,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF00FF;&quot;&gt;ROW_NUMBER&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OVER&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;PARTITION&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; ObjectId &lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; LogTime &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; Num&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;DatabaseSizeLog&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; IndexName &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; E1.&lt;span style=&quot;color: #202020;&quot;&gt;TableName&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Entries E1&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INNER&lt;/span&gt; JOIN Entries E2 &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; E1.&lt;span style=&quot;color: #202020;&quot;&gt;ObjectId&lt;/span&gt; = E2.&lt;span style=&quot;color: #202020;&quot;&gt;ObjectId&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; E1.&lt;span style=&quot;color: #202020;&quot;&gt;Num&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; AND E2.&lt;span style=&quot;color: #202020;&quot;&gt;num&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; AND E2.&lt;span style=&quot;color: #202020;&quot;&gt;SizeInMb&lt;/span&gt; &amp;lt;&amp;gt; &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; AND E1.&lt;span style=&quot;color: #202020;&quot;&gt;SizeInMb&lt;/span&gt; &amp;lt; E2.&lt;span style=&quot;color: #202020;&quot;&gt;SizeInMb&lt;/span&gt; - &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;.&lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt; * E2.&lt;span style=&quot;color: #202020;&quot;&gt;SizeInMb&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb97641&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;And more&lt;/h2&gt;
&lt;p&gt;By now I hope you have even more ideas for ways to slice this simple data set. Logging is a powerful tool in our inventory and often even a simple query can provide a great deal of information if we log it over time. The basic query that started this post probably didn&#039;t look like much, but capturing the values over time allowed us to expose a lot of new information about our database. Having visibility into how our systems are running can be the difference between finding out our database hit a size limit after the fact and knowing that it is growing at an abnormal pace long before the problem occurs.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/t-sql-tuesday-31-logging&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p><img src="http://sqlblog.com/files/folders/30073/download.aspx" style="float: right; margin: 8px" alt="T-SQL Tuesday" /> How big are your core databases right now? Do you know how they got that way? Is that normal? These questions are impossible to answer just by looking at the database options dialog in SSMS. They are also questions I've had to try to answer in several different environments, because without logging we not only didn't know what normal growth looked like for our system, we didn't know what tables and indexes were driving that growth. </p>

<p>The answer lies in a very simple query and a little logging.</p>

<p><i>Aaron Nelson (<a href="https://twitter.com/#!/sqlvariant" title="@SQLVariant on twitter" alt="@sqlvariant on twitter">twitter</a>|<a href="http://sqlvariant.com/" title="Aaron's Blog">blog</a>) is hosting <a href="http://sqlvariant.com/2012/06/t-sql-tuesday-31-logging/" title="T-SQL Tuesday #31 - Logging">T-SQL Tuesday</a> this month with the topic of "Logging". Check out <a href="http://sqlvariant.com/2012/06/t-sql-tuesday-31-logging/" title="T-SQL Tuesday #31 - Logging">his post</a> for a list of other T-SQL Tuesday posts this month and congratulate him on speaking at TechEd on Tuesday morning (he's denied being the loud powershell fan in the keynote, btw).</i></p>

<h2>The Setup</h2>
<p>Monitoring the sizes at the index and table level provides raw material for looking several different statistics. Luckily this is fairly easy to do for on premises and SQL Azure databases. </p>

<p><i>For the purpose of these examples I'm going to be logging the database growth in the same database. Obviously this won't work for every environment and adds an extra variable to our growth statistics.</i></p>

<p>First up, let's create a table to store the raw data in:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb93258'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb93258','cb87798'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb93258" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">DatabaseSizeLog</span><span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; Id&nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span>,</li><li style="" class="li1">&nbsp; &nbsp; LogTime&nbsp;&nbsp; &nbsp; <span style="color: #0000FF;">DateTime2</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; DatabaseId&nbsp; <span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; ObjectId&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; TableName&nbsp; &nbsp;<span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">200</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; IndexId&nbsp;&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NULL,</li><li style="" class="li2">&nbsp; &nbsp; IndexName&nbsp; &nbsp;<span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">200</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li1">&nbsp; &nbsp; AllocatedBytes&nbsp; <span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; UsedBytes&nbsp; &nbsp;<span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; UnusedBytes&nbsp;<span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">RowCount</span><span style="color: #808080;">&#93;</span>&nbsp; <span style="color: #0000FF;">int</span> NOT NULL</li><li style="" class="li1"><span style="color: #808080;">&#41;</span>;</li></ol></div><div id="cb87798" style="display: none; color: red;"></div></div></div>

<p>Note that I am capturing both ids and names for the table and index. We could JOIN to get the names, but being able to run a quick one line statement to look at key information is worth the slightly higher storage cost. I've also used an integer IDENTITY column as the required clustered index for a SQL Azure database. A better option would be LogTime, DatabaseId, and IndexId in the order that makes the most sense for how you intend to look at the data.</p>

<p>Once we have the raw table, we need the query to capture the data:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb64285'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb64285','cb24296'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb64285" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">DatabaseSizeLog</span><span style="color: #808080;">&#40;</span>LogTime, DatabaseId, ObjectId, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TableName, IndexId, IndexName, AllocatedBytes, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UsedBytes, UnusedBytes, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">RowCount</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> &nbsp; &nbsp;<span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; , <span style="color: #FF00FF;">DB_ID</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; , o.<span style="color: #FF00FF;">object_id</span> <span style="color: #0000FF;">AS</span> TableId</li><li style="" class="li1">&nbsp; &nbsp; , o.<span style="color: #202020;">name</span></li><li style="" class="li2">&nbsp; &nbsp; , ps.<span style="color: #202020;">index_id</span> <span style="color: #0000FF;">as</span> <span style="color: #808080;">&#91;</span>IndexId<span style="color: #808080;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; , <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> ps.<span style="color: #202020;">index_id</span> &gt; <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> i.<span style="color: #202020;">name</span> <span style="color: #0000FF;">ELSE</span> NULL <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> IndexName</li><li style="" class="li2">&nbsp; &nbsp; , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>ps.<span style="color: #202020;">reserved_page_count</span> * <span style="color: #000;">8192</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">DECIMAL</span><span style="color: #808080;">&#40;</span><span style="color: #000;">18</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> Allocated</li><li style="" class="li1">&nbsp; &nbsp; , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>ps.<span style="color: #202020;">used_page_count</span> * <span style="color: #000;">8192</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">DECIMAL</span><span style="color: #808080;">&#40;</span><span style="color: #000;">18</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">Used</span></li><li style="" class="li2">&nbsp; &nbsp; , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>ps.<span style="color: #202020;">reserved_page_count</span> - used_page_count<span style="color: #808080;">&#41;</span> * <span style="color: #000;">8192</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">DECIMAL</span><span style="color: #808080;">&#40;</span><span style="color: #000;">18</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> Unused</li><li style="" class="li1">&nbsp; &nbsp; , ps.<span style="color: #202020;">row_count</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">&quot;RowCount&quot;</span></li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_db_partition_stats</span> ps</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> JOIN <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">objects</span> o <span style="color: #0000FF;">ON</span> ps.<span style="color: #FF00FF;">object_id</span> = o.<span style="color: #FF00FF;">object_id</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> JOIN <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">indexes</span> i <span style="color: #0000FF;">on</span> i.<span style="color: #202020;">index_id</span> = ps.<span style="color: #202020;">index_id</span> AND i.<span style="color: #FF00FF;">object_id</span> = ps.<span style="color: #FF00FF;">object_id</span></li><li style="" class="li1"><span style="color: #0000FF;">WHERE</span> o.<span style="color: #0000FF;">type</span> NOT IN <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'S'</span>,<span style="color: #FF0000;">'IT'</span><span style="color: #808080;">&#41;</span>;</li></ol></div><div id="cb24296" style="display: none; color: red;"></div></div></div>

<p>This should be placed in a scheduled job in SQL Agent for an on-premises database, or tied into a scheduled task system (perhaps via a worker role) for a SQL Azure database.</p>

<p>Now that we have some data and we're logging it at some regular interval, lets start getting some use out of it.</p>

<h2>Basic Information</h2>
<p>Once we have some data in the table, we can build some information gathering queries to learn more about our data growth rates. </p>

<p><b>What are our largest tables right now?</b></p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb48072'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb48072','cb72144'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb48072" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @TargetTime <span style="color: #0000FF;">DateTime</span>;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> @TargetTime = LogTime <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">DatabaseSizeLog</span> <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> LogTime <span style="color: #0000FF;">DESC</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">10</span></li><li style="" class="li1">&nbsp; &nbsp; TableName, </li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #FF0000;">&quot;Allocated (MB)&quot;</span> = <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>AllocatedBytes<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">DECIMAL</span><span style="color: #808080;">&#40;</span><span style="color: #000;">18</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> / <span style="color: #000;">1048576</span>, </li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #FF0000;">&quot;Used in MB&quot;</span> = <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>UsedBytes<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">DECIMAL</span><span style="color: #808080;">&#40;</span><span style="color: #000;">18</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> / <span style="color: #000;">1048576</span>, </li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #FF0000;">&quot;Unused in MB&quot;</span> = <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>UnusedBytes<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">DECIMAL</span><span style="color: #808080;">&#40;</span><span style="color: #000;">18</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> / <span style="color: #000;">1048576</span>, </li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #FF0000;">&quot;Row Count&quot;</span> = <span style="color: #FF00FF;">MIN</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">RowCount</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">DatabaseSizeLog</span></li><li style="" class="li1"><span style="color: #0000FF;">WHERE</span> LogTime = @TargetTime</li><li style="" class="li2"><span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> TableName</li><li style="" class="li1"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>AllocatedBytes<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">DESC</span>;</li></ol></div><div id="cb72144" style="display: none; color: red;"></div></div></div>

<p><b>What percentage of our largest tables is indexes?</b></p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb51237'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb51237','cb16835'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb51237" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @TargetTime <span style="color: #0000FF;">DateTime</span>;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> @TargetTime = LogTime <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">DatabaseSizeLog</span> <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> LogTime <span style="color: #0000FF;">DESC</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">10</span></li><li style="" class="li1">&nbsp; &nbsp; TableName, </li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #FF0000;">&quot;Total Allocated (MB)&quot;</span> = <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>AllocatedBytes<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">DECIMAL</span><span style="color: #808080;">&#40;</span><span style="color: #000;">18</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> / <span style="color: #000;">1048576</span>, </li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #FF0000;">&quot;Percent For Indexes&quot;</span> = <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> IndexName <span style="color: #0000FF;">IS</span> NOT NULL <span style="color: #0000FF;">THEN</span> AllocatedBytes <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">DECIMAL</span><span style="color: #808080;">&#40;</span><span style="color: #000;">18</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> / <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>AllocatedBytes<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">DECIMAL</span><span style="color: #808080;">&#40;</span><span style="color: #000;">18</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #FF0000;">&quot;Row Count&quot;</span> = <span style="color: #FF00FF;">MIN</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">RowCount</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">DatabaseSizeLog</span></li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> LogTime = @TargetTime</li><li style="" class="li1"><span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> TableName</li><li style="" class="li2"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>AllocatedBytes<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">DESC</span>;</li></ol></div><div id="cb16835" style="display: none; color: red;"></div></div></div>

<p><b>Which tables are growing the quickest?</b></p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb330'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb330','cb60373'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb330" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">WITH</span> LogHistory <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> Id, LogTime, DatabaseId, ObjectId, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TableName, IndexId, IndexName, AllocatedBytes, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UsedBytes, UnusedBytes, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">RowCount</span><span style="color: #808080;">&#93;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF00FF;">ROW_NUMBER</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">PARTITION</span> <span style="color: #0000FF;">BY</span> ObjectId, IndexId <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> LogTime <span style="color: #0000FF;">DESC</span><span style="color: #808080;">&#41;</span>&nbsp; &nbsp; <span style="color: #0000FF;">AS</span> PastTimeNumber</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">DatabaseSizeLog</span></li><li style="" class="li1"><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; LH_NOW.<span style="color: #202020;">TableName</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; PercentGrowth = <span style="color: #0000FF;">CASE</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>LH_THEN.<span style="color: #202020;">AllocatedBytes</span><span style="color: #808080;">&#41;</span> = <span style="color: #000;">0</span> AND <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>LH_NOW.<span style="color: #202020;">AllocatedBytes</span><span style="color: #808080;">&#41;</span> = <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>LH_THEN.<span style="color: #202020;">AllocatedBytes</span><span style="color: #808080;">&#41;</span> = <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">1</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">1</span> - <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>LH_NOW.<span style="color: #202020;">AllocatedBytes</span><span style="color: #808080;">&#41;</span> / <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>LH_THEN.<span style="color: #202020;">AllocatedBytes</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">DECIMAL</span><span style="color: #808080;">&#40;</span><span style="color: #000;">18</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; BytesPerHour = <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>LH_NOW.<span style="color: #202020;">AllocatedBytes</span><span style="color: #808080;">&#41;</span> - <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>LH_THEN.<span style="color: #202020;">AllocatedBytes</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> / <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">minute</span>, LH_THEN.<span style="color: #202020;">LogTime</span>, LH_NOW.<span style="color: #202020;">LogTime</span><span style="color: #808080;">&#41;</span> / <span style="color: #000;">60.0</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> LogHistory LH_NOW</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> JOIN LogHistory LH_THEN <span style="color: #0000FF;">ON</span> LH_NOW.<span style="color: #202020;">ObjectId</span> = LH_THEN.<span style="color: #202020;">ObjectId</span> AND LH_NOW.<span style="color: #202020;">IndexId</span> = LH_THEN.<span style="color: #202020;">IndexId</span></li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> LH_NOW.<span style="color: #202020;">PastTimeNumber</span> = <span style="color: #000;">1</span></li><li style="" class="li1">&nbsp; &nbsp; AND LH_THEN.<span style="color: #202020;">PastTimeNumber</span> = <span style="color: #000;">3</span>&nbsp; <span style="color: #00AF00;">-- tweak this to widen or narrow the comparison time</span></li><li style="" class="li2"><span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> LH_Now.<span style="color: #202020;">TableName</span>, LH_NOW.<span style="color: #202020;">LogTime</span>, LH_THEN.<span style="color: #202020;">LogTime</span></li><li style="" class="li1"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>LH_NOW.<span style="color: #202020;">AllocatedBytes</span><span style="color: #808080;">&#41;</span> - <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>LH_THEN.<span style="color: #202020;">AllocatedBytes</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">DESC</span></li></ol></div><div id="cb60373" style="display: none; color: red;"></div></div></div>

<h2>Monitoring</h2>
<p>Besides being able to look at some general statistics, if we have a system that can monitor the result of a SQL query, we have a number of options we can start monitoring against:</p>

<p><b>Database size</b><br />
Most recent total allocated size of the database (in M<img src="http://blogs.lessthandot.com/rsc/smilies/icon_cool.gif" title="B)" alt="B)" class="middle" width="15" height="15" />.</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb53963'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb53963','cb98102'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb53963" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>AllocatedBytes<span style="color: #808080;">&#41;</span> / <span style="color: #000;">1048576.0</span></li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">DatabaseSizeLog</span></li><li style="" class="li1"><span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> LogTime</li><li style="" class="li2"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> LogTime <span style="color: #0000FF;">DESC</span>;</li></ol></div><div id="cb98102" style="display: none; color: red;"></div></div></div>

<p><b>Hourly database growth for the last two entries</b><br />
Hourly growth in MB/hour for the last two entries</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb92124'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb92124','cb40876'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb92124" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">WITH</span> LastTwoEntries <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">2</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SizeInMB = <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>AllocatedBytes<span style="color: #808080;">&#41;</span> / <span style="color: #000;">1048576.0</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LogTime</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">DatabaseSizeLog</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> LogTime</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> LogTime <span style="color: #0000FF;">DESC</span></li><li style="" class="li2"><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#40;</span>LTE1.<span style="color: #202020;">SizeInMB</span> - LTE2.<span style="color: #202020;">SizeInMB</span><span style="color: #808080;">&#41;</span> / <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEDIFF</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">minute</span>, LTE1.<span style="color: #202020;">LogTime</span>, LTE2.<span style="color: #202020;">LogTime</span><span style="color: #808080;">&#41;</span> / <span style="color: #000;">60.0</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> LastTwoEntries LTE1</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> JOIN LastTwoEntries LTE2 <span style="color: #0000FF;">ON</span> LTE1.<span style="color: #202020;">LogTime</span> &gt; LTE2.<span style="color: #202020;">LogTime</span></li></ol></div><div id="cb40876" style="display: none; color: red;"></div></div></div>

<p><b>Wait, you deleted how many records?!?</b><br />
This lists all tables that have shrunk by more than 10% since the previous log entry.</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb6056'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb6056','cb5099'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb6056" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">WITH</span> Entries <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> &nbsp;ObjectId,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TableName,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SizeInMB = AllocatedBytes / <span style="color: #000;">1048576.0</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LogTime,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF00FF;">ROW_NUMBER</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">PARTITION</span> <span style="color: #0000FF;">BY</span> ObjectId <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> LogTime <span style="color: #0000FF;">DESC</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> Num</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">DatabaseSizeLog</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> IndexName <span style="color: #0000FF;">IS</span> NULL</li><li style="" class="li1"><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> E1.<span style="color: #202020;">TableName</span></li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Entries E1</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> JOIN Entries E2 <span style="color: #0000FF;">ON</span> E1.<span style="color: #202020;">ObjectId</span> = E2.<span style="color: #202020;">ObjectId</span></li><li style="" class="li1"><span style="color: #0000FF;">WHERE</span> E1.<span style="color: #202020;">Num</span> = <span style="color: #000;">1</span> </li><li style="" class="li2">&nbsp; &nbsp; AND E2.<span style="color: #202020;">num</span> = <span style="color: #000;">2</span></li><li style="" class="li1">&nbsp; &nbsp; AND E2.<span style="color: #202020;">SizeInMb</span> &lt;&gt; <span style="color: #000;">0</span></li><li style="" class="li2">&nbsp; &nbsp; AND E1.<span style="color: #202020;">SizeInMb</span> &lt; E2.<span style="color: #202020;">SizeInMb</span> - <span style="color: #808080;">&#40;</span>.<span style="color: #000;">10</span> * E2.<span style="color: #202020;">SizeInMb</span><span style="color: #808080;">&#41;</span>;</li></ol></div><div id="cb5099" style="display: none; color: red;"></div></div></div>

<h2>And more</h2>
<p>By now I hope you have even more ideas for ways to slice this simple data set. Logging is a powerful tool in our inventory and often even a simple query can provide a great deal of information if we log it over time. The basic query that started this post probably didn't look like much, but capturing the values over time allowed us to expose a lot of new information about our database. Having visibility into how our systems are running can be the difference between finding out our database hit a size limit after the fact and knowing that it is growing at an abnormal pace long before the problem occurs.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/t-sql-tuesday-31-logging">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/t-sql-tuesday-31-logging#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1752</wfw:commentRss>
		</item>
				<item>
			<title>Performance Impacts of Unicode, Equals vs LIKE, and Partially Filled Fixed Width</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/performance-impacts-of-unicode-equals</link>
			<pubDate>Wed, 28 Dec 2011 11:45:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">Microsoft SQL Server</category>			<guid isPermaLink="false">1567@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Several weeks ago I was refreshing my memory on some nvarchar/varchar tradeoffs when I ran into a post by Michael J Swart (&lt;a href=&quot;http://michaeljswart.com/&quot; title=&quot;Michael J Swart&#039;s Blog&quot;&gt;blog&lt;/a&gt;|&lt;a href=&quot;https://twitter.com/#!/MJSwart&quot; title=&quot;@MJSwart on twitter&quot;&gt;twitter&lt;/a&gt;) where he &lt;a href=&quot;http://michaeljswart.com/2011/02/searching-inside-strings-cpu-is-eight-times-worse-for-unicode-strings/&quot; title=&quot;Searching Inside Strings: CPU is Eight Times Worse For Unicode Strings&quot;&gt;shared the results&lt;/a&gt; of investigating a performance problem in one of his live environments. After changing many columns from varchar to nvarchar, he had several procedures that were taking much longer to execute without any changes in I/O profile. This led him to compare the execution times of searching a varchar vs an nvarchar column, where he found that searching an nvarchar took 800% as long as a varchar.&lt;/p&gt;

&lt;p&gt;After reading this, I was curious. Does that follow with char and nchar also? How about looking for exact matches (equals) instead of a search (LIKE)? And partially filled char fields are supposed to be poorer performing too, right?&lt;/p&gt;

&lt;h2&gt;Results&lt;/h2&gt;
&lt;p&gt;The results are based on 4 runs of each query against 1,000,000 rows with explicitly limited parallelism. The name identifies the type, which operation was used (equals or LIKE), and the size of the field. All data was 36 characters, so &quot;(36)&quot; tests are fully populated values and &quot;(72)&quot; tests are half-full values.&lt;/p&gt;

&lt;div style=&quot;font-size: 80%; color: #666666; text-align: center&quot;&gt;
	&lt;img src=&quot;http://tiernok.com/LTDBlog/nchar_all.png&quot; title=&quot;Graph of all Results&quot; /&gt;&lt;br /&gt;
	All Results, Normalized to Shortest Value (Char 32 - Equals)
&lt;/div&gt;

&lt;p&gt;This graph shows the average for each type as a percentage of the smallest value, &#039;Char(72) - Equals&#039;. What&#039;s immediately obvious is the level of difference between an equals operation and a LIKE, especially when we get to the trailing, partially filed NCHAR field (wow!).&lt;/p&gt;

&lt;p&gt;The data was pretty consistent across the 4 runs used for these graphs:&lt;/p&gt;

&lt;p&gt;&lt;style&gt;
.zebra{ margin: .5em 2em;}
.zebra tr:nth-child(2n){ background-color: #eeeeee;}
.zebra tr th{ background-color: #dddddd; text-align: left; }
&lt;/style&gt;&lt;/p&gt;
&lt;table class=&quot;zebra&quot;&gt;
&lt;tr&gt;&lt;th&gt;Test&lt;/th&gt;&lt;th&gt;Average (ms)&lt;/th&gt;&lt;th&gt;StdDev (ms)&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Char(36) - Equals&lt;/td&gt;&lt;td&gt;113&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Char(36) - Like&lt;/td&gt;&lt;td&gt;596&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Char(72) - Equals&lt;/td&gt;&lt;td&gt;117&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Char(72) - Like&lt;/td&gt;&lt;td&gt;1043&lt;/td&gt;&lt;td&gt;1.5&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;NChar(36) - Equals&lt;/td&gt;&lt;td&gt;123&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;NChar(36) - Like&lt;/td&gt;&lt;td&gt;3951&lt;/td&gt;&lt;td&gt;13.08&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;NChar(72) - Equals&lt;/td&gt;&lt;td&gt;121&lt;/td&gt;&lt;td&gt;1.5&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;NChar(72) - Like&lt;/td&gt;&lt;td&gt;7350&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;NVarchar(36) - Equals&lt;/td&gt;&lt;td&gt;153&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;NVarchar(36) - Like&lt;/td&gt;&lt;td&gt;3997&lt;/td&gt;&lt;td&gt;8.85&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;NVarchar(72) - Equals&lt;/td&gt;&lt;td&gt;147&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;NVarchar(72) - Like&lt;/td&gt;&lt;td&gt;3972&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Varchar(36) - Equals&lt;/td&gt;&lt;td&gt;136&lt;/td&gt;&lt;td&gt;2.87&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Varchar(36) - Like&lt;/td&gt;&lt;td&gt;623&lt;/td&gt;&lt;td&gt;2.45&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Varchar(72) - Equals&lt;/td&gt;&lt;td&gt;141&lt;/td&gt;&lt;td&gt;1.5&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Varchar(72) - Like&lt;/td&gt;&lt;td&gt;630&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;&lt;i&gt;Note: I originally had 5 runs, but removed the first since it had consistently higher values, with 2 cases far outside several standard devitations.&lt;/i&gt;&lt;/p&gt;

&lt;h3&gt;Comparing EQUALS statements&lt;/h3&gt;
&lt;p&gt;There is limited statistical significance in the fixed-width EQUALs, but the values do group together by type. &lt;/p&gt;

&lt;div style=&quot;font-size: 80%; color: #666666; text-align: center&quot;&gt;
	&lt;img src=&quot;http://tiernok.com/LTDBlog/nchar_equals.png&quot; title=&quot;Graph of Equals Results&quot; /&gt;&lt;br /&gt;
	Equals Results, Normalized to Shortest Value (Char 32 - Equals)
&lt;/div&gt;

&lt;p&gt;It is interesting to note that there is a pretty consistent 20% gap between fixed and variable widths for the same types (char to varchar, for instance), regardless of whether the column is partially or fully populated.&lt;/p&gt;

&lt;h3&gt;Comparing LIKE statements&lt;/h3&gt;
&lt;p&gt;There is a much broader impact when we start looking at the LIKE statement comparisons. Like Michael&#039;s post above, there is a noticeable difference (on my system, ~650%) between searching a varchar and an nvarchar column.&lt;/p&gt;

&lt;div style=&quot;font-size: 80%; color: #666666; text-align: center&quot;&gt;
	&lt;img src=&quot;http://tiernok.com/LTDBlog/nchar_like.png&quot; title=&quot;Graph of LIKE Results&quot; /&gt;&lt;br /&gt;
	LIKE Results, Normalized to Shortest Value (Char 32 - Like)
&lt;/div&gt;

&lt;p&gt;The conclusions I see are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The previously mentioned difference between unicode and non-unicode fields is present&lt;/li&gt;
&lt;li&gt;Fixed width fields suffer the same performance impact as variable length&lt;/li&gt;
&lt;li&gt;Partially populated fixed width fields ran 75-90% longer than the associated fully populated column&lt;/li&gt;
&lt;li&gt;Unicode fields were consistently 6x slower (on my system) than their non-unicode variants&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Poorly sized nchar seems to be the real winner.&lt;/p&gt;

&lt;h2&gt;Scripts&lt;/h2&gt;
&lt;p&gt;The script for these results was heavily based on the one in Michael&#039;s post above. The prep script assumes you have a numbers table (&lt;a href=&quot;http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/you-require-a-numbers-table.aspx&quot; title=&quot;You Require a Numbers Table by Adam Machanic&quot;&gt;here is a script for one&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Setup Sample Data Table&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb78695&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-----------------------------------&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- Prep - Generate Sample Data&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt; EXISTS &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &lt;span style=&quot;color: #00AF00;&quot;&gt;INFORMATION_SCHEMA&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;TABLES&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; TABLE_SCHEMA = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;dbo&#039;&lt;/span&gt; AND TABLE_NAME = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;CharTypeTest&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DROP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;CharTypeTest&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;CharTypeTest&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; CharTypeTestId &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;identity&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; VarcharField36 &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;36&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; VarcharField72 &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;72&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; CharField36 &lt;span style=&quot;color: #0000FF;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;36&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; CharField72 &lt;span style=&quot;color: #0000FF;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;72&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; NVarcharField36 &lt;span style=&quot;color: #0000FF;&quot;&gt;nvarchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;36&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; NVarcharField72 &lt;span style=&quot;color: #0000FF;&quot;&gt;nvarchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;72&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; NCharField36 &lt;span style=&quot;color: #0000FF;&quot;&gt;nchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;36&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; NCharField72 &lt;span style=&quot;color: #0000FF;&quot;&gt;nchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;72&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL&amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; SampleData &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;NEWID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CHAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;36&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; SampleText&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Numbers N1, Numbers N2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #00AF00;&quot;&gt;-- 50,000 * 20 = 1,000,000&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; N1.&lt;span style=&quot;color: #202020;&quot;&gt;Number&lt;/span&gt; &amp;lt; &lt;span style=&quot;color: #000;&quot;&gt;50000&lt;/span&gt; AND N2.&lt;span style=&quot;color: #202020;&quot;&gt;Number&lt;/span&gt; &amp;lt; &lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;CharTypeTest&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;VarcharField36, CharField36, NvarcharField36, NCharField36,&lt;br /&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;VarcharField72, CharField72, NvarcharField72, NCharField72&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; SampleText, SampleText, SampleText, SampleText, SampleText, SampleText, SampleText, SampleText&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; SampleData;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb7078&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And here is the test code:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Setup Sample Data Table&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb74568&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-----------------------------&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- Test&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; #Results &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, elapsed &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;, run &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @StartTime &lt;span style=&quot;color: #0000FF;&quot;&gt;DateTime&lt;/span&gt;, @run &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @run = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHILE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; #Results &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; name = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Char(36) - Equals&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;lt; &lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; CharField36 = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;abcd&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Char(36) - Equals&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; VarcharField36 = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;abcd&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Varchar(36) - Equals&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; NCharField36 = N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;abcd&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;NChar(36) - Equals&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; NVarcharField36 = N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;abcd&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;NVarchar(36) - Equals&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; CharField36 LIKE &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;%abcd%&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Char(36) - Like&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; VarcharField36 LIKE &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;%abcd%&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Varchar(36) - Like&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; NCharField36 LIKE N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;%abcd%&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;NChar(36) - Like&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; NVarcharField36 LIKE N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;%abcd%&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;NVarchar(36) - Like&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; CharField72 = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;abcd&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Char(72) - Equals&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; VarcharField72 = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;abcd&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Varchar(72) - Equals&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; NCharField72 = N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;abcd&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;NChar(72) - Equals&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; NVarcharField72 = N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;abcd&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;NVarchar(72) - Equals&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; CharField72 LIKE &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;%abcd%&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Char(72) - Like&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; VarcharField72 LIKE &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;%abcd%&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Varchar(72) - Like&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; NCharField72 LIKE N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;%abcd%&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;NChar(72) - Like&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @StartTime = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; CharTypeTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; NVarcharField72 LIKE N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;%abcd%&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OPTION&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MAXDOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #Results&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,elapsed,run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;NVarchar(72) - Like&#039;&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms, @StartTime, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,@run&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @run = @run + &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt;; &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; #Results;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DROP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; #Results;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb23213&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I realize this is not a groundbreaking post, but after finding the results out for myself I thought it would be interesting to share. It also underlines and italicizes the need to fit your data definitions to the data you will be storing.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/performance-impacts-of-unicode-equals&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Several weeks ago I was refreshing my memory on some nvarchar/varchar tradeoffs when I ran into a post by Michael J Swart (<a href="http://michaeljswart.com/" title="Michael J Swart's Blog">blog</a>|<a href="https://twitter.com/#!/MJSwart" title="@MJSwart on twitter">twitter</a>) where he <a href="http://michaeljswart.com/2011/02/searching-inside-strings-cpu-is-eight-times-worse-for-unicode-strings/" title="Searching Inside Strings: CPU is Eight Times Worse For Unicode Strings">shared the results</a> of investigating a performance problem in one of his live environments. After changing many columns from varchar to nvarchar, he had several procedures that were taking much longer to execute without any changes in I/O profile. This led him to compare the execution times of searching a varchar vs an nvarchar column, where he found that searching an nvarchar took 800% as long as a varchar.</p>

<p>After reading this, I was curious. Does that follow with char and nchar also? How about looking for exact matches (equals) instead of a search (LIKE)? And partially filled char fields are supposed to be poorer performing too, right?</p>

<h2>Results</h2>
<p>The results are based on 4 runs of each query against 1,000,000 rows with explicitly limited parallelism. The name identifies the type, which operation was used (equals or LIKE), and the size of the field. All data was 36 characters, so "(36)" tests are fully populated values and "(72)" tests are half-full values.</p>

<div style="font-size: 80%; color: #666666; text-align: center">
	<img src="http://tiernok.com/LTDBlog/nchar_all.png" title="Graph of all Results" /><br />
	All Results, Normalized to Shortest Value (Char 32 - Equals)
</div>

<p>This graph shows the average for each type as a percentage of the smallest value, 'Char(72) - Equals'. What's immediately obvious is the level of difference between an equals operation and a LIKE, especially when we get to the trailing, partially filed NCHAR field (wow!).</p>

<p>The data was pretty consistent across the 4 runs used for these graphs:</p>

<p><style>
.zebra{ margin: .5em 2em;}
.zebra tr:nth-child(2n){ background-color: #eeeeee;}
.zebra tr th{ background-color: #dddddd; text-align: left; }
</style></p>
<table class="zebra">
<tr><th>Test</th><th>Average (ms)</th><th>StdDev (ms)</th></tr>
<tr><td>Char(36) - Equals</td><td>113</td><td>0</td></tr>
<tr><td>Char(36) - Like</td><td>596</td><td>0</td></tr>
<tr><td>Char(72) - Equals</td><td>117</td><td>2</td></tr>
<tr><td>Char(72) - Like</td><td>1043</td><td>1.5</td></tr>
<tr><td>NChar(36) - Equals</td><td>123</td><td>0</td></tr>
<tr><td>NChar(36) - Like</td><td>3951</td><td>13.08</td></tr>
<tr><td>NChar(72) - Equals</td><td>121</td><td>1.5</td></tr>
<tr><td>NChar(72) - Like</td><td>7350</td><td>0</td></tr>
<tr><td>NVarchar(36) - Equals</td><td>153</td><td>0</td></tr>
<tr><td>NVarchar(36) - Like</td><td>3997</td><td>8.85</td></tr>
<tr><td>NVarchar(72) - Equals</td><td>147</td><td>2</td></tr>
<tr><td>NVarchar(72) - Like</td><td>3972</td><td>3</td></tr>
<tr><td>Varchar(36) - Equals</td><td>136</td><td>2.87</td></tr>
<tr><td>Varchar(36) - Like</td><td>623</td><td>2.45</td></tr>
<tr><td>Varchar(72) - Equals</td><td>141</td><td>1.5</td></tr>
<tr><td>Varchar(72) - Like</td><td>630</td><td>0</td></tr>
</table>

<p><i>Note: I originally had 5 runs, but removed the first since it had consistently higher values, with 2 cases far outside several standard devitations.</i></p>

<h3>Comparing EQUALS statements</h3>
<p>There is limited statistical significance in the fixed-width EQUALs, but the values do group together by type. </p>

<div style="font-size: 80%; color: #666666; text-align: center">
	<img src="http://tiernok.com/LTDBlog/nchar_equals.png" title="Graph of Equals Results" /><br />
	Equals Results, Normalized to Shortest Value (Char 32 - Equals)
</div>

<p>It is interesting to note that there is a pretty consistent 20% gap between fixed and variable widths for the same types (char to varchar, for instance), regardless of whether the column is partially or fully populated.</p>

<h3>Comparing LIKE statements</h3>
<p>There is a much broader impact when we start looking at the LIKE statement comparisons. Like Michael's post above, there is a noticeable difference (on my system, ~650%) between searching a varchar and an nvarchar column.</p>

<div style="font-size: 80%; color: #666666; text-align: center">
	<img src="http://tiernok.com/LTDBlog/nchar_like.png" title="Graph of LIKE Results" /><br />
	LIKE Results, Normalized to Shortest Value (Char 32 - Like)
</div>

<p>The conclusions I see are:</p>
<ul>
<li>The previously mentioned difference between unicode and non-unicode fields is present</li>
<li>Fixed width fields suffer the same performance impact as variable length</li>
<li>Partially populated fixed width fields ran 75-90% longer than the associated fully populated column</li>
<li>Unicode fields were consistently 6x slower (on my system) than their non-unicode variants</li>
</ul>

<p>Poorly sized nchar seems to be the real winner.</p>

<h2>Scripts</h2>
<p>The script for these results was heavily based on the one in Michael's post above. The prep script assumes you have a numbers table (<a href="http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/you-require-a-numbers-table.aspx" title="You Require a Numbers Table by Adam Machanic">here is a script for one</a>).</p>

<p><b>Setup Sample Data Table</b></p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb38329'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb38329','cb15763'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb38329" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #00AF00;">-----------------------------------</span></li><li style="" class="li2"><span style="color: #00AF00;">-- Prep - Generate Sample Data</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">IF</span> EXISTS <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">FROM</span> <span style="color: #00AF00;">INFORMATION_SCHEMA</span>.<span style="color: #00AF00;">TABLES</span> <span style="color: #0000FF;">WHERE</span> TABLE_SCHEMA = <span style="color: #FF0000;">'dbo'</span> AND TABLE_NAME = <span style="color: #FF0000;">'CharTypeTest'</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">CharTypeTest</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">CharTypeTest</span><span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; CharTypeTestId <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">identity</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; VarcharField36 <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">36</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; VarcharField72 <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">72</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; CharField36 <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">36</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; CharField72 <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">72</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; NVarcharField36 <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">36</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; NVarcharField72 <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">72</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; NCharField36 <span style="color: #0000FF;">nchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">36</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; NCharField72 <span style="color: #0000FF;">nchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">72</span><span style="color: #808080;">&#41;</span> NOT NULL&nbsp;<span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">WITH</span> SampleData <span style="color: #0000FF;">AS</span><span style="color: #808080;">&#40;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>NEWID<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">36</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> SampleText</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> Numbers N1, Numbers N2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00AF00;">-- 50,000 * 20 = 1,000,000</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> N1.<span style="color: #202020;">Number</span> &lt; <span style="color: #000;">50000</span> AND N2.<span style="color: #202020;">Number</span> &lt; <span style="color: #000;">20</span></li><li style="" class="li2"><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">CharTypeTest</span><span style="color: #808080;">&#40;</span>VarcharField36, CharField36, NvarcharField36, NCharField36,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VarcharField72, CharField72, NvarcharField72, NCharField72<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> SampleText, SampleText, SampleText, SampleText, SampleText, SampleText, SampleText, SampleText</li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> SampleData;</li></ol></div><div id="cb15763" style="display: none; color: red;"></div></div></div>

<p>And here is the test code:</p>

<p><b>Setup Sample Data Table</b></p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb85183'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb85183','cb20408'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb85183" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #00AF00;">-----------------------------</span></li><li style="" class="li2"><span style="color: #00AF00;">-- Test</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #Results <span style="color: #808080;">&#40;</span>name <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span>, elapsed <span style="color: #0000FF;">int</span>, run <span style="color: #0000FF;">int</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @StartTime <span style="color: #0000FF;">DateTime</span>, @run <span style="color: #0000FF;">int</span>;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @run = <span style="color: #000;">1</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> #Results <span style="color: #0000FF;">WHERE</span> name = <span style="color: #FF0000;">'Char(36) - Equals'</span><span style="color: #808080;">&#41;</span> &lt; <span style="color: #000;">2</span></li><li style="" class="li1"><span style="color: #0000FF;">BEGIN</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> CharField36 = <span style="color: #FF0000;">'abcd'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Char(36) - Equals'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> VarcharField36 = <span style="color: #FF0000;">'abcd'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Varchar(36) - Equals'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> NCharField36 = N<span style="color: #FF0000;">'abcd'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'NChar(36) - Equals'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> NVarcharField36 = N<span style="color: #FF0000;">'abcd'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'NVarchar(36) - Equals'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> CharField36 LIKE <span style="color: #FF0000;">'%abcd%'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Char(36) - Like'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> VarcharField36 LIKE <span style="color: #FF0000;">'%abcd%'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Varchar(36) - Like'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> NCharField36 LIKE N<span style="color: #FF0000;">'%abcd%'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'NChar(36) - Like'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> NVarcharField36 LIKE N<span style="color: #FF0000;">'%abcd%'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'NVarchar(36) - Like'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> CharField72 = <span style="color: #FF0000;">'abcd'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Char(72) - Equals'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> VarcharField72 = <span style="color: #FF0000;">'abcd'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Varchar(72) - Equals'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> NCharField72 = N<span style="color: #FF0000;">'abcd'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'NChar(72) - Equals'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> NVarcharField72 = N<span style="color: #FF0000;">'abcd'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'NVarchar(72) - Equals'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> CharField72 LIKE <span style="color: #FF0000;">'%abcd%'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Char(72) - Like'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> VarcharField72 LIKE <span style="color: #FF0000;">'%abcd%'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Varchar(72) - Like'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> NCharField72 LIKE N<span style="color: #FF0000;">'%abcd%'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'NChar(72) - Like'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @StartTime = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> CharTypeTest <span style="color: #0000FF;">WHERE</span> NVarcharField72 LIKE N<span style="color: #FF0000;">'%abcd%'</span> <span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">MAXDOP</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Results<span style="color: #808080;">&#40;</span>name,elapsed,run<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'NVarchar(72) - Like'</span>, <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span>ms, @StartTime, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,@run<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @run = @run + <span style="color: #000;">1</span>;</li><li style="" class="li2"><span style="color: #0000FF;">END</span>; </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</span> #Results;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #Results;</li></ol></div><div id="cb20408" style="display: none; color: red;"></div></div></div>

<p>I realize this is not a groundbreaking post, but after finding the results out for myself I thought it would be interesting to share. It also underlines and italicizes the need to fit your data definitions to the data you will be storing.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/performance-impacts-of-unicode-equals">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/performance-impacts-of-unicode-equals#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1567</wfw:commentRss>
		</item>
				<item>
			<title>Using T-SQL OUTPUT and MERGE To Link Old and New Keys</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/using-t-sql-output-and-merge</link>
			<pubDate>Tue, 13 Sep 2011 11:44:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="alt">Data Modelling &amp; Design</category>
<category domain="main">Microsoft SQL Server</category>			<guid isPermaLink="false">1401@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;You don&#039;t catch me doing SQL posts that often, probably because I spend far more time working in a development world that seems to be moving further and further away from SQL. That being said, the database server is optimized towards handling large sets of data and data manipulations, so rather than second-guessing developers that know far more (and have far larger budgets), I like to take advantage of database-side solutions when I can. The time saved not dragging data back and forth on the network is an extra bonus.&lt;/p&gt;

&lt;p&gt;Recently I needed the ability to transfer records (and control of the data) between two data structures on demand. The original data structure has grown up over the course of 10+ years as part of a larger system, while the newer, purpose-built data structures exist specifically to support this data and support newer capabilities. The tricky part is that the end user gets to pick and chose which records will be managed by the newer system and which will not, on demand.&lt;/p&gt;

&lt;h2&gt;Sample Table Schemas&lt;/h2&gt;

&lt;p&gt;To get us started, I&#039;ve created some simplified, substitute table for the post. Using simpler tables that are far fewer in count will let us focus more on the problem at hand and less on my data definition skills.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb53247&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;use&lt;/span&gt; SampleStuff;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Go&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- &amp;quot;Original&amp;quot; tables with columns for new keys&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Orig_User_Key&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Username&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_User_Key&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NULL&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_Work_Phone&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Orig_Work_Phone_Key &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Orig_User_Key&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Phone&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_Phone_Key&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NULL&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_Home_Phone&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Orig_Home_Phone_Key &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Orig_User_Key&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Phone&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_Phone_Key&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NULL&amp;nbsp; &amp;nbsp; &lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- And the &amp;quot;New&amp;quot; tables&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;New_User&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_User_Key&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Username&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;New_Phone&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_Phone_Key&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_User_Key&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Phone&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Is_Work&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;bit&lt;/span&gt; NOT NULL&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb66389&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;i&gt;Note: For the sake of shorter examples, I&#039;m going to provide examples on the User tables. We don&#039;t actually need anything fancier than SCOPE_IDENTITY() until we get to the step where we transfer Phone records since we are doing one user at a time, but that&#039;s why examples are so much easier than the real world.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;The tricky part of this transform is the ownership of the data. Due to some internal restrictions, the original data structure is going to be the master. This means the original record is responsible for holding both its own key and the key in the newer system. The transform needs to get the record from the original system, push it into the newer system (generating the new key), then update the new key back into the original record.&lt;/p&gt;

&lt;p&gt;If this was a one-shot ETL load, I would probably add a temporary column to the target tables to track the source keys, then update the new keys back into the source table and drop the temporary column. As you can imagine, this is an even less graceful solution when we put control into the users hands and operate at the individual record level (c&#039;mon, it&#039;s only a few hundred thousand table locks during peak hours).&lt;/p&gt;

&lt;h2&gt;The OUTPUT Statement&lt;/h2&gt;
&lt;p&gt;Added in SQL Server 2005 (6 years ago already?), OUTPUT can be used to return data from inside INSERT, UPDATE, DELETE, and MERGE statements. This solves the first part, giving us access to the new key when it&#039;s generated. Well, almost.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb46341&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; #ID_Transfer&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;new_key &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;, old_key &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;New_User&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Username&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;OUTPUT&lt;/span&gt; Inserted.&lt;span style=&quot;color: #202020;&quot;&gt;New_User_Key&lt;/span&gt;, ??? &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #ID_Transfer&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OU.&lt;span style=&quot;color: #202020;&quot;&gt;Username&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt; OU&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; OU.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User_Key&lt;/span&gt; = @TargetUserKey;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb23083&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;While we can easily create a temporary table to capture the resulting New_User_Key that is generated on insert, we don&#039;t have access to the table the data is coming from, so we don&#039;t have the original key to populate next to that new key.&lt;/p&gt;

&lt;p&gt;Which brings us to MERGE.&lt;/p&gt;

&lt;h2&gt;The MERGE Statement&lt;/h2&gt;
&lt;p&gt;MERGE was added in SQL 2008 and allows us to define a source dataset, target data set, and rules to insert or update data from that source to the target. While we don&#039;t really need the rules and update capability, what it also provides is simultaneous access to both datasets AND support for OUTPUT. Aha, I hear you saying.&lt;/p&gt;

&lt;p&gt;By forcing the merge to always perform an INSERT, we will satisfy our needs for inserting the data into the &quot;New&quot; tables as well as have the capability to capture both the original and new key in a single OUTPUT statement.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb31879&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; #ID_Transfer&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;new_key &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;, old_key &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MERGE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;New_User&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Target&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USING&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OU.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User_Key&lt;/span&gt;, OU.&lt;span style=&quot;color: #202020;&quot;&gt;Username&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt; OU &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; OU.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User_Key&lt;/span&gt; = @TargetUserKey&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Source&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHEN&lt;/span&gt; NOT &lt;span style=&quot;color: #0000FF;&quot;&gt;MATCHED&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;THEN&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Username&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;source&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Username&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;OUTPUT&lt;/span&gt; INSERTED.&lt;span style=&quot;color: #202020;&quot;&gt;New_User_Key&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;source&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_USer_Key&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #ID_Transfer;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb82584&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2&gt;Full Roundtrip&lt;/h2&gt;
&lt;p&gt;With the critical pieces out of the way, it&#039;s easy now to create the full round-trip update.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb25392&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; #ID_Transfer&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;new_key &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;, old_key &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @TargetUserKey &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @TargetUserKey = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;MERGE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;New_User&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Target&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USING&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OU.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User_Key&lt;/span&gt;, OU.&lt;span style=&quot;color: #202020;&quot;&gt;Username&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt; OU &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; OU.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User_Key&lt;/span&gt; = @TargetUserKey&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Source&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHEN&lt;/span&gt; NOT &lt;span style=&quot;color: #0000FF;&quot;&gt;MATCHED&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;THEN&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Username&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;source&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Username&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;OUTPUT&lt;/span&gt; INSERTED.&lt;span style=&quot;color: #202020;&quot;&gt;New_User_Key&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;source&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_USer_Key&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; #ID_Transfer;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- and back-update original record with new key&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;UPDATE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; New_User_Key = IT.&lt;span style=&quot;color: #202020;&quot;&gt;new_key&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt; OU&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INNER&lt;/span&gt; JOIN #ID_Transfer IT &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; IT.&lt;span style=&quot;color: #202020;&quot;&gt;old_key&lt;/span&gt; = OU.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User_Key&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DROP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; #ID_Transfer;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb62308&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And we can see the updated values like so:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb47918&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt; *&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt; O&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN dbo.&lt;span style=&quot;color: #202020;&quot;&gt;New_User&lt;/span&gt; N &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; N.&lt;span style=&quot;color: #202020;&quot;&gt;New_User_Key&lt;/span&gt; = O.&lt;span style=&quot;color: #202020;&quot;&gt;New_User_Key&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb51940&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So that was the first step, next is the phone numbers.&lt;/p&gt;

&lt;h2&gt;Homework/Practice&lt;/h2&gt;
&lt;p&gt;Given the sample tables above, it would now be fairly straightforward to apply this idea to the phone number tables, populating the data from the original two into the single new table and back-populating the new keys back into the original records. Rather than give you the final script, I&#039;ll instead give you the pieces needed to get this far. &lt;/p&gt;

&lt;p&gt;Setup script:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb34988&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;use&lt;/span&gt; SampleStuff;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Go&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;/*&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;DROP TABLE dbo.New_Phone;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;DROP TABLE dbo.New_User;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;DROP TABLE dbo.Orig_Home_Phone;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;DROP TABLE dbo.Orig_Work_Phone;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;DROP TABLE dbo.Orig_User;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;DROP TABLE dbo.Number;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;*/&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Orig_User_Key&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Username&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_User_Key&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NULL&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_Work_Phone&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Orig_Work_Phone_Key &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Orig_User_Key&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Phone&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_Phone_Key&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NULL&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_Home_Phone&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Orig_Home_Phone_Key &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Orig_User_Key&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Phone&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_Phone_Key&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NULL&amp;nbsp; &amp;nbsp; &lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;New_User&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_User_Key&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Username&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;New_Phone&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_Phone_Key&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; New_User_Key&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Phone&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Is_Work&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;bit&lt;/span&gt; NOT NULL&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Go&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Number&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;num &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;NOCOUNT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Number&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DEFAULT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; ;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHILE&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;SCOPE_IDENTITY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;lt; &lt;span style=&quot;color: #000;&quot;&gt;500&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Number&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DEFAULT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Username&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;User #&#039;&lt;/span&gt; + &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;num &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Number&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_Work_Phone&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Orig_User_Key, Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; Orig_User_Key, &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Phone &#039;&lt;/span&gt; + &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Orig_User_Key &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; Orig_User_Key % &lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_Work_Phone&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Orig_User_Key, Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; Orig_User_Key, &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Phone &#039;&lt;/span&gt; + &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Orig_User_Key &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; Orig_User_Key % &lt;span style=&quot;color: #000;&quot;&gt;3&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_Home_Phone&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Orig_User_Key, Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; Orig_User_Key, &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Phone &#039;&lt;/span&gt; + &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Orig_User_Key &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; Orig_User_Key % &lt;span style=&quot;color: #000;&quot;&gt;3&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_Home_Phone&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Orig_User_Key, Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; Orig_User_Key, &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Phone &#039;&lt;/span&gt; + &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Orig_User_Key &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;Orig_User&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; Orig_User_Key % &lt;span style=&quot;color: #000;&quot;&gt;4&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb89941&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Given those pieces above and some additional scripts, finishing the script should only take 5-10 minutes. Consider it a free practice problem &lt;img src=&quot;http://blogs.lessthandot.com/rsc/smilies/icon_smile.gif&quot; title=&quot;:)&quot; alt=&quot;:)&quot; class=&quot;middle&quot; width=&quot;15&quot; height=&quot;15&quot; /&gt;&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/using-t-sql-output-and-merge&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>You don't catch me doing SQL posts that often, probably because I spend far more time working in a development world that seems to be moving further and further away from SQL. That being said, the database server is optimized towards handling large sets of data and data manipulations, so rather than second-guessing developers that know far more (and have far larger budgets), I like to take advantage of database-side solutions when I can. The time saved not dragging data back and forth on the network is an extra bonus.</p>

<p>Recently I needed the ability to transfer records (and control of the data) between two data structures on demand. The original data structure has grown up over the course of 10+ years as part of a larger system, while the newer, purpose-built data structures exist specifically to support this data and support newer capabilities. The tricky part is that the end user gets to pick and chose which records will be managed by the newer system and which will not, on demand.</p>

<h2>Sample Table Schemas</h2>

<p>To get us started, I've created some simplified, substitute table for the post. Using simpler tables that are far fewer in count will let us focus more on the problem at hand and less on my data definition skills.</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb65594'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb65594','cb13143'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb65594" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">use</span> SampleStuff;</li><li style="" class="li2"><span style="color: #0000FF;">Go</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">-- &quot;Original&quot; tables with columns for new keys</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Orig_User</span><span style="color: #808080;">&#40;</span></li><li style="" class="li1">&nbsp; &nbsp; Orig_User_Key&nbsp; &nbsp;<span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; Username&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; New_User_Key&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NULL</li><li style="" class="li2"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Orig_Work_Phone</span><span style="color: #808080;">&#40;</span></li><li style="" class="li1">&nbsp; &nbsp; Orig_Work_Phone_Key <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; Orig_User_Key&nbsp; &nbsp;&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; Phone&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">&nbsp; &nbsp; New_Phone_Key&nbsp; &nbsp;&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NULL</li><li style="" class="li1"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Orig_Home_Phone</span><span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; Orig_Home_Phone_Key <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; Orig_User_Key&nbsp; &nbsp;&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; Phone&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li1">&nbsp; &nbsp; New_Phone_Key&nbsp; &nbsp;&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NULL&nbsp; &nbsp; </li><li style="" class="li2"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">-- And the &quot;New&quot; tables</span></li><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">New_User</span><span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; New_User_Key&nbsp; &nbsp; <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; Username&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> NOT NULL</li><li style="" class="li2"><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">New_Phone</span><span style="color: #808080;">&#40;</span></li><li style="" class="li1">&nbsp; &nbsp; New_Phone_Key&nbsp; &nbsp;<span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; New_User_Key&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; Phone&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; Is_Work&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">bit</span> NOT NULL</li><li style="" class="li1"><span style="color: #808080;">&#41;</span>;</li></ol></div><div id="cb13143" style="display: none; color: red;"></div></div></div>

<p><i>Note: For the sake of shorter examples, I'm going to provide examples on the User tables. We don't actually need anything fancier than SCOPE_IDENTITY() until we get to the step where we transfer Phone records since we are doing one user at a time, but that's why examples are so much easier than the real world.</i></p>

<p>The tricky part of this transform is the ownership of the data. Due to some internal restrictions, the original data structure is going to be the master. This means the original record is responsible for holding both its own key and the key in the newer system. The transform needs to get the record from the original system, push it into the newer system (generating the new key), then update the new key back into the original record.</p>

<p>If this was a one-shot ETL load, I would probably add a temporary column to the target tables to track the source keys, then update the new keys back into the source table and drop the temporary column. As you can imagine, this is an even less graceful solution when we put control into the users hands and operate at the individual record level (c'mon, it's only a few hundred thousand table locks during peak hours).</p>

<h2>The OUTPUT Statement</h2>
<p>Added in SQL Server 2005 (6 years ago already?), OUTPUT can be used to return data from inside INSERT, UPDATE, DELETE, and MERGE statements. This solves the first part, giving us access to the new key when it's generated. Well, almost.</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb15608'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb15608','cb57277'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb15608" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #ID_Transfer<span style="color: #808080;">&#40;</span>new_key <span style="color: #0000FF;">int</span>, old_key <span style="color: #0000FF;">int</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">New_User</span><span style="color: #808080;">&#40;</span>Username<span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">OUTPUT</span> Inserted.<span style="color: #202020;">New_User_Key</span>, ??? <span style="color: #0000FF;">INTO</span> #ID_Transfer</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> OU.<span style="color: #202020;">Username</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Orig_User</span> OU</li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> OU.<span style="color: #202020;">Orig_User_Key</span> = @TargetUserKey;</li></ol></div><div id="cb57277" style="display: none; color: red;"></div></div></div>

<p>While we can easily create a temporary table to capture the resulting New_User_Key that is generated on insert, we don't have access to the table the data is coming from, so we don't have the original key to populate next to that new key.</p>

<p>Which brings us to MERGE.</p>

<h2>The MERGE Statement</h2>
<p>MERGE was added in SQL 2008 and allows us to define a source dataset, target data set, and rules to insert or update data from that source to the target. While we don't really need the rules and update capability, what it also provides is simultaneous access to both datasets AND support for OUTPUT. Aha, I hear you saying.</p>

<p>By forcing the merge to always perform an INSERT, we will satisfy our needs for inserting the data into the "New" tables as well as have the capability to capture both the original and new key in a single OUTPUT statement.</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb26020'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb26020','cb22445'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb26020" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #ID_Transfer<span style="color: #808080;">&#40;</span>new_key <span style="color: #0000FF;">int</span>, old_key <span style="color: #0000FF;">int</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">MERGE</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">New_User</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Target</span><span style="color: #808080;">&#93;</span></li><li style="" class="li2"><span style="color: #0000FF;">USING</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> OU.<span style="color: #202020;">Orig_User_Key</span>, OU.<span style="color: #202020;">Username</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Orig_User</span> OU <span style="color: #0000FF;">WHERE</span> OU.<span style="color: #202020;">Orig_User_Key</span> = @TargetUserKey<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">Source</span></li><li style="" class="li1"><span style="color: #0000FF;">ON</span> <span style="color: #000;">1</span> = <span style="color: #000;">0</span></li><li style="" class="li2"><span style="color: #0000FF;">WHEN</span> NOT <span style="color: #0000FF;">MATCHED</span> <span style="color: #0000FF;">THEN</span></li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#40;</span>Username<span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">source</span>.<span style="color: #202020;">Username</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">OUTPUT</span> INSERTED.<span style="color: #202020;">New_User_Key</span>, <span style="color: #0000FF;">source</span>.<span style="color: #202020;">Orig_USer_Key</span> <span style="color: #0000FF;">INTO</span> #ID_Transfer;</li></ol></div><div id="cb22445" style="display: none; color: red;"></div></div></div>
<p><br /></p>

<h2>Full Roundtrip</h2>
<p>With the critical pieces out of the way, it's easy now to create the full round-trip update.</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb51760'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb51760','cb41703'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb51760" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #ID_Transfer<span style="color: #808080;">&#40;</span>new_key <span style="color: #0000FF;">int</span>, old_key <span style="color: #0000FF;">int</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @TargetUserKey <span style="color: #0000FF;">int</span>;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @TargetUserKey = <span style="color: #000;">1</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">MERGE</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">New_User</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Target</span><span style="color: #808080;">&#93;</span></li><li style="" class="li1"><span style="color: #0000FF;">USING</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> OU.<span style="color: #202020;">Orig_User_Key</span>, OU.<span style="color: #202020;">Username</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Orig_User</span> OU <span style="color: #0000FF;">WHERE</span> OU.<span style="color: #202020;">Orig_User_Key</span> = @TargetUserKey<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">Source</span></li><li style="" class="li2"><span style="color: #0000FF;">ON</span> <span style="color: #000;">1</span> = <span style="color: #000;">0</span></li><li style="" class="li1"><span style="color: #0000FF;">WHEN</span> NOT <span style="color: #0000FF;">MATCHED</span> <span style="color: #0000FF;">THEN</span></li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#40;</span>Username<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">source</span>.<span style="color: #202020;">Username</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">OUTPUT</span> INSERTED.<span style="color: #202020;">New_User_Key</span>, <span style="color: #0000FF;">source</span>.<span style="color: #202020;">Orig_USer_Key</span> <span style="color: #0000FF;">INTO</span> #ID_Transfer;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">-- and back-update original record with new key</span></li><li style="" class="li1"><span style="color: #0000FF;">UPDATE</span> dbo.<span style="color: #202020;">Orig_User</span></li><li style="" class="li2"><span style="color: #0000FF;">SET</span> New_User_Key = IT.<span style="color: #202020;">new_key</span></li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Orig_User</span> OU</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> JOIN #ID_Transfer IT <span style="color: #0000FF;">ON</span> IT.<span style="color: #202020;">old_key</span> = OU.<span style="color: #202020;">Orig_User_Key</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #ID_Transfer;</li></ol></div><div id="cb41703" style="display: none; color: red;"></div></div></div>

<p>And we can see the updated values like so:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb40758'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb40758','cb70624'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb40758" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">20</span> *</li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Orig_User</span> O</li><li style="" class="li1"><span style="color: #FF00FF;">LEFT</span> JOIN dbo.<span style="color: #202020;">New_User</span> N <span style="color: #0000FF;">ON</span> N.<span style="color: #202020;">New_User_Key</span> = O.<span style="color: #202020;">New_User_Key</span></li></ol></div><div id="cb70624" style="display: none; color: red;"></div></div></div>

<p>So that was the first step, next is the phone numbers.</p>

<h2>Homework/Practice</h2>
<p>Given the sample tables above, it would now be fairly straightforward to apply this idea to the phone number tables, populating the data from the original two into the single new table and back-populating the new keys back into the original records. Rather than give you the final script, I'll instead give you the pieces needed to get this far. </p>

<p>Setup script:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb90224'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb90224','cb22420'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb90224" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">use</span> SampleStuff;</li><li style="" class="li2"><span style="color: #0000FF;">Go</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">/*</span></li><li style="" class="li1"><span style="color: #00AF00;">DROP TABLE dbo.New_Phone;</span></li><li style="" class="li2"><span style="color: #00AF00;">DROP TABLE dbo.New_User;</span></li><li style="" class="li1"><span style="color: #00AF00;">DROP TABLE dbo.Orig_Home_Phone;</span></li><li style="" class="li2"><span style="color: #00AF00;">DROP TABLE dbo.Orig_Work_Phone;</span></li><li style="" class="li1"><span style="color: #00AF00;">DROP TABLE dbo.Orig_User;</span></li><li style="" class="li2"><span style="color: #00AF00;">DROP TABLE dbo.Number;</span></li><li style="" class="li1"><span style="color: #00AF00;">*/</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Orig_User</span><span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; Orig_User_Key&nbsp; &nbsp;<span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; Username&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; New_User_Key&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NULL</li><li style="" class="li1"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Orig_Work_Phone</span><span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; Orig_Work_Phone_Key <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; Orig_User_Key&nbsp; &nbsp;&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; Phone&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li1">&nbsp; &nbsp; New_Phone_Key&nbsp; &nbsp;&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NULL</li><li style="" class="li2"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Orig_Home_Phone</span><span style="color: #808080;">&#40;</span></li><li style="" class="li1">&nbsp; &nbsp; Orig_Home_Phone_Key <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; Orig_User_Key&nbsp; &nbsp;&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; Phone&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">&nbsp; &nbsp; New_Phone_Key&nbsp; &nbsp;&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NULL&nbsp; &nbsp; </li><li style="" class="li1"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">New_User</span><span style="color: #808080;">&#40;</span></li><li style="" class="li1">&nbsp; &nbsp; New_User_Key&nbsp; &nbsp; <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; Username&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> NOT NULL</li><li style="" class="li1"><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">New_Phone</span><span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; New_Phone_Key&nbsp; &nbsp;<span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; New_User_Key&nbsp; &nbsp; <span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; Phone&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; Is_Work&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">bit</span> NOT NULL</li><li style="" class="li2"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000FF;">Go</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Number</span><span style="color: #808080;">&#40;</span>num <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> NOT NULL<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> dbo.<span style="color: #202020;">Number</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #0000FF;">VALUES</span> ;</li><li style="" class="li1"><span style="color: #0000FF;">WHILE</span> <span style="color: #FF00FF;">SCOPE_IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> &lt; <span style="color: #000;">500</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> dbo.<span style="color: #202020;">Number</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #0000FF;">VALUES</span> ;</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Orig_User</span><span style="color: #808080;">&#40;</span>Username<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'User #'</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>num <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Number</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Orig_Work_Phone</span><span style="color: #808080;">&#40;</span>Orig_User_Key, Phone<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> Orig_User_Key, <span style="color: #FF0000;">'Phone '</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>Orig_User_Key <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Orig_User</span></li><li style="" class="li1"><span style="color: #0000FF;">WHERE</span> Orig_User_Key % <span style="color: #000;">2</span> = <span style="color: #000;">0</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Orig_Work_Phone</span><span style="color: #808080;">&#40;</span>Orig_User_Key, Phone<span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> Orig_User_Key, <span style="color: #FF0000;">'Phone '</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>Orig_User_Key <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Orig_User</span></li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> Orig_User_Key % <span style="color: #000;">3</span> = <span style="color: #000;">0</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Orig_Home_Phone</span><span style="color: #808080;">&#40;</span>Orig_User_Key, Phone<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> Orig_User_Key, <span style="color: #FF0000;">'Phone '</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>Orig_User_Key <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Orig_User</span></li><li style="" class="li1"><span style="color: #0000FF;">WHERE</span> Orig_User_Key % <span style="color: #000;">3</span> = <span style="color: #000;">0</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Orig_Home_Phone</span><span style="color: #808080;">&#40;</span>Orig_User_Key, Phone<span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> Orig_User_Key, <span style="color: #FF0000;">'Phone '</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>Orig_User_Key <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Orig_User</span></li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> Orig_User_Key % <span style="color: #000;">4</span> = <span style="color: #000;">0</span>;</li></ol></div><div id="cb22420" style="display: none; color: red;"></div></div></div>

<p>Given those pieces above and some additional scripts, finishing the script should only take 5-10 minutes. Consider it a free practice problem <img src="http://blogs.lessthandot.com/rsc/smilies/icon_smile.gif" title=":)" alt=":)" class="middle" width="15" height="15" /></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/using-t-sql-output-and-merge">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/using-t-sql-output-and-merge#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1401</wfw:commentRss>
		</item>
				<item>
			<title>SQLAzure - My First Cloud</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sqlazure-my-first-cloud</link>
			<pubDate>Thu, 12 May 2011 13:30:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>
<category domain="alt">Microsoft SQL Server Admin</category>			<guid isPermaLink="false">1237@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;This past weekend I made my first foray into SQL Azure. SQL Azure has been on my list of &quot;to learn&quot; technologies and lately it made it closer to the top. While I had picked up some knowledge from random links in the twitter-sphere, I hadn&#039;t made a concerted effort to learn about it yet.&lt;/p&gt;

&lt;p&gt;I started with a site named &lt;a href=&quot;https://www.microsoftvirtualacademy.com/&quot; title=&quot;Visit MVA&quot;&gt;Microsoft Virtual Academy&lt;/a&gt;. This site (despite some usability issues with the dialog boxes prior to signup) offers a number of virtual &quot;courses&quot; that consist of links to external resources grouped into modules, each of which finishes with a short assessment. As I started working my way through the modules for SQL Azure it so happened that Brian Harry (&lt;a href=&quot;http://blogs.msdn.com/b/bharry/&quot; title=&quot;Visit Brian Harry&#039;s blog&quot;&gt;blog&lt;/a&gt;) posted a link to a &lt;a href=&quot;http://blogs.msdn.com/b/bharry/archive/2011/04/27/free-azure-trial.aspx&quot; title=&quot;Read more on Brian&#039;s blog post&quot;&gt;free Azure trial&lt;/a&gt;, which was handy since I no longer have an MSDN account (MSDN accounts are also offered a free trial, check out your subscription page for more information). &lt;/p&gt;

&lt;p&gt;On top of this information it happened that Buck Woody (&lt;a href=&quot;http://buckwoody.com/&quot; title=&quot;BuckWoody&#039;s blog&quot;&gt;blog&lt;/a&gt;|&lt;a href=&quot;http://twitter.com/buckwoody&quot; title=&quot;@BuckWoody on Twitter&quot;&gt;twitter&lt;/a&gt;) posted up &lt;a href=&quot;http://blogs.msdn.com/b/buckwoody/archive/2010/12/13/windows-azure-learning-plan-sql-azure.aspx&quot; title=&quot;Buck&#039;s SQL Azure List&quot;&gt;an even more extensive list of information&lt;/a&gt; on Azure technologies, so it looks like my to-learn list is going to be full for a while.&lt;/p&gt;

&lt;p&gt;As if that flurry of links wasn&#039;t enough, &lt;a href=&quot;http://deepfriedbytes.com/&quot; title=&quot;Visit DeepFriedBytes&quot;&gt;DeepFriedBytes&lt;/a&gt; posted &lt;a href=&quot;http://deepfriedbytes.com/podcast/episode-68-why-your-career-is-going-away-because-you-refuse-to-change-you-pansy/&quot; title=&quot;DeepFriedBytes - Episode 68 - Why Your Career Is Going Away Because You Refuse To Change You Pansy&quot;&gt;a podcast with Buck Woody&lt;/a&gt; on the importance of cloud (ahem, distributed) technologies. If there is one link you follow-up on, make it that one.&lt;/p&gt;

&lt;h2&gt;My First Azure Database&lt;/h2&gt;
&lt;p&gt;I followed the instructions provided by the resources above to create my account, configure the firewall rules, and create my first database. But then I was stumped, what was I going to put in it?&lt;/p&gt;

&lt;p&gt;It so happens that I&#039;ve been working on an idea for a pet project for some time (well, one of many). In fact I had already used this idea to practice prototyping, requirements elicitation, and creation of an Agile-y product backlog. Some people knit, it never really caught on for me.&lt;/p&gt;

&lt;div style=&quot;padding: .5em; margin: 1.5em .5em .5em 0px; color: #666666; font-size: .8em; text-align: center; position: relative;&quot;&gt;
   &lt;img src=&quot;http://tiernok.com/LTDBlog/azure/prototype3of8.jpg&quot; alt=&quot;Prototype image, 3 of 8&quot; /&gt;&lt;br /&gt;
   Prototype image 3 of 8
&lt;/div&gt;

&lt;p&gt;Not only that, but the act of coming up with user stories (potential features) forced me to invent a common terminology and recognize which terms were coming up the most frequently.&lt;/p&gt;

&lt;div style=&quot; padding: .5em; margin: 1.5em .5em .5em 0px; color: #666666; font-size: .8em; text-align: center; position: relative;&quot;&gt;
   &lt;img src=&quot;http://tiernok.com/LTDBlog/azure/features.jpg&quot; alt=&quot;Prototype image, 3 of 8&quot; /&gt;&lt;br /&gt;
   70+ User Stories/Features
&lt;/div&gt;


&lt;p&gt;So I might have enough information to put together a normalized database to support the core functionality, which is about as close as I am going to get to a realistic data model on a Sunday evening.&lt;/p&gt;

&lt;h2&gt;Creating the Database&lt;/h2&gt;
&lt;p&gt;Creating the first database was &lt;a href=&quot;http://www.microsoft.com/showcase/en/US/details/1bd6bcb1-87c8-4f77-8425-2624a5f96976?WT.mc_id=otc-f-corp-jtc-DPR-MVA_INTROSQLAZURE&quot; title=&quot;Quick video on initial setup&quot;&gt;fairly straightforward (video)&lt;/a&gt; and consisted of setting up my subscription, creating a new server, and then creating the database. The main interface uses Silverlight, so unfortunately I wasn&#039;t able to use my [insert tablet here] (the future is here?). After the initial database &#039;server&#039; was setup, I switched over to SSMS to actually build out my data model.&lt;/p&gt;

&lt;div style=&quot;padding: .5em; margin: 1.5em .5em .5em 0px; color: #666666; font-size: .8em; text-align: center; position: relative;&quot;&gt;
   &lt;img src=&quot;http://tiernok.com/LTDBlog/azure/dbdiagram.jpg&quot; alt=&quot;DB Diagram&quot; /&gt;&lt;br /&gt;
   I Also Drew A Data Model...with Color (Oooo)
&lt;/div&gt;

&lt;p&gt;The first step was to add my database to SSMS. As I already have 2008 RS on my local machine, this was fairly straightforward. I clicked the &quot;Connect&quot; button in SSMS, entered the full server name from the Azure interface (right toolbar after creating &quot;Server&quot;) and the credentials I had created as part of setting up the database &quot;Server&quot;. Voila, it&#039;s that easy. The database server is connected, but instead of the usual yellow cylinder I get this nifty new &lt;img src=&quot;http://tiernok.com/LTDBlog/azure/database.jpg&quot; alt=&quot;Azure DB Icon&quot; /&gt; icon. &lt;/p&gt;

&lt;p&gt;Now that I was connected I right-clicked to wizard my way through database creation only to find myself staring at a new script window. Unfortunately the GUI capabilities for Azure databases are limited and many times in mousing my way around the GUI I found myself staring at unexpected script windows, which was a little disconcerting and showed me how dependent I had grown towards the interface.&lt;/p&gt;

&lt;p&gt;Creating my database, I found that nearly all of the work was done through scripts instead of the usual GUI windows. Attempting to create the database or tables in the GUI generated a script with a SQL snippet to help me out. Luckily I knew well enough to include indexes on each of my tables (Azure requires each table have a clustered index), so this part went smoothly. &lt;/p&gt;

&lt;p&gt;What I hadn&#039;t counted on was how ingrained the usage of the IDE had become. Twice I accidentally left off IDENTITY&#039;s that I had intended to write, and both times I found it easier to drop and recreate the table from scratch. Attempting to script the table from SSMS (Right click, Script Table As, aww no &quot;Alter&quot;, OK then &quot;Create&quot;) resulted in the usual CREATE TABLE command being scripted, including some options that aren&#039;t available in Azure. &lt;/p&gt;

&lt;p&gt;Despite some hiccups, however, I was able to build out my entire data model in an hour. Considering I was having to polish some rusty skills along the way and had never touched Azure before, this seems fair. &lt;/p&gt;

&lt;h2&gt;What&#039;s Next&lt;/h2&gt;
&lt;p&gt;Now that I have a database, I&#039;m tempted to use it. Continuing plans include building a sample application to talk to this database, playing with some fail over options, and generally trying out this whole SQL (and Windows) Azure technology. So far I&#039;ve found the technology to be remarkably easy to start using, lets see if that continues to be true.&lt;/p&gt;

&lt;p&gt;I suspect having missed the distributed computing course in college, this will all get much trickier at some random point in the future.&lt;/p&gt;

&lt;p&gt;And now go click that DeepFriedBytes link up top and listen to Buck, you&#039;ll thank me.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sqlazure-my-first-cloud&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>This past weekend I made my first foray into SQL Azure. SQL Azure has been on my list of "to learn" technologies and lately it made it closer to the top. While I had picked up some knowledge from random links in the twitter-sphere, I hadn't made a concerted effort to learn about it yet.</p>

<p>I started with a site named <a href="https://www.microsoftvirtualacademy.com/" title="Visit MVA">Microsoft Virtual Academy</a>. This site (despite some usability issues with the dialog boxes prior to signup) offers a number of virtual "courses" that consist of links to external resources grouped into modules, each of which finishes with a short assessment. As I started working my way through the modules for SQL Azure it so happened that Brian Harry (<a href="http://blogs.msdn.com/b/bharry/" title="Visit Brian Harry's blog">blog</a>) posted a link to a <a href="http://blogs.msdn.com/b/bharry/archive/2011/04/27/free-azure-trial.aspx" title="Read more on Brian's blog post">free Azure trial</a>, which was handy since I no longer have an MSDN account (MSDN accounts are also offered a free trial, check out your subscription page for more information). </p>

<p>On top of this information it happened that Buck Woody (<a href="http://buckwoody.com/" title="BuckWoody's blog">blog</a>|<a href="http://twitter.com/buckwoody" title="@BuckWoody on Twitter">twitter</a>) posted up <a href="http://blogs.msdn.com/b/buckwoody/archive/2010/12/13/windows-azure-learning-plan-sql-azure.aspx" title="Buck's SQL Azure List">an even more extensive list of information</a> on Azure technologies, so it looks like my to-learn list is going to be full for a while.</p>

<p>As if that flurry of links wasn't enough, <a href="http://deepfriedbytes.com/" title="Visit DeepFriedBytes">DeepFriedBytes</a> posted <a href="http://deepfriedbytes.com/podcast/episode-68-why-your-career-is-going-away-because-you-refuse-to-change-you-pansy/" title="DeepFriedBytes - Episode 68 - Why Your Career Is Going Away Because You Refuse To Change You Pansy">a podcast with Buck Woody</a> on the importance of cloud (ahem, distributed) technologies. If there is one link you follow-up on, make it that one.</p>

<h2>My First Azure Database</h2>
<p>I followed the instructions provided by the resources above to create my account, configure the firewall rules, and create my first database. But then I was stumped, what was I going to put in it?</p>

<p>It so happens that I've been working on an idea for a pet project for some time (well, one of many). In fact I had already used this idea to practice prototyping, requirements elicitation, and creation of an Agile-y product backlog. Some people knit, it never really caught on for me.</p>

<div style="padding: .5em; margin: 1.5em .5em .5em 0px; color: #666666; font-size: .8em; text-align: center; position: relative;">
   <img src="http://tiernok.com/LTDBlog/azure/prototype3of8.jpg" alt="Prototype image, 3 of 8" /><br />
   Prototype image 3 of 8
</div>

<p>Not only that, but the act of coming up with user stories (potential features) forced me to invent a common terminology and recognize which terms were coming up the most frequently.</p>

<div style=" padding: .5em; margin: 1.5em .5em .5em 0px; color: #666666; font-size: .8em; text-align: center; position: relative;">
   <img src="http://tiernok.com/LTDBlog/azure/features.jpg" alt="Prototype image, 3 of 8" /><br />
   70+ User Stories/Features
</div>


<p>So I might have enough information to put together a normalized database to support the core functionality, which is about as close as I am going to get to a realistic data model on a Sunday evening.</p>

<h2>Creating the Database</h2>
<p>Creating the first database was <a href="http://www.microsoft.com/showcase/en/US/details/1bd6bcb1-87c8-4f77-8425-2624a5f96976?WT.mc_id=otc-f-corp-jtc-DPR-MVA_INTROSQLAZURE" title="Quick video on initial setup">fairly straightforward (video)</a> and consisted of setting up my subscription, creating a new server, and then creating the database. The main interface uses Silverlight, so unfortunately I wasn't able to use my [insert tablet here] (the future is here?). After the initial database 'server' was setup, I switched over to SSMS to actually build out my data model.</p>

<div style="padding: .5em; margin: 1.5em .5em .5em 0px; color: #666666; font-size: .8em; text-align: center; position: relative;">
   <img src="http://tiernok.com/LTDBlog/azure/dbdiagram.jpg" alt="DB Diagram" /><br />
   I Also Drew A Data Model...with Color (Oooo)
</div>

<p>The first step was to add my database to SSMS. As I already have 2008 RS on my local machine, this was fairly straightforward. I clicked the "Connect" button in SSMS, entered the full server name from the Azure interface (right toolbar after creating "Server") and the credentials I had created as part of setting up the database "Server". Voila, it's that easy. The database server is connected, but instead of the usual yellow cylinder I get this nifty new <img src="http://tiernok.com/LTDBlog/azure/database.jpg" alt="Azure DB Icon" /> icon. </p>

<p>Now that I was connected I right-clicked to wizard my way through database creation only to find myself staring at a new script window. Unfortunately the GUI capabilities for Azure databases are limited and many times in mousing my way around the GUI I found myself staring at unexpected script windows, which was a little disconcerting and showed me how dependent I had grown towards the interface.</p>

<p>Creating my database, I found that nearly all of the work was done through scripts instead of the usual GUI windows. Attempting to create the database or tables in the GUI generated a script with a SQL snippet to help me out. Luckily I knew well enough to include indexes on each of my tables (Azure requires each table have a clustered index), so this part went smoothly. </p>

<p>What I hadn't counted on was how ingrained the usage of the IDE had become. Twice I accidentally left off IDENTITY's that I had intended to write, and both times I found it easier to drop and recreate the table from scratch. Attempting to script the table from SSMS (Right click, Script Table As, aww no "Alter", OK then "Create") resulted in the usual CREATE TABLE command being scripted, including some options that aren't available in Azure. </p>

<p>Despite some hiccups, however, I was able to build out my entire data model in an hour. Considering I was having to polish some rusty skills along the way and had never touched Azure before, this seems fair. </p>

<h2>What's Next</h2>
<p>Now that I have a database, I'm tempted to use it. Continuing plans include building a sample application to talk to this database, playing with some fail over options, and generally trying out this whole SQL (and Windows) Azure technology. So far I've found the technology to be remarkably easy to start using, lets see if that continues to be true.</p>

<p>I suspect having missed the distributed computing course in college, this will all get much trickier at some random point in the future.</p>

<p>And now go click that DeepFriedBytes link up top and listen to Buck, you'll thank me.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sqlazure-my-first-cloud">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sqlazure-my-first-cloud#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1237</wfw:commentRss>
		</item>
				<item>
			<title>SQL Saturday #46, Raleigh NC</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/sql-saturday-46-raleigh-nc</link>
			<pubDate>Wed, 22 Sep 2010 10:32:50 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="alt">Database Programming</category>
<category domain="main">Database Administration</category>			<guid isPermaLink="false">965@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;It&#039;s been a few days since SQL Saturday 46 and I have finally found some time to jot down my recap. This was the first Raleigh SQL Saturday and it lived up to all of my expectations.&lt;/p&gt;

&lt;h2&gt;The Event&lt;/h2&gt;
&lt;div style=&quot;float: left; margin: .5em 2em .5em 0px; color: #666666; font-size: 80%; text-align: center;&quot;&gt;
&lt;img src=&quot;http://tiernok.com/LTDBlog/sqlsat46_2010/volunteers.png&quot; alt=&quot;The volunteers that made it happen&quot; /&gt;&lt;br /&gt;
The excellent group of volunteers &lt;br /&gt;that pulled it all together
&lt;/div&gt;
&lt;p&gt;There were &lt;a href=&quot;http://www.sqlsaturday.com/46/schedule.aspx&quot; title=&quot;Check out the full schedule&quot;&gt;6 tracks of events&lt;/a&gt;, which were balanced more towards the BI and SSIS side of things than the previous events I&#039;ve attended. Registration was hiccup free (though I was early and missed the rush), rooms were all located in close proximity to one another, and the lunch room flowed pretty quickly despite an early line. The signs and schedule were excellent, with 8.5x11 placards posted for all of the sessions on each room&#039;s door as well as large grids posted on the wall in two locations, displaying the whole schedule. Once I figured out the room numbering, it was easy to locate the sessions or speakers I was interested in. The event was organized by members of &lt;a href=&quot;http://tripass.org&quot; title=&quot;Visit the TriPASS site&quot;&gt;TriPASS&lt;/a&gt;, the local SQL Server User Group in Raleigh, and I&#039;m looking forward to getting involved with the next one they put together. If I had to pick one element of the event that characterized it the most, that element would be how smoothly the execution went.&lt;/p&gt;

&lt;h2&gt;Sessions&lt;/h2&gt;
&lt;div style=&quot;float: right; margin: .5em 0 .5em 2em; color: #666666; font-size: 80%; text-align: center;&quot;&gt;
&lt;img src=&quot;http://tiernok.com/LTDBlog/sqlsat46_2010/grant.png&quot; alt=&quot;Grant Fritchey starting his session&quot; /&gt;&lt;br /&gt;
Grant Fritchey getting &lt;br /&gt; started on his session
&lt;/div&gt;

&lt;p&gt;I started my morning off with Geoff Hiten&#039;s (&lt;a href=&quot;http://weblogs.sqlteam.com/geoffh/default.aspx&quot; title=&quot;Geoff Hiten&#039;s blog&quot;&gt;blog&lt;/a&gt; | &lt;a href=&quot;http://twitter.com/sqlcraftsman&quot; title=&quot;SQLCraftsman on Twitter&quot;&gt;twitter&lt;/a&gt;) &quot;Clustering for Mere Mortals&quot; session, which gave me enough basic knowledge that there may be a clustering blog article in the near future. Geoff did an excellent job of communicating a complex task to his audience, balancing just enough background knowledge, direct examples, and good practices from his own experience.&lt;/p&gt;

&lt;p&gt;I expanded my CDC knowledge in Chris Skorlinski&#039;s (&lt;a href=&quot;http://blogs.msdn.com/b/repltalk/&quot; title=&quot;Visit Chris&#039;s blog&quot;&gt;blog&lt;/a&gt;) first session, but unfortunately wasn&#039;t able to make his SSIS + CDC session later in the afternoon (the disadvantage to being in my home town, had to run home to feed the cats). He provided an excellent overview of how CDC works, how to interact with it, and some of the internals behind the magic.&lt;/p&gt;

&lt;p&gt;After lunch, I attended Grant Fritchey&#039;s (&lt;a href=&quot;http://scarydba.wordpress.com/&quot; title=&quot;Visit Grant&#039;s blog&quot;&gt;blog&lt;/a&gt; | &lt;a href=&quot;http://twitter.com/GFritchey&quot; title=&quot;GFritchey on Twitter&quot;&gt;twitter&lt;/a&gt;) &quot;Identifying and Fixing Performance Problems using&quot; presentation and had a blast (I&#039;m using the SQL Saturday site-ized version of his presentation name). Despite some demo issues, Grant managed to balance delivery of his presentation with good audience feedback, even taking a 2 minute break in the middle to get everyone up on their feet and fight the post-lunch effects. &lt;/p&gt;

&lt;p&gt;After Grant&#039;s session I caught Andy Leonard&#039;s (&lt;a href=&quot;http://sqlblog.com/blogs/andy_leonard/&quot; title=&quot;Visit Andy&#039;s blog&quot;&gt;blog&lt;/a&gt; | &lt;a href=&quot;http://twitter.com/AndyLeonard&quot; title=&quot;AndyLeonard on Twitter&quot;&gt;twitter&lt;/a&gt;) &quot;SSIS Design Patterns&quot; session. I was completely out of my depth here, being far more of a beginner in SSIS (it doesn&#039;t get much more beginner), but I was still able to take away quite a bit of information from the session, including some ideas I will likely try to put into practice in an upcoming project.&lt;/p&gt;
&lt;div style=&quot;margin: .5em 2em .5em 0px; color: #666666; font-size: 80%; text-align: center;&quot;&gt;
&lt;img src=&quot;http://tiernok.com/LTDBlog/sqlsat46_2010/andyquote.png&quot; alt=&quot;Andy Leonard Quote&quot; /&gt;&lt;/div&gt;
 
&lt;h2&gt;The After-thing&lt;/h2&gt;
&lt;p&gt;I managed to run into quite a few of the local members of the user group, who did an excellent job preparing and executing this event. It seemed Janice Lee (&lt;a href=&quot;http://janiceclee.com/&quot; title=&quot;Visit Janice&#039;s blog&quot;&gt;blog&lt;/a&gt; | &lt;a href=&quot;http://twitter.com/JaniceLee&quot; title=&quot;JaniceLee on Twitter&quot;&gt;twitter&lt;/a&gt;) and I were constantly running into each other just long enough to share how great our last session was before heading off to two more completely different ones. It was one of these between-session breaks where I got to meet Eric Humphrey (&lt;a href=&quot;http://lotsahelp.blogspot.com/&quot; title=&quot;Visit Eric&#039;s blog&quot;&gt;blog&lt;/a&gt; | &lt;a href=&quot;http://twitter.com/lotsahelp&quot; title=&quot;Lotsahelp on Twitter&quot;&gt;twitter&lt;/a&gt;) for the first time, another familiar face from the SQL community on twitter. There were many others I met along the way and at least as many people that I intended to meet but missed (or who snuck out early). &lt;/p&gt;

&lt;p&gt;The day ended on a high note, around a table with Grant, Allen White (&lt;a href=&quot;http://sqlblog.com/blogs/allen_white/default.aspx&quot; title=&quot;Check out Allen&#039;s blog&quot;&gt;blog&lt;/a&gt;|&lt;a href=&quot;http://twitter.com/sqlrunr&quot; title=&quot;SQLRunr on Twitter&quot;&gt;twitter&lt;/a&gt;), Brent Kremer, and David Elvikis. Stories were traded, good food was eaten (not counting the fried pickles that showed up), and I think we all had a fun time. It was great to find out that Grant and Allen were just two more normal (well, close to normal) people like the rest of us. Somehow I managed to leave owing a round. So the next time we end up at the same event Grant, don&#039;t be surprised if a drink shows up &lt;img src=&quot;http://blogs.lessthandot.com/rsc/smilies/icon_biggrin.gif&quot; title=&quot;:D&quot; alt=&quot;:D&quot; class=&quot;middle&quot; width=&quot;15&quot; height=&quot;15&quot; /&gt;&lt;/p&gt;

&lt;div style=&quot;margin: .5em 2em .5em 0px; color: #666666; font-size: 80%; text-align: center;&quot;&gt;
&lt;img src=&quot;http://tiernok.com/LTDBlog/sqlsat46_2010/scotch.png&quot; alt=&quot;Glass of Scotch (The Macallen 12)&quot; /&gt;&lt;br /&gt;
&lt;/div&gt;

&lt;p&gt;Unfortunately I fell down on the job where pictures and tweets were concerned, so there aren&#039;t as many as there have been for past events. &lt;/p&gt;

&lt;p&gt;SQL Saturday 46 was an excellent event and ultimately both a lot of fun and a lot of information, what I have grown to expect from SQL Saturday events. The sessions were great, the event went smoothly, and the networking with the local and greater community was everything I had hoped for. I&#039;m already looking for to my next one.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/sql-saturday-46-raleigh-nc&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>It's been a few days since SQL Saturday 46 and I have finally found some time to jot down my recap. This was the first Raleigh SQL Saturday and it lived up to all of my expectations.</p>

<h2>The Event</h2>
<div style="float: left; margin: .5em 2em .5em 0px; color: #666666; font-size: 80%; text-align: center;">
<img src="http://tiernok.com/LTDBlog/sqlsat46_2010/volunteers.png" alt="The volunteers that made it happen" /><br />
The excellent group of volunteers <br />that pulled it all together
</div>
<p>There were <a href="http://www.sqlsaturday.com/46/schedule.aspx" title="Check out the full schedule">6 tracks of events</a>, which were balanced more towards the BI and SSIS side of things than the previous events I've attended. Registration was hiccup free (though I was early and missed the rush), rooms were all located in close proximity to one another, and the lunch room flowed pretty quickly despite an early line. The signs and schedule were excellent, with 8.5x11 placards posted for all of the sessions on each room's door as well as large grids posted on the wall in two locations, displaying the whole schedule. Once I figured out the room numbering, it was easy to locate the sessions or speakers I was interested in. The event was organized by members of <a href="http://tripass.org" title="Visit the TriPASS site">TriPASS</a>, the local SQL Server User Group in Raleigh, and I'm looking forward to getting involved with the next one they put together. If I had to pick one element of the event that characterized it the most, that element would be how smoothly the execution went.</p>

<h2>Sessions</h2>
<div style="float: right; margin: .5em 0 .5em 2em; color: #666666; font-size: 80%; text-align: center;">
<img src="http://tiernok.com/LTDBlog/sqlsat46_2010/grant.png" alt="Grant Fritchey starting his session" /><br />
Grant Fritchey getting <br /> started on his session
</div>

<p>I started my morning off with Geoff Hiten's (<a href="http://weblogs.sqlteam.com/geoffh/default.aspx" title="Geoff Hiten's blog">blog</a> | <a href="http://twitter.com/sqlcraftsman" title="SQLCraftsman on Twitter">twitter</a>) "Clustering for Mere Mortals" session, which gave me enough basic knowledge that there may be a clustering blog article in the near future. Geoff did an excellent job of communicating a complex task to his audience, balancing just enough background knowledge, direct examples, and good practices from his own experience.</p>

<p>I expanded my CDC knowledge in Chris Skorlinski's (<a href="http://blogs.msdn.com/b/repltalk/" title="Visit Chris's blog">blog</a>) first session, but unfortunately wasn't able to make his SSIS + CDC session later in the afternoon (the disadvantage to being in my home town, had to run home to feed the cats). He provided an excellent overview of how CDC works, how to interact with it, and some of the internals behind the magic.</p>

<p>After lunch, I attended Grant Fritchey's (<a href="http://scarydba.wordpress.com/" title="Visit Grant's blog">blog</a> | <a href="http://twitter.com/GFritchey" title="GFritchey on Twitter">twitter</a>) "Identifying and Fixing Performance Problems using" presentation and had a blast (I'm using the SQL Saturday site-ized version of his presentation name). Despite some demo issues, Grant managed to balance delivery of his presentation with good audience feedback, even taking a 2 minute break in the middle to get everyone up on their feet and fight the post-lunch effects. </p>

<p>After Grant's session I caught Andy Leonard's (<a href="http://sqlblog.com/blogs/andy_leonard/" title="Visit Andy's blog">blog</a> | <a href="http://twitter.com/AndyLeonard" title="AndyLeonard on Twitter">twitter</a>) "SSIS Design Patterns" session. I was completely out of my depth here, being far more of a beginner in SSIS (it doesn't get much more beginner), but I was still able to take away quite a bit of information from the session, including some ideas I will likely try to put into practice in an upcoming project.</p>
<div style="margin: .5em 2em .5em 0px; color: #666666; font-size: 80%; text-align: center;">
<img src="http://tiernok.com/LTDBlog/sqlsat46_2010/andyquote.png" alt="Andy Leonard Quote" /></div>
 
<h2>The After-thing</h2>
<p>I managed to run into quite a few of the local members of the user group, who did an excellent job preparing and executing this event. It seemed Janice Lee (<a href="http://janiceclee.com/" title="Visit Janice's blog">blog</a> | <a href="http://twitter.com/JaniceLee" title="JaniceLee on Twitter">twitter</a>) and I were constantly running into each other just long enough to share how great our last session was before heading off to two more completely different ones. It was one of these between-session breaks where I got to meet Eric Humphrey (<a href="http://lotsahelp.blogspot.com/" title="Visit Eric's blog">blog</a> | <a href="http://twitter.com/lotsahelp" title="Lotsahelp on Twitter">twitter</a>) for the first time, another familiar face from the SQL community on twitter. There were many others I met along the way and at least as many people that I intended to meet but missed (or who snuck out early). </p>

<p>The day ended on a high note, around a table with Grant, Allen White (<a href="http://sqlblog.com/blogs/allen_white/default.aspx" title="Check out Allen's blog">blog</a>|<a href="http://twitter.com/sqlrunr" title="SQLRunr on Twitter">twitter</a>), Brent Kremer, and David Elvikis. Stories were traded, good food was eaten (not counting the fried pickles that showed up), and I think we all had a fun time. It was great to find out that Grant and Allen were just two more normal (well, close to normal) people like the rest of us. Somehow I managed to leave owing a round. So the next time we end up at the same event Grant, don't be surprised if a drink shows up <img src="http://blogs.lessthandot.com/rsc/smilies/icon_biggrin.gif" title=":D" alt=":D" class="middle" width="15" height="15" /></p>

<div style="margin: .5em 2em .5em 0px; color: #666666; font-size: 80%; text-align: center;">
<img src="http://tiernok.com/LTDBlog/sqlsat46_2010/scotch.png" alt="Glass of Scotch (The Macallen 12)" /><br />
</div>

<p>Unfortunately I fell down on the job where pictures and tweets were concerned, so there aren't as many as there have been for past events. </p>

<p>SQL Saturday 46 was an excellent event and ultimately both a lot of fun and a lot of information, what I have grown to expect from SQL Saturday events. The sessions were great, the event went smoothly, and the networking with the local and greater community was everything I had hoped for. I'm already looking for to my next one.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/sql-saturday-46-raleigh-nc">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/sql-saturday-46-raleigh-nc#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=965</wfw:commentRss>
		</item>
				<item>
			<title>SQL Server Types - Numeric vs Int</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sql-server-types-numeric-vs-int</link>
			<pubDate>Thu, 02 Sep 2010 12:20:43 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">952@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Generally when we are defining tables, the more specific the column definition the better. Yesterday, however, I ran into a case where better definition actually has increased storage use for no appreciable benefit.&lt;/p&gt;

&lt;h2&gt;Integers - Using Numeric vs Int&lt;/h2&gt;
&lt;p&gt;As I was working on the database I came across a curious sight, multiple columns defined as numeric(7,0), numeric(9,0), and so on. It seemed like someone was trying to provide the database with the most specific definition possible for a number of different pieces of data. Having never run into this particular practice, I immediately started searching for a reason. Was it smaller? faster? better?&lt;/p&gt;

&lt;h3&gt;Storage&lt;/h3&gt;
&lt;p&gt;Using a very specific, well-defined numeric has actually cost us storage space, not reduced it.  A numeric (or decimal) with a precision value of 1 to 9 requires 5 bytes and with 10-19 requires 9 bytes. Compare this to the many varieties of int:&lt;/p&gt;

&lt;div class=&quot;tables&quot;&gt;
&lt;table&gt;
&lt;tr&gt;&lt;th&gt;Digits&lt;/th&gt;&lt;th&gt;Int Variety&lt;/th&gt;&lt;th&gt;Int Bytes&lt;/th&gt;&lt;th&gt;Numeric(*,0) Bytes&lt;/th&gt;&lt;th&gt;Difference&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1 - 2&lt;/td&gt;&lt;td&gt;tinyint&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;4 bytes&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3 - 4&lt;/td&gt;&lt;td&gt;smallint&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;3 bytes&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;5 - 9&lt;/td&gt;&lt;td&gt;int&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;1 byte&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;10 - 18&lt;/td&gt;&lt;td&gt;bigint&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;1 byte&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;So for every row and every index that includes this value, we lose storage space.&lt;/p&gt;

&lt;p&gt;References: &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms187745.aspx&quot; title=&quot;Int reference on MSDN&quot;&gt;Int reference on MSDN&lt;/a&gt; and &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms187746.aspx&quot; title=&quot;Numeric/Decimal reference on MSDN&quot;&gt;Numeric/Decimal reference on MSDN&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Performance #1&lt;/h3&gt;
&lt;p&gt;When SQL Server is asked to execute a math function (+,-,*,/), it uses a defined set of rules to determine the output type, then implicitly converts the arguments to that type (see &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms190476.aspx&quot; title=&quot;Precision, Scale, and Length article on MSDN&quot;&gt;this article&lt;/a&gt; for a subset that relates to decimals). This means that in many cases there could be implicit conversions to numeric from int, so it&#039;s possible someone believed we could try and tweak our performance by defining the field as numeric instead of an int. &lt;/p&gt;

&lt;p&gt;Let&#039;s test out implicit conversions:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb27765&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;/* ****** Creation of some number tables ****** */&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Create&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Table&lt;/span&gt; NumberIntTest&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Num &lt;span style=&quot;color: #0000FF;&quot;&gt;Int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Identity&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Primary&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Key&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;go&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Set&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;NOCOUNT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Begin&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Tran&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Into&lt;/span&gt; NumberIntTest &lt;span style=&quot;color: #0000FF;&quot;&gt;Default&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Values&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Into&lt;/span&gt; NumberIntTest &lt;span style=&quot;color: #0000FF;&quot;&gt;Default&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Values&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Into&lt;/span&gt; NumberIntTest &lt;span style=&quot;color: #0000FF;&quot;&gt;Default&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Values&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Into&lt;/span&gt; NumberIntTest &lt;span style=&quot;color: #0000FF;&quot;&gt;Default&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Values&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Into&lt;/span&gt; NumberIntTest &lt;span style=&quot;color: #0000FF;&quot;&gt;Default&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Values&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Into&lt;/span&gt; NumberIntTest &lt;span style=&quot;color: #0000FF;&quot;&gt;Default&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Values&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Into&lt;/span&gt; NumberIntTest &lt;span style=&quot;color: #0000FF;&quot;&gt;Default&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Values&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Into&lt;/span&gt; NumberIntTest &lt;span style=&quot;color: #0000FF;&quot;&gt;Default&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Values&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Into&lt;/span&gt; NumberIntTest &lt;span style=&quot;color: #0000FF;&quot;&gt;Default&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Values&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Into&lt;/span&gt; NumberIntTest &lt;span style=&quot;color: #0000FF;&quot;&gt;Default&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Values&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Commit&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Tran&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;go&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;100000&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Create&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Table&lt;/span&gt; NumberNumericTest&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Num &lt;span style=&quot;color: #0000FF;&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;7&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Primary&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Key&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Go&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Set&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;NOCOUNT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Begin&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Tran&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Into&lt;/span&gt; NumberNumericTest&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Num&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Select&lt;/span&gt; Num &lt;span style=&quot;color: #0000FF;&quot;&gt;From&lt;/span&gt; NumberIntTest &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; Num &amp;gt; &lt;span style=&quot;color: #000;&quot;&gt;10001&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Commit&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Tran&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Go&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;/* ****** Execute a variety of test scripts ****** */&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;Start&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DateTime&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @Garbage &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;, @Junk &lt;span style=&quot;color: #0000FF;&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;7&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;Int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;Int&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @Num &lt;span style=&quot;color: #0000FF;&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;7&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @Num = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- Divide an int by an int&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;start&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @Garbage = Num/@&lt;span style=&quot;color: #0000FF;&quot;&gt;Int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; NumberIntTest n;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Millisecond&lt;/span&gt;, @&lt;span style=&quot;color: #0000FF;&quot;&gt;Start&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GetDate&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- Divide a numeric(7,0) by an int&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;start&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @Junk = Num/@&lt;span style=&quot;color: #0000FF;&quot;&gt;Int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; NumberNumericTest n;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Millisecond&lt;/span&gt;, @&lt;span style=&quot;color: #0000FF;&quot;&gt;Start&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GetDate&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- Divide a numeric(7,0) by a numeric(7,0)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;start&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @Junk = Num/@Num &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; NumberNumericTest n;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Millisecond&lt;/span&gt;, @&lt;span style=&quot;color: #0000FF;&quot;&gt;Start&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GetDate&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- Divide an int by an int w/ explicit casting to numeric&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;start&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @Junk = Num/&lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;Int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;7&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; NumberIntTest n;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Millisecond&lt;/span&gt;, @&lt;span style=&quot;color: #0000FF;&quot;&gt;Start&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GetDate&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- Divide an numeric(7,0) by an int w/ explicit casting&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;start&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @Junk = Num/&lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;Int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;7&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; NumberNumericTest n;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DateDiff&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Millisecond&lt;/span&gt;, @&lt;span style=&quot;color: #0000FF;&quot;&gt;Start&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;GetDate&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb22780&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Initially I compared the execution plans and didn&#039;t see much difference, but after some modifications (thanks George!) and additions we can see the differences between a number of different situations.&lt;/p&gt;

&lt;p&gt;Sample Results:&lt;/p&gt;
&lt;div class=&quot;tables&quot;&gt;
&lt;table&gt;
&lt;tr&gt;&lt;th&gt;Test&lt;/th&gt;&lt;th&gt;time (ms)&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;int/int - No Cast&lt;/td&gt;&lt;td&gt;170&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;numeric/int - Implicit Cast&lt;/td&gt;&lt;td&gt;313&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;numeric/numeric - Implicit Cast&lt;/td&gt;&lt;td&gt;296&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;int/CAST(int as numeric) - Implicit Cast&lt;/td&gt;&lt;td&gt;320&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;numeric/CAST(int as numeric)&lt;/td&gt;&lt;td&gt;290&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;In the second test&#039;s plan we can see an example of that implicit cast:&lt;/p&gt;
&lt;pre&gt;
numeric/int w/ Implicit Cast:

  |--Compute Scalar(DEFINE:([Expr1002]=CONVERT_IMPLICIT(numeric(7,0),[utils].[dbo].[NumberNumericTest].[Num] as [n].[Num]/CONVERT_IMPLICIT(numeric(10,0),[@Int],0),0)))
       |--Clustered Index Scan(OBJECT:([utils].[dbo].[NumberNumericTest].[PK__NumberNu__C7D08B630AD2A005] AS [n]))
&lt;/pre&gt;

&lt;p&gt;So if we did have an integer that we need to operate on with a float value (the last case), the addition of a simple cast on the integer argument will bring the execution performance in line with having two numerics, meaning there is no gain in storing the value as a numeric(7,0).&lt;/p&gt;

&lt;h3&gt;Performance #2&lt;/h3&gt;
&lt;p&gt;The other potential performance impact is with auto-parameterization. Auto-parameterization occurs when you provide SQL Server with a non-parameterized SQL statement. The server determines a type for those parameters and parameterizes them (part of the magic that makes plans reusable for non-parameterized queries). I couldn&#039;t find anything terribly recent, but as far back as SQL Server 6.5 and 7.0 the engine was documented as using the int type for any non-decimal value of 9 digits or less. This means that in the unlikely situation that you&#039;re executing inline, non-parameterized SQL statements and have used numeric(*,0) types in your table definitions, you will actually be taking a performance hit for the implicit conversion from auto-parameterized integer to the numeric(*,0) field.&lt;/p&gt;

&lt;p&gt;And if that wasn&#039;t bad enough, the same SQL Server documentation says that SQL Server treats integers as more exact than numeric and decimal types. It doesn&#039;t specify why the document goes out of its way to share this information with us, but generally when someone goes out of their way to point out something like this in a document, I get a little nervous and tend to focus more heavily on their &#039;recommended&#039; practice (use int).&lt;/p&gt;

&lt;p&gt;More information on &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms186219.aspx&quot; title=&quot;MSDN article on Simple Parameterization&quot;&gt;Parameterization&lt;/a&gt; and &lt;a href=&quot;http://support.microsoft.com/kb/198625&quot; title=&quot;KB article on SQL 7 Comparison Optimization&quot;&gt;SQL 7 Comparison Optimization&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Wrap-up&lt;/h2&gt;
&lt;p&gt;So at the end of the day, using a numeric(*,0) requires more space, provides no appreciable benefit over explicit casting, and can actually harm you if you are executing non-parameterized SQL statements against your server.&lt;/p&gt;

&lt;p&gt;There are two options for finding these columns, using a SQL query like the one below or &lt;a href=&quot;http://sqlcop.lessthandot.com/&quot; title=&quot;Download the SQLCop application&quot;&gt;downloading SQLCop&lt;/a&gt; to check for this and many other common situations.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb32390&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, NUMERIC_PRECISION, NUMERIC_SCALE&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &lt;span style=&quot;color: #00AF00;&quot;&gt;INFORMATION_SCHEMA&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;COLUMNS&lt;/span&gt; C&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; C.&lt;span style=&quot;color: #202020;&quot;&gt;DATA_TYPE&lt;/span&gt; IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;numeric&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;decimal&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; AND NUMERIC_SCALE = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt; AND NUMERIC_PRECISION &amp;lt;= &lt;span style=&quot;color: #000;&quot;&gt;18&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb29762&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sql-server-types-numeric-vs-int&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Generally when we are defining tables, the more specific the column definition the better. Yesterday, however, I ran into a case where better definition actually has increased storage use for no appreciable benefit.</p>

<h2>Integers - Using Numeric vs Int</h2>
<p>As I was working on the database I came across a curious sight, multiple columns defined as numeric(7,0), numeric(9,0), and so on. It seemed like someone was trying to provide the database with the most specific definition possible for a number of different pieces of data. Having never run into this particular practice, I immediately started searching for a reason. Was it smaller? faster? better?</p>

<h3>Storage</h3>
<p>Using a very specific, well-defined numeric has actually cost us storage space, not reduced it.  A numeric (or decimal) with a precision value of 1 to 9 requires 5 bytes and with 10-19 requires 9 bytes. Compare this to the many varieties of int:</p>

<div class="tables">
<table>
<tr><th>Digits</th><th>Int Variety</th><th>Int Bytes</th><th>Numeric(*,0) Bytes</th><th>Difference</th></tr>
<tr><td>1 - 2</td><td>tinyint</td><td>1</td><td>5</td><td>4 bytes</td></tr>
<tr><td>3 - 4</td><td>smallint</td><td>2</td><td>5</td><td>3 bytes</td></tr>
<tr><td>5 - 9</td><td>int</td><td>4</td><td>5</td><td>1 byte</td></tr>
<tr><td>10 - 18</td><td>bigint</td><td>8</td><td>9</td><td>1 byte</td></tr>
</table>
</div>

<p>So for every row and every index that includes this value, we lose storage space.</p>

<p>References: <a href="http://msdn.microsoft.com/en-us/library/ms187745.aspx" title="Int reference on MSDN">Int reference on MSDN</a> and <a href="http://msdn.microsoft.com/en-us/library/ms187746.aspx" title="Numeric/Decimal reference on MSDN">Numeric/Decimal reference on MSDN</a></p>

<h3>Performance #1</h3>
<p>When SQL Server is asked to execute a math function (+,-,*,/), it uses a defined set of rules to determine the output type, then implicitly converts the arguments to that type (see <a href="http://msdn.microsoft.com/en-us/library/ms190476.aspx" title="Precision, Scale, and Length article on MSDN">this article</a> for a subset that relates to decimals). This means that in many cases there could be implicit conversions to numeric from int, so it's possible someone believed we could try and tweak our performance by defining the field as numeric instead of an int. </p>

<p>Let's test out implicit conversions:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb94615'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb94615','cb46643'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb94615" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #00AF00;">/* ****** Creation of some number tables ****** */</span></li><li style="" class="li2"><span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> NumberIntTest<span style="color: #808080;">&#40;</span>Num <span style="color: #0000FF;">Int</span> <span style="color: #0000FF;">Identity</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">Primary</span> <span style="color: #0000FF;">Key</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">go</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span></li><li style="" class="li2"><span style="color: #0000FF;">Begin</span> <span style="color: #0000FF;">Tran</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> NumberIntTest <span style="color: #0000FF;">Default</span> <span style="color: #0000FF;">Values</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> NumberIntTest <span style="color: #0000FF;">Default</span> <span style="color: #0000FF;">Values</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> NumberIntTest <span style="color: #0000FF;">Default</span> <span style="color: #0000FF;">Values</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> NumberIntTest <span style="color: #0000FF;">Default</span> <span style="color: #0000FF;">Values</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> NumberIntTest <span style="color: #0000FF;">Default</span> <span style="color: #0000FF;">Values</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> NumberIntTest <span style="color: #0000FF;">Default</span> <span style="color: #0000FF;">Values</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> NumberIntTest <span style="color: #0000FF;">Default</span> <span style="color: #0000FF;">Values</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> NumberIntTest <span style="color: #0000FF;">Default</span> <span style="color: #0000FF;">Values</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> NumberIntTest <span style="color: #0000FF;">Default</span> <span style="color: #0000FF;">Values</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> NumberIntTest <span style="color: #0000FF;">Default</span> <span style="color: #0000FF;">Values</span></li><li style="" class="li1"><span style="color: #0000FF;">Commit</span> <span style="color: #0000FF;">Tran</span></li><li style="" class="li2"><span style="color: #0000FF;">go</span> <span style="color: #000;">100000</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> NumberNumericTest<span style="color: #808080;">&#40;</span>Num <span style="color: #0000FF;">numeric</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">Primary</span> <span style="color: #0000FF;">Key</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">Go</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span></li><li style="" class="li2"><span style="color: #0000FF;">Begin</span> <span style="color: #0000FF;">Tran</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> NumberNumericTest<span style="color: #808080;">&#40;</span>Num<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">Select</span> Num <span style="color: #0000FF;">From</span> NumberIntTest <span style="color: #0000FF;">WHERE</span> Num &gt; <span style="color: #000;">10001</span></li><li style="" class="li2"><span style="color: #0000FF;">Commit</span> <span style="color: #0000FF;">Tran</span></li><li style="" class="li1"><span style="color: #0000FF;">Go</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">/* ****** Execute a variety of test scripts ****** */</span></li><li style="" class="li2"><span style="color: #0000FF;">DECLARE</span> @<span style="color: #0000FF;">Start</span> <span style="color: #0000FF;">DateTime</span>;</li><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @Garbage <span style="color: #0000FF;">int</span>, @Junk <span style="color: #0000FF;">numeric</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @<span style="color: #0000FF;">Int</span> <span style="color: #0000FF;">int</span>; <span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">Int</span> = <span style="color: #000;">1</span>;</li><li style="" class="li2"><span style="color: #0000FF;">DECLARE</span> @Num <span style="color: #0000FF;">numeric</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>; <span style="color: #0000FF;">SET</span> @Num = <span style="color: #000;">1</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">-- Divide an int by an int</span></li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">start</span> = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @Garbage = Num/@<span style="color: #0000FF;">Int</span> <span style="color: #0000FF;">FROM</span> NumberIntTest n;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Millisecond</span>, @<span style="color: #0000FF;">Start</span>, <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">-- Divide a numeric(7,0) by an int</span></li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">start</span> = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> @Junk = Num/@<span style="color: #0000FF;">Int</span> <span style="color: #0000FF;">FROM</span> NumberNumericTest n;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Millisecond</span>, @<span style="color: #0000FF;">Start</span>, <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">-- Divide a numeric(7,0) by a numeric(7,0)</span></li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">start</span> = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @Junk = Num/@Num <span style="color: #0000FF;">FROM</span> NumberNumericTest n;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Millisecond</span>, @<span style="color: #0000FF;">Start</span>, <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">-- Divide an int by an int w/ explicit casting to numeric</span></li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">start</span> = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> @Junk = Num/<span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">Int</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">numeric</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> NumberIntTest n;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Millisecond</span>, @<span style="color: #0000FF;">Start</span>, <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">-- Divide an numeric(7,0) by an int w/ explicit casting</span></li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">start</span> = <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @Junk = Num/<span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">Int</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">numeric</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> NumberNumericTest n;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">DateDiff</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Millisecond</span>, @<span style="color: #0000FF;">Start</span>, <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>;</li></ol></div><div id="cb46643" style="display: none; color: red;"></div></div></div>

<p>Initially I compared the execution plans and didn't see much difference, but after some modifications (thanks George!) and additions we can see the differences between a number of different situations.</p>

<p>Sample Results:</p>
<div class="tables">
<table>
<tr><th>Test</th><th>time (ms)</th></tr>
<tr><td>int/int - No Cast</td><td>170</td></tr>
<tr><td>numeric/int - Implicit Cast</td><td>313</td></tr>
<tr><td>numeric/numeric - Implicit Cast</td><td>296</td></tr>
<tr><td>int/CAST(int as numeric) - Implicit Cast</td><td>320</td></tr>
<tr><td>numeric/CAST(int as numeric)</td><td>290</td></tr>
</table>
</div>

<p>In the second test's plan we can see an example of that implicit cast:</p>
<pre>
numeric/int w/ Implicit Cast:

  |--Compute Scalar(DEFINE:([Expr1002]=CONVERT_IMPLICIT(numeric(7,0),[utils].[dbo].[NumberNumericTest].[Num] as [n].[Num]/CONVERT_IMPLICIT(numeric(10,0),[@Int],0),0)))
       |--Clustered Index Scan(OBJECT:([utils].[dbo].[NumberNumericTest].[PK__NumberNu__C7D08B630AD2A005] AS [n]))
</pre>

<p>So if we did have an integer that we need to operate on with a float value (the last case), the addition of a simple cast on the integer argument will bring the execution performance in line with having two numerics, meaning there is no gain in storing the value as a numeric(7,0).</p>

<h3>Performance #2</h3>
<p>The other potential performance impact is with auto-parameterization. Auto-parameterization occurs when you provide SQL Server with a non-parameterized SQL statement. The server determines a type for those parameters and parameterizes them (part of the magic that makes plans reusable for non-parameterized queries). I couldn't find anything terribly recent, but as far back as SQL Server 6.5 and 7.0 the engine was documented as using the int type for any non-decimal value of 9 digits or less. This means that in the unlikely situation that you're executing inline, non-parameterized SQL statements and have used numeric(*,0) types in your table definitions, you will actually be taking a performance hit for the implicit conversion from auto-parameterized integer to the numeric(*,0) field.</p>

<p>And if that wasn't bad enough, the same SQL Server documentation says that SQL Server treats integers as more exact than numeric and decimal types. It doesn't specify why the document goes out of its way to share this information with us, but generally when someone goes out of their way to point out something like this in a document, I get a little nervous and tend to focus more heavily on their 'recommended' practice (use int).</p>

<p>More information on <a href="http://msdn.microsoft.com/en-us/library/ms186219.aspx" title="MSDN article on Simple Parameterization">Parameterization</a> and <a href="http://support.microsoft.com/kb/198625" title="KB article on SQL 7 Comparison Optimization">SQL 7 Comparison Optimization</a></p>

<h2>The Wrap-up</h2>
<p>So at the end of the day, using a numeric(*,0) requires more space, provides no appreciable benefit over explicit casting, and can actually harm you if you are executing non-parameterized SQL statements against your server.</p>

<p>There are two options for finding these columns, using a SQL query like the one below or <a href="http://sqlcop.lessthandot.com/" title="Download the SQLCop application">downloading SQLCop</a> to check for this and many other common situations.</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb87015'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb87015','cb73820'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb87015" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, NUMERIC_PRECISION, NUMERIC_SCALE</li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> <span style="color: #00AF00;">INFORMATION_SCHEMA</span>.<span style="color: #00AF00;">COLUMNS</span> C</li><li style="" class="li1"><span style="color: #0000FF;">WHERE</span> C.<span style="color: #202020;">DATA_TYPE</span> IN <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'numeric'</span>,<span style="color: #FF0000;">'decimal'</span><span style="color: #808080;">&#41;</span> AND NUMERIC_SCALE = <span style="color: #000;">0</span> AND NUMERIC_PRECISION &lt;= <span style="color: #000;">18</span></li></ol></div><div id="cb73820" style="display: none; color: red;"></div></div></div><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sql-server-types-numeric-vs-int">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sql-server-types-numeric-vs-int#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=952</wfw:commentRss>
		</item>
				<item>
			<title>SQL Saturday 28, Baton Rouge</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/sql-saturday-28-baton-rouge</link>
			<pubDate>Mon, 16 Aug 2010 09:47:36 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="alt">Data Modelling &amp; Design</category>
<category domain="alt">Database Programming</category>
<category domain="main">Database Administration</category>			<guid isPermaLink="false">933@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;I&#039;m in the air flying back from SQL Saturday 28 and wanted to capture my thoughts while the event was still fresh. Overall I would have to rate it as a resounding success. Last year the event attracted 200 professionals from Baton Rouge and the surrounding area, this year attendance doubled to 400, with additional interest from about 200 more that weren&#039;t able to make it. &lt;/p&gt;

&lt;div style=&quot;float: left; margin: .5em 2em .5em 0px; color: #666666; font-size: 80%; text-align: center;&quot;&gt;
&lt;a href=&quot;http://yfrog.com/jd1bej&quot; title=&quot;See the fullsize picture&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/sqlsat28_2010/patrick_leblanc.png&quot; alt=&quot;Patrick LeBlanc (@patrickdba) speaking Friday night&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
Patrick LeBlanc (&lt;a href=&quot;http://sqldownsouth.blogspot.com/&quot; title=&quot;Patrick LeBlanc&#039;s Blog&quot;&gt;blog&lt;/a&gt;|&lt;a href=&quot;http://twitter.com/patrickdba&quot; title=&quot;Patrick LeBlanc (@patrickdba) on twitter&quot;&gt;twitter&lt;/a&gt;)&lt;br /&gt; Speaking Friday night
&lt;/div&gt;
&lt;p&gt;The event featured &lt;a href=&quot;http://www.sqlsaturday.com/28/schedule.aspx&quot; title=&quot;Check out the full schedule&quot;&gt;8 tracks&lt;/a&gt;, including SQL and .Net sessions. Several early-bird sessions kicked off around 7:30AM - I have to applaud those brave enough to speak in front of a crowd that early, as I was still getting appropriately caffeinated.  In addition, INETA made it possible for Keith Elder (&lt;a href=&quot;http://keithelder.net/blog/&quot; title=&quot;Keith Elder&#039;s blog&quot;&gt;blog&lt;/a&gt;|&lt;a href=&quot;http://twitter.com/keithelder&quot; title=&quot;@KeithElder on twitter&quot;&gt;twitter&lt;/a&gt;) to provide a full day&#039;s &quot;.Net From Scratch&quot; training session. Steve Jones (&lt;a href=&quot;http://www.sqlservercentral.com/blogs/steve_jones/default.aspx&quot; title=&quot;Steve Jones on SQL Server Central&quot;&gt;blog&lt;/a&gt;|&lt;a href=&quot;http://twitter.com/way0utwest&quot; title=&quot;Steve Jones (@way0utwest) on twitter&quot;&gt;twitter&lt;/a&gt;) delivered the keynote, starting the day off with commentary on professional development, technical skill, and the importance of enjoying our jobs and finding balance in life. &lt;/p&gt;

&lt;p&gt;The dinner the night before was in a large enough venue that we were able to mingle and talk a good bit before the dinner appeared, and the food was both good and plentiful (which worked out well for those of us who had been eating airport fare). Patrick LeBlanc (&lt;a href=&quot;http://sqldownsouth.blogspot.com/&quot; title=&quot;Patrick LeBlanc&#039;s Blog&quot;&gt;blog&lt;/a&gt;|&lt;a href=&quot;http://twitter.com/patrickdba&quot; title=&quot;Patrick LeBlanc (@patrickdba) on twitter&quot;&gt;twitter&lt;/a&gt;) provided our mid-evening entertainment, welcoming everyone to the area and introducing us to the team that had worked so hard to put the event together. Somehow I managed to end up at the &lt;a href=&quot;http://www.sparkhound.com/&quot; title=&quot;Check out the Sparkhound website&quot;&gt;Sparkhound&lt;/a&gt; table, but they&#039;re a friendly group and it made for a fun dinner.&lt;/p&gt;

&lt;div style=&quot;float: right; margin: 2em; color: #666666; font-size: 80%; text-align: center;&quot;&gt;
&lt;a href=&quot;http://yfrog.com/83a1rxj&quot; title=&quot;See the fullsize picture&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/sqlsat28_2010/chris_eargle.png&quot; alt=&quot;Chris Eargle (@kodefuguru) documenting his audience&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
Chris Eargle (&lt;a href=&quot;http://www.kodefuguru.com/&quot; title=&quot;Chris Eargle&#039;s blog&quot;&gt;blog&lt;/a&gt;|&lt;a href=&quot;http://twitter.com/KodefuGuru&quot; title=&quot;Chris Eargle (@kodefuguru) on twitter&quot;&gt;twitter&lt;/a&gt;)&lt;br /&gt; Documenting his Audience
&lt;/div&gt;
&lt;p&gt;The event took place at LSU in a lecture hall, which meant we had classrooms with integrated power and overheads, simplifying some of the setup. A full size classroom was set aside for the speakers and featured plenty of space for us to get off our feet, plug in, or just get a snack throughout the day.&lt;/p&gt;

&lt;p&gt;My session was right after lunch (see the &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/creating-a-conceptual-data-model&quot; title=&quot;Check out my pre-session post&quot;&gt;Conceptual Data Modeling&lt;/a&gt; post from last week). Despite speaking with no code examples in an after lunch session, I still managed to draw a number of inquisitive people. Despite ending a few minutes early, we still managed to almost run over due to the great conversation we had at the end. Thomas LeBlanc (&lt;a href=&quot;http://thesmilingdba.blogspot.com/&quot; title=&quot;Thomas LeBlanc&#039;s blog&quot;&gt;blog&lt;/a&gt;|&lt;a href=&quot;http://twitter.com/TheSmilingDBA&quot; title=&quot;Thomas LeBlanc (@TheSmilingDBA) on twitter&quot;&gt;twitter&lt;/a&gt;) presented on normalization and data modeling after my session and I stuck around to watch his presentation as well, picking up some ideas for the next version of my presentation.&lt;/p&gt;

&lt;p&gt;Of the three SQL Saturday events I attended this year, this one was probably the best represented on twitter during the day. William Assaf (&lt;a href=&quot;http://sqltact.blogspot.com/&quot; title=&quot;William Assaf&#039;s blog&quot;&gt;blog&lt;/a&gt;|&lt;a href=&quot;http://twitter.com/william_a_dba&quot; title=&quot;William Assaf (@william_a_dba) on twitter&quot;&gt;twitter&lt;/a&gt;) kept the media flowing, constantly uploading pictures and commentary throughout the day and keeping the &lt;a href=&quot;http://twitter.com/SQLSatBR&quot; title=&quot;[Sorta] Official SQLSatBR Twitter account&quot;&gt;@SQLSatBR&lt;/a&gt; account busy.  &lt;/p&gt;

&lt;div style=&quot;float: left; margin: .5em 2em .5em 0em; color: #666666; font-size: 80%; text-align: center;&quot;&gt;
&lt;a href=&quot;http://yfrog.com/mcg1sp&quot; title=&quot;See the fullsize picture&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/sqlsat28_2010/sqlsatteam.png&quot; alt=&quot;A great team of people&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
A great team of people (click for larger)
&lt;/div&gt;
&lt;p&gt;As is always the case with the events, the best part was the people. I met a number of new people, at varying places in their respective careers, and had interesting conversations that lasted until the restaurant hosting the after party finally asked the last four of us to leave. I wasn&#039;t able to work everyone&#039;s names into the post, but it was a great group and there a lot of names both familiar and new. From what I hear, the planning for next year is already in progress, so maybe I&#039;ll see you there.&lt;/p&gt;

&lt;p&gt;For more pictures, check out &lt;a href=&quot;http://picasaweb.google.com/willassaf/SQLSaturday28?authkey=Gv1sRgCIDCruLCuNDy4wE&amp;amp;feat=directlink#&quot; title=&quot;William A&#039;s full Picasa Gallery&quot;&gt;William&#039;s full gallery&lt;/a&gt; on Picasa.&lt;br /&gt;
&lt;br style=&quot;clear: left;&quot; /&gt;&lt;br /&gt;
And the slide decks for my session &lt;a href=&quot;http://tiernok.com/presentation.php&quot; title=&quot;Eli&#039;s slide decks&quot;&gt;are posted now&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/sql-saturday-28-baton-rouge&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>I'm in the air flying back from SQL Saturday 28 and wanted to capture my thoughts while the event was still fresh. Overall I would have to rate it as a resounding success. Last year the event attracted 200 professionals from Baton Rouge and the surrounding area, this year attendance doubled to 400, with additional interest from about 200 more that weren't able to make it. </p>

<div style="float: left; margin: .5em 2em .5em 0px; color: #666666; font-size: 80%; text-align: center;">
<a href="http://yfrog.com/jd1bej" title="See the fullsize picture"><img src="http://tiernok.com/LTDBlog/sqlsat28_2010/patrick_leblanc.png" alt="Patrick LeBlanc (@patrickdba) speaking Friday night" /></a><br />
Patrick LeBlanc (<a href="http://sqldownsouth.blogspot.com/" title="Patrick LeBlanc's Blog">blog</a>|<a href="http://twitter.com/patrickdba" title="Patrick LeBlanc (@patrickdba) on twitter">twitter</a>)<br /> Speaking Friday night
</div>
<p>The event featured <a href="http://www.sqlsaturday.com/28/schedule.aspx" title="Check out the full schedule">8 tracks</a>, including SQL and .Net sessions. Several early-bird sessions kicked off around 7:30AM - I have to applaud those brave enough to speak in front of a crowd that early, as I was still getting appropriately caffeinated.  In addition, INETA made it possible for Keith Elder (<a href="http://keithelder.net/blog/" title="Keith Elder's blog">blog</a>|<a href="http://twitter.com/keithelder" title="@KeithElder on twitter">twitter</a>) to provide a full day's ".Net From Scratch" training session. Steve Jones (<a href="http://www.sqlservercentral.com/blogs/steve_jones/default.aspx" title="Steve Jones on SQL Server Central">blog</a>|<a href="http://twitter.com/way0utwest" title="Steve Jones (@way0utwest) on twitter">twitter</a>) delivered the keynote, starting the day off with commentary on professional development, technical skill, and the importance of enjoying our jobs and finding balance in life. </p>

<p>The dinner the night before was in a large enough venue that we were able to mingle and talk a good bit before the dinner appeared, and the food was both good and plentiful (which worked out well for those of us who had been eating airport fare). Patrick LeBlanc (<a href="http://sqldownsouth.blogspot.com/" title="Patrick LeBlanc's Blog">blog</a>|<a href="http://twitter.com/patrickdba" title="Patrick LeBlanc (@patrickdba) on twitter">twitter</a>) provided our mid-evening entertainment, welcoming everyone to the area and introducing us to the team that had worked so hard to put the event together. Somehow I managed to end up at the <a href="http://www.sparkhound.com/" title="Check out the Sparkhound website">Sparkhound</a> table, but they're a friendly group and it made for a fun dinner.</p>

<div style="float: right; margin: 2em; color: #666666; font-size: 80%; text-align: center;">
<a href="http://yfrog.com/83a1rxj" title="See the fullsize picture"><img src="http://tiernok.com/LTDBlog/sqlsat28_2010/chris_eargle.png" alt="Chris Eargle (@kodefuguru) documenting his audience" /></a><br />
Chris Eargle (<a href="http://www.kodefuguru.com/" title="Chris Eargle's blog">blog</a>|<a href="http://twitter.com/KodefuGuru" title="Chris Eargle (@kodefuguru) on twitter">twitter</a>)<br /> Documenting his Audience
</div>
<p>The event took place at LSU in a lecture hall, which meant we had classrooms with integrated power and overheads, simplifying some of the setup. A full size classroom was set aside for the speakers and featured plenty of space for us to get off our feet, plug in, or just get a snack throughout the day.</p>

<p>My session was right after lunch (see the <a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/creating-a-conceptual-data-model" title="Check out my pre-session post">Conceptual Data Modeling</a> post from last week). Despite speaking with no code examples in an after lunch session, I still managed to draw a number of inquisitive people. Despite ending a few minutes early, we still managed to almost run over due to the great conversation we had at the end. Thomas LeBlanc (<a href="http://thesmilingdba.blogspot.com/" title="Thomas LeBlanc's blog">blog</a>|<a href="http://twitter.com/TheSmilingDBA" title="Thomas LeBlanc (@TheSmilingDBA) on twitter">twitter</a>) presented on normalization and data modeling after my session and I stuck around to watch his presentation as well, picking up some ideas for the next version of my presentation.</p>

<p>Of the three SQL Saturday events I attended this year, this one was probably the best represented on twitter during the day. William Assaf (<a href="http://sqltact.blogspot.com/" title="William Assaf's blog">blog</a>|<a href="http://twitter.com/william_a_dba" title="William Assaf (@william_a_dba) on twitter">twitter</a>) kept the media flowing, constantly uploading pictures and commentary throughout the day and keeping the <a href="http://twitter.com/SQLSatBR" title="[Sorta] Official SQLSatBR Twitter account">@SQLSatBR</a> account busy.  </p>

<div style="float: left; margin: .5em 2em .5em 0em; color: #666666; font-size: 80%; text-align: center;">
<a href="http://yfrog.com/mcg1sp" title="See the fullsize picture"><img src="http://tiernok.com/LTDBlog/sqlsat28_2010/sqlsatteam.png" alt="A great team of people" /></a><br />
A great team of people (click for larger)
</div>
<p>As is always the case with the events, the best part was the people. I met a number of new people, at varying places in their respective careers, and had interesting conversations that lasted until the restaurant hosting the after party finally asked the last four of us to leave. I wasn't able to work everyone's names into the post, but it was a great group and there a lot of names both familiar and new. From what I hear, the planning for next year is already in progress, so maybe I'll see you there.</p>

<p>For more pictures, check out <a href="http://picasaweb.google.com/willassaf/SQLSaturday28?authkey=Gv1sRgCIDCruLCuNDy4wE&amp;feat=directlink#" title="William A's full Picasa Gallery">William's full gallery</a> on Picasa.<br />
<br style="clear: left;" /><br />
And the slide decks for my session <a href="http://tiernok.com/presentation.php" title="Eli's slide decks">are posted now</a>.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/sql-saturday-28-baton-rouge">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/sql-saturday-28-baton-rouge#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=933</wfw:commentRss>
		</item>
				<item>
			<title>Creating a Conceptual Data Model</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/creating-a-conceptual-data-model</link>
			<pubDate>Tue, 10 Aug 2010 11:20:20 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">925@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;There are three types of data models: Conceptual, Logical, and Physical. The conceptual model provides a high-level view of the data, defining the general entities and entity relationships using the language of the business or organization. The logical model adds attributes to these entities, providing a technology-agnostic foundation for a database design. The physical model assigns table names, column names, and data types to the entities and attributes defined in the prior models. Defining the data model in distinct layers helps us manage the complexity of design and focus as we refine our understanding of the problem space.&lt;/p&gt;

&lt;div style=&quot;background-color: #ffffcc; border: 2px solid #cccccc; padding: .5em; margin: .5em .5em 1em .5em;&quot;&gt;This topic is a sub-set of my presentation later this week at Baton Rouge, &lt;a href=&quot;http://www.sqlsaturday.com/28/eventhome.aspx&quot;&gt;SQL Saturday #28&lt;/a&gt;. If you are planning on being in the area I urge you to sign up, there are going to be a wide range of speakers presenting on SQL and .Net development topics and so far it sounds like we have over 500 attendees (read: networking opportunities). My slides are posted up on &lt;a href=&quot;http://tiernok.com/presentation.php&quot;&gt;my website&lt;/a&gt;.&lt;/div&gt;

&lt;h2&gt;Building the Conceptual Model&lt;/h2&gt;
&lt;p&gt;My approach is to start with the business drivers for the data and progress through external materials, technical inputs, and an analysis phase. Whether we are in a university environment, software development house, manufacturing company or non-profit organization, there is generally one key business driver. That driver has a business owner, technical or non-technical, and it is there that I prefer to start. Conversations with the business owner will often include less than specific terms and examples of competing services or existing data stores (think excel and MS Access) in the organization. Factors like maintainability, reliability, scalability, and so on will be mentioned in some cases, so we will want to document these if they come up. This provides the raw materials we need to build our data model.&lt;/p&gt;


&lt;h3&gt;What is the purpose of [fill in the blank]?&lt;/h3&gt;
&lt;div style=&quot;text-align: center; color: #666666; float: right; margin: 1em;&quot;&gt;&lt;a href=&quot;http://picasaweb.google.com/lh/photo/-0iJqUji9TzP6kINPqkyqw&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/conceptual/dogcatdinner.jpg&quot; alt=&quot;Context and expectations are key&quot; title=&quot;Context and expectations are key&quot; style=&quot;width: 260px&quot; /&gt;&lt;/a&gt;&lt;br /&gt;Context and expectations are key&lt;br /&gt;Unnamed Picture on &lt;a href=&quot;http://picasaweb.google.com/lh/photo/-0iJqUji9TzP6kINPqkyqw&quot; title=&quot;See original on Picasa&quot;&gt;Picasa&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Start with the purpose of the project or initiative. As the business owner describes the purpose of the project, we collect the nouns and specific terms they use and how those words are related. A notepad can be sufficient but a small recorder is probably the best tool (especially if your memory and handwriting are less than stellar). The terms we collect during this conversation generally fall into three buckets: initial entities to populate a rough model with, attributes that may need to be analyzed in later steps, and existing materials/systems/projects/people to follow up with. In this step I often find it easier to deal with non-technical people, as a technical person will sometimes communicate the design they have started in their head rather than the original business case.&lt;/p&gt;

&lt;h3&gt;What does the business do?&lt;/h3&gt;
&lt;p&gt;It may not always be necessary to ask the business this (hopefully if you work there you already know the answer), but at a minimum it should be written somewhere near your other notes. The business provides context for the data and drives the relative importance of different terms and entities. It&#039;s important to remember that one company&#039;s definition of &quot;customer&quot; could be entirely different from another company&#039;s. Knowing the context helps us determine how important the terms from the prior conversation are and the wide variety of contexts is the reason there isn&#039;t one world-wide standard for the &quot;Customer&quot; entity.&lt;/p&gt;

&lt;div style=&quot;text-align: center; color: #666666; float: left; margin: 1em;&quot;&gt;&lt;a href=&quot;http://www.flickr.com/photos/miranda_jc/3406275289/&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/conceptual/sneakycat.png&quot; alt=&quot;Evaluate outside influences&quot; title=&quot;Evaluate outside influences&quot; /&gt;&lt;/a&gt;&lt;br /&gt;Outside influences can sneak up&lt;br /&gt;Sneaky Cat on &lt;a href=&quot;http://www.flickr.com/photos/miranda_jc/3406275289/&quot; title=&quot;See original on Flickr&quot;&gt;Flickr&lt;/a&gt;&lt;/div&gt;

&lt;h3&gt;About those other materials...&lt;/h3&gt;
&lt;p&gt;Any additional materials, products, or people referenced in that first conversation need to be tracked down. Materials will expose a much more detailed list of potential data fields then you will hear in a conversation, competing products or products priced out of the organizations range will provide additional terminology and purpose, while other people will provide both a sounding board for the assumptions so far and a source of secondary translations and assumptions. The important part is that we all started with the same base story (the initial business owner) before getting this far, as that frames these conversations and investigations. And yes, I really want to use the word &lt;em&gt;context&lt;/em&gt; again.&lt;/p&gt;

&lt;h3&gt;What&#039;s sneaking up on us?&lt;/h3&gt;
&lt;p&gt;The last source of information we want to address is projects that are starting in the near future, strategic initiatives, and similar surprises waiting only a few weeks down the road. Going back to the business to ask about planned projects in the near future can often uncover additional information that will be critical to the model. Changes in business direction, assumptions of geographic growth or acquisitions, and even projects that are intended to tie into this data source can all mean the difference between a completed model and a redo of the modeling effort.&lt;/p&gt;

&lt;h3&gt;Put it all together...&lt;/h3&gt;
&lt;p&gt;So by now we should be able to put it all together, right? Well, maybe. Probably not.&lt;/p&gt;

&lt;p&gt;Now that we have all of those raw inputs, all of those conversations, all of those identified terms and topics we need to start trying to fit it all into a cohesive structure. So as we start drawing pieces (and usually I am drawing all along) we are going to run into more questions, we are going to have moments where we stare dumbfounded at our whiteboards, and there will be a number of odd moments of clarity (generally in the shower or coffee line). &lt;/p&gt;

&lt;div style=&quot;text-align: center; color: #666666; float: right; margin: 0em 1em;&quot;&gt;&lt;a href=&quot;http://www.flickr.com/photos/mcnutcase/2229434187/&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/conceptual/catbasket.jpg&quot; alt=&quot;Turn the problem sideways&quot; title=&quot;Turn the problem sideways&quot; /&gt;&lt;/a&gt;&lt;br /&gt;Turn the problem sideways&lt;br /&gt;Cat in Basket on &lt;a href=&quot;http://www.flickr.com/photos/mcnutcase/2229434187/&quot; title=&quot;See original on Flickr&quot;&gt;Flickr&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;This last step incorporates the &quot;Analysis&quot; phase that I mentioned above. This is where we look at all of the potential attributes we have collected (you kept a list to make the logical phase easier, right?) and consider them from multiple different directions.&lt;/p&gt;

&lt;p&gt;As we look through our list of entities and potential attributes, we want to question them on the dimensions of time, availability, location/person, and categorization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do we need to track the value as it changes over time?&lt;/li&gt;
&lt;li&gt;Do we need to provide a mechanism to enter future values?&lt;/li&gt;
&lt;li&gt;Do these values get deleted, flagged as inactive, or ...?&lt;/li&gt;
&lt;li&gt;Do different variations of the value exist based on Location? Language? Currency? Person viewing the data?&lt;/li&gt;
&lt;li&gt;Can entities be re-categorized (ie, relationships changed)?&lt;/li&gt;
&lt;li&gt;Can relationships/categorizations be duplicated? Many-to-Many? Time sensitive?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These questions can often spark the creation of new entities or extra information to consider in later modeling efforts. This extra analysis (and the rigor with which we gathered information) is what brings everything together and, in my mind, is where our expertise and experience should shine. &lt;/p&gt;

&lt;h3&gt;And Done-ish&lt;/h3&gt;
&lt;p&gt;There will be a time when you have to call it done (for now). I probably spend the most time on the conceptual model because the clearer and better a fit it is, the easier it is to come back and start attaching attributes and to communicate amongst the business and other technical personnel. But even so, perfect is the enemy of good, so we want to reach good and call it a day.&lt;/p&gt;

&lt;h2&gt;The Finished Conceptual Model&lt;/h2&gt;
&lt;p&gt;At the point we call it &quot;Finished [for now]&quot;, we will have one or more diagrams that display the different entities and relationships, using business terms. We will also have a whole slew of notes on what those terms meant, other terms that were mentioned during the exploration (potential attributes, technical requirements, etc), and a partially worn out whiteboard. These our our deliverables from the conceptual modeling process and provide the foundation for our next steps.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/creating-a-conceptual-data-model&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>There are three types of data models: Conceptual, Logical, and Physical. The conceptual model provides a high-level view of the data, defining the general entities and entity relationships using the language of the business or organization. The logical model adds attributes to these entities, providing a technology-agnostic foundation for a database design. The physical model assigns table names, column names, and data types to the entities and attributes defined in the prior models. Defining the data model in distinct layers helps us manage the complexity of design and focus as we refine our understanding of the problem space.</p>

<div style="background-color: #ffffcc; border: 2px solid #cccccc; padding: .5em; margin: .5em .5em 1em .5em;">This topic is a sub-set of my presentation later this week at Baton Rouge, <a href="http://www.sqlsaturday.com/28/eventhome.aspx">SQL Saturday #28</a>. If you are planning on being in the area I urge you to sign up, there are going to be a wide range of speakers presenting on SQL and .Net development topics and so far it sounds like we have over 500 attendees (read: networking opportunities). My slides are posted up on <a href="http://tiernok.com/presentation.php">my website</a>.</div>

<h2>Building the Conceptual Model</h2>
<p>My approach is to start with the business drivers for the data and progress through external materials, technical inputs, and an analysis phase. Whether we are in a university environment, software development house, manufacturing company or non-profit organization, there is generally one key business driver. That driver has a business owner, technical or non-technical, and it is there that I prefer to start. Conversations with the business owner will often include less than specific terms and examples of competing services or existing data stores (think excel and MS Access) in the organization. Factors like maintainability, reliability, scalability, and so on will be mentioned in some cases, so we will want to document these if they come up. This provides the raw materials we need to build our data model.</p>


<h3>What is the purpose of [fill in the blank]?</h3>
<div style="text-align: center; color: #666666; float: right; margin: 1em;"><a href="http://picasaweb.google.com/lh/photo/-0iJqUji9TzP6kINPqkyqw"><img src="http://tiernok.com/LTDBlog/conceptual/dogcatdinner.jpg" alt="Context and expectations are key" title="Context and expectations are key" style="width: 260px" /></a><br />Context and expectations are key<br />Unnamed Picture on <a href="http://picasaweb.google.com/lh/photo/-0iJqUji9TzP6kINPqkyqw" title="See original on Picasa">Picasa</a></div>
<p>Start with the purpose of the project or initiative. As the business owner describes the purpose of the project, we collect the nouns and specific terms they use and how those words are related. A notepad can be sufficient but a small recorder is probably the best tool (especially if your memory and handwriting are less than stellar). The terms we collect during this conversation generally fall into three buckets: initial entities to populate a rough model with, attributes that may need to be analyzed in later steps, and existing materials/systems/projects/people to follow up with. In this step I often find it easier to deal with non-technical people, as a technical person will sometimes communicate the design they have started in their head rather than the original business case.</p>

<h3>What does the business do?</h3>
<p>It may not always be necessary to ask the business this (hopefully if you work there you already know the answer), but at a minimum it should be written somewhere near your other notes. The business provides context for the data and drives the relative importance of different terms and entities. It's important to remember that one company's definition of "customer" could be entirely different from another company's. Knowing the context helps us determine how important the terms from the prior conversation are and the wide variety of contexts is the reason there isn't one world-wide standard for the "Customer" entity.</p>

<div style="text-align: center; color: #666666; float: left; margin: 1em;"><a href="http://www.flickr.com/photos/miranda_jc/3406275289/"><img src="http://tiernok.com/LTDBlog/conceptual/sneakycat.png" alt="Evaluate outside influences" title="Evaluate outside influences" /></a><br />Outside influences can sneak up<br />Sneaky Cat on <a href="http://www.flickr.com/photos/miranda_jc/3406275289/" title="See original on Flickr">Flickr</a></div>

<h3>About those other materials...</h3>
<p>Any additional materials, products, or people referenced in that first conversation need to be tracked down. Materials will expose a much more detailed list of potential data fields then you will hear in a conversation, competing products or products priced out of the organizations range will provide additional terminology and purpose, while other people will provide both a sounding board for the assumptions so far and a source of secondary translations and assumptions. The important part is that we all started with the same base story (the initial business owner) before getting this far, as that frames these conversations and investigations. And yes, I really want to use the word <em>context</em> again.</p>

<h3>What's sneaking up on us?</h3>
<p>The last source of information we want to address is projects that are starting in the near future, strategic initiatives, and similar surprises waiting only a few weeks down the road. Going back to the business to ask about planned projects in the near future can often uncover additional information that will be critical to the model. Changes in business direction, assumptions of geographic growth or acquisitions, and even projects that are intended to tie into this data source can all mean the difference between a completed model and a redo of the modeling effort.</p>

<h3>Put it all together...</h3>
<p>So by now we should be able to put it all together, right? Well, maybe. Probably not.</p>

<p>Now that we have all of those raw inputs, all of those conversations, all of those identified terms and topics we need to start trying to fit it all into a cohesive structure. So as we start drawing pieces (and usually I am drawing all along) we are going to run into more questions, we are going to have moments where we stare dumbfounded at our whiteboards, and there will be a number of odd moments of clarity (generally in the shower or coffee line). </p>

<div style="text-align: center; color: #666666; float: right; margin: 0em 1em;"><a href="http://www.flickr.com/photos/mcnutcase/2229434187/"><img src="http://tiernok.com/LTDBlog/conceptual/catbasket.jpg" alt="Turn the problem sideways" title="Turn the problem sideways" /></a><br />Turn the problem sideways<br />Cat in Basket on <a href="http://www.flickr.com/photos/mcnutcase/2229434187/" title="See original on Flickr">Flickr</a></div>

<p>This last step incorporates the "Analysis" phase that I mentioned above. This is where we look at all of the potential attributes we have collected (you kept a list to make the logical phase easier, right?) and consider them from multiple different directions.</p>

<p>As we look through our list of entities and potential attributes, we want to question them on the dimensions of time, availability, location/person, and categorization:</p>

<ul>
<li>Do we need to track the value as it changes over time?</li>
<li>Do we need to provide a mechanism to enter future values?</li>
<li>Do these values get deleted, flagged as inactive, or ...?</li>
<li>Do different variations of the value exist based on Location? Language? Currency? Person viewing the data?</li>
<li>Can entities be re-categorized (ie, relationships changed)?</li>
<li>Can relationships/categorizations be duplicated? Many-to-Many? Time sensitive?</li>
</ul>

<p>These questions can often spark the creation of new entities or extra information to consider in later modeling efforts. This extra analysis (and the rigor with which we gathered information) is what brings everything together and, in my mind, is where our expertise and experience should shine. </p>

<h3>And Done-ish</h3>
<p>There will be a time when you have to call it done (for now). I probably spend the most time on the conceptual model because the clearer and better a fit it is, the easier it is to come back and start attaching attributes and to communicate amongst the business and other technical personnel. But even so, perfect is the enemy of good, so we want to reach good and call it a day.</p>

<h2>The Finished Conceptual Model</h2>
<p>At the point we call it "Finished [for now]", we will have one or more diagrams that display the different entities and relationships, using business terms. We will also have a whole slew of notes on what those terms meant, other terms that were mentioned during the exploration (potential attributes, technical requirements, etc), and a partially worn out whiteboard. These our our deliverables from the conceptual modeling process and provide the foundation for our next steps.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/creating-a-conceptual-data-model">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/creating-a-conceptual-data-model#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=925</wfw:commentRss>
		</item>
				<item>
			<title>Virtual Lab: Setting up Database Mail on SQL Server 2008 R2</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/virtual-lab-setting-up-database-mail-on</link>
			<pubDate>Tue, 06 Jul 2010 10:02:26 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">890@http://blogs.lessthandot.com/</guid>
						<description>&lt;div class=&quot;acc_header&quot;&gt;
After our installation of SQL Server 2008 R2 &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/virtual-lab-creating-a-basic-sql-2008-r2&quot; alt=&quot;Click here to catch up on that article if you missed it&quot; target=&quot;_blank&quot;&gt;a few weeks ago&lt;/a&gt;, I mentioned that we still had some additional setup tasks before we could consider the server to be done. This article covers setting up DatabaseMail, the internal mail client/management system that will allow us to use built-in alerts (among other things).&lt;br /&gt;
&lt;br /&gt;
&lt;label&gt;Technical Area:&lt;/label&gt; Accidental Database Administrator&lt;br /&gt;&lt;br /&gt;
&lt;label class=&quot;diff&quot;&gt;Level of Difficulty: &lt;/label&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/dr_basic.png&quot; alt=&quot;Basic Difficulty&quot; /&gt;&lt;br /&gt;
&lt;label&gt;Additional Articles:&lt;/label&gt;&lt;a href=&quot;http://wiki.lessthandot.com/index.php/Virtual_Lab&quot; title=&quot;View the wiki entry&quot;&gt;Virtual Lab entry on the LTD Wiki&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;

&lt;p&gt;Database Mail is a subsystem that acts like a &lt;a href=&quot;http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol&quot; title=&quot;Simple Mail Transport Protocol&quot;&gt;SMTP&lt;/a&gt; client, allowing us to send emails from SQL Server processes and scripts. It replaces SQL Mail and doesn&#039;t require a local installation of additional software (like Outlook, the quick fix) to function. &lt;/p&gt;

&lt;h2&gt;Account, Profile, Service Broker, what?&lt;/h2&gt;
&lt;p&gt;Database Mail expands on the concept of sending mail to include all of the features we wanted in SQLMail. This grown up version of mail uses SMTP to communicate with mail servers, no longer requiring a MAPI component to communicate messages (the reason we used to install products like Outlook to make SQLMail work). Behind the scenes, Database Mail uses Service Broker to manage email in queues, rather than trying an immediate send. Profiles allow us to define a chain of fail-over accounts to send from, so that messages are never left undelivered due to a primary email server being unavailable. And accounts are exactly what they sound like, individual, unique accounts that we can setup to use a variety of authentication and SMTP options.&lt;/p&gt;


&lt;h2&gt;From the GUI&lt;/h2&gt;
&lt;p&gt;Database Mail is something we want to setup immediately on creating a new server. When we are working with a relatively small number of servers, the GUI is not a bad option for setting up some accounts.&lt;/p&gt;

&lt;p&gt;We&#039;ll start by setting up an administrator profile with then intent to send critical notifications and alerts to this profile.&lt;/p&gt;

&lt;p&gt;In SSMS we can right click the Database Mail entry under Management to begin the wizard.&lt;/p&gt;
&lt;div class=&quot;screenshot&quot;&gt;
	&lt;a href=&quot;http://tiernok.com/LTDBlog/DatabaseMail/orig/01_config.png&quot; title=&quot;View Fullsize&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/DatabaseMail/01_config.png&quot; alt=&quot;SSMS DatabaseMail&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
	DatabaseMail Menu Item in SSMS
&lt;/div&gt;


&lt;p&gt;The Intro screen is fairly boring, so lets press &quot;Next&quot; and head to the main configuration page. This being the first time we are configuring DatabaseMail on our new server, we want to leave the first option selected and continue.&lt;/p&gt;
&lt;div class=&quot;screenshot&quot;&gt;
	&lt;a href=&quot;http://tiernok.com/LTDBlog/DatabaseMail/orig/02_config.png&quot; title=&quot;View Fullsize&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/DatabaseMail/02_config.png&quot; alt=&quot;SSMS DatabaseMail Wizard&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
	DatabaseMail Wizard in SSMS
&lt;/div&gt;

&lt;p&gt;A quick check by the wizard and it determines that components for Database Mail aren&#039;t available on the server yet, so it asks us if we want to go ahead and install them (yes).&lt;/p&gt;

&lt;div class=&quot;hint&quot;&gt;
How did the server know Database Mail wasn&#039;t configured, and what did it do when we pressed &#039;Yes&#039;? &lt;br /&gt;&lt;br /&gt;
There is an advanced option in the system configurations called &#039;Database Mail XPs&#039;. When the option is set to &#039;0&#039; (which is the default), then the Database Mail process doesn&#039;t run. So the dialog simply checked the current value and, when we selected &#039;Yes&#039;, updated the configuration to a value of &#039;1&#039;.
&lt;/div&gt;

&lt;p&gt;Profiles are used to represent a set of email addresses which allows us to represent a single person (or system) with multiple fail-over accounts. If an error occurs when the system is attempting to send mail from the first account in a profile, it fails to the next and retries, continuing until it either runs out of accounts or successfully sends it&#039;s message.&lt;/p&gt;

&lt;p&gt;Lets enter a Profile Name of &quot;The Accidental Admin&quot;.&lt;/p&gt;
&lt;div class=&quot;screenshot&quot;&gt;
	&lt;a href=&quot;http://tiernok.com/LTDBlog/DatabaseMail/orig/03_config.png&quot; title=&quot;View Fullsize&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/DatabaseMail/02_config.png&quot; alt=&quot;SSMS DatabaseMail Wizard&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
	DatabaseMail Wizard in SSMS
&lt;/div&gt;

&lt;p&gt;Now we&#039;ll add the first two accounts to this profile, each on a different network. Many admins will choose a non-local account with an external provider as their primary to reduce the chances that emails will not get through when other issues are happening. As many of you know, when a server room overheats the exchange server is always the first to go, so we will start with an external provider and fail-over to an internal one. Keep in mind that these are the accounts the database will send emails &lt;i&gt;from&lt;/i&gt; when using the specified profile, not to.&lt;/p&gt;

&lt;p&gt;Creating an account is fairly straightforward, simply enter the details for where you would like the email to be sent and any authentication options that are necessary. In this case we want to create a minimum of two accounts so the failover can work properly.&lt;/p&gt;
&lt;div class=&quot;screenshot&quot;&gt;
	&lt;a href=&quot;http://tiernok.com/LTDBlog/DatabaseMail/orig/04_config.png&quot; title=&quot;View Fullsize&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/DatabaseMail/04_config.png&quot; alt=&quot;SSMS DatabaseMail Wizard&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
	DatabaseMail Wizard in SSMS - Account Entry
&lt;/div&gt;

&lt;p&gt;Now that we have two accounts, we can move on.&lt;/p&gt;
&lt;div class=&quot;screenshot&quot;&gt;
	&lt;a href=&quot;http://tiernok.com/LTDBlog/DatabaseMail/orig/05_config.png&quot; title=&quot;View Fullsize&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/DatabaseMail/05_config.png&quot; alt=&quot;SSMS DatabaseMail Wizard&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
	DatabaseMail Wizard in SSMS - Account View
&lt;/div&gt;

&lt;p&gt;The next step is to decide whether our profile is going to be public or private. A public profile will be accessible to other people working on the system, whereas a private one will not. In this case we will make the profile public but be aware that this means anyone will be able to us the database server to send mail from this particular profile.&lt;/p&gt;
&lt;div class=&quot;screenshot&quot;&gt;
	&lt;a href=&quot;http://tiernok.com/LTDBlog/DatabaseMail/orig/06_config.png&quot; title=&quot;View Fullsize&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/DatabaseMail/06_config.png&quot; alt=&quot;SSMS DatabaseMail Wizard&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
	DatabaseMail Wizard in SSMS - Profile Security
&lt;/div&gt;

&lt;p&gt;The last interactive step is to confirm or modify some additional system parameters. This step is not specific to configuring an email profile and accounts, it is displayed as the final step of setting up DatabaseMail (when setting up for the first time). I suggest reviewing the settings to ensure your comfortable with each of them.&lt;/p&gt;
&lt;div class=&quot;screenshot&quot;&gt;
	&lt;a href=&quot;http://tiernok.com/LTDBlog/DatabaseMail/orig/07_config.png&quot; title=&quot;View Fullsize&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/DatabaseMail/07_config.png&quot; alt=&quot;SSMS DatabaseMail Wizard&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
	DatabaseMail Wizard in SSMS - System Parameters
&lt;/div&gt;

&lt;p&gt;After a final review screen, press the &#039;Finish&#039; button to implement the changes.&lt;/p&gt;
&lt;div class=&quot;screenshot&quot;&gt;
	&lt;a href=&quot;http://tiernok.com/LTDBlog/DatabaseMail/orig/08_config.png&quot; title=&quot;View Fullsize&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/DatabaseMail/08_config.png&quot; alt=&quot;SSMS DatabaseMail Wizard&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
	DatabaseMail Wizard in SSMS - Finished
&lt;/div&gt;

&lt;p&gt;The last step is to test our new setup. Right-click the Database Mail menu option in SSMS and select &quot;Send Test Email...&quot;.&lt;/p&gt;
&lt;div class=&quot;screenshot&quot;&gt;
	&lt;a href=&quot;http://tiernok.com/LTDBlog/DatabaseMail/orig/09_config.png&quot; title=&quot;View Fullsize&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/DatabaseMail/09_config.png&quot; alt=&quot;SSMS DatabaseMail - Test Email&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
	SSMS Menu - Sending a Test Email
&lt;/div&gt;

&lt;p&gt;After entering an email address for our test message, we can hit the &quot;Send&quot; button and wait for confirmation of our ability to hit &quot;Next, Next, Finish&quot;.&lt;/p&gt;
&lt;div class=&quot;screenshot&quot;&gt;
	&lt;a href=&quot;http://tiernok.com/LTDBlog/DatabaseMail/orig/10_config.png&quot; title=&quot;View Fullsize&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/DatabaseMail/10_config.png&quot; alt=&quot;SSMS DatabaseMail - Test Email Dialog&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
	SSMS DatabaseMail - Test Email Dialog
&lt;/div&gt;

&lt;h2&gt;or From a Script&lt;/h2&gt;
&lt;p&gt;Doing this on one server is pretty quick. Banging through the dialog on 20 (or with multiple profiles) is not only going to be a bit slower but probably guarantees at least one bad entry along the way. Luckily there is nothing magical about what SSMS is doing to configure these accounts and profiles, just a wizard driving system procedures behind the scenes. We can follow the same process programmatically and create a setup script. Once we have a setup script, of course, we can then use it against multiple systems and if we save it then we not only can set up future servers the same way, but we have detailed documentation on exactly how our servers were setup when we need it 3 years later.&lt;/p&gt;

&lt;p&gt;Here are the commands we will be using:&lt;/p&gt;
&lt;dl&gt;
	&lt;dt&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms189631.aspx&quot; onclick=&quot;_blank&quot; title=&quot;Read more at MSDN&quot;&gt;sp_configure&lt;/a&gt;&lt;/dt&gt;&lt;dd&gt;This procedure is used to change SQL Server configurations&lt;/dd&gt;
	&lt;dt&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms188058.aspx&quot; onclick=&quot;_blank&quot; title=&quot;Read more at MSDN&quot;&gt;sysmail_add_profile_sp&lt;/a&gt;&lt;/dt&gt;&lt;dd&gt;Used to create a Database Mail Profile&lt;/dd&gt;
	&lt;dt&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms182804.aspx&quot; onclick=&quot;_blank&quot; title=&quot;Read more at MSDN&quot;&gt;sysmail_add_account_sp&lt;/a&gt;&lt;/dt&gt;&lt;dd&gt;Used to create a Database Mail Account&lt;/dd&gt;
	&lt;dt&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms186258.aspx&quot; onclick=&quot;_blank&quot; title=&quot;Read more at MSDN&quot;&gt;sysmail_add_profileaccount_sp&lt;/a&gt;&lt;/dt&gt;&lt;dd&gt;Used to tie a Database Mail Account to a Profile (Tab A in Slot &lt;img src=&quot;http://blogs.lessthandot.com/rsc/smilies/icon_cool.gif&quot; title=&quot;B)&quot; alt=&quot;B)&quot; class=&quot;middle&quot; width=&quot;15&quot; height=&quot;15&quot; /&gt;&lt;/dd&gt;
	&lt;dt&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms187911.aspx&quot; onclick=&quot;_blank&quot; title=&quot;Read more at MSDN&quot;&gt;sysmail_add_principalprofile_sp&lt;/a&gt;&lt;/dt&gt;&lt;dd&gt;Assign access rights to a Database Mail profile&lt;/dd&gt;
	&lt;dt&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms190307.aspx&quot; onclick=&quot;_blank&quot; title=&quot;Read more at MSDN&quot;&gt;sp_send_dbmail&lt;/a&gt;&lt;/dt&gt;&lt;dd&gt;And sending an email, of course&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;And if we put it all together then we can duplicate the wizardy approach above like so:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb6102&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;---- Enable the XPs for Database Mail&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt; &lt;span style=&quot;color: #AF0000;&quot;&gt;sp_configure&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;show advanced&#039;&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;RECONFIGURE&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt; &lt;span style=&quot;color: #AF0000;&quot;&gt;sp_configure&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Database Mail XPs&#039;&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;RECONFIGURE&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt; &lt;span style=&quot;color: #AF0000;&quot;&gt;sp_configure&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;show advanced&#039;&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;RECONFIGURE&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;---- Create our Default Profile&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @newProfileId &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt; msdb.&lt;span style=&quot;color: #202020;&quot;&gt;dbo&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;sysmail_add_profile_sp&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; @profile_name = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;The Accidental Admin v2&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @description = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A second version of the DB Admin email profile&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @profile_id = @newProfileId &lt;span style=&quot;color: #0000FF;&quot;&gt;OUTPUT&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Profile Created&#039;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;---- Create our accounts (You did plan on a minimum of two, right?)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @newPriAccountId &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @newSecAccountId &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @sender &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @sender = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;DB SERVER - &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;@@SERVERNAME&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- External account using username/password&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt; msdb.&lt;span style=&quot;color: #202020;&quot;&gt;dbo&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;sysmail_add_account_sp&lt;/span&gt; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; @account_name = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;External DBAdmin&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @email_address = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;AnAccount@ExternalService.com&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @display_name = @sender,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @description = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Primary account for DB Admin profile&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @mailserver_name = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;mail.ExternalService.com&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @username = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;username&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @password = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;password&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @account_id = @newPriAccountId &lt;span style=&quot;color: #0000FF;&quot;&gt;OUTPUT&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- Internal account using credentials from the account DB server is running under&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt; msdb.&lt;span style=&quot;color: #202020;&quot;&gt;dbo&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;sysmail_add_account_sp&lt;/span&gt; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; @account_name = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Internal DBAdmin&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @email_address = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;YourAccount@YourDomain.com&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @display_name = @sender,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @description = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Secondary account for DB Admin profile&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @mailserver_name = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;mail.YourDomain.com&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @use_default_credentials = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @account_id = @newSecAccountId &lt;span style=&quot;color: #0000FF;&quot;&gt;OUTPUT&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Accounts Created: &#039;&lt;/span&gt; + @sender;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;---- Add Accounts to Profile&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt; msdb.&lt;span style=&quot;color: #202020;&quot;&gt;dbo&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;sysmail_add_profileaccount_sp&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; @profile_id = @newProfileId,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @account_id = @newPriAccountId,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @sequence_number = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt; msdb.&lt;span style=&quot;color: #202020;&quot;&gt;dbo&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;sysmail_add_profileaccount_sp&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; @profile_id = @newProfileId,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @account_id = @newSecAccountId,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @sequence_number = &lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Accounts assigned to Profile&#039;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Go&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;---- And Then We Test&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- Make sure you update the profile name with the value from above&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt; msdb.&lt;span style=&quot;color: #202020;&quot;&gt;dbo&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;sp_send_dbmail&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; @profile_name = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;The Accidental Admin v2&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @subject = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Test Database Mail Message&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @recipients = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;You@YourDomain.Com&#039;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @body = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Test Message&#039;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Test Email Away!&#039;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb93333&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Running this script creates a setup similar to what we created in the first section, though some of the names have been changed (and I have removed my email address from the list so I don&#039;t get server updates when someone comes along and forgets to swap out the addresses).&lt;/p&gt;

&lt;h2&gt;And Now, Uses&lt;/h2&gt;
&lt;p&gt;So now that we have Database Mail setup, what are we going to use it for? Well, besides creating our own monitoring and email scripts, the built in Alerts component in SQL Server uses Database mail to tell us when critical parts of our server have decided to misbehave. SQL Agent jobs have the option to notify us when they fail and they do so using, yep, Database Mail. Setting up Database Mail and using it in each of these situations won&#039;t prevent problems from occurring, but it is the difference between finding out when the problem is occurring and finding out after the server has melted down. &lt;/p&gt;

&lt;p&gt;Next week will be something interesting, but I haven&#039;t determined what that will be yet, so we&#039;ll figure it out as we go.&lt;/p&gt;

&lt;p&gt;&lt;style&gt;
.mylab, .hint{
   margin: .5em 2em;
   padding: 1em 1em 1em 90px;
}
.mylab{
   border: 2px solid #CCEECC;
   background-color: #DDFFDD;
   background-image: url(http://tiernok.com/LTDBlog/mylab.png);
   background-repeat: no-repeat;
   background-position: 20px 20px;
}
.hint{
   border: 2px solid #EEEECC;
   background-color: #FFFFDD;
   background-image: url(http://tiernok.com/LTDBlog/tip.png);
   background-repeat: no-repeat;
   background-position: 20px 20px;
}
.screenshot{
   text-align: center;
   font-size: .8em;
   margin-bottom: 1em;
}
.acc_header{ position: relative; }
.acc_header label{ float: left; width: 150px; font-weight: bold;}
.acc_header br{  clear: left; }
.acc_header .diff{ margin-top: 3px; }
.evo_post_text dt{ width: 220px; }
.evo_post_text dd{ margin-left: 230px; }
&lt;/style&gt;&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/virtual-lab-setting-up-database-mail-on&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<div class="acc_header">
After our installation of SQL Server 2008 R2 <a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/virtual-lab-creating-a-basic-sql-2008-r2" alt="Click here to catch up on that article if you missed it" target="_blank">a few weeks ago</a>, I mentioned that we still had some additional setup tasks before we could consider the server to be done. This article covers setting up DatabaseMail, the internal mail client/management system that will allow us to use built-in alerts (among other things).<br />
<br />
<label>Technical Area:</label> Accidental Database Administrator<br /><br />
<label class="diff">Level of Difficulty: </label><img src="http://tiernok.com/LTDBlog/dr_basic.png" alt="Basic Difficulty" /><br />
<label>Additional Articles:</label><a href="http://wiki.lessthandot.com/index.php/Virtual_Lab" title="View the wiki entry">Virtual Lab entry on the LTD Wiki</a><br />
</div>

<p>Database Mail is a subsystem that acts like a <a href="http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol" title="Simple Mail Transport Protocol">SMTP</a> client, allowing us to send emails from SQL Server processes and scripts. It replaces SQL Mail and doesn't require a local installation of additional software (like Outlook, the quick fix) to function. </p>

<h2>Account, Profile, Service Broker, what?</h2>
<p>Database Mail expands on the concept of sending mail to include all of the features we wanted in SQLMail. This grown up version of mail uses SMTP to communicate with mail servers, no longer requiring a MAPI component to communicate messages (the reason we used to install products like Outlook to make SQLMail work). Behind the scenes, Database Mail uses Service Broker to manage email in queues, rather than trying an immediate send. Profiles allow us to define a chain of fail-over accounts to send from, so that messages are never left undelivered due to a primary email server being unavailable. And accounts are exactly what they sound like, individual, unique accounts that we can setup to use a variety of authentication and SMTP options.</p>


<h2>From the GUI</h2>
<p>Database Mail is something we want to setup immediately on creating a new server. When we are working with a relatively small number of servers, the GUI is not a bad option for setting up some accounts.</p>

<p>We'll start by setting up an administrator profile with then intent to send critical notifications and alerts to this profile.</p>

<p>In SSMS we can right click the Database Mail entry under Management to begin the wizard.</p>
<div class="screenshot">
	<a href="http://tiernok.com/LTDBlog/DatabaseMail/orig/01_config.png" title="View Fullsize" target="_blank"><img src="http://tiernok.com/LTDBlog/DatabaseMail/01_config.png" alt="SSMS DatabaseMail" /></a><br />
	DatabaseMail Menu Item in SSMS
</div>


<p>The Intro screen is fairly boring, so lets press "Next" and head to the main configuration page. This being the first time we are configuring DatabaseMail on our new server, we want to leave the first option selected and continue.</p>
<div class="screenshot">
	<a href="http://tiernok.com/LTDBlog/DatabaseMail/orig/02_config.png" title="View Fullsize" target="_blank"><img src="http://tiernok.com/LTDBlog/DatabaseMail/02_config.png" alt="SSMS DatabaseMail Wizard" /></a><br />
	DatabaseMail Wizard in SSMS
</div>

<p>A quick check by the wizard and it determines that components for Database Mail aren't available on the server yet, so it asks us if we want to go ahead and install them (yes).</p>

<div class="hint">
How did the server know Database Mail wasn't configured, and what did it do when we pressed 'Yes'? <br /><br />
There is an advanced option in the system configurations called 'Database Mail XPs'. When the option is set to '0' (which is the default), then the Database Mail process doesn't run. So the dialog simply checked the current value and, when we selected 'Yes', updated the configuration to a value of '1'.
</div>

<p>Profiles are used to represent a set of email addresses which allows us to represent a single person (or system) with multiple fail-over accounts. If an error occurs when the system is attempting to send mail from the first account in a profile, it fails to the next and retries, continuing until it either runs out of accounts or successfully sends it's message.</p>

<p>Lets enter a Profile Name of "The Accidental Admin".</p>
<div class="screenshot">
	<a href="http://tiernok.com/LTDBlog/DatabaseMail/orig/03_config.png" title="View Fullsize" target="_blank"><img src="http://tiernok.com/LTDBlog/DatabaseMail/02_config.png" alt="SSMS DatabaseMail Wizard" /></a><br />
	DatabaseMail Wizard in SSMS
</div>

<p>Now we'll add the first two accounts to this profile, each on a different network. Many admins will choose a non-local account with an external provider as their primary to reduce the chances that emails will not get through when other issues are happening. As many of you know, when a server room overheats the exchange server is always the first to go, so we will start with an external provider and fail-over to an internal one. Keep in mind that these are the accounts the database will send emails <i>from</i> when using the specified profile, not to.</p>

<p>Creating an account is fairly straightforward, simply enter the details for where you would like the email to be sent and any authentication options that are necessary. In this case we want to create a minimum of two accounts so the failover can work properly.</p>
<div class="screenshot">
	<a href="http://tiernok.com/LTDBlog/DatabaseMail/orig/04_config.png" title="View Fullsize" target="_blank"><img src="http://tiernok.com/LTDBlog/DatabaseMail/04_config.png" alt="SSMS DatabaseMail Wizard" /></a><br />
	DatabaseMail Wizard in SSMS - Account Entry
</div>

<p>Now that we have two accounts, we can move on.</p>
<div class="screenshot">
	<a href="http://tiernok.com/LTDBlog/DatabaseMail/orig/05_config.png" title="View Fullsize" target="_blank"><img src="http://tiernok.com/LTDBlog/DatabaseMail/05_config.png" alt="SSMS DatabaseMail Wizard" /></a><br />
	DatabaseMail Wizard in SSMS - Account View
</div>

<p>The next step is to decide whether our profile is going to be public or private. A public profile will be accessible to other people working on the system, whereas a private one will not. In this case we will make the profile public but be aware that this means anyone will be able to us the database server to send mail from this particular profile.</p>
<div class="screenshot">
	<a href="http://tiernok.com/LTDBlog/DatabaseMail/orig/06_config.png" title="View Fullsize" target="_blank"><img src="http://tiernok.com/LTDBlog/DatabaseMail/06_config.png" alt="SSMS DatabaseMail Wizard" /></a><br />
	DatabaseMail Wizard in SSMS - Profile Security
</div>

<p>The last interactive step is to confirm or modify some additional system parameters. This step is not specific to configuring an email profile and accounts, it is displayed as the final step of setting up DatabaseMail (when setting up for the first time). I suggest reviewing the settings to ensure your comfortable with each of them.</p>
<div class="screenshot">
	<a href="http://tiernok.com/LTDBlog/DatabaseMail/orig/07_config.png" title="View Fullsize" target="_blank"><img src="http://tiernok.com/LTDBlog/DatabaseMail/07_config.png" alt="SSMS DatabaseMail Wizard" /></a><br />
	DatabaseMail Wizard in SSMS - System Parameters
</div>

<p>After a final review screen, press the 'Finish' button to implement the changes.</p>
<div class="screenshot">
	<a href="http://tiernok.com/LTDBlog/DatabaseMail/orig/08_config.png" title="View Fullsize" target="_blank"><img src="http://tiernok.com/LTDBlog/DatabaseMail/08_config.png" alt="SSMS DatabaseMail Wizard" /></a><br />
	DatabaseMail Wizard in SSMS - Finished
</div>

<p>The last step is to test our new setup. Right-click the Database Mail menu option in SSMS and select "Send Test Email...".</p>
<div class="screenshot">
	<a href="http://tiernok.com/LTDBlog/DatabaseMail/orig/09_config.png" title="View Fullsize" target="_blank"><img src="http://tiernok.com/LTDBlog/DatabaseMail/09_config.png" alt="SSMS DatabaseMail - Test Email" /></a><br />
	SSMS Menu - Sending a Test Email
</div>

<p>After entering an email address for our test message, we can hit the "Send" button and wait for confirmation of our ability to hit "Next, Next, Finish".</p>
<div class="screenshot">
	<a href="http://tiernok.com/LTDBlog/DatabaseMail/orig/10_config.png" title="View Fullsize" target="_blank"><img src="http://tiernok.com/LTDBlog/DatabaseMail/10_config.png" alt="SSMS DatabaseMail - Test Email Dialog" /></a><br />
	SSMS DatabaseMail - Test Email Dialog
</div>

<h2>or From a Script</h2>
<p>Doing this on one server is pretty quick. Banging through the dialog on 20 (or with multiple profiles) is not only going to be a bit slower but probably guarantees at least one bad entry along the way. Luckily there is nothing magical about what SSMS is doing to configure these accounts and profiles, just a wizard driving system procedures behind the scenes. We can follow the same process programmatically and create a setup script. Once we have a setup script, of course, we can then use it against multiple systems and if we save it then we not only can set up future servers the same way, but we have detailed documentation on exactly how our servers were setup when we need it 3 years later.</p>

<p>Here are the commands we will be using:</p>
<dl>
	<dt><a href="http://msdn.microsoft.com/en-us/library/ms189631.aspx" onclick="_blank" title="Read more at MSDN">sp_configure</a></dt><dd>This procedure is used to change SQL Server configurations</dd>
	<dt><a href="http://msdn.microsoft.com/en-us/library/ms188058.aspx" onclick="_blank" title="Read more at MSDN">sysmail_add_profile_sp</a></dt><dd>Used to create a Database Mail Profile</dd>
	<dt><a href="http://msdn.microsoft.com/en-us/library/ms182804.aspx" onclick="_blank" title="Read more at MSDN">sysmail_add_account_sp</a></dt><dd>Used to create a Database Mail Account</dd>
	<dt><a href="http://msdn.microsoft.com/en-us/library/ms186258.aspx" onclick="_blank" title="Read more at MSDN">sysmail_add_profileaccount_sp</a></dt><dd>Used to tie a Database Mail Account to a Profile (Tab A in Slot <img src="http://blogs.lessthandot.com/rsc/smilies/icon_cool.gif" title="B)" alt="B)" class="middle" width="15" height="15" /></dd>
	<dt><a href="http://msdn.microsoft.com/en-us/library/ms187911.aspx" onclick="_blank" title="Read more at MSDN">sysmail_add_principalprofile_sp</a></dt><dd>Assign access rights to a Database Mail profile</dd>
	<dt><a href="http://msdn.microsoft.com/en-us/library/ms190307.aspx" onclick="_blank" title="Read more at MSDN">sp_send_dbmail</a></dt><dd>And sending an email, of course</dd>
</dl>

<p>And if we put it all together then we can duplicate the wizardy approach above like so:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb36016'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb36016','cb91230'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb36016" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #00AF00;">---- Enable the XPs for Database Mail</span></li><li style="" class="li2"><span style="color: #0000FF;">EXECUTE</span> <span style="color: #AF0000;">sp_configure</span> <span style="color: #FF0000;">'show advanced'</span>, <span style="color: #000;">1</span>;</li><li style="" class="li1"><span style="color: #0000FF;">RECONFIGURE</span>;</li><li style="" class="li2"><span style="color: #0000FF;">EXECUTE</span> <span style="color: #AF0000;">sp_configure</span> <span style="color: #FF0000;">'Database Mail XPs'</span>,<span style="color: #000;">1</span>;</li><li style="" class="li1"><span style="color: #0000FF;">RECONFIGURE</span>;</li><li style="" class="li2"><span style="color: #0000FF;">EXECUTE</span> <span style="color: #AF0000;">sp_configure</span> <span style="color: #FF0000;">'show advanced'</span>, <span style="color: #000;">0</span>;</li><li style="" class="li1"><span style="color: #0000FF;">RECONFIGURE</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">---- Create our Default Profile</span></li><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @newProfileId <span style="color: #0000FF;">int</span>;</li><li style="" class="li2"><span style="color: #0000FF;">EXECUTE</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysmail_add_profile_sp</span></li><li style="" class="li1">&nbsp; &nbsp; @profile_name = <span style="color: #FF0000;">'The Accidental Admin v2'</span>,</li><li style="" class="li2">&nbsp; &nbsp; @description = <span style="color: #FF0000;">'A second version of the DB Admin email profile'</span>,</li><li style="" class="li1">&nbsp; &nbsp; @profile_id = @newProfileId <span style="color: #0000FF;">OUTPUT</span>;</li><li style="" class="li2"><span style="color: #0000FF;">print</span> <span style="color: #FF0000;">'Profile Created'</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">---- Create our accounts (You did plan on a minimum of two, right?)</span></li><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @newPriAccountId <span style="color: #0000FF;">int</span>,</li><li style="" class="li2">&nbsp; &nbsp; @newSecAccountId <span style="color: #0000FF;">int</span>;</li><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @sender <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @sender = <span style="color: #FF0000;">'DB SERVER - '</span> + <span style="color: #FF00FF;">@@SERVERNAME</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">-- External account using username/password</span></li><li style="" class="li1"><span style="color: #0000FF;">EXECUTE</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysmail_add_account_sp</span> &nbsp;</li><li style="" class="li2">&nbsp; &nbsp; @account_name = <span style="color: #FF0000;">'External DBAdmin'</span>,</li><li style="" class="li1">&nbsp; &nbsp; @email_address = <span style="color: #FF0000;">'AnAccount@ExternalService.com'</span>,</li><li style="" class="li2">&nbsp; &nbsp; @display_name = @sender,</li><li style="" class="li1">&nbsp; &nbsp; @description = <span style="color: #FF0000;">'Primary account for DB Admin profile'</span>,</li><li style="" class="li2">&nbsp; &nbsp; @mailserver_name = <span style="color: #FF0000;">'mail.ExternalService.com'</span>,</li><li style="" class="li1">&nbsp; &nbsp; @username = <span style="color: #FF0000;">'username'</span>,</li><li style="" class="li2">&nbsp; &nbsp; @password = <span style="color: #FF0000;">'password'</span>,</li><li style="" class="li1">&nbsp; &nbsp; @account_id = @newPriAccountId <span style="color: #0000FF;">OUTPUT</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">-- Internal account using credentials from the account DB server is running under</span></li><li style="" class="li2"><span style="color: #0000FF;">EXECUTE</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysmail_add_account_sp</span> &nbsp;</li><li style="" class="li1">&nbsp; &nbsp; @account_name = <span style="color: #FF0000;">'Internal DBAdmin'</span>,</li><li style="" class="li2">&nbsp; &nbsp; @email_address = <span style="color: #FF0000;">'YourAccount@YourDomain.com'</span>,</li><li style="" class="li1">&nbsp; &nbsp; @display_name = @sender,</li><li style="" class="li2">&nbsp; &nbsp; @description = <span style="color: #FF0000;">'Secondary account for DB Admin profile'</span>,</li><li style="" class="li1">&nbsp; &nbsp; @mailserver_name = <span style="color: #FF0000;">'mail.YourDomain.com'</span>,</li><li style="" class="li2">&nbsp; &nbsp; @use_default_credentials = <span style="color: #000;">1</span>,</li><li style="" class="li1">&nbsp; &nbsp; @account_id = @newSecAccountId <span style="color: #0000FF;">OUTPUT</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">print</span> <span style="color: #FF0000;">'Accounts Created: '</span> + @sender;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2"><span style="color: #00AF00;">---- Add Accounts to Profile</span></li><li style="" class="li1"><span style="color: #0000FF;">EXECUTE</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysmail_add_profileaccount_sp</span></li><li style="" class="li2">&nbsp; &nbsp; @profile_id = @newProfileId,</li><li style="" class="li1">&nbsp; &nbsp; @account_id = @newPriAccountId,</li><li style="" class="li2">&nbsp; &nbsp; @sequence_number = <span style="color: #000;">1</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">EXECUTE</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysmail_add_profileaccount_sp</span></li><li style="" class="li1">&nbsp; &nbsp; @profile_id = @newProfileId,</li><li style="" class="li2">&nbsp; &nbsp; @account_id = @newSecAccountId,</li><li style="" class="li1">&nbsp; &nbsp; @sequence_number = <span style="color: #000;">2</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">print</span> <span style="color: #FF0000;">'Accounts assigned to Profile'</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">Go</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">---- And Then We Test</span></li><li style="" class="li2"><span style="color: #00AF00;">-- Make sure you update the profile name with the value from above</span></li><li style="" class="li1"><span style="color: #0000FF;">EXECUTE</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sp_send_dbmail</span></li><li style="" class="li2">&nbsp; &nbsp; @profile_name = <span style="color: #FF0000;">'The Accidental Admin v2'</span>,</li><li style="" class="li1">&nbsp; &nbsp; @subject = <span style="color: #FF0000;">'Test Database Mail Message'</span>,</li><li style="" class="li2">&nbsp; &nbsp; @recipients = <span style="color: #FF0000;">'You@YourDomain.Com'</span>,</li><li style="" class="li1">&nbsp; &nbsp; @body = <span style="color: #FF0000;">'Test Message'</span>;</li><li style="" class="li2"><span style="color: #0000FF;">print</span> <span style="color: #FF0000;">'Test Email Away!'</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb91230" style="display: none; color: red;"></div></div></div>

<p>Running this script creates a setup similar to what we created in the first section, though some of the names have been changed (and I have removed my email address from the list so I don't get server updates when someone comes along and forgets to swap out the addresses).</p>

<h2>And Now, Uses</h2>
<p>So now that we have Database Mail setup, what are we going to use it for? Well, besides creating our own monitoring and email scripts, the built in Alerts component in SQL Server uses Database mail to tell us when critical parts of our server have decided to misbehave. SQL Agent jobs have the option to notify us when they fail and they do so using, yep, Database Mail. Setting up Database Mail and using it in each of these situations won't prevent problems from occurring, but it is the difference between finding out when the problem is occurring and finding out after the server has melted down. </p>

<p>Next week will be something interesting, but I haven't determined what that will be yet, so we'll figure it out as we go.</p>

<p><style>
.mylab, .hint{
   margin: .5em 2em;
   padding: 1em 1em 1em 90px;
}
.mylab{
   border: 2px solid #CCEECC;
   background-color: #DDFFDD;
   background-image: url(http://tiernok.com/LTDBlog/mylab.png);
   background-repeat: no-repeat;
   background-position: 20px 20px;
}
.hint{
   border: 2px solid #EEEECC;
   background-color: #FFFFDD;
   background-image: url(http://tiernok.com/LTDBlog/tip.png);
   background-repeat: no-repeat;
   background-position: 20px 20px;
}
.screenshot{
   text-align: center;
   font-size: .8em;
   margin-bottom: 1em;
}
.acc_header{ position: relative; }
.acc_header label{ float: left; width: 150px; font-weight: bold;}
.acc_header br{  clear: left; }
.acc_header .diff{ margin-top: 3px; }
.evo_post_text dt{ width: 220px; }
.evo_post_text dd{ margin-left: 230px; }
</style></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/virtual-lab-setting-up-database-mail-on">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/virtual-lab-setting-up-database-mail-on#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=890</wfw:commentRss>
		</item>
			</channel>
</rss>
