<?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>All Blogs - Author(s): Naomi Nosonovsky</title>
		<link>http://blogs.lessthandot.com/index.php/All/</link>
		<atom:link rel="self" type="application/rss+xml" href="http://blogs.lessthandot.com/index.php/All/?tempskin=_rss2" />
		<description>LessThanDot A Technical Community for IT Professionals</description>
		<language>en-US</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>ASP.NET MVC project with Modal dialogs and Flexigrid</title>
			<link>http://blogs.lessthandot.com/index.php/WebDev/UIDevelopment/AJAX/asp-net-mvc-project-with</link>
			<pubDate>Sun, 27 Jan 2013 20:24:00 +0000</pubDate>			<dc:creator>Naomi Nosonovsky</dc:creator>
			<category domain="main">AJAX</category>			<guid isPermaLink="false">2053@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;In the last few months I started working on my first ASP.NET MVC project. I already had a Visual FoxPro application I wrote in a few days and I wanted to replicate it for the Web using ASP.NET MVC. This project is still in its infancy, but I want to share my experience and some success I have with it.&lt;/p&gt;

&lt;p&gt;I also first want to thank people who helped me to get where I am now with this project: Viv Phillips and Paul Mrozowski from UniversalThread, Darin Dimitrov from stackoverflow, Alex Ullrich and Sergey Kosik and also Jazzen Chen from MS. I also want to thank authors of many blogs and articles I read while working on this project.&lt;/p&gt;

&lt;p&gt;I started my project from defining my models using Entity Framework Code first pattern since I already had the database defined. I am going to discuss only one model &quot;Client&quot; for the Clients table.&lt;/p&gt;

&lt;p&gt;This is how the table looks in the database:&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;cb38694&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ClientID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_no&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;smallint&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;client_name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&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;#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; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact1&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact2&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; 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;Address&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;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&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;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;max&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;char&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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;6&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;smalldatetime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;char&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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;smalldatetime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; NULL,&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;CONSTRAINT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;PK_Clients_ClientID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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: #0000FF;&quot;&gt;CLUSTERED&lt;/span&gt; &lt;br /&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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ClientID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ASC&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;PAD_INDEX&lt;/span&gt; &amp;nbsp;= &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;STATISTICS_NORECOMPUTE&lt;/span&gt; &amp;nbsp;= &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;IGNORE_DUP_KEY&lt;/span&gt; = &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;ALLOW_ROW_LOCKS&lt;/span&gt; &amp;nbsp;= &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;ALLOW_PAGE_LOCKS&lt;/span&gt; &amp;nbsp;= &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;PRIMARY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;PRIMARY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; TEXTIMAGE_ON &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;PRIMARY&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;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ANSI_PADDING&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&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;NONCLUSTERED&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INDEX&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;idxClients_Client_No&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;br /&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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_no&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ASC&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;PAD_INDEX&lt;/span&gt; &amp;nbsp;= &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;STATISTICS_NORECOMPUTE&lt;/span&gt; &amp;nbsp;= &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;SORT_IN_TEMPDB&lt;/span&gt; = &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;IGNORE_DUP_KEY&lt;/span&gt; = &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;, DROP_EXISTING = &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;ONLINE&lt;/span&gt; = &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;ALLOW_ROW_LOCKS&lt;/span&gt; &amp;nbsp;= &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;ALLOW_PAGE_LOCKS&lt;/span&gt; &amp;nbsp;= &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;PRIMARY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;/****** Object: &amp;nbsp;Default [DF_Clients_EnteredBy] &amp;nbsp; &amp;nbsp;Script Date: 12/19/2012 07:00:35 ******/&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ADD&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;CONSTRAINT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;DF_Clients_EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;DEFAULT&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;ADMIN&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FOR&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;/****** Object: &amp;nbsp;Default [DF_Clients_EnteredOn] &amp;nbsp; &amp;nbsp;Script Date: 12/19/2012 07:00:35 ******/&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ADD&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;CONSTRAINT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;DF_Clients_EnteredOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;DEFAULT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&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;span style=&quot;color: #0000FF;&quot;&gt;FOR&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;/****** Object: &amp;nbsp;Check [CK_Clients_C1_Phone] &amp;nbsp; &amp;nbsp;Script Date: 12/19/2012 07:00:35 ******/&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CHECK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ADD&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;CONSTRAINT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;CK_Clients_C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CHECK&lt;/span&gt; &amp;nbsp;&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;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; like &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]&#039;&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;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CHECK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CONSTRAINT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;CK_Clients_C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;/****** Object: &amp;nbsp;Check [CK_Clients_C2_Phone] &amp;nbsp; &amp;nbsp;Script Date: 12/19/2012 07:00:35 ******/&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CHECK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ADD&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;CONSTRAINT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;CK_Clients_C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CHECK&lt;/span&gt; &amp;nbsp;&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;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; like &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]&#039;&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;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CHECK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CONSTRAINT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;CK_Clients_C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;/****** Object: &amp;nbsp;ForeignKey [FK_Clients_Operators_EnteredBy] &amp;nbsp; &amp;nbsp;Script Date: 12/19/2012 07:00:35 ******/&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CHECK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ADD&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;CONSTRAINT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;FK_Clients_Operators_EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FOREIGN&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;KEY&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;EnteredBy&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;REFERENCES&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Operators&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;op_code&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;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CHECK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CONSTRAINT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;FK_Clients_Operators_EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;/****** Object: &amp;nbsp;ForeignKey [FK_Clients_Operators_ModifiedBy] &amp;nbsp; &amp;nbsp;Script Date: 12/19/2012 07:00:35 ******/&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CHECK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ADD&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;CONSTRAINT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;FK_Clients_Operators_ModifiedBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FOREIGN&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;KEY&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;ModifiedBy&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;REFERENCES&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Operators&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;op_code&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;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CHECK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;CONSTRAINT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;FK_Clients_Operators_ModifiedBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&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;cb84089&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Last 2 constraints in the above script refer to the Operators table I am not showing here.&lt;/p&gt;

&lt;p&gt;Let&#039;s add some data to insert:&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;cb57857&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY_INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;ClientID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_no&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact1&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact2&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&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;#91;&lt;/span&gt;EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedOn&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;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: #000;&quot;&gt;3&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;8096&lt;/span&gt;, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Wachusett&#039;&lt;/span&gt;, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;ADMIN &#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;0x9F9002DD &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;SmallDateTime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, NULL, 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;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;ClientID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_no&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact1&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact2&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&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;#91;&lt;/span&gt;EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedOn&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;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: #000;&quot;&gt;4&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;6700&lt;/span&gt;, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Buck Hill&#039;&lt;/span&gt;, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;ADMIN &#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;0x9F9002DD &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;SmallDateTime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, NULL, 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;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;ClientID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_no&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact1&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact2&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&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;#91;&lt;/span&gt;EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedOn&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;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: #000;&quot;&gt;5&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;6238&lt;/span&gt;, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Jamz&#039;&lt;/span&gt;, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;ADMIN &#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;0x9F9002DD &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;SmallDateTime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, NULL, 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;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;ClientID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_no&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact1&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact2&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&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;#91;&lt;/span&gt;EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedOn&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;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: #000;&quot;&gt;7&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;8363&lt;/span&gt;, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Okemo&#039;&lt;/span&gt;, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;ADMIN &#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;0x9F9002DD &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;SmallDateTime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, NULL, 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;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;ClientID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_no&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact1&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact2&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&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;#91;&lt;/span&gt;EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedOn&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;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: #000;&quot;&gt;8&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;1161&lt;/span&gt;, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Shawnee Mountain&#039;&lt;/span&gt;, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;ADMIN &#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;0x9F9002DD &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;SmallDateTime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, NULL, 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;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;ClientID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_no&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact1&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact2&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&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;#91;&lt;/span&gt;EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedOn&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;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: #000;&quot;&gt;9&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;7497&lt;/span&gt;, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Southern Star Obs Wheel&#039;&lt;/span&gt;, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;ADMIN &#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;0x9F9002DD &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;SmallDateTime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, NULL, 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;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;ClientID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_no&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact1&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact2&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&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;#91;&lt;/span&gt;EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedOn&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;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: #000;&quot;&gt;10&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;1636&lt;/span&gt;, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Mount Rose&#039;&lt;/span&gt;, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;ADMIN &#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;0x9F9002DD &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;SmallDateTime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, NULL, 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;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Clients&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;ClientID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_no&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;client_name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact1&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Contact2&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Email&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C1_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Phone&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;C2_Ext&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&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;#91;&lt;/span&gt;EnteredBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;EnteredOn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedBy&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ModifiedOn&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;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: #000;&quot;&gt;11&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;3951&lt;/span&gt;, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Crystal Mtn Washington&#039;&lt;/span&gt;, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;ADMIN &#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;0x9F9002DD &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;SmallDateTime&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, NULL, NULL&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;cb95092&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It took me many iterations and consultations with various blogs about Entity Framework to come up with the following model:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;c#&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;c#&quot; id=&quot;cb44292&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;using System;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;ComponentModel&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;DataAnnotations&lt;/span&gt;;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;ComponentModel&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;using DataAnnotationsExtensions;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;ComponentModel&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;DataAnnotations&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Schema&lt;/span&gt;;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;Collections&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Generic&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;namespace CardNumbers.&lt;span style=&quot;color: #202020;&quot;&gt;Objects&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;ComplexType&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; public class PhoneInfo&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DataType&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;DataType.&lt;span style=&quot;color: #202020;&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DisplayName&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Phone&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;RegularExpression&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;^((&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\(&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\d&lt;/span&gt;{3}&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\)&lt;/span&gt;|&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\d&lt;/span&gt;{3})&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\s&lt;/span&gt;?)?&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\d&lt;/span&gt;{3}[-&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\s&lt;/span&gt;]?&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\d&lt;/span&gt;{4}&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\s&lt;/span&gt;*$&amp;quot;&lt;/span&gt;, ErrorMessage = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Please enter valid Phone Number&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; Phone &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;StringLength&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DisplayName&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Ext&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; Ext &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public bool HasValue&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; get&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Phone != &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;null&lt;/span&gt; || Ext != &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;ComplexType&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; public class ContactDetail&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;//Constructor&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public ContactDetail&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; phoneInfo = new PhoneInfo&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;StringLength&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DisplayName&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Contact Name&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DisplayFormat&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;NullDisplayText = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; Contact &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;Email&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;StringLength&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DataType&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;DataType.&lt;span style=&quot;color: #202020;&quot;&gt;EmailAddress&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DisplayName&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Email&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; Email &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual PhoneInfo phoneInfo &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public bool HasValue&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; get&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Contact != &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;null&lt;/span&gt; || Email != &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;null&lt;/span&gt; || phoneInfo.&lt;span style=&quot;color: #202020;&quot;&gt;HasValue&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/// Client class (Client No, Client Name, Address, Contact1, Contact2 info, Created By, Modified By (operator and date)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; public class Client&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public Client&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Contact1 = new ContactDetail&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Contact2 = new ContactDetail&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;Key&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;Editable&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;Column&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;ClientId&amp;quot;&lt;/span&gt;,TypeName = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;int&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; Id &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;//[Required]&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DisplayName&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Client No&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;Column&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;client_no&amp;quot;&lt;/span&gt;, TypeName = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;smallint&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual Int16 Number &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;//[Required]&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;Column&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;client_name&amp;quot;&lt;/span&gt;, TypeName = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;varchar&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DisplayName&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Client Name&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;MaxLength&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;30&lt;/span&gt;, ErrorMessage = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Client Name should not be longer than 30 characters&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;MinLength&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;3&lt;/span&gt;, ErrorMessage = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Client Name is too short&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; Name &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DataType&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;DataType.&lt;span style=&quot;color: #202020;&quot;&gt;MultilineText&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; Address &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual ContactDetail Contact1 &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;get; set;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual ContactDetail Contact2 &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;get; set;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;ForeignKey&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;EnteredByOperator&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; EnteredBy &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;InverseProperty&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;ClientsEnteredBy&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual Operator EnteredByOperator &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;ForeignKey&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;ModifiedByOperator&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; ModifiedBy &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;InverseProperty&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;ClientsUpdatedBy&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual Operator ModifiedByOperator &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DataType&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;DataType.&lt;span style=&quot;color: #202020;&quot;&gt;DateTime&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DisplayName&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Created on&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public DateTime EnteredOn &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DataType&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;DataType.&lt;span style=&quot;color: #202020;&quot;&gt;DateTime&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;DisplayName&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Modified on&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public DateTime? ModifiedOn &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual ICollection&amp;lt;ClientOrder&amp;gt; ClientOrders &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public virtual ICollection&amp;lt;Reorder&amp;gt; Reorders &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb65862&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The interesting thing in this model is 2 Complex Types. Originally this model didn&#039;t have Complex Types, but I found later that I need them in order to create one Editor for each Contact information (as we see, the table is not properly normalized and contains 2 contacts columns). &lt;/p&gt;

&lt;p&gt;I created all my models in one project called CardNumbers.Objects. I later was criticized for such project name, but I used the same naming convention that was used by our 3 days course teacher and therefore left it as is. In my next project I am going to be more thoughtful in regards to project names.&lt;/p&gt;

&lt;p&gt;I also created another project called CardNumbers.Data where I placed repository objects. I also originally built them the same way we were taught by the instructor, but as I found later it was not the best way, so I re-designed based on some blog posts and StackOverflow questions this way:&lt;/p&gt;

&lt;p&gt;IClientRepository.cs:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;c#&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;c#&quot; id=&quot;cb91847&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;using CardNumbers.&lt;span style=&quot;color: #202020;&quot;&gt;Objects&lt;/span&gt;;&lt;br /&gt;using System;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;Collections&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Generic&lt;/span&gt;;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;Linq&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;namespace CardNumbers.&lt;span style=&quot;color: #202020;&quot;&gt;Data&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; public interface IClientRepository:IDisposable&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; IQueryable&amp;lt;Client&amp;gt; Clients &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Client GetClientById&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; clientId&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; Commit&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; AddClient&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Client client, bool autoCommit = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; DeleteClient&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Client client, bool autoCommit = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; DeleteClient&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; clientId&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; UpdateClient&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Client client, bool autoCommit = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb70429&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and the actual implementation of the above interface:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;c#&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;c#&quot; id=&quot;cb49280&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;using System;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;Collections&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Generic&lt;/span&gt;;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;Data&lt;/span&gt;;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;Data&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Entity&lt;/span&gt;;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;Linq&lt;/span&gt;;&lt;br /&gt;using CardNumbers.&lt;span style=&quot;color: #202020;&quot;&gt;Objects&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;namespace CardNumbers.&lt;span style=&quot;color: #202020;&quot;&gt;Data&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; public class ClientRepository : IClientRepository, IDisposable &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; private CardNumbersContext context;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public ClientRepository&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;CardNumbersContext context&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.&lt;span style=&quot;color: #202020;&quot;&gt;context&lt;/span&gt; = context;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&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; IQueryable&amp;lt;Client&amp;gt; IClientRepository.&lt;span style=&quot;color: #202020;&quot;&gt;Clients&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; get &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; this.&lt;span style=&quot;color: #202020;&quot;&gt;context&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Clients&lt;/span&gt;; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; Commit&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.&lt;span style=&quot;color: #202020;&quot;&gt;context&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;SaveChanges&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; AddClient&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Client client, bool autoCommit = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; client.&lt;span style=&quot;color: #202020;&quot;&gt;EnteredOn&lt;/span&gt; = DateTime.&lt;span style=&quot;color: #202020;&quot;&gt;Now&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.&lt;span style=&quot;color: #202020;&quot;&gt;context&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Clients&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;client&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;nbsp; ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;autoCommit&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; this.&lt;span style=&quot;color: #202020;&quot;&gt;Commit&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; DeleteClient&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Client client, bool autoCommit = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.&lt;span style=&quot;color: #202020;&quot;&gt;context&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Clients&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Remove&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;client&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;autoCommit&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; this.&lt;span style=&quot;color: #202020;&quot;&gt;Commit&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; DeleteClient&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; id&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.&lt;span style=&quot;color: #202020;&quot;&gt;context&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Clients&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Remove&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;this.&lt;span style=&quot;color: #202020;&quot;&gt;GetClientById&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;id&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.&lt;span style=&quot;color: #202020;&quot;&gt;Commit&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public Client GetClientById&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; id&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; this.&lt;span style=&quot;color: #202020;&quot;&gt;context&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Clients&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Find&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;id&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; UpdateClient&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Client client, bool autoCommit = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; client.&lt;span style=&quot;color: #202020;&quot;&gt;ModifiedOn&lt;/span&gt; = DateTime.&lt;span style=&quot;color: #202020;&quot;&gt;Now&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.&lt;span style=&quot;color: #202020;&quot;&gt;context&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Entry&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;client&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;State&lt;/span&gt; = EntityState.&lt;span style=&quot;color: #202020;&quot;&gt;Modified&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;autoCommit&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; this.&lt;span style=&quot;color: #202020;&quot;&gt;Commit&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; private bool disposed = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;false&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; protected virtual &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; Dispose&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;bool disposing&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;!this.&lt;span style=&quot;color: #202020;&quot;&gt;disposed&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;disposing&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; context.&lt;span style=&quot;color: #202020;&quot;&gt;Dispose&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.&lt;span style=&quot;color: #202020;&quot;&gt;disposed&lt;/span&gt; = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; Dispose&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Dispose&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; GC.&lt;span style=&quot;color: #202020;&quot;&gt;SuppressFinalize&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;this&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb27443&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and I show CardNumbersContext.cs for completion:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;c#&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;c#&quot; id=&quot;cb46621&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;using System;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;Collections&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Generic&lt;/span&gt;;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;Data&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Entity&lt;/span&gt;;&lt;br /&gt;using CardNumbers.&lt;span style=&quot;color: #202020;&quot;&gt;Objects&lt;/span&gt;;&lt;br /&gt;using System.&lt;span style=&quot;color: #202020;&quot;&gt;Data&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Entity&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;ModelConfiguration&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Conventions&lt;/span&gt;;&lt;br /&gt;using CardNumbers.&lt;span style=&quot;color: #202020;&quot;&gt;Objects&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Mapping&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;namespace CardNumbers.&lt;span style=&quot;color: #202020;&quot;&gt;Data&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; public class CardNumbersContext : DbContext&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public DbSet&amp;lt;Client&amp;gt; Clients &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public DbSet&amp;lt;Operator&amp;gt; Operators &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public DbSet&amp;lt;Reorder&amp;gt; Reorders &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public DbSet&amp;lt;ClientOrder&amp;gt; ClientOrders &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; protected override &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; OnModelCreating&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;DbModelBuilder modelBuilder&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// base.OnModelCreating(modelBuilder);&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; modelBuilder.&lt;span style=&quot;color: #202020;&quot;&gt;Configurations&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;new ClientMap&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; modelBuilder.&lt;span style=&quot;color: #202020;&quot;&gt;Configurations&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;new OperatorMap&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; modelBuilder.&lt;span style=&quot;color: #202020;&quot;&gt;Configurations&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;new ClientOrdersMap&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; modelBuilder.&lt;span style=&quot;color: #202020;&quot;&gt;Configurations&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;new ReorderMap&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb76691&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, the above two projects describe my models and means to work with them.&lt;/p&gt;

&lt;p&gt;Now I am going to describe the CardNumbers.Web project. &lt;/p&gt;

&lt;p&gt;I started from designing the view for working with the Clients. Here I didn&#039;t want to go with the standard shown in all tutorials ways of displaying all clients information in a one page grid with inline Edit/Delete buttons. I didn&#039;t like this interface much. So, I started my research as what existing grids I may try to implement. I looked up a few different grid implementations and at some point someone suggested me to look into &lt;a href=&quot;http://flexigrid.info/&quot;&gt;flexigrid&lt;/a&gt; CodePlex project. I really liked its look and decided to go with it. Also, I believe that same person suggested me &lt;a href=&quot;http://mvc4beginner.com/Sample-Code/Insert-Update-Delete/Asp-.Net-MVC-Ajax-Insert-Update-Delete-Using-Flexigrid.html&quot;&gt;his site&lt;/a&gt; which was enough to get me started. I got the Clients view working relatively quickly based on that information.&lt;/p&gt;

&lt;p&gt;However, I didn&#039;t want to start from displaying all Clients. I wanted to simulate my current VFP application where I started from the empty grid, typed some letters in the client name search textbox and based on that show only small subset of rows initially.&lt;/p&gt;

&lt;p&gt;This took me a while to figure out. I was almost ready to give up, but finally found addFormData method (which was there all the time) and figured out how to use it for my purpose. It may seem silly to most of the experienced jquery developers, but for me it was my first mini-success.&lt;/p&gt;

&lt;p&gt;So, I will show you how my Client View now looks and the relevant portions of Clients.js script file and the ClientController.cs file:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;html&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;html&quot; id=&quot;cb87561&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;@model CardNumbers.Models.ClientViewModel&lt;br /&gt;&amp;nbsp;&lt;br /&gt;@section scripts {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/script.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;script&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;src&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;@Url.Content(&amp;quot;&lt;/span&gt;~/Scripts/Clients.js&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;)&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/form.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;form&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;frmClientsSearch&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/label.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;label&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;for&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;clientNo&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Client No: &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/input.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;input&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;number&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;searchClientNo&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;class&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;numericOnly&amp;quot;&lt;/span&gt; /&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/br.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;br&lt;/span&gt;&lt;/a&gt; /&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/label.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;label&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;for&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;clientName&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Client Name: &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/input.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;input&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;search&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;size&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;25&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Please enter the search value&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;class&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;SelectOnEntry&amp;quot;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;searchClientName&amp;quot;&lt;/span&gt; /&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/input.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;input&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;button&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;btnClientsSearch&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Find / Refresh&amp;quot;&lt;/span&gt; /&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/div.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;div&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;style&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;padding-left: 150px; padding-top: 50px; padding-bottom: 50px;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;ClientsResults&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/table.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;table&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;flexClients&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;style&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;display: none&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/div.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;div&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;add-edit-dialog&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;style&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;display: none&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;title&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Add / Edit Client&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; @Html.Partial(&amp;quot;_ClientForm&amp;quot;, Model)&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb90254&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see, this main view has just few controls and lines of code. At the top I display two search controls and at the bottom the flexigrid placeholder and the dialog (invisible initially) for my modal Add/Edit dialogs.&lt;/p&gt;

&lt;p&gt;Now I want to show the Clients.js part that is responsible for the flexigrid and passing Search form data to the controller through form&#039;s collection:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;javascript&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;javascript&quot; id=&quot;cb40388&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;$(&amp;quot;#flexClients&amp;quot;).flexigrid({&lt;br /&gt;&amp;nbsp; &amp;nbsp; url: &#039;/Client/Client/&#039;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; dataType: &#039;json&#039;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; colModel: [&lt;br /&gt;&amp;nbsp; &amp;nbsp; { display: &#039;Client Id&#039;, name: &#039;Id&#039;, width: 100, sortable: true, align: &#039;center&#039;, hide: true },&lt;br /&gt;&amp;nbsp; &amp;nbsp; { display: &#039;Client #&#039;, name: &#039;Number&#039;, width: 100, sortable: true, align: &#039;center&#039; },&lt;br /&gt;&amp;nbsp; &amp;nbsp; { display: &#039;Name&#039;, name: &#039;Name&#039;, width: 350, sortable: true, align: &#039;center&#039; },&lt;br /&gt;&amp;nbsp; &amp;nbsp; { display: &#039;Contact 1&#039;, name: &#039;Contact1&#039;, width: 350, sortable: true, align: &#039;center&#039; },&lt;br /&gt;&amp;nbsp; &amp;nbsp; ],&lt;br /&gt;&amp;nbsp; &amp;nbsp; buttons: [&lt;br /&gt;&amp;nbsp; &amp;nbsp; { name: &#039;Add&#039;, bclass: &#039;add&#039;, onpress: add },&lt;br /&gt;&amp;nbsp; &amp;nbsp; { name: &#039;Edit&#039;, bclass: &#039;edit&#039;, onpress: edit },&lt;br /&gt;&amp;nbsp; &amp;nbsp; { name: &#039;Delete&#039;, bclass: &#039;delete&#039;, onpress: del },&lt;br /&gt;&amp;nbsp; &amp;nbsp; { separator: true }&lt;br /&gt;&amp;nbsp; &amp;nbsp; ],&lt;br /&gt;&amp;nbsp; &amp;nbsp; searchitems: [&lt;br /&gt;&amp;nbsp; &amp;nbsp; { display: &#039;Client Name&#039;, name: &#039;Name&#039; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; ],&lt;br /&gt;&amp;nbsp; &amp;nbsp; sortname: &amp;quot;Name&amp;quot;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; sortorder: &amp;quot;asc&amp;quot;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; usepager: true,&lt;br /&gt;&amp;nbsp; &amp;nbsp; title: &#039;Clients&#039;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; useRp: true,&lt;br /&gt;&amp;nbsp; &amp;nbsp; rp: 15,&lt;br /&gt;&amp;nbsp; &amp;nbsp; rpOptions: [5, 10, 15, 20, 25, 40],&lt;br /&gt;&amp;nbsp; &amp;nbsp; showTableToggleBtn: true,&lt;br /&gt;&amp;nbsp; &amp;nbsp; width: 900,&lt;br /&gt;&amp;nbsp; &amp;nbsp; onSuccess: bindDblClick,&lt;br /&gt;&amp;nbsp; &amp;nbsp; onSubmit: addFormData,&lt;br /&gt;&amp;nbsp; &amp;nbsp; hideOnSubmit: false,&lt;br /&gt;&amp;nbsp; &amp;nbsp; height: &#039;auto&#039;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; singleSelect: true&lt;br /&gt;});&lt;br /&gt;&amp;nbsp;&lt;br /&gt;//$(&#039;.flexigrid&#039;).on(&#039;dblclick&#039;, &#039;tr[id*=&amp;quot;row&amp;quot;]&#039;, function () {&lt;br /&gt;// &amp;nbsp; &amp;nbsp;// console.log(&#039;mouseenter rowId: &#039; + $(this).attr(&#039;id&#039;).substr(3));&lt;br /&gt;// &amp;nbsp; &amp;nbsp;alert($(this).attr(&#039;id&#039;).substr(3));&lt;br /&gt;// &amp;nbsp; &amp;nbsp;//Edit();&lt;br /&gt;//});&lt;br /&gt;&amp;nbsp;&lt;br /&gt;function bindDblClick()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; &amp;nbsp; $(&#039;#flexClients tr&#039;).dblclick(function () { &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;edit(&#039;Edit&#039;);&lt;br /&gt;&amp;nbsp; &amp;nbsp; });&lt;br /&gt;}&lt;br /&gt;&amp;nbsp;&lt;br /&gt;//This function adds parameters to the post of flexigrid. You can add a verification as well by return to false if you don&#039;t want flexigrid to submit&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;function addFormData() {&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; //passing a form object to serializeArray will get the valid data from all the objects, but, if the you pass a non-form object, you have to specify the input elements that the data will come from&lt;br /&gt;&amp;nbsp; &amp;nbsp; var dt = $(&amp;quot;#add-edit-form&amp;quot;).serializeArray();&lt;br /&gt;&amp;nbsp; &amp;nbsp; dt = dt.concat($(&#039;#frmClientsSearch&#039;).serializeArray());&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; $(&amp;quot;#flexClients&amp;quot;).flexOptions({ params: dt });&lt;br /&gt;&amp;nbsp; &amp;nbsp; return true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb70355&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, the first part defines flexigrid (you can see that it used Client controller Client method to return its data). Then the next piece of code binds grid&#039;s double click with the same method that is called by the Edit button (in other words, I want to either click on the Edit button or double click on the selected row in a grid to get the same behavior) and the last piece of code (addFormData) is used to pass both Search form and add-edit-form data back to my controller. &lt;/p&gt;

&lt;p&gt;And this is how Client method of the controller looks like (I separated this method into 2 based on the idea I took from looking at the &lt;a href=&quot;http://www.s4sme.com/Blog/Post/aspnet-mvc-4-application-with-flexigrid-jquery-ui-and-jquery-validation&quot;&gt;following solution&lt;/a&gt;)&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;c#&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;c#&quot; id=&quot;cb69685&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;HttpPost&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public ActionResult Client&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;FormCollection formValues&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Assume we want to select everything&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var clients = Db.&lt;span style=&quot;color: #202020;&quot;&gt;Clients&lt;/span&gt;; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Should set type of clients to IQueryable&amp;lt;Clients&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; searchClientNo = &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;TryParse&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;formValues&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;searchClientNo&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;, out searchClientNo &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; searchClientName = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;formValues&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;searchClientName&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;??&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;ToString&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;searchClientNo == &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt; &amp;amp;&amp;amp; searchClientName == &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Please enter the search value&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; clients = clients.&lt;span style=&quot;color: #202020;&quot;&gt;Where&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;c =&amp;gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;c.&lt;span style=&quot;color: #202020;&quot;&gt;Number&lt;/span&gt; == searchClientNo&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;searchClientNo != &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;//Number was supplied&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; clients = clients.&lt;span style=&quot;color: #202020;&quot;&gt;Where&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;c =&amp;gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;c.&lt;span style=&quot;color: #202020;&quot;&gt;Number&lt;/span&gt; == searchClientNo&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// If clientNo was supplied, clients is now filtered by that. If not, it still has the full list. The following will further filter it.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;!&lt;span style=&quot;color: #993333;&quot;&gt;String&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;IsNullOrWhiteSpace&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;searchClientName&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Part of the name was supplied&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; clients = clients.&lt;span style=&quot;color: #202020;&quot;&gt;Where&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;c =&amp;gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;c.&lt;span style=&quot;color: #202020;&quot;&gt;Name&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;searchClientName&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; page = &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Parse&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;formValues&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;page&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; ?? &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; rp = &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Parse&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;formValues&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;rp&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;??&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;15&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; qtype = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;formValues&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;qtype&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;??&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;ToString&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; query = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;formValues&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;query&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; ?? &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;ToString&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; sortname = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;formValues&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;sortname&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; ?? &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;ToString&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt; sortorder = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;formValues&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;sortorder&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; ?? &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;asc&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;ToString&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;!&lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;IsNullOrEmpty&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;sortname&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;amp;&amp;amp; !&lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;IsNullOrEmpty&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;sortorder&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; clients = clients.&lt;span style=&quot;color: #202020;&quot;&gt;OrderBy&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;sortname, &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;sortorder == &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;asc&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;!&lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;IsNullOrEmpty&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;qtype&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;amp;&amp;amp; !&lt;span style=&quot;color: #993333;&quot;&gt;string&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;IsNullOrEmpty&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;query&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; clients = clients.&lt;span style=&quot;color: #202020;&quot;&gt;Like&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;qtype, query&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; Total = clients.&lt;span style=&quot;color: #202020;&quot;&gt;Count&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; clients = clients.&lt;span style=&quot;color: #202020;&quot;&gt;Skip&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;page - &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; * rp&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Take&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;rp&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; CreateFlexiJson&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;clients, page, Total&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; private JsonResult CreateFlexiJson&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;IEnumerable&amp;lt;Client&amp;gt; items, &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; page, &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; total&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; Json&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; new&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&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; page,&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; total,&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; rows =&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; items&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; .&lt;span style=&quot;color: #202020;&quot;&gt;Select&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;x =&amp;gt;&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; &amp;nbsp; new&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; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; id = x.&lt;span style=&quot;color: #202020;&quot;&gt;Id&lt;/span&gt;,&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// either use GetPropertyList(x) method to get all columns &lt;/span&gt;&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; cell = new &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt; Id = x.&lt;span style=&quot;color: #202020;&quot;&gt;Id&lt;/span&gt;, Number = x.&lt;span style=&quot;color: #202020;&quot;&gt;Number&lt;/span&gt;, Name = x.&lt;span style=&quot;color: #202020;&quot;&gt;Name&lt;/span&gt;, Contact1 = x.&lt;span style=&quot;color: #202020;&quot;&gt;Contact1&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Contact&lt;/span&gt; ?? &lt;span style=&quot;color: #993333;&quot;&gt;String&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;Empty&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&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; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;, JsonRequestBehavior.&lt;span style=&quot;color: #202020;&quot;&gt;AllowGet&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb88737&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So far so good. Now I want to describe the modal dialogs implementation for Add and Edit buttons. This really took me a very long time to get working correctly and I was lucky to finally got this resolved with Jazzen Chen help.&lt;/p&gt;

&lt;p&gt;First I want to show the partial view _ClientForm. There is nothing too interesting in that view besides using special EditorForm I implemented based on &lt;a href=&quot;http://dotnetspeak.com/index.php/2012/10/asp-net-mvc-template-and-knockout-js/&quot;&gt;this blog post&lt;/a&gt; by Sergey Barskiy:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;html&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;html&quot; id=&quot;cb50813&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;@using CardNumbers.Helper&lt;br /&gt;@model CardNumbers.Models.ClientViewModel&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/form.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;form&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;add-edit-form&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/fieldset.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;fieldset&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/legend.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;legend&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;Client Info&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/legend&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @Html.ValidationSummary(true)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/input.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;input&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;hidden&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;fntype&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;fntype&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @Html.HiddenFor(m =&amp;gt; m.ClientId)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @Html.EditorFor(m =&amp;gt; m.ClientNumber, EditorTemplate.TextBox)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @Html.EditorFor(m =&amp;gt; m.ClientName, EditorTemplate.TextBox)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @Html.EditorFor(m =&amp;gt; m.Client.Address, EditorTemplate.EditBox)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/div.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;div&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;ContactsInfo&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/div.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;div&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Contact1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @Html.EditorFor(m =&amp;gt; m.Client.Contact1)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/div.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;div&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Contact2&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @Html.EditorFor(m =&amp;gt; m.Client.Contact2)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;@* &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/div.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;div&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;SaveCancel&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;class&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;float-right&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/button.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;button&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;btnSave&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Save&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://december.com/html/4/element/button.html&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;button&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;reset&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt; =&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;btnCancel&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Reset&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Cancel&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;*@&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/fieldset&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb22432&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is the Clients.js relevant code which drives that Add, Edit and Delete (for completeness) buttons:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;javascript&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;javascript&quot; id=&quot;cb90897&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;function del(com, grid) {&lt;br /&gt;&amp;nbsp; &amp;nbsp; try {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var clientName = $(&#039;.trSelected td:eq(2)&#039;).text();&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (clientName) //Variable is defined and not empty&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (confirm(&amp;quot;Are you sure you want to delete &amp;quot; + $.trim(clientName) + &amp;quot;?&amp;quot;)===false)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return false; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $(&#039;.trSelected&#039;, grid).each(function () {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var id = $(this).attr(&#039;id&#039;);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; id = id.substring(id.lastIndexOf(&#039;row&#039;) + 3);&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; addFormData(); $(&#039;#flexClients&#039;).flexOptions({ url: &#039;/Client/Delete/&#039;+ id }).flexReload();&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; });&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(e) {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; alert(&#039;Error &#039; + e);&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;}&lt;br /&gt;&amp;nbsp;&lt;br /&gt;var validator = $(&amp;quot;#add-edit-form&amp;quot;).validate();&lt;br /&gt;&amp;nbsp;&lt;br /&gt;var $dlg = $(&amp;quot;#add-edit-dialog&amp;quot;).dialog({&lt;br /&gt;&amp;nbsp; &amp;nbsp; autoOpen: false,&lt;br /&gt;&amp;nbsp; &amp;nbsp; show: &amp;quot;blind&amp;quot;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; closeOnEscape: true,&lt;br /&gt;&amp;nbsp; &amp;nbsp; resizable: true,&lt;br /&gt;&amp;nbsp; &amp;nbsp; width: 1200,&lt;br /&gt;&amp;nbsp; &amp;nbsp; height: 750,&lt;br /&gt;&amp;nbsp; &amp;nbsp; minHeight: 600,&lt;br /&gt;&amp;nbsp; &amp;nbsp; minWidth: 950,&lt;br /&gt;&amp;nbsp; &amp;nbsp; buttons: {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;quot;Save&amp;quot;: function () {&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if ($(&amp;quot;#add-edit-form&amp;quot;).valid()) {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // jobPost.setVals(txtId.val(), txtRole.val(), txtLocation.val(), txtJobType.val(), txtDescription.val()); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $.ajax({&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; type: &#039;POST&#039;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; //data: JSON.stringify(clientInformation),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; url: &#039;/Client/Save&#039;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dataType: &#039;json&#039;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; contentType: &#039;application/json&#039;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; success: function (result) {&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; // insert new list into grid&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; $(&#039;#flexClients&#039;).flexAddData(result);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; });&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $(this).dialog(&#039;close&#039;);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } else return false;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; },&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Cancel: function () {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $(this).dialog(&amp;quot;close&amp;quot;);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; clearForm();&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (validator)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; validator.resetForm();&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; close: function () { clearForm(); },&lt;br /&gt;&amp;nbsp; &amp;nbsp; open: function () {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; //$(&amp;quot;#add-edit-dialog&amp;quot;).parent().appendTo($(&amp;quot;#add-edit-form&amp;quot;));&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;});&lt;br /&gt;&amp;nbsp;&lt;br /&gt;function RunModalDialog(title, url)&lt;br /&gt;{ &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; if (title)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $dlg.dialog(&amp;quot;option&amp;quot;, {&amp;quot;title&amp;quot;: title });&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; if (url)&lt;br /&gt;&amp;nbsp; &amp;nbsp; { &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $dlg.load(url).dialog(&amp;quot;option&amp;quot;, { &amp;quot;title&amp;quot;: title }).dialog(&amp;quot;open&amp;quot;);&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; //$dlg.load(url, function () {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // &amp;nbsp; &amp;nbsp;var validator = $(&amp;quot;#sform&amp;quot;).validate();&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // &amp;nbsp; &amp;nbsp;if (validator)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; validator.resetForm();&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // &amp;nbsp; &amp;nbsp;$dlg.dialog(&amp;quot;option&amp;quot;, { &amp;quot;title&amp;quot;: title }).dialog(&amp;quot;open&amp;quot;);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; //});&lt;br /&gt;&amp;nbsp; &amp;nbsp; else {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $dlg.dialog(&amp;quot;open&amp;quot;);&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;}&lt;br /&gt;&amp;nbsp;&lt;br /&gt;function add(com, grid) {&lt;br /&gt;&amp;nbsp; &amp;nbsp; $(&#039;#fntype&#039;).val(&#039;Add&#039;);&lt;br /&gt;&amp;nbsp; &amp;nbsp; var url = &#039;/Client/Add/&#039;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; //location.replace(url);&lt;br /&gt;&amp;nbsp; &amp;nbsp; RunModalDialog(&amp;quot;Create New Client&amp;quot;);&lt;br /&gt;&amp;nbsp; &amp;nbsp;// clearForm(); &amp;nbsp; &amp;nbsp;&lt;br /&gt;}&lt;br /&gt;&amp;nbsp;&lt;br /&gt;function edit(com, grid)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $(&#039;.trSelected&#039;, grid).each(function () {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var id = $(this).attr(&#039;id&#039;);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; id = id.substring(id.lastIndexOf(&#039;row&#039;) + 3);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; currentId = id;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $(&#039;#fntype&#039;).val(&#039;Edit&#039;);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var ClientName;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ClientName =$(&#039;.trSelected td:eq(2)&#039;).text();&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var url = &#039;/Client/Edit/&#039; + id ;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $.get(url, function (html) {&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;setFormControls(data.Id, data.Role, data.Location, data.JobType, data.Description);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // alert(data);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $($dlg).html(html);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; });&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//location.replace(url);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;RunModalDialog(&amp;quot;Edit Client: &amp;quot; + ClientName);&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;cb60694&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The most complicated part in the above script was to figure out how to make a server trip to get data for the Edit button and return that page back for editing. &lt;br /&gt;
This is done with these two lines of code:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;javascript&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;javascript&quot; id=&quot;cb44997&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;$.get(url, function (html) { &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $($dlg).html(html);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; });&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb56993&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And to make the whole picture complete I&#039;ll show the Edit method of my controller that returns the above data:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;c#&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;c#&quot; id=&quot;cb80223&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;public ActionResult Edit&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; id&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ClientViewModel model = new ClientViewModel&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var client = Db.&lt;span style=&quot;color: #202020;&quot;&gt;GetClientById&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;id&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; model.&lt;span style=&quot;color: #202020;&quot;&gt;Client&lt;/span&gt; = client;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; PartialView&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;_ClientForm&amp;quot;&lt;/span&gt;,model&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb97647&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see, just a few lines of code to return the model data and the partial view.&lt;/p&gt;

&lt;p&gt;This all may seem very easy now, but I can tell you it was not easy to figure all the pieces out.&lt;/p&gt;

&lt;p&gt;I am not showing certain functionality here such as my view model or EditorFor custom editors. I also don&#039;t show the RemoteValidator used to provide uniqueness of the client number and client name. If there will be an interest, I may discuss these details in the separate blog post.&lt;/p&gt;

&lt;p&gt;Hope this blog post can help people struggling with the modal dialogs implementation in ASP.NET MVC application.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
*** &lt;strong&gt;Remember, if you have a Web application related question, try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=6&amp;amp;sid=ebc29b65310d77fa409af74cf8d5cd70&quot;&gt;Web Developer&lt;/a&gt; forum.&lt;/strong&gt;&lt;ins&gt;&lt;/ins&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/WebDev/UIDevelopment/AJAX/asp-net-mvc-project-with&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>In the last few months I started working on my first ASP.NET MVC project. I already had a Visual FoxPro application I wrote in a few days and I wanted to replicate it for the Web using ASP.NET MVC. This project is still in its infancy, but I want to share my experience and some success I have with it.</p>

<p>I also first want to thank people who helped me to get where I am now with this project: Viv Phillips and Paul Mrozowski from UniversalThread, Darin Dimitrov from stackoverflow, Alex Ullrich and Sergey Kosik and also Jazzen Chen from MS. I also want to thank authors of many blogs and articles I read while working on this project.</p>

<p>I started my project from defining my models using Entity Framework Code first pattern since I already had the database defined. I am going to discuss only one model "Client" for the Clients table.</p>

<p>This is how the table looks in the database:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb18081'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb18081','cb50673'); 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="cb18081" 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> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>ClientID<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">int</span><span style="color: #808080;">&#93;</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; <span style="color: #808080;">&#91;</span>client_no<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">smallint</span><span style="color: #808080;">&#93;</span> NOT NULL,</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>client_name<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>Contact1<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>C1_Email<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>Contact2<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>C2_Email<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>C1_Phone<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>C1_Ext<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">5</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>C2_Phone<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>C2_Ext<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">5</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Address</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>EnteredBy<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">char</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>EnteredOn<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">smalldatetime</span><span style="color: #808080;">&#93;</span> NULL,</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>ModifiedBy<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">char</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span> NULL,</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>ModifiedOn<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">smalldatetime</span><span style="color: #808080;">&#93;</span> NULL,</li><li style="" class="li2">&nbsp;<span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>PK_Clients_ClientID<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span> </li><li style="" class="li1"><span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>ClientID<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ASC</span></li><li style="" class="li1"><span style="color: #808080;">&#41;</span><span style="color: #0000FF;">WITH</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">PAD_INDEX</span> &nbsp;= <span style="color: #0000FF;">OFF</span>, <span style="color: #0000FF;">STATISTICS_NORECOMPUTE</span> &nbsp;= <span style="color: #0000FF;">OFF</span>, <span style="color: #0000FF;">IGNORE_DUP_KEY</span> = <span style="color: #0000FF;">OFF</span>, <span style="color: #0000FF;">ALLOW_ROW_LOCKS</span> &nbsp;= <span style="color: #0000FF;">ON</span>, <span style="color: #0000FF;">ALLOW_PAGE_LOCKS</span> &nbsp;= <span style="color: #0000FF;">ON</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">ON</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">PRIMARY</span><span style="color: #808080;">&#93;</span></li><li style="" class="li2"><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">ON</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">PRIMARY</span><span style="color: #808080;">&#93;</span> TEXTIMAGE_ON <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">PRIMARY</span><span style="color: #808080;">&#93;</span></li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li><li style="" class="li2"><span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">ANSI_PADDING</span> <span style="color: #0000FF;">OFF</span></li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">NONCLUSTERED</span> <span style="color: #0000FF;">INDEX</span> <span style="color: #808080;">&#91;</span>idxClients_Client_No<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ON</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> </li><li style="" class="li1"><span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #808080;">&#91;</span>client_no<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ASC</span></li><li style="" class="li1"><span style="color: #808080;">&#41;</span><span style="color: #0000FF;">WITH</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">PAD_INDEX</span> &nbsp;= <span style="color: #0000FF;">OFF</span>, <span style="color: #0000FF;">STATISTICS_NORECOMPUTE</span> &nbsp;= <span style="color: #0000FF;">OFF</span>, <span style="color: #0000FF;">SORT_IN_TEMPDB</span> = <span style="color: #0000FF;">OFF</span>, <span style="color: #0000FF;">IGNORE_DUP_KEY</span> = <span style="color: #0000FF;">OFF</span>, DROP_EXISTING = <span style="color: #0000FF;">OFF</span>, <span style="color: #0000FF;">ONLINE</span> = <span style="color: #0000FF;">OFF</span>, <span style="color: #0000FF;">ALLOW_ROW_LOCKS</span> &nbsp;= <span style="color: #0000FF;">ON</span>, <span style="color: #0000FF;">ALLOW_PAGE_LOCKS</span> &nbsp;= <span style="color: #0000FF;">ON</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">ON</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">PRIMARY</span><span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">/****** Object: &nbsp;Default [DF_Clients_EnteredBy] &nbsp; &nbsp;Script Date: 12/19/2012 07:00:35 ******/</span></li><li style="" class="li2"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ADD</span> &nbsp;<span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>DF_Clients_EnteredBy<span style="color: #808080;">&#93;</span> &nbsp;<span style="color: #0000FF;">DEFAULT</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'ADMIN'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FOR</span> <span style="color: #808080;">&#91;</span>EnteredBy<span style="color: #808080;">&#93;</span></li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li><li style="" class="li2"><span style="color: #00AF00;">/****** Object: &nbsp;Default [DF_Clients_EnteredOn] &nbsp; &nbsp;Script Date: 12/19/2012 07:00:35 ******/</span></li><li style="" class="li1"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ADD</span> &nbsp;<span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>DF_Clients_EnteredOn<span style="color: #808080;">&#93;</span> &nbsp;<span style="color: #0000FF;">DEFAULT</span> <span style="color: #808080;">&#40;</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> <span style="color: #0000FF;">FOR</span> <span style="color: #808080;">&#91;</span>EnteredOn<span style="color: #808080;">&#93;</span></li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1"><span style="color: #00AF00;">/****** Object: &nbsp;Check [CK_Clients_C1_Phone] &nbsp; &nbsp;Script Date: 12/19/2012 07:00:35 ******/</span></li><li style="" class="li2"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> &nbsp;<span style="color: #0000FF;">WITH</span> <span style="color: #0000FF;">CHECK</span> <span style="color: #0000FF;">ADD</span> &nbsp;<span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>CK_Clients_C1_Phone<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">CHECK</span> &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>C1_Phone<span style="color: #808080;">&#93;</span> like <span style="color: #FF0000;">'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li><li style="" class="li2"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">CHECK</span> <span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>CK_Clients_C1_Phone<span style="color: #808080;">&#93;</span></li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li><li style="" class="li2"><span style="color: #00AF00;">/****** Object: &nbsp;Check [CK_Clients_C2_Phone] &nbsp; &nbsp;Script Date: 12/19/2012 07:00:35 ******/</span></li><li style="" class="li1"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> &nbsp;<span style="color: #0000FF;">WITH</span> <span style="color: #0000FF;">CHECK</span> <span style="color: #0000FF;">ADD</span> &nbsp;<span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>CK_Clients_C2_Phone<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">CHECK</span> &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>C2_Phone<span style="color: #808080;">&#93;</span> like <span style="color: #FF0000;">'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">CHECK</span> <span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>CK_Clients_C2_Phone<span style="color: #808080;">&#93;</span></li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1"><span style="color: #00AF00;">/****** Object: &nbsp;ForeignKey [FK_Clients_Operators_EnteredBy] &nbsp; &nbsp;Script Date: 12/19/2012 07:00:35 ******/</span></li><li style="" class="li2"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> &nbsp;<span style="color: #0000FF;">WITH</span> <span style="color: #0000FF;">CHECK</span> <span style="color: #0000FF;">ADD</span> &nbsp;<span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>FK_Clients_Operators_EnteredBy<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FOREIGN</span> <span style="color: #0000FF;">KEY</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>EnteredBy<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">REFERENCES</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Operators<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>op_code<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">CHECK</span> <span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>FK_Clients_Operators_EnteredBy<span style="color: #808080;">&#93;</span></li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1"><span style="color: #00AF00;">/****** Object: &nbsp;ForeignKey [FK_Clients_Operators_ModifiedBy] &nbsp; &nbsp;Script Date: 12/19/2012 07:00:35 ******/</span></li><li style="" class="li2"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> &nbsp;<span style="color: #0000FF;">WITH</span> <span style="color: #0000FF;">CHECK</span> <span style="color: #0000FF;">ADD</span> &nbsp;<span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>FK_Clients_Operators_ModifiedBy<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FOREIGN</span> <span style="color: #0000FF;">KEY</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>ModifiedBy<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">REFERENCES</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Operators<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>op_code<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">CHECK</span> <span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>FK_Clients_Operators_ModifiedBy<span style="color: #808080;">&#93;</span></li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb50673" style="display: none; color: red;"></div></div></div>

<p>Last 2 constraints in the above script refer to the Operators table I am not showing here.</p>

<p>Let's add some data to insert:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb38667'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb38667','cb5320'); 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="cb38667" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">GO</span></li><li style="" class="li2"><span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">IDENTITY_INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ON</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>ClientID<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_no<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_name<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact1<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact2<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Address</span><span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredOn<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedOn<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">3</span>, <span style="color: #000;">8096</span>, N<span style="color: #FF0000;">'Wachusett'</span>, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N<span style="color: #FF0000;">'ADMIN '</span>, <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>0x9F9002DD <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">SmallDateTime</span><span style="color: #808080;">&#41;</span>, NULL, NULL<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>ClientID<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_no<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_name<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact1<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact2<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Address</span><span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredOn<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedOn<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>, <span style="color: #000;">6700</span>, N<span style="color: #FF0000;">'Buck Hill'</span>, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N<span style="color: #FF0000;">'ADMIN '</span>, <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>0x9F9002DD <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">SmallDateTime</span><span style="color: #808080;">&#41;</span>, NULL, NULL<span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>ClientID<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_no<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_name<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact1<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact2<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Address</span><span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredOn<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedOn<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">5</span>, <span style="color: #000;">6238</span>, N<span style="color: #FF0000;">'Jamz'</span>, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N<span style="color: #FF0000;">'ADMIN '</span>, <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>0x9F9002DD <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">SmallDateTime</span><span style="color: #808080;">&#41;</span>, NULL, NULL<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>ClientID<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_no<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_name<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact1<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact2<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Address</span><span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredOn<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedOn<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">7</span>, <span style="color: #000;">8363</span>, N<span style="color: #FF0000;">'Okemo'</span>, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N<span style="color: #FF0000;">'ADMIN '</span>, <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>0x9F9002DD <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">SmallDateTime</span><span style="color: #808080;">&#41;</span>, NULL, NULL<span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>ClientID<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_no<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_name<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact1<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact2<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Address</span><span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredOn<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedOn<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">8</span>, <span style="color: #000;">1161</span>, N<span style="color: #FF0000;">'Shawnee Mountain'</span>, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N<span style="color: #FF0000;">'ADMIN '</span>, <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>0x9F9002DD <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">SmallDateTime</span><span style="color: #808080;">&#41;</span>, NULL, NULL<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>ClientID<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_no<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_name<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact1<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact2<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Address</span><span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredOn<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedOn<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">9</span>, <span style="color: #000;">7497</span>, N<span style="color: #FF0000;">'Southern Star Obs Wheel'</span>, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N<span style="color: #FF0000;">'ADMIN '</span>, <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>0x9F9002DD <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">SmallDateTime</span><span style="color: #808080;">&#41;</span>, NULL, NULL<span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>ClientID<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_no<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_name<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact1<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact2<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Address</span><span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredOn<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedOn<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">10</span>, <span style="color: #000;">1636</span>, N<span style="color: #FF0000;">'Mount Rose'</span>, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N<span style="color: #FF0000;">'ADMIN '</span>, <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>0x9F9002DD <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">SmallDateTime</span><span style="color: #808080;">&#41;</span>, NULL, NULL<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Clients<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>ClientID<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_no<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>client_name<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact1<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Contact2<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Email<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C1_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Phone<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>C2_Ext<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Address</span><span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>EnteredOn<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedBy<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>ModifiedOn<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">11</span>, <span style="color: #000;">3951</span>, N<span style="color: #FF0000;">'Crystal Mtn Washington'</span>, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N<span style="color: #FF0000;">'ADMIN '</span>, <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>0x9F9002DD <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">SmallDateTime</span><span style="color: #808080;">&#41;</span>, NULL, NULL<span style="color: #808080;">&#41;</span></li></ol></div><div id="cb5320" style="display: none; color: red;"></div></div></div>

<p>It took me many iterations and consultations with various blogs about Entity Framework to come up with the following model:</p>

<div class="codebox"><div class="codeheader"><span>c#</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb11351'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb11351','cb52300'); 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="c#" id="cb11351" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">using System;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">using System.<span style="color: #202020;">ComponentModel</span>.<span style="color: #202020;">DataAnnotations</span>;</li><li style="" class="li2">using System.<span style="color: #202020;">ComponentModel</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">using DataAnnotationsExtensions;</li><li style="" class="li1">using System.<span style="color: #202020;">ComponentModel</span>.<span style="color: #202020;">DataAnnotations</span>.<span style="color: #202020;">Schema</span>;</li><li style="" class="li2">using System.<span style="color: #202020;">Collections</span>.<span style="color: #202020;">Generic</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">namespace CardNumbers.<span style="color: #202020;">Objects</span></li><li style="" class="li1"><span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>ComplexType<span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; public class PhoneInfo</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DataType<span style="color: #66cc66;">&#40;</span>DataType.<span style="color: #202020;">PhoneNumber</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DisplayName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Phone&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>RegularExpression<span style="color: #66cc66;">&#40;</span>@<span style="color: #ff0000;">&quot;^((<span style="color: #000099; font-weight: bold;">\(</span><span style="color: #000099; font-weight: bold;">\d</span>{3}<span style="color: #000099; font-weight: bold;">\)</span>|<span style="color: #000099; font-weight: bold;">\d</span>{3})<span style="color: #000099; font-weight: bold;">\s</span>?)?<span style="color: #000099; font-weight: bold;">\d</span>{3}[-<span style="color: #000099; font-weight: bold;">\s</span>]?<span style="color: #000099; font-weight: bold;">\d</span>{4}<span style="color: #000099; font-weight: bold;">\s</span>*$&quot;</span>, ErrorMessage = <span style="color: #ff0000;">&quot;Please enter valid Phone Number&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public virtual <span style="color: #993333;">string</span> Phone <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>StringLength<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DisplayName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Ext&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public virtual <span style="color: #993333;">string</span> Ext <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public bool HasValue</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>Phone != <span style="color: #000000; font-weight: bold;">null</span> || Ext != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>ComplexType<span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; public class ContactDetail</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//Constructor</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public ContactDetail<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; phoneInfo = new PhoneInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>StringLength<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DisplayName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Contact Name&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DisplayFormat<span style="color: #66cc66;">&#40;</span>NullDisplayText = <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public virtual <span style="color: #993333;">string</span> Contact <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>Email<span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>StringLength<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DataType<span style="color: #66cc66;">&#40;</span>DataType.<span style="color: #202020;">EmailAddress</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DisplayName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Email&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public virtual <span style="color: #993333;">string</span> Email <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public virtual PhoneInfo phoneInfo <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public bool HasValue</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>Contact != <span style="color: #000000; font-weight: bold;">null</span> || Email != <span style="color: #000000; font-weight: bold;">null</span> || phoneInfo.<span style="color: #202020;">HasValue</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #808080; font-style: italic;">/// &lt;summary&gt;</span></li><li style="" class="li2"><span style="color: #808080; font-style: italic;">/// Client class (Client No, Client Name, Address, Contact1, Contact2 info, Created By, Modified By (operator and date)</span></li><li style="" class="li1"><span style="color: #808080; font-style: italic;">/// &lt;/summary&gt;</span></li><li style="" class="li2">&nbsp; &nbsp; public class Client</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Contact1 = new ContactDetail<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Contact2 = new ContactDetail<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>Key<span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>Editable<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>Column<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ClientId&quot;</span>,TypeName = <span style="color: #ff0000;">&quot;int&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public virtual <span style="color: #993333;">int</span> Id <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//[Required]</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DisplayName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Client No&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> &nbsp; &nbsp; &nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>Column<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;client_no&quot;</span>, TypeName = <span style="color: #ff0000;">&quot;smallint&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public virtual Int16 Number <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//[Required]</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>Column<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;client_name&quot;</span>, TypeName = <span style="color: #ff0000;">&quot;varchar&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DisplayName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Client Name&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>MaxLength<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span>, ErrorMessage = <span style="color: #ff0000;">&quot;Client Name should not be longer than 30 characters&quot;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>MinLength<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>, ErrorMessage = <span style="color: #ff0000;">&quot;Client Name is too short&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public virtual <span style="color: #993333;">string</span> Name <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DataType<span style="color: #66cc66;">&#40;</span>DataType.<span style="color: #202020;">MultilineText</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public virtual <span style="color: #993333;">string</span> Address <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public virtual ContactDetail Contact1 <span style="color: #66cc66;">&#123;</span>get; set;<span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public virtual ContactDetail Contact2 <span style="color: #66cc66;">&#123;</span>get; set;<span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>ForeignKey<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;EnteredByOperator&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public <span style="color: #993333;">string</span> EnteredBy <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>InverseProperty<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ClientsEnteredBy&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public virtual Operator EnteredByOperator <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>ForeignKey<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ModifiedByOperator&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public <span style="color: #993333;">string</span> ModifiedBy <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>InverseProperty<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ClientsUpdatedBy&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public virtual Operator ModifiedByOperator <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DataType<span style="color: #66cc66;">&#40;</span>DataType.<span style="color: #202020;">DateTime</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DisplayName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Created on&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public DateTime EnteredOn <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DataType<span style="color: #66cc66;">&#40;</span>DataType.<span style="color: #202020;">DateTime</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>DisplayName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Modified on&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public DateTime? ModifiedOn <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public virtual ICollection&lt;ClientOrder&gt; ClientOrders <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public virtual ICollection&lt;Reorder&gt; Reorders <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2"><span style="color: #66cc66;">&#125;</span></li></ol></div><div id="cb52300" style="display: none; color: red;"></div></div></div>

<p>The interesting thing in this model is 2 Complex Types. Originally this model didn't have Complex Types, but I found later that I need them in order to create one Editor for each Contact information (as we see, the table is not properly normalized and contains 2 contacts columns). </p>

<p>I created all my models in one project called CardNumbers.Objects. I later was criticized for such project name, but I used the same naming convention that was used by our 3 days course teacher and therefore left it as is. In my next project I am going to be more thoughtful in regards to project names.</p>

<p>I also created another project called CardNumbers.Data where I placed repository objects. I also originally built them the same way we were taught by the instructor, but as I found later it was not the best way, so I re-designed based on some blog posts and StackOverflow questions this way:</p>

<p>IClientRepository.cs:</p>
<div class="codebox"><div class="codeheader"><span>c#</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb51441'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb51441','cb48202'); 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="c#" id="cb51441" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">using CardNumbers.<span style="color: #202020;">Objects</span>;</li><li style="" class="li2">using System;</li><li style="" class="li1">using System.<span style="color: #202020;">Collections</span>.<span style="color: #202020;">Generic</span>;</li><li style="" class="li2">using System.<span style="color: #202020;">Linq</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">namespace CardNumbers.<span style="color: #202020;">Data</span></li><li style="" class="li1"><span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; public interface IClientRepository:IDisposable</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; IQueryable&lt;Client&gt; Clients <span style="color: #66cc66;">&#123;</span> get; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Client GetClientById<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> clientId<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">void</span> Commit<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">void</span> AddClient<span style="color: #66cc66;">&#40;</span>Client client, bool autoCommit = <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">void</span> DeleteClient<span style="color: #66cc66;">&#40;</span>Client client, bool autoCommit = <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">void</span> DeleteClient<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> clientId<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">void</span> UpdateClient<span style="color: #66cc66;">&#40;</span>Client client, bool autoCommit = <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2"><span style="color: #66cc66;">&#125;</span></li></ol></div><div id="cb48202" style="display: none; color: red;"></div></div></div>

<p>and the actual implementation of the above interface:</p>
<div class="codebox"><div class="codeheader"><span>c#</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb26376'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb26376','cb13635'); 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="c#" id="cb26376" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">using System;</li><li style="" class="li2">using System.<span style="color: #202020;">Collections</span>.<span style="color: #202020;">Generic</span>;</li><li style="" class="li1">using System.<span style="color: #202020;">Data</span>;</li><li style="" class="li2">using System.<span style="color: #202020;">Data</span>.<span style="color: #202020;">Entity</span>;</li><li style="" class="li1">using System.<span style="color: #202020;">Linq</span>;</li><li style="" class="li2">using CardNumbers.<span style="color: #202020;">Objects</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">namespace CardNumbers.<span style="color: #202020;">Data</span></li><li style="" class="li1"><span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; public class ClientRepository : IClientRepository, IDisposable </li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; private CardNumbersContext context;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public ClientRepository<span style="color: #66cc66;">&#40;</span>CardNumbersContext context<span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #202020;">context</span> = context;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; IQueryable&lt;Client&gt; IClientRepository.<span style="color: #202020;">Clients</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get <span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">return</span> this.<span style="color: #202020;">context</span>.<span style="color: #202020;">Clients</span>; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public <span style="color: #993333;">void</span> Commit<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #202020;">context</span>.<span style="color: #202020;">SaveChanges</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public <span style="color: #993333;">void</span> AddClient<span style="color: #66cc66;">&#40;</span>Client client, bool autoCommit = <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client.<span style="color: #202020;">EnteredOn</span> = DateTime.<span style="color: #202020;">Now</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #202020;">context</span>.<span style="color: #202020;">Clients</span>.<span style="color: #202020;">Add</span><span style="color: #66cc66;">&#40;</span>client<span style="color: #66cc66;">&#41;</span> &nbsp; ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>autoCommit<span style="color: #66cc66;">&#41;</span> this.<span style="color: #202020;">Commit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public <span style="color: #993333;">void</span> DeleteClient<span style="color: #66cc66;">&#40;</span>Client client, bool autoCommit = <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #202020;">context</span>.<span style="color: #202020;">Clients</span>.<span style="color: #202020;">Remove</span><span style="color: #66cc66;">&#40;</span>client<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>autoCommit<span style="color: #66cc66;">&#41;</span> this.<span style="color: #202020;">Commit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public <span style="color: #993333;">void</span> DeleteClient<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> id<span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #202020;">context</span>.<span style="color: #202020;">Clients</span>.<span style="color: #202020;">Remove</span><span style="color: #66cc66;">&#40;</span>this.<span style="color: #202020;">GetClientById</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #202020;">Commit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public Client GetClientById<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> id<span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> this.<span style="color: #202020;">context</span>.<span style="color: #202020;">Clients</span>.<span style="color: #202020;">Find</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public <span style="color: #993333;">void</span> UpdateClient<span style="color: #66cc66;">&#40;</span>Client client, bool autoCommit = <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client.<span style="color: #202020;">ModifiedOn</span> = DateTime.<span style="color: #202020;">Now</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #202020;">context</span>.<span style="color: #202020;">Entry</span><span style="color: #66cc66;">&#40;</span>client<span style="color: #66cc66;">&#41;</span>.<span style="color: #202020;">State</span> = EntityState.<span style="color: #202020;">Modified</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>autoCommit<span style="color: #66cc66;">&#41;</span> this.<span style="color: #202020;">Commit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; private bool disposed = <span style="color: #000000; font-weight: bold;">false</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; protected virtual <span style="color: #993333;">void</span> Dispose<span style="color: #66cc66;">&#40;</span>bool disposing<span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!this.<span style="color: #202020;">disposed</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>disposing<span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #202020;">Dispose</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #202020;">disposed</span> = <span style="color: #000000; font-weight: bold;">true</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public <span style="color: #993333;">void</span> Dispose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dispose<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GC.<span style="color: #202020;">SuppressFinalize</span><span style="color: #66cc66;">&#40;</span>this<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1"><span style="color: #66cc66;">&#125;</span></li></ol></div><div id="cb13635" style="display: none; color: red;"></div></div></div>

<p>and I show CardNumbersContext.cs for completion:</p>
<div class="codebox"><div class="codeheader"><span>c#</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb27178'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb27178','cb9066'); 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="c#" id="cb27178" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">using System;</li><li style="" class="li2">using System.<span style="color: #202020;">Collections</span>.<span style="color: #202020;">Generic</span>;</li><li style="" class="li1">using System.<span style="color: #202020;">Data</span>.<span style="color: #202020;">Entity</span>;</li><li style="" class="li2">using CardNumbers.<span style="color: #202020;">Objects</span>;</li><li style="" class="li1">using System.<span style="color: #202020;">Data</span>.<span style="color: #202020;">Entity</span>.<span style="color: #202020;">ModelConfiguration</span>.<span style="color: #202020;">Conventions</span>;</li><li style="" class="li2">using CardNumbers.<span style="color: #202020;">Objects</span>.<span style="color: #202020;">Mapping</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">namespace CardNumbers.<span style="color: #202020;">Data</span></li><li style="" class="li1"><span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; public class CardNumbersContext : DbContext</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public DbSet&lt;Client&gt; Clients <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public DbSet&lt;Operator&gt; Operators <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; public DbSet&lt;Reorder&gt; Reorders <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public DbSet&lt;ClientOrder&gt; ClientOrders <span style="color: #66cc66;">&#123;</span> get; set; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; protected override <span style="color: #993333;">void</span> OnModelCreating<span style="color: #66cc66;">&#40;</span>DbModelBuilder modelBuilder<span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// base.OnModelCreating(modelBuilder);</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modelBuilder.<span style="color: #202020;">Configurations</span>.<span style="color: #202020;">Add</span><span style="color: #66cc66;">&#40;</span>new ClientMap<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modelBuilder.<span style="color: #202020;">Configurations</span>.<span style="color: #202020;">Add</span><span style="color: #66cc66;">&#40;</span>new OperatorMap<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modelBuilder.<span style="color: #202020;">Configurations</span>.<span style="color: #202020;">Add</span><span style="color: #66cc66;">&#40;</span>new ClientOrdersMap<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modelBuilder.<span style="color: #202020;">Configurations</span>.<span style="color: #202020;">Add</span><span style="color: #66cc66;">&#40;</span>new ReorderMap<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1"><span style="color: #66cc66;">&#125;</span></li></ol></div><div id="cb9066" style="display: none; color: red;"></div></div></div>

<p>So, the above two projects describe my models and means to work with them.</p>

<p>Now I am going to describe the CardNumbers.Web project. </p>

<p>I started from designing the view for working with the Clients. Here I didn't want to go with the standard shown in all tutorials ways of displaying all clients information in a one page grid with inline Edit/Delete buttons. I didn't like this interface much. So, I started my research as what existing grids I may try to implement. I looked up a few different grid implementations and at some point someone suggested me to look into <a href="http://flexigrid.info/">flexigrid</a> CodePlex project. I really liked its look and decided to go with it. Also, I believe that same person suggested me <a href="http://mvc4beginner.com/Sample-Code/Insert-Update-Delete/Asp-.Net-MVC-Ajax-Insert-Update-Delete-Using-Flexigrid.html">his site</a> which was enough to get me started. I got the Clients view working relatively quickly based on that information.</p>

<p>However, I didn't want to start from displaying all Clients. I wanted to simulate my current VFP application where I started from the empty grid, typed some letters in the client name search textbox and based on that show only small subset of rows initially.</p>

<p>This took me a while to figure out. I was almost ready to give up, but finally found addFormData method (which was there all the time) and figured out how to use it for my purpose. It may seem silly to most of the experienced jquery developers, but for me it was my first mini-success.</p>

<p>So, I will show you how my Client View now looks and the relevant portions of Clients.js script file and the ClientController.cs file:</p>

<div class="codebox"><div class="codeheader"><span>html</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb60384'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb60384','cb10141'); 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="html" id="cb60384" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">@model CardNumbers.Models.ClientViewModel</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">@section scripts {</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;@Url.Content(&quot;</span>~/Scripts/Clients.js<span style="color: #ff0000;">&quot;)&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></li><li style="" class="li1">}</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #009900;"><a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">&lt;form</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;frmClientsSearch&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/label.html"><span style="color: #000000; font-weight: bold;">&lt;label</span></a> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;clientNo&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Client No: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;number&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchClientNo&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;numericOnly&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">&lt;br</span></a> /<span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/label.html"><span style="color: #000000; font-weight: bold;">&lt;label</span></a> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;clientName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Client Name: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;search&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;25&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Please enter the search value&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;SelectOnEntry&quot;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchClientName&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;btnClientsSearch&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Find / Refresh&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li1"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form&gt;</span></span></li><li style="" class="li2"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;padding-left: 150px; padding-top: 50px; padding-bottom: 50px;&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ClientsResults&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/table.html"><span style="color: #000000; font-weight: bold;">&lt;table</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;flexClients&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: none&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table&gt;</span></span></li><li style="" class="li1"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp;<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;add-edit-dialog&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: none&quot;</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;Add / Edit Client&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> </li><li style="" class="li2">&nbsp; &nbsp; @Html.Partial(&quot;_ClientForm&quot;, Model)</li><li style="" class="li1"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></li></ol></div><div id="cb10141" style="display: none; color: red;"></div></div></div>

<p>As you can see, this main view has just few controls and lines of code. At the top I display two search controls and at the bottom the flexigrid placeholder and the dialog (invisible initially) for my modal Add/Edit dialogs.</p>

<p>Now I want to show the Clients.js part that is responsible for the flexigrid and passing Search form data to the controller through form's collection:</p>

<div class="codebox"><div class="codeheader"><span>javascript</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb89439'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb89439','cb48996'); 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="javascript" id="cb89439" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">$(&quot;#flexClients&quot;).flexigrid({</li><li style="" class="li2">&nbsp; &nbsp; url: '/Client/Client/',</li><li style="" class="li1">&nbsp; &nbsp; dataType: 'json',</li><li style="" class="li2">&nbsp; &nbsp; colModel: [</li><li style="" class="li1">&nbsp; &nbsp; { display: 'Client Id', name: 'Id', width: 100, sortable: true, align: 'center', hide: true },</li><li style="" class="li2">&nbsp; &nbsp; { display: 'Client #', name: 'Number', width: 100, sortable: true, align: 'center' },</li><li style="" class="li1">&nbsp; &nbsp; { display: 'Name', name: 'Name', width: 350, sortable: true, align: 'center' },</li><li style="" class="li2">&nbsp; &nbsp; { display: 'Contact 1', name: 'Contact1', width: 350, sortable: true, align: 'center' },</li><li style="" class="li1">&nbsp; &nbsp; ],</li><li style="" class="li2">&nbsp; &nbsp; buttons: [</li><li style="" class="li1">&nbsp; &nbsp; { name: 'Add', bclass: 'add', onpress: add },</li><li style="" class="li2">&nbsp; &nbsp; { name: 'Edit', bclass: 'edit', onpress: edit },</li><li style="" class="li1">&nbsp; &nbsp; { name: 'Delete', bclass: 'delete', onpress: del },</li><li style="" class="li2">&nbsp; &nbsp; { separator: true }</li><li style="" class="li1">&nbsp; &nbsp; ],</li><li style="" class="li2">&nbsp; &nbsp; searchitems: [</li><li style="" class="li1">&nbsp; &nbsp; { display: 'Client Name', name: 'Name' }</li><li style="" class="li2">&nbsp; &nbsp; ],</li><li style="" class="li1">&nbsp; &nbsp; sortname: &quot;Name&quot;,</li><li style="" class="li2">&nbsp; &nbsp; sortorder: &quot;asc&quot;,</li><li style="" class="li1">&nbsp; &nbsp; usepager: true,</li><li style="" class="li2">&nbsp; &nbsp; title: 'Clients',</li><li style="" class="li1">&nbsp; &nbsp; useRp: true,</li><li style="" class="li2">&nbsp; &nbsp; rp: 15,</li><li style="" class="li1">&nbsp; &nbsp; rpOptions: [5, 10, 15, 20, 25, 40],</li><li style="" class="li2">&nbsp; &nbsp; showTableToggleBtn: true,</li><li style="" class="li1">&nbsp; &nbsp; width: 900,</li><li style="" class="li2">&nbsp; &nbsp; onSuccess: bindDblClick,</li><li style="" class="li1">&nbsp; &nbsp; onSubmit: addFormData,</li><li style="" class="li2">&nbsp; &nbsp; hideOnSubmit: false,</li><li style="" class="li1">&nbsp; &nbsp; height: 'auto',</li><li style="" class="li2">&nbsp; &nbsp; singleSelect: true</li><li style="" class="li1">});</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">//$('.flexigrid').on('dblclick', 'tr[id*=&quot;row&quot;]', function () {</li><li style="" class="li2">// &nbsp; &nbsp;// console.log('mouseenter rowId: ' + $(this).attr('id').substr(3));</li><li style="" class="li1">// &nbsp; &nbsp;alert($(this).attr('id').substr(3));</li><li style="" class="li2">// &nbsp; &nbsp;//Edit();</li><li style="" class="li1">//});</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">function bindDblClick()</li><li style="" class="li2">{</li><li style="" class="li1">&nbsp; &nbsp; $('#flexClients tr').dblclick(function () { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;edit('Edit');</li><li style="" class="li1">&nbsp; &nbsp; });</li><li style="" class="li2">}</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">//This function adds parameters to the post of flexigrid. You can add a verification as well by return to false if you don't want flexigrid to submit&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">function addFormData() {</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; //passing a form object to serializeArray will get the valid data from all the objects, but, if the you pass a non-form object, you have to specify the input elements that the data will come from</li><li style="" class="li2">&nbsp; &nbsp; var dt = $(&quot;#add-edit-form&quot;).serializeArray();</li><li style="" class="li1">&nbsp; &nbsp; dt = dt.concat($('#frmClientsSearch').serializeArray());</li><li style="" class="li2">&nbsp; </li><li style="" class="li1">&nbsp; &nbsp; $(&quot;#flexClients&quot;).flexOptions({ params: dt });</li><li style="" class="li2">&nbsp; &nbsp; return true;</li><li style="" class="li1">}</li></ol></div><div id="cb48996" style="display: none; color: red;"></div></div></div>

<p>So, the first part defines flexigrid (you can see that it used Client controller Client method to return its data). Then the next piece of code binds grid's double click with the same method that is called by the Edit button (in other words, I want to either click on the Edit button or double click on the selected row in a grid to get the same behavior) and the last piece of code (addFormData) is used to pass both Search form and add-edit-form data back to my controller. </p>

<p>And this is how Client method of the controller looks like (I separated this method into 2 based on the idea I took from looking at the <a href="http://www.s4sme.com/Blog/Post/aspnet-mvc-4-application-with-flexigrid-jquery-ui-and-jquery-validation">following solution</a>)</p>

<div class="codebox"><div class="codeheader"><span>c#</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb75099'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb75099','cb56733'); 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="c#" id="cb75099" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #66cc66;">&#91;</span>HttpPost<span style="color: #66cc66;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; public ActionResult Client<span style="color: #66cc66;">&#40;</span>FormCollection formValues<span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Assume we want to select everything</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var clients = Db.<span style="color: #202020;">Clients</span>; <span style="color: #808080; font-style: italic;">// Should set type of clients to IQueryable&lt;Clients&gt;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> searchClientNo = <span style="color: #cc66cc;">0</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span>.<span style="color: #202020;">TryParse</span><span style="color: #66cc66;">&#40;</span>formValues<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;searchClientNo&quot;</span><span style="color: #66cc66;">&#93;</span>, out searchClientNo <span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">string</span> searchClientName = <span style="color: #66cc66;">&#40;</span>formValues<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;searchClientName&quot;</span><span style="color: #66cc66;">&#93;</span>??<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #202020;">ToString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>searchClientNo == <span style="color: #cc66cc;">0</span> &amp;&amp; searchClientName == <span style="color: #ff0000;">&quot;Please enter the search value&quot;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clients = clients.<span style="color: #202020;">Where</span><span style="color: #66cc66;">&#40;</span>c =&gt; <span style="color: #66cc66;">&#40;</span>c.<span style="color: #202020;">Number</span> == searchClientNo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>searchClientNo != <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">//Number was supplied</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clients = clients.<span style="color: #202020;">Where</span><span style="color: #66cc66;">&#40;</span>c =&gt; <span style="color: #66cc66;">&#40;</span>c.<span style="color: #202020;">Number</span> == searchClientNo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// If clientNo was supplied, clients is now filtered by that. If not, it still has the full list. The following will further filter it.</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #993333;">String</span>.<span style="color: #202020;">IsNullOrWhiteSpace</span><span style="color: #66cc66;">&#40;</span>searchClientName<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// Part of the name was supplied</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clients = clients.<span style="color: #202020;">Where</span><span style="color: #66cc66;">&#40;</span>c =&gt; <span style="color: #66cc66;">&#40;</span>c.<span style="color: #202020;">Name</span>.<span style="color: #202020;">Contains</span><span style="color: #66cc66;">&#40;</span>searchClientName<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> page = <span style="color: #993333;">int</span>.<span style="color: #202020;">Parse</span><span style="color: #66cc66;">&#40;</span>formValues<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;page&quot;</span><span style="color: #66cc66;">&#93;</span> ?? <span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> rp = <span style="color: #993333;">int</span>.<span style="color: #202020;">Parse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>formValues<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;rp&quot;</span><span style="color: #66cc66;">&#93;</span>??<span style="color: #ff0000;">&quot;15&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">string</span> qtype = <span style="color: #66cc66;">&#40;</span>formValues<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;qtype&quot;</span><span style="color: #66cc66;">&#93;</span>??<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #202020;">ToString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">string</span> query = <span style="color: #66cc66;">&#40;</span>formValues<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;query&quot;</span><span style="color: #66cc66;">&#93;</span> ?? <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #202020;">ToString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">string</span> sortname = <span style="color: #66cc66;">&#40;</span>formValues<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;sortname&quot;</span><span style="color: #66cc66;">&#93;</span> ?? <span style="color: #ff0000;">&quot;Name&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #202020;">ToString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">string</span> sortorder = <span style="color: #66cc66;">&#40;</span>formValues<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;sortorder&quot;</span><span style="color: #66cc66;">&#93;</span> ?? <span style="color: #ff0000;">&quot;asc&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #202020;">ToString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #993333;">string</span>.<span style="color: #202020;">IsNullOrEmpty</span><span style="color: #66cc66;">&#40;</span>sortname<span style="color: #66cc66;">&#41;</span> &amp;&amp; !<span style="color: #993333;">string</span>.<span style="color: #202020;">IsNullOrEmpty</span><span style="color: #66cc66;">&#40;</span>sortorder<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clients = clients.<span style="color: #202020;">OrderBy</span><span style="color: #66cc66;">&#40;</span>sortname, <span style="color: #66cc66;">&#40;</span>sortorder == <span style="color: #ff0000;">&quot;asc&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #993333;">string</span>.<span style="color: #202020;">IsNullOrEmpty</span><span style="color: #66cc66;">&#40;</span>qtype<span style="color: #66cc66;">&#41;</span> &amp;&amp; !<span style="color: #993333;">string</span>.<span style="color: #202020;">IsNullOrEmpty</span><span style="color: #66cc66;">&#40;</span>query<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clients = clients.<span style="color: #202020;">Like</span><span style="color: #66cc66;">&#40;</span>qtype, query<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> Total = clients.<span style="color: #202020;">Count</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clients = clients.<span style="color: #202020;">Skip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>page - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> * rp<span style="color: #66cc66;">&#41;</span>.<span style="color: #202020;">Take</span><span style="color: #66cc66;">&#40;</span>rp<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> CreateFlexiJson<span style="color: #66cc66;">&#40;</span>clients, page, Total<span style="color: #66cc66;">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; private JsonResult CreateFlexiJson<span style="color: #66cc66;">&#40;</span>IEnumerable&lt;Client&gt; items, <span style="color: #993333;">int</span> page, <span style="color: #993333;">int</span> total<span style="color: #66cc66;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> Json<span style="color: #66cc66;">&#40;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; page,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rows =</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #202020;">Select</span><span style="color: #66cc66;">&#40;</span>x =&gt;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = x.<span style="color: #202020;">Id</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// either use GetPropertyList(x) method to get all columns </span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell = new <span style="color: #66cc66;">&#123;</span> Id = x.<span style="color: #202020;">Id</span>, Number = x.<span style="color: #202020;">Number</span>, Name = x.<span style="color: #202020;">Name</span>, Contact1 = x.<span style="color: #202020;">Contact1</span>.<span style="color: #202020;">Contact</span> ?? <span style="color: #993333;">String</span>.<span style="color: #202020;">Empty</span> <span style="color: #66cc66;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>, JsonRequestBehavior.<span style="color: #202020;">AllowGet</span><span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li></ol></div><div id="cb56733" style="display: none; color: red;"></div></div></div>

<p>So far so good. Now I want to describe the modal dialogs implementation for Add and Edit buttons. This really took me a very long time to get working correctly and I was lucky to finally got this resolved with Jazzen Chen help.</p>

<p>First I want to show the partial view _ClientForm. There is nothing too interesting in that view besides using special EditorForm I implemented based on <a href="http://dotnetspeak.com/index.php/2012/10/asp-net-mvc-template-and-knockout-js/">this blog post</a> by Sergey Barskiy:</p>

<div class="codebox"><div class="codeheader"><span>html</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb21962'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb21962','cb96631'); 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="html" id="cb21962" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">@using CardNumbers.Helper</li><li style="" class="li2">@model CardNumbers.Models.ClientViewModel</li><li style="" class="li1">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">&lt;form</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;add-edit-form&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/fieldset.html"><span style="color: #000000; font-weight: bold;">&lt;fieldset&gt;</span></a></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/legend.html"><span style="color: #000000; font-weight: bold;">&lt;legend&gt;</span></a></span>Client Info<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/legend&gt;</span></span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; @Html.ValidationSummary(true)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;fntype&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;fntype&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; @Html.HiddenFor(m =&gt; m.ClientId)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; @Html.EditorFor(m =&gt; m.ClientNumber, EditorTemplate.TextBox)</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; @Html.EditorFor(m =&gt; m.ClientName, EditorTemplate.TextBox)</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; @Html.EditorFor(m =&gt; m.Client.Address, EditorTemplate.EditBox)</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ContactsInfo&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;Contact1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.EditorFor(m =&gt; m.Client.Contact1)</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;Contact2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.EditorFor(m =&gt; m.Client.Contact2)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;@* <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;SaveCancel&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;float-right&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/button.html"><span style="color: #000000; font-weight: bold;">&lt;button</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;btnSave&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Save<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/button&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/button.html"><span style="color: #000000; font-weight: bold;">&lt;button</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;reset&quot;</span> <span style="color: #000066;">id</span> =<span style="color: #ff0000;">&quot;btnCancel&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Reset&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Cancel<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/button&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>*@</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fieldset&gt;</span></span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form&gt;</span></span></li></ol></div><div id="cb96631" style="display: none; color: red;"></div></div></div>

<p>This is the Clients.js relevant code which drives that Add, Edit and Delete (for completeness) buttons:</p>

<div class="codebox"><div class="codeheader"><span>javascript</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb14694'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb14694','cb69747'); 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="javascript" id="cb14694" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">function del(com, grid) {</li><li style="" class="li2">&nbsp; &nbsp; try {</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var clientName = $('.trSelected td:eq(2)').text();</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; if (clientName) //Variable is defined and not empty</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; {</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (confirm(&quot;Are you sure you want to delete &quot; + $.trim(clientName) + &quot;?&quot;)===false)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.trSelected', grid).each(function () {</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var id = $(this).attr('id');</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = id.substring(id.lastIndexOf('row') + 3);</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addFormData(); $('#flexClients').flexOptions({ url: '/Client/Delete/'+ id }).flexReload();</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; }</li><li style="" class="li2">&nbsp; &nbsp; }</li><li style="" class="li1">&nbsp; &nbsp; catch(e) {</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; alert('Error ' + e);</li><li style="" class="li1">&nbsp; &nbsp; }</li><li style="" class="li2">}</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">var validator = $(&quot;#add-edit-form&quot;).validate();</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">var $dlg = $(&quot;#add-edit-dialog&quot;).dialog({</li><li style="" class="li1">&nbsp; &nbsp; autoOpen: false,</li><li style="" class="li2">&nbsp; &nbsp; show: &quot;blind&quot;,</li><li style="" class="li1">&nbsp; &nbsp; closeOnEscape: true,</li><li style="" class="li2">&nbsp; &nbsp; resizable: true,</li><li style="" class="li1">&nbsp; &nbsp; width: 1200,</li><li style="" class="li2">&nbsp; &nbsp; height: 750,</li><li style="" class="li1">&nbsp; &nbsp; minHeight: 600,</li><li style="" class="li2">&nbsp; &nbsp; minWidth: 950,</li><li style="" class="li1">&nbsp; &nbsp; buttons: {</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &quot;Save&quot;: function () {</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($(&quot;#add-edit-form&quot;).valid()) {</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // jobPost.setVals(txtId.val(), txtRole.val(), txtLocation.val(), txtJobType.val(), txtDescription.val()); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //data: JSON.stringify(clientInformation),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '/Client/Save',</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType: 'application/json',</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (result) {</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // insert new list into grid</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#flexClients').flexAddData(result);</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).dialog('close');</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else return false;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; },</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; Cancel: function () {</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).dialog(&quot;close&quot;);</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearForm();</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (validator)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validator.resetForm();</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; }</li><li style="" class="li2">&nbsp; &nbsp; },</li><li style="" class="li1">&nbsp; &nbsp; close: function () { clearForm(); },</li><li style="" class="li2">&nbsp; &nbsp; open: function () {</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; //$(&quot;#add-edit-dialog&quot;).parent().appendTo($(&quot;#add-edit-form&quot;));</li><li style="" class="li2">&nbsp; &nbsp; }</li><li style="" class="li1">});</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">function RunModalDialog(title, url)</li><li style="" class="li2">{ &nbsp; &nbsp;</li><li style="" class="li1">&nbsp; &nbsp; if (title)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $dlg.dialog(&quot;option&quot;, {&quot;title&quot;: title });</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; if (url)</li><li style="" class="li1">&nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $dlg.load(url).dialog(&quot;option&quot;, { &quot;title&quot;: title }).dialog(&quot;open&quot;);</li><li style="" class="li1">&nbsp; &nbsp; }</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; //$dlg.load(url, function () {</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; // &nbsp; &nbsp;var validator = $(&quot;#sform&quot;).validate();</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; // &nbsp; &nbsp;if (validator)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; // &nbsp; &nbsp; &nbsp; &nbsp; validator.resetForm();</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; // &nbsp; &nbsp;$dlg.dialog(&quot;option&quot;, { &quot;title&quot;: title }).dialog(&quot;open&quot;);</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; //});</li><li style="" class="li2">&nbsp; &nbsp; else {</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $dlg.dialog(&quot;open&quot;);</li><li style="" class="li1">&nbsp; &nbsp; }</li><li style="" class="li2">}</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">function add(com, grid) {</li><li style="" class="li1">&nbsp; &nbsp; $('#fntype').val('Add');</li><li style="" class="li2">&nbsp; &nbsp; var url = '/Client/Add/';</li><li style="" class="li1">&nbsp; &nbsp; //location.replace(url);</li><li style="" class="li2">&nbsp; &nbsp; RunModalDialog(&quot;Create New Client&quot;);</li><li style="" class="li1">&nbsp; &nbsp;// clearForm(); &nbsp; &nbsp;</li><li style="" class="li2">}</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">function edit(com, grid)</li><li style="" class="li1">{</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; $('.trSelected', grid).each(function () {</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var id = $(this).attr('id');</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = id.substring(id.lastIndexOf('row') + 3);</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentId = id;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#fntype').val('Edit');</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var ClientName;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ClientName =$('.trSelected td:eq(2)').text();</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var url = '/Client/Edit/' + id ;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.get(url, function (html) {</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;setFormControls(data.Id, data.Role, data.Location, data.JobType, data.Description);</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // alert(data);</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $($dlg).html(html);</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//location.replace(url);</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RunModalDialog(&quot;Edit Client: &quot; + ClientName);</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="cb69747" style="display: none; color: red;"></div></div></div>

<p>The most complicated part in the above script was to figure out how to make a server trip to get data for the Edit button and return that page back for editing. <br />
This is done with these two lines of code:</p>

<div class="codebox"><div class="codeheader"><span>javascript</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb95006'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb95006','cb66440'); 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="javascript" id="cb95006" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">$.get(url, function (html) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $($dlg).html(html);</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });</li></ol></div><div id="cb66440" style="display: none; color: red;"></div></div></div>

<p>And to make the whole picture complete I'll show the Edit method of my controller that returns the above data:</p>

<div class="codebox"><div class="codeheader"><span>c#</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb25301'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb25301','cb12844'); 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="c#" id="cb25301" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">public ActionResult Edit<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> id<span style="color: #66cc66;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ClientViewModel model = new ClientViewModel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var client = Db.<span style="color: #202020;">GetClientById</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color: #202020;">Client</span> = client;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> PartialView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;_ClientForm&quot;</span>,model<span style="color: #66cc66;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></li></ol></div><div id="cb12844" style="display: none; color: red;"></div></div></div>

<p>As you can see, just a few lines of code to return the model data and the partial view.</p>

<p>This all may seem very easy now, but I can tell you it was not easy to figure all the pieces out.</p>

<p>I am not showing certain functionality here such as my view model or EditorFor custom editors. I also don't show the RemoteValidator used to provide uniqueness of the client number and client name. If there will be an interest, I may discuss these details in the separate blog post.</p>

<p>Hope this blog post can help people struggling with the modal dialogs implementation in ASP.NET MVC application.</p>

<p><br /><br />
*** <strong>Remember, if you have a Web application related question, try our <a href="http://forum.lessthandot.com/viewforum.php?f=6&amp;sid=ebc29b65310d77fa409af74cf8d5cd70">Web forum</a>.</strong><ins></ins></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/WebDev/UIDevelopment/AJAX/asp-net-mvc-project-with">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/WebDev/UIDevelopment/AJAX/asp-net-mvc-project-with#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/WebDev/?tempskin=_rss2&#38;disp=comments&#38;p=2053</wfw:commentRss>
		</item>
				<item>
			<title>Dealing with the "The text, ntext, or image pointer value conflicts with the column name specified." error from Visual Foxpro</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/dealing-with-the-the-text</link>
			<pubDate>Tue, 03 Apr 2012 22:58:00 +0000</pubDate>			<dc:creator>Naomi Nosonovsky</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">1672@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Recently my colleague sent me a strange error she was getting in our VFP application&lt;br /&gt;
&quot;The text, ntext, or image pointer value conflicts with the column name specified.&quot;&lt;/p&gt;

&lt;p&gt;I was not getting the same error, so it was puzzling indeed. After some research online I found the &lt;a href=&quot;http://social.msdn.microsoft.com/Forums/en/transactsql/thread/d26e2143-630b-46e7-a81b-a33a24af20ce&quot;&gt;following thread&lt;/a&gt; and suspected the problem was in the image size. And yes, after I chose a file about 2MB in size I was able to reproduce the same error. I haven&#039;t been able to determine the exact image size when this error occurs, but in my tests the files less than 400Kb worked and bigger files produced the error. &lt;/p&gt;

&lt;p&gt;This is how our VFP application is structured. We use SPT for data retrieval, then using an approach similar to described in &lt;a href=&quot;http://www.universalthread.com/ViewPageNewFAQ.aspx?ID=8153&quot;&gt;this FAQ&lt;/a&gt; we make this cursor updateable and then send updates using TABLEUPDATE function. We&#039;re also using DSN and old SQL Server driver. In the table that was giving this error the field for storing images is varbinary(max).&lt;/p&gt;

&lt;p&gt;I tested a similar form where the table used an image column. The same error didn&#039;t occur and I was able to import 2MB files without a problem.&lt;/p&gt;

&lt;p&gt;So, we now determine an internal SQL Server bug (most likely in the old SQL Server driver) that can not work with big varbinary(max) columns by using remote views form Visual FoxPro.&lt;br /&gt;
I also found a thread on a &lt;a href=&quot;http://www.foxite.com/archives/warning-possible-bug-sql-server-0000239346.htm&quot;&gt;similar topic&lt;/a&gt; by Cetin Basoz in foxite.com&lt;/p&gt;

&lt;p&gt;Luckily there was a relatively simple solution for this problem: use direct updates/inserts with parameters instead of remote views. So, in my forms I was checking for big size images and used a different approach when saving data.&lt;/p&gt;

&lt;p&gt;Here is a test program that can demonstrate the problem:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb74121&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;ON ERROR &lt;br /&gt;CLEAR ALL&lt;br /&gt;CLEAR &lt;br /&gt;oAppObj = NEWOBJECT(&#039;my_appObj&#039;,&#039;my_Class.vcx&#039;)&lt;br /&gt;=CURSORSETPROP(&#039;MapBinary&#039;,.t.,0)&lt;br /&gt;WAIT WINDOW NOWAIT NOCLEAR &#039;Connecting to server data ...&#039;&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; SQLSETPROP(0, &amp;quot;ConnectTimeOut&amp;quot;, 15)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; SQLSETPROP(0, &amp;quot;DispWarnings&amp;quot;, .F.)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; SQLSETPROP(0, &amp;quot;DispLogin&amp;quot;, 3) &amp;nbsp;&amp;amp;&amp;amp; disable login dialog if there is a problem&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; SQLHandle=SQLCONNECT(&#039;MyDB&#039;, &#039;userid&#039;, &#039;password&#039;)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; sqlexec(SQLHandle, &#039;use MyDB&#039; ) &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;lcPicFile = GETFILE(&#039;jpg;png&#039;)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;lcStr = CAST(FILETOSTR(m.lcPicFile) &amp;nbsp;as blob) &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp;&lt;br /&gt;TEXT TO lcSQL TEXTMERGE NOSHOW &lt;br /&gt;update dbo.rsSeatMaps set SeatMap = ?m.lcStr&lt;br /&gt;where SeatMapID = 10&lt;br /&gt;ENDTEXT &lt;br /&gt;&amp;nbsp;&lt;br /&gt;?SQLEXEC(SQLHandle, m.lcSQL)&lt;br /&gt;aError(laError)&lt;br /&gt;DISPLAY MEMORY LIKE laError&lt;br /&gt;&amp;nbsp;&lt;br /&gt;? SQLEXEC(SQLHandle,&#039;select * from dbo.rsSeatMaps where SeatMapID = 10&#039;,&#039;rsSeatMaps&#039;)&lt;br /&gt;?make_view_updatable(&#039;rsSeatMaps&#039;,5, SQLHandle)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;replace SeatMap WITH m.lcStr IN rsSeatMaps&lt;br /&gt;&amp;nbsp;&lt;br /&gt;?TABLEUPDATE(.t.,.t., &#039;rsSeatMaps&#039;)&lt;br /&gt;?AERROR(laError)&lt;br /&gt;DISPLAY MEMORY LIKE laError&lt;br /&gt;SQLDISCONNECT(0)&lt;br /&gt;WAIT CLEAR&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb64430&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first direct update command succeeds, but the second remote view update fails.&lt;/p&gt;

&lt;p&gt;Hopefully this blog post will be helpful.&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/dealing-with-the-the-text&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 my colleague sent me a strange error she was getting in our VFP application<br />
"The text, ntext, or image pointer value conflicts with the column name specified."</p>

<p>I was not getting the same error, so it was puzzling indeed. After some research online I found the <a href="http://social.msdn.microsoft.com/Forums/en/transactsql/thread/d26e2143-630b-46e7-a81b-a33a24af20ce">following thread</a> and suspected the problem was in the image size. And yes, after I chose a file about 2MB in size I was able to reproduce the same error. I haven't been able to determine the exact image size when this error occurs, but in my tests the files less than 400Kb worked and bigger files produced the error. </p>

<p>This is how our VFP application is structured. We use SPT for data retrieval, then using an approach similar to described in <a href="http://www.universalthread.com/ViewPageNewFAQ.aspx?ID=8153">this FAQ</a> we make this cursor updateable and then send updates using TABLEUPDATE function. We're also using DSN and old SQL Server driver. In the table that was giving this error the field for storing images is varbinary(max).</p>

<p>I tested a similar form where the table used an image column. The same error didn't occur and I was able to import 2MB files without a problem.</p>

<p>So, we now determine an internal SQL Server bug (most likely in the old SQL Server driver) that can not work with big varbinary(max) columns by using remote views form Visual FoxPro.<br />
I also found a thread on a <a href="http://www.foxite.com/archives/warning-possible-bug-sql-server-0000239346.htm">similar topic</a> by Cetin Basoz in foxite.com</p>

<p>Luckily there was a relatively simple solution for this problem: use direct updates/inserts with parameters instead of remote views. So, in my forms I was checking for big size images and used a different approach when saving data.</p>

<p>Here is a test program that can demonstrate the problem:</p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb2196'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb2196','cb28031'); 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="text" id="cb2196" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">ON ERROR </li><li style="" class="li2">CLEAR ALL</li><li style="" class="li1">CLEAR </li><li style="" class="li2">oAppObj = NEWOBJECT('my_appObj','my_Class.vcx')</li><li style="" class="li1">=CURSORSETPROP('MapBinary',.t.,0)</li><li style="" class="li2">WAIT WINDOW NOWAIT NOCLEAR 'Connecting to server data ...'</li><li style="" class="li1">&nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; SQLSETPROP(0, &quot;ConnectTimeOut&quot;, 15)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; SQLSETPROP(0, &quot;DispWarnings&quot;, .F.)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; SQLSETPROP(0, &quot;DispLogin&quot;, 3) &nbsp;&amp;&amp; disable login dialog if there is a problem</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; SQLHandle=SQLCONNECT('MyDB', 'userid', 'password')</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; sqlexec(SQLHandle, 'use MyDB' ) &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</li><li style="" class="li2">lcPicFile = GETFILE('jpg;png')</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">lcStr = CAST(FILETOSTR(m.lcPicFile) &nbsp;as blob) &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp;</li><li style="" class="li2">TEXT TO lcSQL TEXTMERGE NOSHOW </li><li style="" class="li1">update dbo.rsSeatMaps set SeatMap = ?m.lcStr</li><li style="" class="li2">where SeatMapID = 10</li><li style="" class="li1">ENDTEXT </li><li style="" class="li2">&nbsp;</li><li style="" class="li1">?SQLEXEC(SQLHandle, m.lcSQL)</li><li style="" class="li2">aError(laError)</li><li style="" class="li1">DISPLAY MEMORY LIKE laError</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">? SQLEXEC(SQLHandle,'select * from dbo.rsSeatMaps where SeatMapID = 10','rsSeatMaps')</li><li style="" class="li2">?make_view_updatable('rsSeatMaps',5, SQLHandle)</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">replace SeatMap WITH m.lcStr IN rsSeatMaps</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">?TABLEUPDATE(.t.,.t., 'rsSeatMaps')</li><li style="" class="li1">?AERROR(laError)</li><li style="" class="li2">DISPLAY MEMORY LIKE laError</li><li style="" class="li1">SQLDISCONNECT(0)</li><li style="" class="li2">WAIT CLEAR</li></ol></div><div id="cb28031" style="display: none; color: red;"></div></div></div>

<p>The first direct update command succeeds, but the second remote view update fails.</p>

<p>Hopefully this blog post will be helpful.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/dealing-with-the-the-text">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/dealing-with-the-the-text#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1672</wfw:commentRss>
		</item>
				<item>
			<title>Displaying and Saving Unicode data in Visual FoxPro desktop application</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/displaying-and-saving-unicode-data</link>
			<pubDate>Sun, 01 Jan 2012 17:09:00 +0000</pubDate>			<dc:creator>Naomi Nosonovsky</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">1574@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Recently I had to deal with the problem of displaying and saving Unicode data in a Visual FoxPro form. As we all know, Visual FoxPro does not support Unicode, so this was  quite a challenge for me. I would never have solved this problem by myself if Gregory Adam from the &lt;a href=&quot;http://universalthread.com&quot;&gt;UniversalThread&lt;/a&gt; would not lend me his generous support and practically solved the problem of saving for me. I want to show the full solution we came to together as this may be a very important topic for the remaining Visual FoxPro developers (and because Koen asked me to write this blog post).&lt;/p&gt;

&lt;p&gt;Ok, we all know the classical and very comprehensive article on this topic written by Rick Strahl &lt;a href=&quot;http://www.west-wind.com/presentations/foxunicode/foxunicode.asp&quot;&gt;Using Unicode in Visual FoxPro Web and Desktop Applications&lt;/a&gt;. So, I started from this article. Our application uses SQL Server to hold data and uses sql pass thru technique to send information from and to SQL Server. &lt;/p&gt;

&lt;p&gt;Now, since I am describing the solution I used for the particular application, I will start with the SQL Server table I was using. That table was named Prefs_sl and it was a flat table with 1 record containing all preferences for a sales point. Because of its flat nature it was badly normalized. Say, it had 11 language fields language00 - language10 (nvarchar(100)) to hold name of the language the Sales Point can display and 11 image lngImage00-lngImage10 (varbinary(max)) fields to hold the image of the country flag. So, this was the table I was working with on the SQL Server side.&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Prefs_sl SQL Server table.JPG?mtime=1325442752&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Prefs_sl SQL Server table.JPG?mtime=1325442752&quot; width=&quot;1764&quot; height=&quot;162&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Now, I wanted to display these 11 languages with their corresponding flags on the form, so the logical choice was to create a class with the textbox for the unicode text and image for the image flag and then instantiate this class on the form.&lt;/p&gt;

&lt;p&gt;The first question was what to chose for displaying unicode data? Rick mentioned MS Edit Box control ActiveX, but I didn&#039;t find it in the list of my installed ActiveX. But I found MS Forms2 TextBox ActiveX control and since I also remembered &lt;a href=&quot;http://www.foxite.com/archives/how-to-implement-unicode-in-vfp-0000209431.htm&quot;&gt;this thread&lt;/a&gt; on foxite forum, I decided to try this ActiveX control.&lt;br /&gt;
The first problem I encountered with this ActiveX as that it didn&#039;t show up until I clicked on it when I was running the form. Luckily I found the following solution - invoke SetFocus of this control (I called it txtLanguage in my class) and then it was displayed correctly. I am not sure if my solution is the best to handle this ActiveX behavior quirks, but hey, it works.&lt;/p&gt;

&lt;p&gt;Ok, so far so good. Now, following Rick&#039;s article I used the following in my form to get the data in the binary format:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb91905&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;LOCAL lcSQL&lt;br /&gt;TEXT TO lcSQL noshow&lt;br /&gt;SELECT pri_key &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,defltlang&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,cast(language00 as varbinary(100)) as language00&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,lngimage00 &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,cast(language01 as varbinary(100)) as language01&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,lngimage01&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,cast(language02 as varbinary(100)) as language02&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,lngimage02&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,cast(language03 as varbinary(100)) as language03&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,lngimage03&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,cast(language04 as varbinary(100)) as language04&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,lngimage04&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,cast(language05 as varbinary(100)) as language05&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,lngimage05&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,cast(language06 as varbinary(100)) as language06&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,lngimage06&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,cast(language07 as varbinary(100)) as language07&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,lngimage07&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,cast(language08 as varbinary(100)) as language08&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,lngimage08&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,cast(language09 as varbinary(100)) as language09&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,lngimage09&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,cast(language10 as varbinary(100)) as language10&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ,lngimage10 &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; FROM dbo.prefs_sl&lt;br /&gt;endtext &amp;nbsp;&lt;br /&gt;mysqlexec(m.lcSQL, &#039;Prefs_sl&#039;, program())&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb33622&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;mysqlexec here is a wrapper for the sqlexec function.&lt;/p&gt;

&lt;p&gt;Now I found the first discrepancy with Rick&#039;s article - I found that I don&#039;t need any conversion after that, I can assign the field&#039;s content to the text property of this ActiveX (or ControlSource property) and the control correctly displays the unicode data!&lt;/p&gt;

&lt;p&gt;I was happy - the first part of displaying data turned out to be quite easy!&lt;/p&gt;

&lt;p&gt;Here is the code I used to display the data:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb63775&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;FOR EACH loObject IN thisform.Objects foxobject&lt;br /&gt;&amp;nbsp; &amp;nbsp;IF loObject.Baseclass = &#039;Container&#039;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; loObject.LanguageField = SUBSTR(loObject.name,4,LEN(loObject.name))&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; loObject.ImageField = &#039;lngImage&#039; + RIGHT(loObject.Name,2)&lt;br /&gt;&amp;nbsp; &amp;nbsp;ENDIF &lt;br /&gt;next &amp;nbsp; &lt;br /&gt;&amp;nbsp;&lt;br /&gt;thisform.cntLanguage01.SetFocus()&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb3611&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And in the container itself the following code in the LanguageField assign method:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb66019&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;this.lblLanguage.caption = this.lblLanguage.caption + &#039; &#039; + transform(val(right(tLanguageField,2)))&lt;br /&gt;&amp;nbsp; this.txtLanguage.Enabled = .t.&lt;br /&gt;&amp;nbsp; this.txtLanguage.TabStop = .t. &amp;nbsp; &lt;br /&gt;&amp;nbsp; this.lProgrammaticChange = .t.&lt;br /&gt;&amp;nbsp; this.txtLanguage.text = evaluate(&#039;prefs_sl.&#039; + tLanguageField)&lt;br /&gt;&amp;nbsp; this.lProgrammaticChange = .f.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb19893&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Everything was good so far. However, I haven&#039;t expected how hard (for me) it will be the &#039;saving&#039; data part. I was spending days trying various ideas from Rick&#039;s article and was still unable to achieve the desired result. I was about to throw all I had away and try to switch to ADO for data retrieval, but luckily Gregory Adam helped me here and published a working sample.&lt;/p&gt;

&lt;p&gt;First of all, we need to make sure there is no UT8 translation to the current code page. For this we need to do the following&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb82339&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;COMPROP(this,&#039;UTF8&#039;,1)&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb33667&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the txtLanguage Init method.&lt;/p&gt;

&lt;p&gt;We also need the following functions:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb70042&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&amp;amp;&amp;amp; StringConversion&lt;br /&gt;&amp;amp;&amp;amp; Gregory Adam 2011&lt;br /&gt;*===============================================================================&lt;br /&gt;#define true&amp;nbsp; &amp;nbsp; .T.&lt;br /&gt;#define false&amp;nbsp; &amp;nbsp;.F.&lt;br /&gt;&amp;nbsp;&lt;br /&gt;*_______________________________________________________________________________&lt;br /&gt;function StringToUTF8(utf8Out, stringIn, codepageIn)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; local success&lt;br /&gt;&amp;nbsp; &amp;nbsp; success = true&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; do case&lt;br /&gt;&amp;nbsp; &amp;nbsp; case !m.success&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; case !StringToUTF16(@m.utf8Out, m.stringIn, m.codepageIn)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; assert false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; success = false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; case !StringUTF16ToUTF8(@m.utf8Out, m.utf8Out)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; assert false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; success = false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; endcase&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; return m.success&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;endfunc&lt;br /&gt;*_______________________________________________________________________________&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;*_______________________________________________________________________________&lt;br /&gt;*_______________________________________________________________________________&lt;br /&gt;#define CP_ACP&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;br /&gt;#define CP_MACCP&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;br /&gt;#define CP_OEMCP&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;br /&gt;#define CP_SYMBOL&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 42&lt;br /&gt;#define CP_THREAD_ACP&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;br /&gt;#define CP_UTF7&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 65000&lt;br /&gt;#define CP_UTF8&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 65001&lt;br /&gt;#define MB_PRECOMPOSED&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0x1&lt;br /&gt;#define MB_COMPOSITE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0x2&lt;br /&gt;#define MB_USEGLYPHCHARS&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0x4&lt;br /&gt;#define MB_ERR_INVALID_CHARS&amp;nbsp; &amp;nbsp; 0x8&lt;br /&gt;&amp;nbsp;&lt;br /&gt;#define WC_DEFAULTCHAR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0x00000040 &lt;br /&gt;#define WC_ERR_INVALID_CHARS&amp;nbsp; &amp;nbsp; 0x00000080 &lt;br /&gt;#define WC_NO_BEST_FIT_CHARS &amp;nbsp; &amp;nbsp;0x00000400 &lt;br /&gt;*_______________________________________________________________________________&lt;br /&gt;function StringToUTF16(utf16Out, stringIn, codepageIn)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; local success&lt;br /&gt;&amp;nbsp; &amp;nbsp; success = true&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; do case&lt;br /&gt;&amp;nbsp; &amp;nbsp; case !m.success&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; case empty(len(m.stringIn))&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; utf16Out = &#039;&#039;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; otherwise&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; local lpWideCharStr, result&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lpWideCharStr = space(len(m.stringIn)*2)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; result = MultiByteToWideChar( ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; evl(m.codepageIn, cpcurrent()), ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; MB_ERR_INVALID_CHARS, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @m.stringIn, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; len(m.stringIn), ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @m.lpWideCharStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; len(m.lpWideCharStr) ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; )&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; do case&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; case !m.success&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; case empty(m.result)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; assert false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; success = false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; otherwise&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; utf16Out = left(m.lpWideCharStr, m.result * 2) &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; endcase&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; endcase&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; return m.success&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;endfunc&lt;br /&gt;*_______________________________________________________________________________&lt;br /&gt;function StringUTF16ToUTF8(utf8Out, utf16In)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; local success&lt;br /&gt;&amp;nbsp; &amp;nbsp; success = true&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; do case&lt;br /&gt;&amp;nbsp; &amp;nbsp; case !m.success&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; case empty(len(m.utf16In))&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; utf8Out = &#039;&#039;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; otherwise&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; local lpMultiByteStr, lpUsedDefaultChar, result&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lpMultiByteStr = space(len(m.utf16In) * 2)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lpUsedDefaultChar = 0&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; result = WideCharToMultiByte( ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; CP_UTF8, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WC_ERR_INVALID_CHARS, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @m.utf16In, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; len(m.utf16In)/2, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @m.lpMultiByteStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; len(m.lpMultiByteStr), ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; null, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @m.lpUsedDefaultChar ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; )&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; do case&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; case !m.success&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; case empty(m.result)&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; assert false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; success = false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; otherwise&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; utf8Out = left(m.lpMultiByteStr, m.result)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; endcase&lt;br /&gt;&amp;nbsp; &amp;nbsp; endcase&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; return m.success&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;endfunc&lt;br /&gt;*_______________________________________________________________________________&lt;br /&gt;function StringUTF8ToUTF16(utf16Out, uft8In)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; return StringToUTF16(@m.utf16Out, uft8In, CP_UTF8)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;endfunc&lt;br /&gt;*_______________________________________________________________________________&lt;br /&gt;&amp;nbsp;&lt;br /&gt;function MultiByteToWideChar&lt;br /&gt;&amp;nbsp; &amp;nbsp; lparameters codepage, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dwFlags, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lpMultiByteStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cbMultiByte, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lpWideCharStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cchWideChar&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; local success&lt;br /&gt;&amp;nbsp; &amp;nbsp; success = true&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; local result&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; do case&lt;br /&gt;&amp;nbsp; &amp;nbsp; case !m.success&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; otherwise&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; try&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; declare integer MultiByteToWideChar in Kernel32.dll ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; long&amp;nbsp; &amp;nbsp; codepage, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; long&amp;nbsp; &amp;nbsp; dwFlags, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; string@&amp;nbsp;lpMultiByteStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; integer&amp;nbsp;cbMultiByte, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; string@&amp;nbsp;lpWideCharStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; integer&amp;nbsp;cchWideChar&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; result = MultiByteToWideChar( ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m.codepage, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m.dwFlags, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @m.lpMultiByteStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m.cbMultiByte, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @m.lpWideCharStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m.cchWideChar ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; )&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; catch&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; assert false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; success = false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; endtry&lt;br /&gt;&amp;nbsp; &amp;nbsp; endcase&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; return iif(m.success, m.result, 0)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;endfunc&lt;br /&gt;*_______________________________________________________________________________&lt;br /&gt;function WideCharToMultiByte&lt;br /&gt;&amp;nbsp; &amp;nbsp; lparameters codepage, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dwFlags, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lpWideCharStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cchWideChar, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lpMultiByteStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cbMultiByte, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lpDefaultChar, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lpUsedDefaultChar&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; local success&lt;br /&gt;&amp;nbsp; &amp;nbsp; success = true&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; local result&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; do case&lt;br /&gt;&amp;nbsp; &amp;nbsp; case !m.success&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; otherwise&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; try&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; declare integer WideCharToMultiByte in Kernel32.dll ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; long&amp;nbsp; &amp;nbsp; codepage, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; long&amp;nbsp; &amp;nbsp; dwFlags, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; string@&amp;nbsp;lpWideCharStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; integer&amp;nbsp;cchWideChar, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; string@&amp;nbsp;lpMultiByteStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; integer&amp;nbsp;cbMultiByte, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; string&amp;nbsp; lpDefaultChar, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; integer&amp;nbsp;@lpUsedDefaultChar&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; result = WideCharToMultiByte ( ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m.codepage, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m.dwFlags, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @m.lpWideCharStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m.cchWideChar, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @m.lpMultiByteStr, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m.cbMultiByte, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m.lpDefaultChar, ;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @m.lpUsedDefaultChar;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; )&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; catch&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; assert false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; success = false&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; endtry&lt;br /&gt;&amp;nbsp; &amp;nbsp; endcase&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; return iif(m.success, m.result, 0)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;endfunc&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb85964&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt; &lt;/p&gt;

&lt;p&gt;and then in the Change event of the ActiveX textbox I added the following code&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb24410&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;IF this.parent.lProgrammaticChange &lt;br /&gt;&amp;nbsp; &amp;nbsp; RETURN&lt;br /&gt;ENDIF &amp;nbsp; &amp;nbsp; &lt;br /&gt;if thisform.IsChanged OR (not this.CurrentValue == this.Text)&lt;br /&gt;&amp;nbsp; &amp;nbsp;thisform.IsChanged = .t.&lt;br /&gt;&amp;nbsp; &amp;nbsp;local utf8, utf16Out&lt;br /&gt;&amp;nbsp; &amp;nbsp;utf8 = this.text&lt;br /&gt;&amp;nbsp; &amp;nbsp;if not empty(utf8)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; = StringUtf8ToUTF16(@m.utf16Out, m.utf8)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; replace (this.parent.LanguageField) with utf16Out&lt;br /&gt;&amp;nbsp; &amp;nbsp;else&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; replace (this.parent.LanguageField) with &#039;&#039;&lt;br /&gt;&amp;nbsp; &amp;nbsp;endif&lt;br /&gt;endif&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb63766&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and finally in the Save method of the form&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb8183&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;IF THISFORM.IsChanged &lt;br /&gt;&amp;nbsp; LOCAL lcSQL&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; TEXT TO lcSQL noshow&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;UPDATE dbo.prefs_sl &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;SET defltlang = ?prefs_sl.DefltLang,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;language00 = cast(?prefs_sl.language00 as nvarchar(100)),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;lngimage00 = ?prefs_sl.lngimage00,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;language01 = cast(?prefs_sl.language01 as nvarchar(100)),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;lngimage01 = ?prefs_sl.lngimage01,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;language02 = cast(?prefs_sl.language02 as nvarchar(100)),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;lngimage02 = ?prefs_sl.lngimage02,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;language03 = cast(?prefs_sl.language03 as nvarchar(100)),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;lngimage03 = ?prefs_sl.lngimage03,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;language04 = cast(?prefs_sl.language04 as nvarchar(100)),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;lngimage04 = ?prefs_sl.lngimage04,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;language05 = cast(?prefs_sl.language05 as nvarchar(100)),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;lngimage05 = ?prefs_sl.lngimage05,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;language06 = cast(?prefs_sl.language06 as nvarchar(100)),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;lngimage06 = ?prefs_sl.lngimage06,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;language07 = cast(?prefs_sl.language07 as nvarchar(100)),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;lngimage07 = ?prefs_sl.lngimage07,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;language08 = cast(?prefs_sl.language08 as nvarchar(100)),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;lngimage08 = ?prefs_sl.lngimage08,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;language09 = cast(?prefs_sl.language09 as nvarchar(100)),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;lngimage09 = ?prefs_sl.lngimage09,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;language10 = cast(?prefs_sl.language10 as nvarchar(100)),&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;lngimage10 = ?prefs_sl.lngimage10&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;where pri_key = ?prefs_sl.pri_key&lt;br /&gt;&amp;nbsp; ENDTEXT&lt;br /&gt;&amp;nbsp;RETURN mySQLExec(m.lcSQL, &#039;&#039;,PROGRAM())&lt;br /&gt;ENDIF&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb41&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As we can see, we need to convert data back from binary to nvarchar.&lt;br /&gt;
With all this code in place, we display the unicode data and save them back to SQL Server.&lt;/p&gt;

&lt;p&gt;A big thanks to Gregory for helping me with the code - I don&#039;t know where I would be without his help.&lt;/p&gt;

&lt;p&gt;Olaf Doschke showed another way which is even simpler than implemented solution and in accordance to the original Rick&#039;s article.&lt;/p&gt;

&lt;p&gt;In the form&#039;s Load we need the following:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb3411&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;Sys(987,.F.)&lt;br /&gt;Sys(3101,65001)&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb40103&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, after getting binary data from SQL Server the same way as I show in this blog, we still need to use createbinary, e.g.&lt;/p&gt;

&lt;p&gt;this.txtLanguage.text = createbinary(prefs_sl.Language00)&lt;/p&gt;

&lt;p&gt;We don&#039;t want to use COMPROP now for the ActiveX.&lt;/p&gt;

&lt;p&gt;and then, saving data, we need to follow Rick&#039;s steps:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb40642&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;pcSavedText1 = Strconv(This.Text,12)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;*** Must explicitly force to binary &amp;#8211; can also use CAST in 9.0&lt;br /&gt;pcSavedText1 = CREATEBINARY(pcSavedText1)&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb28124&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;and then convert this value back to nvarchar(max) when saving with sqlexec.&lt;/p&gt;

&lt;p&gt;This is how the form with many languages looked like&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/ManyLanguages.JPG?mtime=1325442752&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/ManyLanguages.JPG?mtime=1325442752&quot; width=&quot;927&quot; height=&quot;654&quot; /&gt;&lt;/a&gt;&lt;/div&gt;


&lt;p&gt;---------------------------&lt;br /&gt;
I need to also tell, that after I finished the form and was happy to show it to my colleagues, I was up to a big disappointment. It turned out we are not going to support unicode (our main application is not up to support it yet) and so I will be re-doing this form after we will agree on the tables&#039; structures (they also are going to change). So, what I showed in this blog post we&#039;re not going to use at present in production.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;MT_red&quot;&gt;UPDATE.&lt;/span&gt; My colleague also re-designed prefs_sl table to one EAV Settings table. We know that EAV design has its own problems, but for saving various application level settings it can be used. I already re-designed the forms we&#039;re going to use. &lt;/p&gt;

&lt;p&gt;But in any case, it was a very important exercise and I hope it will help other people who need to display and save Unicode data in their Visual FoxPro applications.&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/displaying-and-saving-unicode-data&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 I had to deal with the problem of displaying and saving Unicode data in a Visual FoxPro form. As we all know, Visual FoxPro does not support Unicode, so this was  quite a challenge for me. I would never have solved this problem by myself if Gregory Adam from the <a href="http://universalthread.com">UniversalThread</a> would not lend me his generous support and practically solved the problem of saving for me. I want to show the full solution we came to together as this may be a very important topic for the remaining Visual FoxPro developers (and because Koen asked me to write this blog post).</p>

<p>Ok, we all know the classical and very comprehensive article on this topic written by Rick Strahl <a href="http://www.west-wind.com/presentations/foxunicode/foxunicode.asp">Using Unicode in Visual FoxPro Web and Desktop Applications</a>. So, I started from this article. Our application uses SQL Server to hold data and uses sql pass thru technique to send information from and to SQL Server. </p>

<p>Now, since I am describing the solution I used for the particular application, I will start with the SQL Server table I was using. That table was named Prefs_sl and it was a flat table with 1 record containing all preferences for a sales point. Because of its flat nature it was badly normalized. Say, it had 11 language fields language00 - language10 (nvarchar(100)) to hold name of the language the Sales Point can display and 11 image lngImage00-lngImage10 (varbinary(max)) fields to hold the image of the country flag. So, this was the table I was working with on the SQL Server side.</p>

<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Prefs_sl SQL Server table.JPG?mtime=1325442752"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Prefs_sl SQL Server table.JPG?mtime=1325442752" width="1764" height="162" /></a></div>

<p>Now, I wanted to display these 11 languages with their corresponding flags on the form, so the logical choice was to create a class with the textbox for the unicode text and image for the image flag and then instantiate this class on the form.</p>

<p>The first question was what to chose for displaying unicode data? Rick mentioned MS Edit Box control ActiveX, but I didn't find it in the list of my installed ActiveX. But I found MS Forms2 TextBox ActiveX control and since I also remembered <a href="http://www.foxite.com/archives/how-to-implement-unicode-in-vfp-0000209431.htm">this thread</a> on foxite forum, I decided to try this ActiveX control.<br />
The first problem I encountered with this ActiveX as that it didn't show up until I clicked on it when I was running the form. Luckily I found the following solution - invoke SetFocus of this control (I called it txtLanguage in my class) and then it was displayed correctly. I am not sure if my solution is the best to handle this ActiveX behavior quirks, but hey, it works.</p>

<p>Ok, so far so good. Now, following Rick's article I used the following in my form to get the data in the binary format:</p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb29261'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb29261','cb87472'); 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="text" id="cb29261" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">LOCAL lcSQL</li><li style="" class="li2">TEXT TO lcSQL noshow</li><li style="" class="li1">SELECT pri_key &nbsp; &nbsp; &nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,defltlang</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; ,cast(language00 as varbinary(100)) as language00</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,lngimage00 &nbsp; &nbsp; &nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; ,cast(language01 as varbinary(100)) as language01</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,lngimage01</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; ,cast(language02 as varbinary(100)) as language02</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,lngimage02</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; ,cast(language03 as varbinary(100)) as language03</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,lngimage03</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; ,cast(language04 as varbinary(100)) as language04</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,lngimage04</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; ,cast(language05 as varbinary(100)) as language05</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,lngimage05</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; ,cast(language06 as varbinary(100)) as language06</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,lngimage06</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; ,cast(language07 as varbinary(100)) as language07</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,lngimage07</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; ,cast(language08 as varbinary(100)) as language08</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,lngimage08</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; ,cast(language09 as varbinary(100)) as language09</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,lngimage09</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; ,cast(language10 as varbinary(100)) as language10</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; ,lngimage10 &nbsp; &nbsp; &nbsp;</li><li style="" class="li1">&nbsp; FROM dbo.prefs_sl</li><li style="" class="li2">endtext &nbsp;</li><li style="" class="li1">mysqlexec(m.lcSQL, 'Prefs_sl', program())</li></ol></div><div id="cb87472" style="display: none; color: red;"></div></div></div>

<p>mysqlexec here is a wrapper for the sqlexec function.</p>

<p>Now I found the first discrepancy with Rick's article - I found that I don't need any conversion after that, I can assign the field's content to the text property of this ActiveX (or ControlSource property) and the control correctly displays the unicode data!</p>

<p>I was happy - the first part of displaying data turned out to be quite easy!</p>

<p>Here is the code I used to display the data:</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb24282'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb24282','cb37933'); 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="text" id="cb24282" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">FOR EACH loObject IN thisform.Objects foxobject</li><li style="" class="li2">&nbsp; &nbsp;IF loObject.Baseclass = 'Container'</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; loObject.LanguageField = SUBSTR(loObject.name,4,LEN(loObject.name))</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; loObject.ImageField = 'lngImage' + RIGHT(loObject.Name,2)</li><li style="" class="li1">&nbsp; &nbsp;ENDIF </li><li style="" class="li2">next &nbsp; </li><li style="" class="li1">&nbsp;</li><li style="" class="li2">thisform.cntLanguage01.SetFocus()</li></ol></div><div id="cb37933" style="display: none; color: red;"></div></div></div>

<p>And in the container itself the following code in the LanguageField assign method:</p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb4784'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb4784','cb89993'); 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="text" id="cb4784" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">this.lblLanguage.caption = this.lblLanguage.caption + ' ' + transform(val(right(tLanguageField,2)))</li><li style="" class="li2">&nbsp; this.txtLanguage.Enabled = .t.</li><li style="" class="li1">&nbsp; this.txtLanguage.TabStop = .t. &nbsp; </li><li style="" class="li2">&nbsp; this.lProgrammaticChange = .t.</li><li style="" class="li1">&nbsp; this.txtLanguage.text = evaluate('prefs_sl.' + tLanguageField)</li><li style="" class="li2">&nbsp; this.lProgrammaticChange = .f.</li></ol></div><div id="cb89993" style="display: none; color: red;"></div></div></div>

<p>Everything was good so far. However, I haven't expected how hard (for me) it will be the 'saving' data part. I was spending days trying various ideas from Rick's article and was still unable to achieve the desired result. I was about to throw all I had away and try to switch to ADO for data retrieval, but luckily Gregory Adam helped me here and published a working sample.</p>

<p>First of all, we need to make sure there is no UT8 translation to the current code page. For this we need to do the following</p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb65405'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb65405','cb16085'); 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="text" id="cb65405" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">COMPROP(this,'UTF8',1)</li></ol></div><div id="cb16085" style="display: none; color: red;"></div></div></div>

<p>In the txtLanguage Init method.</p>

<p>We also need the following functions:</p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb66678'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb66678','cb39526'); 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="text" id="cb66678" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">&amp;&amp; StringConversion</li><li style="" class="li2">&amp;&amp; Gregory Adam 2011</li><li style="" class="li1">*===============================================================================</li><li style="" class="li2">#define true&nbsp; &nbsp; .T.</li><li style="" class="li1">#define false&nbsp; &nbsp;.F.</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">*_______________________________________________________________________________</li><li style="" class="li2">function StringToUTF8(utf8Out, stringIn, codepageIn)</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; local success</li><li style="" class="li1">&nbsp; &nbsp; success = true</li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; do case</li><li style="" class="li2">&nbsp; &nbsp; case !m.success</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; case !StringToUTF16(@m.utf8Out, m.stringIn, m.codepageIn)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; assert false</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; success = false</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; case !StringUTF16ToUTF8(@m.utf8Out, m.utf8Out)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; assert false</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; success = false</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; endcase</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; return m.success</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">endfunc</li><li style="" class="li1">*_______________________________________________________________________________</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">*_______________________________________________________________________________</li><li style="" class="li1">*_______________________________________________________________________________</li><li style="" class="li2">#define CP_ACP&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0</li><li style="" class="li1">#define CP_MACCP&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2</li><li style="" class="li2">#define CP_OEMCP&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1</li><li style="" class="li1">#define CP_SYMBOL&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 42</li><li style="" class="li2">#define CP_THREAD_ACP&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 3</li><li style="" class="li1">#define CP_UTF7&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 65000</li><li style="" class="li2">#define CP_UTF8&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 65001</li><li style="" class="li1">#define MB_PRECOMPOSED&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x1</li><li style="" class="li2">#define MB_COMPOSITE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x2</li><li style="" class="li1">#define MB_USEGLYPHCHARS&nbsp; &nbsp; &nbsp; &nbsp; 0x4</li><li style="" class="li2">#define MB_ERR_INVALID_CHARS&nbsp; &nbsp; 0x8</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">#define WC_DEFAULTCHAR&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x00000040 </li><li style="" class="li1">#define WC_ERR_INVALID_CHARS&nbsp; &nbsp; 0x00000080 </li><li style="" class="li2">#define WC_NO_BEST_FIT_CHARS &nbsp; &nbsp;0x00000400 </li><li style="" class="li1">*_______________________________________________________________________________</li><li style="" class="li2">function StringToUTF16(utf16Out, stringIn, codepageIn)</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; local success</li><li style="" class="li1">&nbsp; &nbsp; success = true</li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; do case</li><li style="" class="li2">&nbsp; &nbsp; case !m.success</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; case empty(len(m.stringIn))</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; utf16Out = ''</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; otherwise</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; local lpWideCharStr, result</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; lpWideCharStr = space(len(m.stringIn)*2)</li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; result = MultiByteToWideChar( ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; evl(m.codepageIn, cpcurrent()), ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MB_ERR_INVALID_CHARS, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @m.stringIn, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; len(m.stringIn), ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @m.lpWideCharStr, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; len(m.lpWideCharStr) ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; do case</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; case !m.success</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; case empty(m.result)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assert false</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success = false</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; otherwise</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; utf16Out = left(m.lpWideCharStr, m.result * 2) </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; endcase</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; endcase</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; return m.success</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">endfunc</li><li style="" class="li2">*_______________________________________________________________________________</li><li style="" class="li1">function StringUTF16ToUTF8(utf8Out, utf16In)</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; local success</li><li style="" class="li2">&nbsp; &nbsp; success = true</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; do case</li><li style="" class="li1">&nbsp; &nbsp; case !m.success</li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; case empty(len(m.utf16In))</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; utf8Out = ''</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; otherwise</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; local lpMultiByteStr, lpUsedDefaultChar, result</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; lpMultiByteStr = space(len(m.utf16In) * 2)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; lpUsedDefaultChar = 0</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; result = WideCharToMultiByte( ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CP_UTF8, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WC_ERR_INVALID_CHARS, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @m.utf16In, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; len(m.utf16In)/2, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @m.lpMultiByteStr, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; len(m.lpMultiByteStr), ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @m.lpUsedDefaultChar ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; do case</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; case !m.success</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; case empty(m.result)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assert false</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success = false</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; otherwise</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; utf8Out = left(m.lpMultiByteStr, m.result)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; endcase</li><li style="" class="li1">&nbsp; &nbsp; endcase</li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; return m.success</li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">endfunc</li><li style="" class="li1">*_______________________________________________________________________________</li><li style="" class="li2">function StringUTF8ToUTF16(utf16Out, uft8In)</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; return StringToUTF16(@m.utf16Out, uft8In, CP_UTF8)</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">endfunc</li><li style="" class="li1">*_______________________________________________________________________________</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">function MultiByteToWideChar</li><li style="" class="li2">&nbsp; &nbsp; lparameters codepage, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwFlags, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpMultiByteStr, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cbMultiByte, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpWideCharStr, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cchWideChar</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; local success</li><li style="" class="li2">&nbsp; &nbsp; success = true</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; local result</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; do case</li><li style="" class="li1">&nbsp; &nbsp; case !m.success</li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; otherwise</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; try</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; declare integer MultiByteToWideChar in Kernel32.dll ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long&nbsp; &nbsp; codepage, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long&nbsp; &nbsp; dwFlags, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string@&nbsp;lpMultiByteStr, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer&nbsp;cbMultiByte, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string@&nbsp;lpWideCharStr, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer&nbsp;cchWideChar</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = MultiByteToWideChar( ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.codepage, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.dwFlags, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @m.lpMultiByteStr, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.cbMultiByte, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @m.lpWideCharStr, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.cchWideChar ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; catch</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assert false</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success = false</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; endtry</li><li style="" class="li2">&nbsp; &nbsp; endcase</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; return iif(m.success, m.result, 0)</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">endfunc</li><li style="" class="li1">*_______________________________________________________________________________</li><li style="" class="li2">function WideCharToMultiByte</li><li style="" class="li1">&nbsp; &nbsp; lparameters codepage, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwFlags, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpWideCharStr, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cchWideChar, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpMultiByteStr, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cbMultiByte, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpDefaultChar, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpUsedDefaultChar</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; local success</li><li style="" class="li1">&nbsp; &nbsp; success = true</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; local result</li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; do case</li><li style="" class="li2">&nbsp; &nbsp; case !m.success</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; otherwise</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; try</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; declare integer WideCharToMultiByte in Kernel32.dll ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long&nbsp; &nbsp; codepage, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long&nbsp; &nbsp; dwFlags, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string@&nbsp;lpWideCharStr, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer&nbsp;cchWideChar, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string@&nbsp;lpMultiByteStr, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer&nbsp;cbMultiByte, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; lpDefaultChar, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer&nbsp;@lpUsedDefaultChar</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = WideCharToMultiByte ( ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.codepage, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.dwFlags, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @m.lpWideCharStr, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.cchWideChar, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @m.lpMultiByteStr, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.cbMultiByte, ;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.lpDefaultChar, ;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @m.lpUsedDefaultChar;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; catch</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assert false</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success = false</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; endtry</li><li style="" class="li1">&nbsp; &nbsp; endcase</li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; return iif(m.success, m.result, 0)</li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">endfunc</li></ol></div><div id="cb39526" style="display: none; color: red;"></div></div></div><p> </p>

<p>and then in the Change event of the ActiveX textbox I added the following code</p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb80516'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb80516','cb68874'); 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="text" id="cb80516" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">IF this.parent.lProgrammaticChange </li><li style="" class="li2">&nbsp; &nbsp; RETURN</li><li style="" class="li1">ENDIF &nbsp; &nbsp; </li><li style="" class="li2">if thisform.IsChanged OR (not this.CurrentValue == this.Text)</li><li style="" class="li1">&nbsp; &nbsp;thisform.IsChanged = .t.</li><li style="" class="li2">&nbsp; &nbsp;local utf8, utf16Out</li><li style="" class="li1">&nbsp; &nbsp;utf8 = this.text</li><li style="" class="li2">&nbsp; &nbsp;if not empty(utf8)</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; = StringUtf8ToUTF16(@m.utf16Out, m.utf8)</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; replace (this.parent.LanguageField) with utf16Out</li><li style="" class="li1">&nbsp; &nbsp;else</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; replace (this.parent.LanguageField) with ''</li><li style="" class="li1">&nbsp; &nbsp;endif</li><li style="" class="li2">endif</li></ol></div><div id="cb68874" style="display: none; color: red;"></div></div></div>

<p>and finally in the Save method of the form</p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb67558'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb67558','cb72422'); 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="text" id="cb67558" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">IF THISFORM.IsChanged </li><li style="" class="li2">&nbsp; LOCAL lcSQL</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; TEXT TO lcSQL noshow</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;UPDATE dbo.prefs_sl </li><li style="" class="li2">&nbsp; &nbsp; &nbsp;SET defltlang = ?prefs_sl.DefltLang,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;language00 = cast(?prefs_sl.language00 as nvarchar(100)),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;lngimage00 = ?prefs_sl.lngimage00,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;language01 = cast(?prefs_sl.language01 as nvarchar(100)),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;lngimage01 = ?prefs_sl.lngimage01,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;language02 = cast(?prefs_sl.language02 as nvarchar(100)),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;lngimage02 = ?prefs_sl.lngimage02,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;language03 = cast(?prefs_sl.language03 as nvarchar(100)),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;lngimage03 = ?prefs_sl.lngimage03,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;language04 = cast(?prefs_sl.language04 as nvarchar(100)),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;lngimage04 = ?prefs_sl.lngimage04,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;language05 = cast(?prefs_sl.language05 as nvarchar(100)),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;lngimage05 = ?prefs_sl.lngimage05,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;language06 = cast(?prefs_sl.language06 as nvarchar(100)),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;lngimage06 = ?prefs_sl.lngimage06,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;language07 = cast(?prefs_sl.language07 as nvarchar(100)),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;lngimage07 = ?prefs_sl.lngimage07,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;language08 = cast(?prefs_sl.language08 as nvarchar(100)),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;lngimage08 = ?prefs_sl.lngimage08,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;language09 = cast(?prefs_sl.language09 as nvarchar(100)),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;lngimage09 = ?prefs_sl.lngimage09,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;language10 = cast(?prefs_sl.language10 as nvarchar(100)),</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;lngimage10 = ?prefs_sl.lngimage10</li><li style="" class="li1">&nbsp; &nbsp; &nbsp;where pri_key = ?prefs_sl.pri_key</li><li style="" class="li2">&nbsp; ENDTEXT</li><li style="" class="li1">&nbsp;RETURN mySQLExec(m.lcSQL, '',PROGRAM())</li><li style="" class="li2">ENDIF</li></ol></div><div id="cb72422" style="display: none; color: red;"></div></div></div>

<p>As we can see, we need to convert data back from binary to nvarchar.<br />
With all this code in place, we display the unicode data and save them back to SQL Server.</p>

<p>A big thanks to Gregory for helping me with the code - I don't know where I would be without his help.</p>

<p>Olaf Doschke showed another way which is even simpler than implemented solution and in accordance to the original Rick's article.</p>

<p>In the form's Load we need the following:</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb2496'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb2496','cb31333'); 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="text" id="cb2496" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">Sys(987,.F.)</li><li style="" class="li2">Sys(3101,65001)</li></ol></div><div id="cb31333" style="display: none; color: red;"></div></div></div>

<p>Then, after getting binary data from SQL Server the same way as I show in this blog, we still need to use createbinary, e.g.</p>

<p>this.txtLanguage.text = createbinary(prefs_sl.Language00)</p>

<p>We don't want to use COMPROP now for the ActiveX.</p>

<p>and then, saving data, we need to follow Rick's steps:</p>

<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb76033'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb76033','cb68515'); 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="text" id="cb76033" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">pcSavedText1 = Strconv(This.Text,12)</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">*** Must explicitly force to binary &#8211; can also use CAST in 9.0</li><li style="" class="li2">pcSavedText1 = CREATEBINARY(pcSavedText1)</li></ol></div><div id="cb68515" style="display: none; color: red;"></div></div></div>

<p>and then convert this value back to nvarchar(max) when saving with sqlexec.</p>

<p>This is how the form with many languages looked like</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/ManyLanguages.JPG?mtime=1325442752"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/ManyLanguages.JPG?mtime=1325442752" width="927" height="654" /></a></div>


<p>---------------------------<br />
I need to also tell, that after I finished the form and was happy to show it to my colleagues, I was up to a big disappointment. It turned out we are not going to support unicode (our main application is not up to support it yet) and so I will be re-doing this form after we will agree on the tables' structures (they also are going to change). So, what I showed in this blog post we're not going to use at present in production.</p>

<p><span class="MT_red">UPDATE.</span> My colleague also re-designed prefs_sl table to one EAV Settings table. We know that EAV design has its own problems, but for saving various application level settings it can be used. I already re-designed the forms we're going to use. </p>

<p>But in any case, it was a very important exercise and I hope it will help other people who need to display and save Unicode data in their Visual FoxPro applications.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/displaying-and-saving-unicode-data">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/displaying-and-saving-unicode-data#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1574</wfw:commentRss>
		</item>
				<item>
			<title>Maximum length of data in every column in a table</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/maximum-length-of-data-in</link>
			<pubDate>Tue, 06 Dec 2011 13:41:00 +0000</pubDate>			<dc:creator>Naomi Nosonovsky</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">1518@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;In this short blog post I would like to share a script I wrote yesterday as an answer to &lt;a href=&quot;http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/d2d765e2-7994-4b3e-826b-c49c12f3b6fe&quot;&gt;this MSDN thread&lt;/a&gt; to find maximum length of data in every column of a table passed as a parameter.&lt;/p&gt;

&lt;p&gt;Some notes for the script:&lt;/p&gt;

&lt;p&gt;LEN function can be used with many SQL Server types excluding text and new types such as Geography or HierarchyID. I added exclusion of these types directly in the query, there may be some other types which need to be also excluded.&lt;/p&gt;

&lt;p&gt;I am using one of my favorite ideas of generating script to run using available meta-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;cb51226&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; AdventureWorks2008R2&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--declare @TableName sysname = &#039;Employee&#039;, @TableSchema sysname = &#039;HumanResources&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @TableName &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Address&#039;&lt;/span&gt;, @TableSchema &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Person&#039;&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;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;MAX&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;SELECT&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;STUFF&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;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;UNION ALL &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;select &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Table_Name,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; AS Table_Name, &#039;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Column_Name,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; AS ColumnName, MAX(LEN(&#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Column_Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;)) as [Max Length], &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;C.&lt;span style=&quot;color: #202020;&quot;&gt;DATA_TYPE&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; AS Data_Type, &#039;&lt;/span&gt; + &lt;br /&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;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;C.&lt;span style=&quot;color: #202020;&quot;&gt;CHARACTER_MAXIMUM_LENGTH&lt;/span&gt;, C.&lt;span style=&quot;color: #202020;&quot;&gt;NUMERIC_SCALE&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;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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&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;span style=&quot;color: #FF0000;&quot;&gt;&#039; &amp;nbsp;AS Data_Width FROM &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;TABLE_SCHEMA&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.&#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Table_Name&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; &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; TABLE_NAME = @TableName &lt;br /&gt;AND table_schema = @TableSchema&lt;br /&gt;AND DATA_TYPE NOT IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;text&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;ntext&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;XML&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;HierarchyID&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Geometry&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Geography&#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;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; COLUMN_NAME &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FOR&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;XML&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;PATH&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;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #0000FF;&quot;&gt;Type&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;value&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;.&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;varchar(max)&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;11&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--print @SQL&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;cb58373&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here is another version of the same script which attempts to list every column in a table but uses DATALENGTH function instead of LEN for the types where we can not use LEN function:&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;cb2183&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; AdventureWorks2008R2&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--declare @TableName sysname = &#039;Employee&#039;, @TableSchema sysname = &#039;HumanResources&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @TableName &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Address&#039;&lt;/span&gt;, @TableSchema &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Person&#039;&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;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;MAX&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;SELECT&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;STUFF&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;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;UNION ALL &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;select &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Table_Name,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; AS Table_Name, &#039;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Column_Name,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; AS ColumnName, MAX(&#039;&lt;/span&gt; + &lt;br /&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; DATA_TYPE IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;XML&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;HierarchyID&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Geometry&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Geography&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;text&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;ntext&#039;&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;&lt;span style=&quot;color: #0000FF;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;DATALENGTH(&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ELSE&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;LEN(&#039;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Column_Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;)) as [Max Length], &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;C.&lt;span style=&quot;color: #202020;&quot;&gt;DATA_TYPE&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; AS Data_Type, &#039;&lt;/span&gt; + &lt;br /&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;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;C.&lt;span style=&quot;color: #202020;&quot;&gt;CHARACTER_MAXIMUM_LENGTH&lt;/span&gt;, C.&lt;span style=&quot;color: #202020;&quot;&gt;NUMERIC_SCALE&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;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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&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;span style=&quot;color: #FF0000;&quot;&gt;&#039; &amp;nbsp;AS Data_Width FROM &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;TABLE_SCHEMA&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.&#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Table_Name&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; &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; TABLE_NAME = @TableName &lt;br /&gt;AND table_schema = @TableSchema&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--AND DATA_TYPE NOT IN (&#039;XML&#039;,&#039;HierarchyID&#039;,&#039;Geometry&#039;,&#039;Geography&#039;)&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; COLUMN_NAME &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FOR&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;XML&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;PATH&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;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #0000FF;&quot;&gt;Type&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;value&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;.&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;varchar(max)&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;11&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--print @SQL&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;cb21269&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;



&lt;p&gt;*** &lt;strong&gt;Remember, if you have a SQL related question, try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&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/maximum-length-of-data-in&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>In this short blog post I would like to share a script I wrote yesterday as an answer to <a href="http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/d2d765e2-7994-4b3e-826b-c49c12f3b6fe">this MSDN thread</a> to find maximum length of data in every column of a table passed as a parameter.</p>

<p>Some notes for the script:</p>

<p>LEN function can be used with many SQL Server types excluding text and new types such as Geography or HierarchyID. I added exclusion of these types directly in the query, there may be some other types which need to be also excluded.</p>

<p>I am using one of my favorite ideas of generating script to run using available meta-data.</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb44337'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb44337','cb26594'); 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="cb44337" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">USE</span> AdventureWorks2008R2</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">--declare @TableName sysname = 'Employee', @TableSchema sysname = 'HumanResources'</span></li><li style="" class="li2"><span style="color: #0000FF;">DECLARE</span> @TableName <span style="color: #0000FF;">sysname</span> = <span style="color: #FF0000;">'Address'</span>, @TableSchema <span style="color: #0000FF;">sysname</span> = <span style="color: #FF0000;">'Person'</span></li><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF00FF;">STUFF</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> </li><li style="" class="li2"><span style="color: #FF0000;">'</span></li><li style="" class="li1"><span style="color: #FF0000;">UNION ALL </span></li><li style="" class="li2"><span style="color: #FF0000;">select '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Table_Name,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' AS Table_Name, '</span> + </li><li style="" class="li1"><span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Column_Name,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' AS ColumnName, MAX(LEN('</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Column_Name<span style="color: #808080;">&#41;</span> + </li><li style="" class="li2"><span style="color: #FF0000;">')) as [Max Length], '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>C.<span style="color: #202020;">DATA_TYPE</span>,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' AS Data_Type, '</span> + </li><li style="" class="li1"><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>C.<span style="color: #202020;">CHARACTER_MAXIMUM_LENGTH</span>, C.<span style="color: #202020;">NUMERIC_SCALE</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + </li><li style="" class="li2"><span style="color: #FF0000;">' &nbsp;AS Data_Width FROM '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>TABLE_SCHEMA<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'.'</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Table_Name<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> <span style="color: #00AF00;">INFORMATION_SCHEMA</span>.<span style="color: #00AF00;">COLUMNS</span> C </li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> TABLE_NAME = @TableName </li><li style="" class="li1">AND table_schema = @TableSchema</li><li style="" class="li2">AND DATA_TYPE NOT IN <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'text'</span>,<span style="color: #FF0000;">'ntext'</span>,<span style="color: #FF0000;">'XML'</span>,<span style="color: #FF0000;">'HierarchyID'</span>,<span style="color: #FF0000;">'Geometry'</span>,<span style="color: #FF0000;">'Geography'</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> COLUMN_NAME </li><li style="" class="li2"><span style="color: #0000FF;">FOR</span> <span style="color: #0000FF;">XML</span> <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>,<span style="color: #0000FF;">Type</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>,<span style="color: #FF0000;">'varchar(max)'</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">1</span>,<span style="color: #000;">11</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> &nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">--print @SQL</span></li><li style="" class="li2"><span style="color: #0000FF;">EXECUTE</span> <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb26594" style="display: none; color: red;"></div></div></div>

<p>Here is another version of the same script which attempts to list every column in a table but uses DATALENGTH function instead of LEN for the types where we can not use LEN function:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb85035'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb85035','cb52520'); 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="cb85035" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">USE</span> AdventureWorks2008R2</li><li style="" class="li2"><span style="color: #00AF00;">--declare @TableName sysname = 'Employee', @TableSchema sysname = 'HumanResources'</span></li><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @TableName <span style="color: #0000FF;">sysname</span> = <span style="color: #FF0000;">'Address'</span>, @TableSchema <span style="color: #0000FF;">sysname</span> = <span style="color: #FF0000;">'Person'</span></li><li style="" class="li2"><span style="color: #0000FF;">DECLARE</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF00FF;">STUFF</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> </li><li style="" class="li1"><span style="color: #FF0000;">'</span></li><li style="" class="li2"><span style="color: #FF0000;">UNION ALL </span></li><li style="" class="li1"><span style="color: #FF0000;">select '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Table_Name,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' AS Table_Name, '</span> + </li><li style="" class="li2"><span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Column_Name,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' AS ColumnName, MAX('</span> + </li><li style="" class="li1"><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> DATA_TYPE IN <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'XML'</span>,<span style="color: #FF0000;">'HierarchyID'</span>,<span style="color: #FF0000;">'Geometry'</span>,<span style="color: #FF0000;">'Geography'</span>,<span style="color: #FF0000;">'text'</span>,<span style="color: #FF0000;">'ntext'</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'DATALENGTH('</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'LEN('</span> <span style="color: #0000FF;">END</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Column_Name<span style="color: #808080;">&#41;</span> + </li><li style="" class="li1"><span style="color: #FF0000;">')) as [Max Length], '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>C.<span style="color: #202020;">DATA_TYPE</span>,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' AS Data_Type, '</span> + </li><li style="" class="li2"><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>C.<span style="color: #202020;">CHARACTER_MAXIMUM_LENGTH</span>, C.<span style="color: #202020;">NUMERIC_SCALE</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + </li><li style="" class="li1"><span style="color: #FF0000;">' &nbsp;AS Data_Width FROM '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>TABLE_SCHEMA<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'.'</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Table_Name<span style="color: #808080;">&#41;</span></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> TABLE_NAME = @TableName </li><li style="" class="li2">AND table_schema = @TableSchema</li><li style="" class="li1"><span style="color: #00AF00;">--AND DATA_TYPE NOT IN ('XML','HierarchyID','Geometry','Geography')</span></li><li style="" class="li2"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> COLUMN_NAME </li><li style="" class="li1"><span style="color: #0000FF;">FOR</span> <span style="color: #0000FF;">XML</span> <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>,<span style="color: #0000FF;">Type</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>,<span style="color: #FF0000;">'varchar(max)'</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">1</span>,<span style="color: #000;">11</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> &nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">--print @SQL</span></li><li style="" class="li1"><span style="color: #0000FF;">EXECUTE</span> <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb52520" style="display: none; color: red;"></div></div></div>



<p>*** <strong>Remember, if you have a SQL related question, try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/maximum-length-of-data-in">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/maximum-length-of-data-in#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1518</wfw:commentRss>
		</item>
				<item>
			<title>How to get information about all databases without a loop</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas</link>
			<pubDate>Wed, 30 Nov 2011 12:47:00 +0000</pubDate>			<dc:creator>Naomi Nosonovsky</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">846@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Quite often we want to consolidate query information across all databases (or all user databases). When this question is asked in forums, the usual recommendation is to either try running &lt;a href=&quot;http://www.mssqltips.com/tip.asp?tip=1905&amp;amp;ctc&quot;&gt;undocumented sp_MSForEachDB stored procedure&lt;/a&gt; or do a loop and use dynamic SQL. The example of such stored procedures can be found, for example, &lt;a href=&quot;http://sqlusa.com/bestpractices/training/scripts/dynamicsql/&quot;&gt;THIRD EXAMPLE - SPROC to enumerate all objects in databases&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The idea occurred to me last night that while we do need a dynamic SQL to solve this problem, we don&#039;t really need a loop unless we need a second loop involving looping through all tables.&lt;/p&gt;

&lt;h3&gt;Table of Contents:&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#1&quot;&gt;Indexes in all databases with their usage&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#2&quot;&gt;Indexes in all databases with their physical stats&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#3&quot;&gt;Count of all objects in all databases&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#4&quot;&gt;Record Count in every table in a database&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#5&quot;&gt;Quick Record Count in All Tables in All Databases&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#6&quot;&gt;Quick Record Count in All Tables in All Databases (2)&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#7&quot;&gt;Sizes of All Tables in a Database&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#8&quot;&gt;Database Files Sizes in All Databases&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#9&quot;&gt;Database Files Sizes in All Databases and used space&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#10&quot;&gt;Backup All Databases with Compression (SQL 2008)&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#11&quot;&gt;All Schema Names in All Databases&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#12&quot;&gt;List of All Tables in All Databases&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#13&quot;&gt;List of All Tables in All Databases (2)&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#14&quot;&gt;List of all Stored Procedures in All Databases&lt;/a&gt;&lt;/p&gt;


&lt;p&gt;Here is a script demonstrating this idea - it lists indexes in all databases with their usage. This script was an answer to &lt;a href=&quot;http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/ab4b39b0-5bb2-4ffc-a25d-6bb8fe6124ef/#f3a3a011-4318-44e2-97c1-5bdadd7b2444&quot;&gt;Help in T-SQL Query thread&lt;/a&gt; in MSDN T-SQL forum.&lt;br /&gt;
 &lt;/p&gt;
&lt;h2 id=&quot;1&quot;&gt;Indexes in all databases with their usage&lt;/h2&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;cb26635&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;declare&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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;if&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;object_id&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;tempdb..#Result&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;U&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; not NULL&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;drop&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;table&lt;/span&gt; #Result&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; #Result &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;DBName &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, TableName &lt;span style=&quot;color: #0000FF;&quot;&gt;Sysname&lt;/span&gt;, IndexName &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;Usage&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;bigint&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;select&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;coalesce&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&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;13&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&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;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; use &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&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;Name&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;span style=&quot;color: #FF0000;&quot;&gt;&#039;;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;insert into #Result select &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&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;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; as DbName, &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;object_name(i.object_id) as tablename, &amp;nbsp;i.name as indexname, &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;s.user_seeks + s.user_scans + s.user_lookups + s.user_updates as usage&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;from sys.indexes i &amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;inner join sys.dm_db_index_usage_stats s &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;on s.object_id = i.object_id &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;and s.index_id = i.index_id &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;and s.database_id = db_id()&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;where objectproperty(i.object_id, &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;IsUserTable&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;) = 1 &amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;and i.index_id &amp;gt; 0 &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;order by usage;&#039;&lt;/span&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: #202020;&quot;&gt;databases&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--print (@SQL)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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; * &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; #Result &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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;DbName&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Usage&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;drop&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;table&lt;/span&gt; #Result&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb88446&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;br /&gt;&lt;/p&gt;
&lt;h2 id=&quot;2&quot;&gt;Indexes in all databases with their physical stats&lt;/h2&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;cb92624&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;declare&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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;set&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&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;SQL&lt;/span&gt; = @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; +&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Select &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; as [DB Name], &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;object_Name(PS.Object_ID,&#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;convert&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&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;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,database_id&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;) as [Object],&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;I.Name as [Index Name], PS.Partition_Number, PS.Index_Type_Desc, &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;PS.alloc_unit_type_desc,&amp;nbsp; &amp;nbsp; PS.index_depth,&amp;nbsp;PS.index_level,&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;PS.avg_fragmentation_in_percent,&amp;nbsp; &amp;nbsp; PS.fragment_count,&amp;nbsp; PS.avg_fragment_size_in_pages,&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;PS.page_count,&amp;nbsp; PS.avg_page_space_used_in_percent,&amp;nbsp; PS.record_count,&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;PS.ghost_record_count,&amp;nbsp; PS.version_ghost_record_count,&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;PS.min_record_size_in_bytes,&amp;nbsp; &amp;nbsp; PS.max_record_size_in_bytes,&amp;nbsp; &amp;nbsp; PS.avg_record_size_in_bytes,&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;PS.forwarded_record_count,&amp;nbsp; PS.compressed_page_count&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; from &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.sys.dm_db_index_physical_stats(&#039;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;convert&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&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;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,database_id&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;, NULL, NULL, NULL, NULL) PS &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;INNER JOIN &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.sys.Indexes I on PS.Object_ID = I.Object_ID and PS.Index_ID = I.Index_ID &#039;&lt;/span&gt; &lt;br /&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;13&lt;/span&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;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: #202020;&quot;&gt;databases&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;where&lt;/span&gt; state_desc = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;ONLINE&#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;cb67277&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;


&lt;p&gt;Another example of the same idea you can find in &lt;a href=&quot;http://wiki.lessthandot.com/index.php/Finding_Record_with_Last_Modified_date_in_all_tables&quot;&gt;Finding Record with Last Modified date in all tables&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using this same idea you can get a count of all objects in all your databases using this PIVOT query:&lt;/p&gt;
&lt;h2 id=&quot;3&quot;&gt;Count of all objects in all databases&lt;/h2&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;cb16570&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;declare&lt;/span&gt; @Qry &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: #FF00FF;&quot;&gt;max&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; @Qry = &lt;span style=&quot;color: #FF00FF;&quot;&gt;coalesce&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@Qry + &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;13&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&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;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; UNION ALL &#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;select &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&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;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; as DBName, [AGGREGATE_FUNCTION], [CHECK_CONSTRAINT],[DEFAULT_CONSTRAINT],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[FOREIGN_KEY_CONSTRAINT],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[SQL_SCALAR_FUNCTION],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[CLR_SCALAR_FUNCTION],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[CLR_TABLE_VALUED_FUNCTION],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[SQL_INLINE_TABLE_VALUED_FUNCTION],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[INTERNAL_TABLE],[SQL_STORED_PROCEDURE],[CLR_STORED_PROCEDURE],[PLAN_GUIDE],[PRIMARY_KEY_CONSTRAINT],&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[RULE],[REPLICATION_FILTER_PROCEDURE],[SYNONYM],[SERVICE_QUEUE],[CLR_TRIGGER],[SQL_TABLE_VALUED_FUNCTION],[SQL_TRIGGER],&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[TABLE_TYPE],[USER_TABLE],[UNIQUE_CONSTRAINT],[VIEW],[EXTENDED_STORED_PROCEDURE]&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;from (select [Name], type_Desc from &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&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;Name&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;span style=&quot;color: #FF0000;&quot;&gt;&#039;.sys.objects where is_ms_shipped = 0) src &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;PIVOT (count([Name]) FOR type_desc in ([AGGREGATE_FUNCTION], [CHECK_CONSTRAINT],[DEFAULT_CONSTRAINT],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[FOREIGN_KEY_CONSTRAINT],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[SQL_SCALAR_FUNCTION],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[CLR_SCALAR_FUNCTION],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[CLR_TABLE_VALUED_FUNCTION],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[SQL_INLINE_TABLE_VALUED_FUNCTION],&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[INTERNAL_TABLE],[SQL_STORED_PROCEDURE],[CLR_STORED_PROCEDURE],[PLAN_GUIDE],[PRIMARY_KEY_CONSTRAINT],&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[RULE],[REPLICATION_FILTER_PROCEDURE],[SYNONYM],[SERVICE_QUEUE],[CLR_TRIGGER],[SQL_TABLE_VALUED_FUNCTION],[SQL_TRIGGER],&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;[TABLE_TYPE],[USER_TABLE],[UNIQUE_CONSTRAINT],[VIEW],[EXTENDED_STORED_PROCEDURE])) pvt&#039;&lt;/span&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: #202020;&quot;&gt;databases&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;where&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;master&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;tempdb&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;model&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;msdb&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@Qry&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;cb21886&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can only list types you&#039;re interested in, of course.&lt;/p&gt;

&lt;p&gt;The script below will give you a count of records in every table in a database:&lt;/p&gt;
&lt;h2 id=&quot;4&quot;&gt;Record Count in every table in a database&lt;/h2&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;cb54750&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; &amp;nbsp;@DynamicSQL &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: #FF00FF;&quot;&gt;MAX&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;SELECT&lt;/span&gt; &amp;nbsp; @DynamicSQL = &lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@DynamicSQL + &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;13&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; UNION ALL &#039;&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;13&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;SELECT &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;table_schema,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; as [Schema Name], &#039;&lt;/span&gt; +&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; &amp;nbsp; &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;TABLE_NAME,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; as [Table Name], COUNT(*) AS [Records Count] FROM &#039;&lt;/span&gt; + &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; &amp;nbsp; &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Table_schema&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.&#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;TABLE_NAME&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; &amp;nbsp; &amp;nbsp; &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;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; TABLE_NAME&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--print (@DynamicSQL) -- we may want to use PRINT to debug the SQL&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXEC&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; @DynamicSQL&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;cb40853&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Quick row count in all tables in all databases in the server instance (you can exclude system databases from that loop, obviously)&lt;br /&gt;&lt;/p&gt;
&lt;h2 id=&quot;5&quot;&gt;Quick Record Count in All Tables in All Databases&lt;/h2&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;cb59819&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;declare&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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;set&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--select * from sys.databases &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;SQL&lt;/span&gt; = @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;13&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;USE &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&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;name&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;span style=&quot;color: #FF0000;&quot;&gt;&#039;;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;SELECT &#039;&lt;/span&gt; +&lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&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;name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;as [Database Name], &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; SchemaName=s.name&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp;,TableName=t.name&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp;,CreateDate=t.create_date&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp;,ModifyDate=t.modify_date&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp;,p.rows&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp;,DataInKB=sum(a.used_pages)*8&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;FROM sys.schemas s&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;JOIN sys.tables t on s.schema_id=t.schema_id&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;JOIN sys.partitions p on t.object_id=p.object_id&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;JOIN sys.allocation_units a on a.container_id=p.partition_id&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;GROUP BY s.name, t.name, t.create_date, t.modify_date, p.rows&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;ORDER BY SchemaName, TableName&#039;&lt;/span&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: #202020;&quot;&gt;databases&lt;/span&gt; &amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;cb59534&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Another way with less info:&lt;/p&gt;
&lt;h2 id=&quot;6&quot;&gt;Quick Record Count in All Tables in All Databases&lt;/h2&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;cb30847&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;declare&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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;set&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--select * from sys.databases &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;SQL&lt;/span&gt; = @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;13&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;USE &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&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;name&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;span style=&quot;color: #FF0000;&quot;&gt;&#039;;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;SELECT &#039;&lt;/span&gt; +&lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&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;name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;as [Database Name], so.name AS [Table Name], &amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp;rows AS [RowCount] &amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;FROM sysindexes AS si &amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp;join sysobjects AS so on si.id = so.id &amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;WHERE indid IN (0,1) &amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp;AND xtype = &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;U&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&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: #202020;&quot;&gt;databases&lt;/span&gt; &amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;cb25225&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here is a script showing sizes from all tables in a database.&lt;/p&gt;
&lt;h2 id=&quot;7&quot;&gt;Sizes of All Tables in a Database&lt;/h2&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;cb75620&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--exec sp_MSforeachtable &#039;print &#039;&#039;?&#039;&#039; exec sp_spaceused &#039;&#039;?&#039;&#039;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;OBJECT_ID&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;tempdb..#TablesSizes&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NOT NULL&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;drop&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;table&lt;/span&gt; #TablesSizes&lt;br /&gt;&amp;nbsp; &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; #TablesSizes &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;TableName &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;Rows&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;bigint&lt;/span&gt;, reserved &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;100&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;data&lt;/span&gt; &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;100&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, index_size &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;100&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, unused &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;100&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: #0000FF;&quot;&gt;declare&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;sql&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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; @&lt;span style=&quot;color: #0000FF;&quot;&gt;sql&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;coalesce&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;sql&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;insert into #TablesSizes execute sp_spaceused &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Table_Name,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&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; &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;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--print (@SQL)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;select&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; #TablesSizes &lt;span style=&quot;color: #0000FF;&quot;&gt;order&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;by&lt;/span&gt; TableName&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb97525&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here is a script showing database files sizes for all databases&lt;/p&gt;

&lt;p&gt;Before I show the T-SQL code I&#039;d like to point to this &lt;a href=&quot;http://blogs.technet.com/b/heyscriptingguy/archive/2010/11/02/use-powershell-to-obtain-sql-server-database-sizes.aspx&quot;&gt;very interesting blog&lt;/a&gt; as how to get database sizes in all SQL Server instances using PowerShell&lt;/p&gt;

&lt;h2 id=&quot;8&quot;&gt;Database Files Sizes in All Databases&lt;/h2&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;cb64751&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;create&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;table&lt;/span&gt; #FileSizes &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;DBName &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;File&lt;/span&gt; Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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;#91;&lt;/span&gt;Physical Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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;Size&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;12&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;&lt;span style=&quot;color: #0000FF;&quot;&gt;declare&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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;set&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&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;SQL&lt;/span&gt; = @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;USE&#039;&lt;/span&gt; &amp;nbsp;+ &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;insert into #FileSizes&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;select &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;, Name, Physical_Name, size/1024.0 from sys.database_files &#039;&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: #202020;&quot;&gt;databases&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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; * &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; #FileSizes &lt;span style=&quot;color: #0000FF;&quot;&gt;order&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;by&lt;/span&gt; DBName, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;File&lt;/span&gt; Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb56136&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;p&gt;You can also find the script to show all database sizes using sp_MsForEachDB &lt;a href=&quot;http://www.kodyaz.com/articles/list-database-size-using-sql-server-sp_msforeachdb.aspx&quot;&gt;here&lt;/a&gt;. See also this &lt;a href=&quot;http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/226bbffc-2cfa-4fa8-8873-48dec6b5f17f&quot;&gt;relevant thread&lt;/a&gt; at MSDN.&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;cb66400&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;declare&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;Sql&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; =&lt;span style=&quot;color: #FF00FF;&quot;&gt;coalesce&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;13&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;UNION ALL &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt; ,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;SELECT &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt; + name + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; AS DBNAME,&#039;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;sum(size * 8 /1024.0) AS MB from &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.dbo.sysfiles&#039;&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: #202020;&quot;&gt;databases&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; name&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;cb32309&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 id=&quot;9&quot;&gt;Database Files Sizes in All Databases and used space&lt;/h2&gt;

&lt;p&gt;Note, that this script assumes that database files have the same name as the database itself. If this is not true, this script will not return correct result.&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;cb28558&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; #Test &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;DbName &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, TotalSize &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;20&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: #0000FF;&quot;&gt;Used&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;20&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;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;free&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;space&lt;/span&gt; percentage&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;20&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;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;declare&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;coalesce&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#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: #FF0000;&quot;&gt;&#039;USE &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;insert into #Test&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;select DB.name, ssf.size*8 as total, &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;FILEPROPERTY (AF.name, &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;spaceused&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;)*8 as used, &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;((ssf.size*8) - (FILEPROPERTY (AF.name, &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;spaceused&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;)*8))*100/(ssf.size*8) as [free space percentage]&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;from sys.sysALTfiles AF &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;inner join sys.sysfiles ssf on ssf.name=AF.name COLLATE SQL_Latin1_General_CP1_CI_AS&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;INNER JOIN sys.databases DB ON AF.dbid=DB.database_id &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;where ssf.groupid&amp;lt;&amp;gt;1&#039;&lt;/span&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: #202020;&quot;&gt;databases&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;select&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; #Test &lt;span style=&quot;color: #0000FF;&quot;&gt;order&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;by&lt;/span&gt; DbName&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb68897&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;br /&gt;
This script will backup all databases (using compression):&lt;/p&gt;
&lt;h2 id=&quot;10&quot;&gt;Backup All Databases with Compression (SQL 2008)&lt;/h2&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;cb63643&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Declare&lt;/span&gt; @ToExecute &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;8000&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;Select&lt;/span&gt; @ToExecute = &lt;span style=&quot;color: #FF00FF;&quot;&gt;Coalesce&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@ToExecute + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Backup Database &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&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;Name&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: #FF0000;&quot;&gt;&#039; To Disk = &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;C:\SQL DB Backups\All DBs\&#039;&lt;/span&gt; + &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.bak&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;WITH NOFORMAT, NOINIT, &amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; SKIP, NOREWIND, NOUNLOAD, COMPRESSION, &amp;nbsp;STATS = 10&#039;&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;13&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&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;From&lt;/span&gt; &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #202020;&quot;&gt;databases&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Where&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;tempdb&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; and &lt;span style=&quot;color: #FF00FF;&quot;&gt;databasepropertyex&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;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Status&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;online&#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Execute&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@ToExecute&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;--print @ToExecute&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb4592&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Get all schema names in all databases:&lt;/p&gt;
&lt;h2 id=&quot;11&quot;&gt;All Schema Names in All Databases&lt;/h2&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;cb37413&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;declare&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;Sql&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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;create&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;table&lt;/span&gt; AllDBSchemas &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;DB Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Schema&lt;/span&gt; Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&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;select&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;Sql&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;coalesce&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;Sql&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;insert into AllDBSchemas&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;select &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; as [DB Name], [Name] as [Schema Name] from &#039;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.sys.schemas order by [DB Name],[Name];&#039;&lt;/span&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: #202020;&quot;&gt;databases&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; name&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;select&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; AllDBSchemas &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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;DB Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SCHEMA&lt;/span&gt; NAME&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb14870&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 id=&quot;12&quot;&gt;List of All Tables in All Databases&lt;/h2&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;cb62966&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; AllTables &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;DB Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Schema&lt;/span&gt; Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Table&lt;/span&gt; Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&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;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;MAX&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;SELECT&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;insert into AllTables&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;select &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; as [DB Name], [Table_Schema] as [Table Schema], [Table_Name] as [Table Name] from &#039;&lt;/span&gt; +&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.INFORMATION_SCHEMA.Tables;&#039;&lt;/span&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: #202020;&quot;&gt;databases&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; name&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;SELECT&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; AllTables &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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;DB Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SCHEMA&lt;/span&gt; NAME&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Table&lt;/span&gt; Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb39596&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Alternative way to get all tables in all databases:&lt;/p&gt;

&lt;h2 id=&quot;13&quot;&gt;List of All Tables in All Databases&lt;/h2&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;cb36139&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;object_ID&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;TempDB..#AllTables&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;U&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NOT NULL &lt;span style=&quot;color: #0000FF;&quot;&gt;drop&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;table&lt;/span&gt; #AllTables&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; #AllTables &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;DB Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Schema&lt;/span&gt; Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &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;128&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NULL, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Table&lt;/span&gt; Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, create_date &lt;span style=&quot;color: #0000FF;&quot;&gt;datetime&lt;/span&gt;, modify_date &lt;span style=&quot;color: #0000FF;&quot;&gt;datetime&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;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;MAX&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;SELECT&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;USE &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;insert into #AllTables &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;select &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; as [DB Name], schema_name(schema_id) as [Table Schema], [Name] as [Table Name], Create_Date, Modify_Date&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; from &#039;&lt;/span&gt; +&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.sys.Tables;&#039;&lt;/span&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: #202020;&quot;&gt;databases&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; name&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--print @SQL &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;cb7304&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;br /&gt;&lt;/p&gt;
&lt;h2 id=&quot;14&quot;&gt;List of all Stored Procedures in All Databases&lt;/h2&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;cb66190&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; #SPList &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;DB Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;SP Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;sysname&lt;/span&gt;, create_date &lt;span style=&quot;color: #0000FF;&quot;&gt;datetime&lt;/span&gt;, modify_date &lt;span style=&quot;color: #0000FF;&quot;&gt;datetime&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;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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;set&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&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;SQL&lt;/span&gt; = @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; insert into #SPList &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;select &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name, &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;, name, create_date, modify_date&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;from &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.sys.procedures&#039;&lt;/span&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: #202020;&quot;&gt;databases&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;select&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; #SPList &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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;DB Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;SP Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb21175&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Database Files Growth:&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;cb59824&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--select * from sys.sysfiles &amp;nbsp;&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;SQL&lt;/span&gt; &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: #FF00FF;&quot;&gt;max&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; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF00FF;&quot;&gt;coalesce&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;UNION ALL &#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&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: #FF0000;&quot;&gt;&#039;SELECT CONVERT(varchar(100),&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;SERVERPROPERTY(&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Servername&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;)) AS Server, &#039;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; +&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; &amp;nbsp;as DatabaseName,&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp;CAST(name as varchar(128)) COLLATE SQL_Latin1_General_CP1_CI_AS as name,&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp;CAST(filename as varchar(128)) COLLATE SQL_Latin1_General_CP1_CI_AS as FileName,&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp;Autogrowth = &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Autogrowth: &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;+&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CASE&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WHEN (status &amp;amp; 0x100000 = 0 AND CEILING((growth * 8192.0) / (1024.0 * 1024.0)) = 0.00) OR growth = 0 THEN &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;None&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WHEN status &amp;amp; 0x100000 = 0 THEN &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;By &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; + &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CONVERT(VARCHAR,CEILING((growth * 8192.0) / (1024.0 * 1024.0))) + &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; MB&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ELSE &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;By &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; + CONVERT(VARCHAR,growth) + &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; percent&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;END&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;+&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CASE&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WHEN (status &amp;amp; 0x100000 = 0 AND CEILING((growth * 8192.0) / (1024.0 * 1024.0)) = 0.00) OR growth = 0 THEN &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WHEN CAST([maxsize] * 8.0 / 1024 AS DEC(20,2)) &amp;lt;= 0.00 THEN &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;, unrestricted growth&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ELSE &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;, restricted growth to &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; + CAST(CAST([maxsize] * 8.0 / 1024 AS DEC(20)) AS VARCHAR) + &#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; MB&#039;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;END&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;FROM &#039;&lt;/span&gt; &amp;nbsp;+ &lt;span style=&quot;color: #FF00FF;&quot;&gt;quotename&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;.sys.sysfiles &amp;nbsp;s&#039;&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: #202020;&quot;&gt;databases&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;SQL&lt;/span&gt; = @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;ORDER BY DatabaseName&#039;&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: #0000FF;&quot;&gt;SQL&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;execute&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;cb92825&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The possibilities are endless.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
*** &lt;strong&gt;Remember, if you have a SQL related question, try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum.&lt;/strong&gt;&lt;ins&gt;&lt;/ins&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/how-to-get-information-about-all-databas&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>Quite often we want to consolidate query information across all databases (or all user databases). When this question is asked in forums, the usual recommendation is to either try running <a href="http://www.mssqltips.com/tip.asp?tip=1905&amp;ctc">undocumented sp_MSForEachDB stored procedure</a> or do a loop and use dynamic SQL. The example of such stored procedures can be found, for example, <a href="http://sqlusa.com/bestpractices/training/scripts/dynamicsql/">THIRD EXAMPLE - SPROC to enumerate all objects in databases</a>.</p>

<p>The idea occurred to me last night that while we do need a dynamic SQL to solve this problem, we don't really need a loop unless we need a second loop involving looping through all tables.</p>

<h3>Table of Contents:</h3>
<p><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#1">Indexes in all databases with their usage</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#2">Indexes in all databases with their physical stats</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#3">Count of all objects in all databases</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#4">Record Count in every table in a database</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#5">Quick Record Count in All Tables in All Databases</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#6">Quick Record Count in All Tables in All Databases (2)</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#7">Sizes of All Tables in a Database</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#8">Database Files Sizes in All Databases</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#9">Database Files Sizes in All Databases and used space</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#10">Backup All Databases with Compression (SQL 2008)</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#11">All Schema Names in All Databases</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#12">List of All Tables in All Databases</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#13">List of All Tables in All Databases (2)</a><br />
<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas#14">List of all Stored Procedures in All Databases</a></p>


<p>Here is a script demonstrating this idea - it lists indexes in all databases with their usage. This script was an answer to <a href="http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/ab4b39b0-5bb2-4ffc-a25d-6bb8fe6124ef/#f3a3a011-4318-44e2-97c1-5bdadd7b2444">Help in T-SQL Query thread</a> in MSDN T-SQL forum.<br />
 </p>
<h2 id="1">Indexes in all databases with their usage</h2>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb9621'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb9621','cb52449'); 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="cb9621" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">declare</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">if</span> <span style="color: #FF00FF;">object_id</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#Result'</span>,<span style="color: #FF0000;">'U'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> not NULL</li><li style="" class="li1">&nbsp;<span style="color: #0000FF;">drop</span> <span style="color: #0000FF;">table</span> #Result</li><li style="" class="li2"><span style="color: #0000FF;">create</span> <span style="color: #0000FF;">table</span> #Result <span style="color: #808080;">&#40;</span>DBName <span style="color: #0000FF;">sysname</span>, TableName <span style="color: #0000FF;">Sysname</span>, IndexName <span style="color: #0000FF;">sysname</span>, <span style="color: #0000FF;">Usage</span> <span style="color: #0000FF;">bigint</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">select</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF00FF;">coalesce</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">13</span><span style="color: #808080;">&#41;</span> + <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' use '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">';</span></li><li style="" class="li1"><span style="color: #FF0000;">insert into #Result select '</span> + <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span>,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' as DbName, </span></li><li style="" class="li2"><span style="color: #FF0000;">object_name(i.object_id) as tablename, &nbsp;i.name as indexname, </span></li><li style="" class="li1"><span style="color: #FF0000;">s.user_seeks + s.user_scans + s.user_lookups + s.user_updates as usage</span></li><li style="" class="li2"><span style="color: #FF0000;">from sys.indexes i &nbsp; </span></li><li style="" class="li1"><span style="color: #FF0000;">inner join sys.dm_db_index_usage_stats s &nbsp; &nbsp; &nbsp; &nbsp;</span></li><li style="" class="li2"><span style="color: #FF0000;">on s.object_id = i.object_id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></li><li style="" class="li1"><span style="color: #FF0000;">and s.index_id = i.index_id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></li><li style="" class="li2"><span style="color: #FF0000;">and s.database_id = db_id()</span></li><li style="" class="li1"><span style="color: #FF0000;">where objectproperty(i.object_id, '</span><span style="color: #FF0000;">'IsUserTable'</span><span style="color: #FF0000;">') = 1 &nbsp; </span></li><li style="" class="li2"><span style="color: #FF0000;">and i.index_id &gt; 0 </span></li><li style="" class="li1"><span style="color: #FF0000;">order by usage;'</span> <span style="color: #0000FF;">from</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span> </li><li style="" class="li2"><span style="color: #00AF00;">--print (@SQL)</span></li><li style="" class="li1"><span style="color: #0000FF;">execute</span> <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">select</span> * <span style="color: #0000FF;">from</span> #Result <span style="color: #0000FF;">order</span> <span style="color: #0000FF;">by</span> <span style="color: #808080;">&#91;</span>DbName<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Usage</span><span style="color: #808080;">&#93;</span></li><li style="" class="li1"><span style="color: #0000FF;">drop</span> <span style="color: #0000FF;">table</span> #Result</li></ol></div><div id="cb52449" style="display: none; color: red;"></div></div></div>
<p><br /><br /></p>
<h2 id="2">Indexes in all databases with their physical stats</h2>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb60103'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb60103','cb26192'); 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="cb60103" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">declare</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">set</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF0000;">''</span></li><li style="" class="li1"><span style="color: #0000FF;">select</span> @<span style="color: #0000FF;">SQL</span> = @<span style="color: #0000FF;">SQL</span> +</li><li style="" class="li2"><span style="color: #FF0000;">'Select '</span> + <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span>name,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' as [DB Name], </span></li><li style="" class="li1"><span style="color: #FF0000;">object_Name(PS.Object_ID,'</span> + <span style="color: #FF00FF;">convert</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>,database_id<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">') as [Object],</span></li><li style="" class="li2"><span style="color: #FF0000;">I.Name as [Index Name], PS.Partition_Number, PS.Index_Type_Desc, </span></li><li style="" class="li1"><span style="color: #FF0000;">PS.alloc_unit_type_desc,&nbsp; &nbsp; PS.index_depth,&nbsp;PS.index_level,</span></li><li style="" class="li2"><span style="color: #FF0000;">PS.avg_fragmentation_in_percent,&nbsp; &nbsp; PS.fragment_count,&nbsp; PS.avg_fragment_size_in_pages,</span></li><li style="" class="li1"><span style="color: #FF0000;">PS.page_count,&nbsp; PS.avg_page_space_used_in_percent,&nbsp; PS.record_count,&nbsp; &nbsp; </span></li><li style="" class="li2"><span style="color: #FF0000;">PS.ghost_record_count,&nbsp; PS.version_ghost_record_count,</span></li><li style="" class="li1"><span style="color: #FF0000;">PS.min_record_size_in_bytes,&nbsp; &nbsp; PS.max_record_size_in_bytes,&nbsp; &nbsp; PS.avg_record_size_in_bytes,</span></li><li style="" class="li2"><span style="color: #FF0000;">PS.forwarded_record_count,&nbsp; PS.compressed_page_count</span></li><li style="" class="li1"><span style="color: #FF0000;"> from '</span> + <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span>name<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'.sys.dm_db_index_physical_stats('</span> + </li><li style="" class="li2"><span style="color: #FF00FF;">convert</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>,database_id<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">', NULL, NULL, NULL, NULL) PS </span></li><li style="" class="li1"><span style="color: #FF0000;">INNER JOIN '</span> + <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span>name<span style="color: #808080;">&#41;</span> + </li><li style="" class="li2"><span style="color: #FF0000;">'.sys.Indexes I on PS.Object_ID = I.Object_ID and PS.Index_ID = I.Index_ID '</span> </li><li style="" class="li1">+ <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">13</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp;<span style="color: #0000FF;">from</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span> <span style="color: #0000FF;">where</span> state_desc = <span style="color: #FF0000;">'ONLINE'</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">execute</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb26192" style="display: none; color: red;"></div></div></div>


<p>Another example of the same idea you can find in <a href="http://wiki.lessthandot.com/index.php/Finding_Record_with_Last_Modified_date_in_all_tables">Finding Record with Last Modified date in all tables</a></p>

<p>Using this same idea you can get a count of all objects in all your databases using this PIVOT query:</p>
<h2 id="3">Count of all objects in all databases</h2>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb74335'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb74335','cb14853'); 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="cb74335" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">declare</span> @Qry <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li2"><span style="color: #0000FF;">select</span> @Qry = <span style="color: #FF00FF;">coalesce</span><span style="color: #808080;">&#40;</span>@Qry + <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">13</span><span style="color: #808080;">&#41;</span> + <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' UNION ALL '</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'</span></li><li style="" class="li1"><span style="color: #FF0000;">select '</span> + <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span>,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' as DBName, [AGGREGATE_FUNCTION], [CHECK_CONSTRAINT],[DEFAULT_CONSTRAINT],</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #FF0000;">[FOREIGN_KEY_CONSTRAINT],</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #FF0000;">[SQL_SCALAR_FUNCTION],</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #FF0000;">[CLR_SCALAR_FUNCTION],</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #FF0000;">[CLR_TABLE_VALUED_FUNCTION],</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #FF0000;">[SQL_INLINE_TABLE_VALUED_FUNCTION],</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #FF0000;">[INTERNAL_TABLE],[SQL_STORED_PROCEDURE],[CLR_STORED_PROCEDURE],[PLAN_GUIDE],[PRIMARY_KEY_CONSTRAINT],</span></li><li style="" class="li2"><span style="color: #FF0000;">[RULE],[REPLICATION_FILTER_PROCEDURE],[SYNONYM],[SERVICE_QUEUE],[CLR_TRIGGER],[SQL_TABLE_VALUED_FUNCTION],[SQL_TRIGGER],</span></li><li style="" class="li1"><span style="color: #FF0000;">[TABLE_TYPE],[USER_TABLE],[UNIQUE_CONSTRAINT],[VIEW],[EXTENDED_STORED_PROCEDURE]</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #FF0000;">from (select [Name], type_Desc from '</span> + <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'.sys.objects where is_ms_shipped = 0) src </span></li><li style="" class="li2"><span style="color: #FF0000;">PIVOT (count([Name]) FOR type_desc in ([AGGREGATE_FUNCTION], [CHECK_CONSTRAINT],[DEFAULT_CONSTRAINT],</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #FF0000;">[FOREIGN_KEY_CONSTRAINT],</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #FF0000;">[SQL_SCALAR_FUNCTION],</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #FF0000;">[CLR_SCALAR_FUNCTION],</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #FF0000;">[CLR_TABLE_VALUED_FUNCTION],</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #FF0000;">[SQL_INLINE_TABLE_VALUED_FUNCTION],</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #FF0000;">[INTERNAL_TABLE],[SQL_STORED_PROCEDURE],[CLR_STORED_PROCEDURE],[PLAN_GUIDE],[PRIMARY_KEY_CONSTRAINT],</span></li><li style="" class="li1"><span style="color: #FF0000;">[RULE],[REPLICATION_FILTER_PROCEDURE],[SYNONYM],[SERVICE_QUEUE],[CLR_TRIGGER],[SQL_TABLE_VALUED_FUNCTION],[SQL_TRIGGER],</span></li><li style="" class="li2"><span style="color: #FF0000;">[TABLE_TYPE],[USER_TABLE],[UNIQUE_CONSTRAINT],[VIEW],[EXTENDED_STORED_PROCEDURE])) pvt'</span> <span style="color: #0000FF;">from</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span> </li><li style="" class="li1"><span style="color: #0000FF;">where</span> <span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> NOT IN <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'master'</span>,<span style="color: #FF0000;">'tempdb'</span>,<span style="color: #FF0000;">'model'</span>,<span style="color: #FF0000;">'msdb'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">order</span> <span style="color: #0000FF;">by</span> <span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">execute</span><span style="color: #808080;">&#40;</span>@Qry<span style="color: #808080;">&#41;</span></li></ol></div><div id="cb14853" style="display: none; color: red;"></div></div></div>

<p>You can only list types you're interested in, of course.</p>

<p>The script below will give you a count of records in every table in a database:</p>
<h2 id="4">Record Count in every table in a database</h2>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb67045'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb67045','cb34155'); 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="cb67045" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> &nbsp;@DynamicSQL <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> &nbsp; @DynamicSQL = <span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>@DynamicSQL + <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">13</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' UNION ALL '</span> + <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">13</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">'SELECT '</span> + <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span>table_schema,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' as [Schema Name], '</span> +</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>TABLE_NAME,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">' as [Table Name], COUNT(*) AS [Records Count] FROM '</span> + </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span>Table_schema<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'.'</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>TABLE_NAME<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> &nbsp; &nbsp; <span style="color: #00AF00;">INFORMATION_SCHEMA</span>.<span style="color: #00AF00;">TABLES</span></li><li style="" class="li2"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> TABLE_NAME</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">--print (@DynamicSQL) -- we may want to use PRINT to debug the SQL</span></li><li style="" class="li1"><span style="color: #0000FF;">EXEC</span><span style="color: #808080;">&#40;</span> @DynamicSQL<span style="color: #808080;">&#41;</span></li></ol></div><div id="cb34155" style="display: none; color: red;"></div></div></div>

<p>Quick row count in all tables in all databases in the server instance (you can exclude system databases from that loop, obviously)<br /></p>
<h2 id="5">Quick Record Count in All Tables in All Databases</h2>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb74388'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb74388','cb97892'); 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="cb74388" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">declare</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">set</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF0000;">''</span></li><li style="" class="li2"><span style="color: #00AF00;">--select * from sys.databases </span></li><li style="" class="li1"><span style="color: #0000FF;">select</span> @<span style="color: #0000FF;">SQL</span> = @<span style="color: #0000FF;">SQL</span> + <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">13</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'USE '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">';</span></li><li style="" class="li2"><span style="color: #FF0000;">SELECT '</span> +<span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span>,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'as [Database Name], </span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; SchemaName=s.name</span></li><li style="" class="li2"><span style="color: #FF0000;"> &nbsp;,TableName=t.name</span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp;,CreateDate=t.create_date</span></li><li style="" class="li2"><span style="color: #FF0000;"> &nbsp;,ModifyDate=t.modify_date</span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp;,p.rows</span></li><li style="" class="li2"><span style="color: #FF0000;"> &nbsp;,DataInKB=sum(a.used_pages)*8</span></li><li style="" class="li1"><span style="color: #FF0000;">FROM sys.schemas s</span></li><li style="" class="li2"><span style="color: #FF0000;">JOIN sys.tables t on s.schema_id=t.schema_id</span></li><li style="" class="li1"><span style="color: #FF0000;">JOIN sys.partitions p on t.object_id=p.object_id</span></li><li style="" class="li2"><span style="color: #FF0000;">JOIN sys.allocation_units a on a.container_id=p.partition_id</span></li><li style="" class="li1"><span style="color: #FF0000;">GROUP BY s.name, t.name, t.create_date, t.modify_date, p.rows</span></li><li style="" class="li2"><span style="color: #FF0000;">ORDER BY SchemaName, TableName'</span> <span style="color: #0000FF;">from</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span> &nbsp;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">execute</span> <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb97892" style="display: none; color: red;"></div></div></div>

<p>Another way with less info:</p>
<h2 id="6">Quick Record Count in All Tables in All Databases</h2>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb59380'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb59380','cb50008'); 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="cb59380" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">declare</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">set</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF0000;">''</span></li><li style="" class="li2"><span style="color: #00AF00;">--select * from sys.databases </span></li><li style="" class="li1"><span style="color: #0000FF;">select</span> @<span style="color: #0000FF;">SQL</span> = @<span style="color: #0000FF;">SQL</span> + <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">13</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'USE '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">';</span></li><li style="" class="li2"><span style="color: #FF0000;">SELECT '</span> +<span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span>,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'as [Database Name], so.name AS [Table Name], &nbsp; </span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; &nbsp;rows AS [RowCount] &nbsp; </span></li><li style="" class="li2"><span style="color: #FF0000;">FROM sysindexes AS si &nbsp; </span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; &nbsp;join sysobjects AS so on si.id = so.id &nbsp; </span></li><li style="" class="li2"><span style="color: #FF0000;">WHERE indid IN (0,1) &nbsp; </span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; &nbsp;AND xtype = '</span><span style="color: #FF0000;">'U'</span><span style="color: #FF0000;">''</span> <span style="color: #0000FF;">from</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span> &nbsp;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">execute</span> <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb50008" style="display: none; color: red;"></div></div></div>

<p>Here is a script showing sizes from all tables in a database.</p>
<h2 id="7">Sizes of All Tables in a Database</h2>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb95418'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb95418','cb24132'); 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="cb95418" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #00AF00;">--exec sp_MSforeachtable 'print ''?'' exec sp_spaceused ''?'''</span></li><li style="" class="li2"><span style="color: #0000FF;">if</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#TablesSizes'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> NOT NULL</li><li style="" class="li1">&nbsp; &nbsp;<span style="color: #0000FF;">drop</span> <span style="color: #0000FF;">table</span> #TablesSizes</li><li style="" class="li2">&nbsp; &nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">create</span> <span style="color: #0000FF;">table</span> #TablesSizes <span style="color: #808080;">&#40;</span>TableName <span style="color: #0000FF;">sysname</span>, <span style="color: #0000FF;">Rows</span> <span style="color: #0000FF;">bigint</span>, reserved <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>, <span style="color: #0000FF;">data</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>, index_size <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>, unused <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</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: #0000FF;">declare</span> @<span style="color: #0000FF;">sql</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">select</span> @<span style="color: #0000FF;">sql</span> = <span style="color: #FF00FF;">coalesce</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">sql</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'</span></li><li style="" class="li1"><span style="color: #FF0000;">insert into #TablesSizes execute sp_spaceused '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Table_Name,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">from</span> <span style="color: #00AF00;">INFORMATION_SCHEMA</span>.<span style="color: #00AF00;">TABLES</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">--print (@SQL)</span></li><li style="" class="li2"><span style="color: #0000FF;">execute</span> <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">select</span> * <span style="color: #0000FF;">from</span> #TablesSizes <span style="color: #0000FF;">order</span> <span style="color: #0000FF;">by</span> TableName</li></ol></div><div id="cb24132" style="display: none; color: red;"></div></div></div>

<p>Here is a script showing database files sizes for all databases</p>

<p>Before I show the T-SQL code I'd like to point to this <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/11/02/use-powershell-to-obtain-sql-server-database-sizes.aspx">very interesting blog</a> as how to get database sizes in all SQL Server instances using PowerShell</p>

<h2 id="8">Database Files Sizes in All Databases</h2>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb6145'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb6145','cb61818'); 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="cb6145" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">create</span> &nbsp;<span style="color: #0000FF;">table</span> #FileSizes <span style="color: #808080;">&#40;</span>DBName <span style="color: #0000FF;">sysname</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">File</span> Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#91;</span>Physical Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2"><span style="color: #0000FF;">Size</span> <span style="color: #0000FF;">decimal</span><span style="color: #808080;">&#40;</span><span style="color: #000;">12</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">declare</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">set</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF0000;">''</span></li><li style="" class="li1"><span style="color: #0000FF;">select</span> @<span style="color: #0000FF;">SQL</span> = @<span style="color: #0000FF;">SQL</span> + <span style="color: #FF0000;">'USE'</span> &nbsp;+ <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>name<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'</span></li><li style="" class="li2"><span style="color: #FF0000;">insert into #FileSizes</span></li><li style="" class="li1"><span style="color: #FF0000;">select '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>name,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">', Name, Physical_Name, size/1024.0 from sys.database_files '</span> </li><li style="" class="li2"><span style="color: #0000FF;">from</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">execute</span> <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #0000FF;">select</span> * <span style="color: #0000FF;">from</span> #FileSizes <span style="color: #0000FF;">order</span> <span style="color: #0000FF;">by</span> DBName, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">File</span> Name<span style="color: #808080;">&#93;</span></li></ol></div><div id="cb61818" style="display: none; color: red;"></div></div></div>

<p><br /></p>

<p>You can also find the script to show all database sizes using sp_MsForEachDB <a href="http://www.kodyaz.com/articles/list-database-size-using-sql-server-sp_msforeachdb.aspx">here</a>. See also this <a href="http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/226bbffc-2cfa-4fa8-8873-48dec6b5f17f">relevant thread</a> at MSDN.</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb56442'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb56442','cb34704'); 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="cb56442" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">declare</span> @<span style="color: #0000FF;">Sql</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">select</span> @<span style="color: #0000FF;">SQL</span> =<span style="color: #FF00FF;">coalesce</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span> + <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">13</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'UNION ALL </span></li><li style="" class="li1"><span style="color: #FF0000;">'</span> ,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'SELECT '</span><span style="color: #FF0000;">''</span> + name + <span style="color: #FF0000;">''</span><span style="color: #FF0000;">' AS DBNAME,'</span> + </li><li style="" class="li2"><span style="color: #FF0000;">'sum(size * 8 /1024.0) AS MB from '</span> + <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span>name<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'.dbo.sysfiles'</span> </li><li style="" class="li1"><span style="color: #0000FF;">from</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span></li><li style="" class="li2"><span style="color: #0000FF;">order</span> <span style="color: #0000FF;">by</span> name</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">execute</span> <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb34704" style="display: none; color: red;"></div></div></div><p><br /></p>
<h2 id="9">Database Files Sizes in All Databases and used space</h2>

<p>Note, that this script assumes that database files have the same name as the database itself. If this is not true, this script will not return correct result.</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb30715'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb30715','cb20085'); 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="cb30715" 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> #Test <span style="color: #808080;">&#40;</span>DbName <span style="color: #0000FF;">sysname</span>, TotalSize <span style="color: #0000FF;">decimal</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>, <span style="color: #0000FF;">Used</span> <span style="color: #0000FF;">decimal</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">free</span> <span style="color: #0000FF;">space</span> percentage<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">decimal</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</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;</li><li style="" class="li1"><span style="color: #0000FF;">declare</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">select</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF00FF;">coalesce</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + </li><li style="" class="li1"><span style="color: #FF0000;">'USE '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Name<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'</span></li><li style="" class="li2"><span style="color: #FF0000;">insert into #Test</span></li><li style="" class="li1"><span style="color: #FF0000;">select DB.name, ssf.size*8 as total, </span></li><li style="" class="li2"><span style="color: #FF0000;">FILEPROPERTY (AF.name, '</span><span style="color: #FF0000;">'spaceused'</span><span style="color: #FF0000;">')*8 as used, </span></li><li style="" class="li1"><span style="color: #FF0000;">((ssf.size*8) - (FILEPROPERTY (AF.name, '</span><span style="color: #FF0000;">'spaceused'</span><span style="color: #FF0000;">')*8))*100/(ssf.size*8) as [free space percentage]</span></li><li style="" class="li2"><span style="color: #FF0000;">from sys.sysALTfiles AF </span></li><li style="" class="li1"><span style="color: #FF0000;">inner join sys.sysfiles ssf on ssf.name=AF.name COLLATE SQL_Latin1_General_CP1_CI_AS</span></li><li style="" class="li2"><span style="color: #FF0000;">INNER JOIN sys.databases DB ON AF.dbid=DB.database_id </span></li><li style="" class="li1"><span style="color: #FF0000;">where ssf.groupid&lt;&gt;1'</span> <span style="color: #0000FF;">from</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">execute</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">select</span> * <span style="color: #0000FF;">from</span> #Test <span style="color: #0000FF;">order</span> <span style="color: #0000FF;">by</span> DbName</li></ol></div><div id="cb20085" style="display: none; color: red;"></div></div></div>
<p><br /><br />
This script will backup all databases (using compression):</p>
<h2 id="10">Backup All Databases with Compression (SQL 2008)</h2>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb39297'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb39297','cb68128'); 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="cb39297" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">Declare</span> @ToExecute <span style="color: #0000FF;">VarChar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8000</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">Select</span> @ToExecute = <span style="color: #FF00FF;">Coalesce</span><span style="color: #808080;">&#40;</span>@ToExecute + <span style="color: #FF0000;">'Backup Database '</span> + <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> + </li><li style="" class="li2"><span style="color: #FF0000;">' To Disk = '</span><span style="color: #FF0000;">'C:\SQL DB Backups\All DBs\'</span> + <span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span> + <span style="color: #FF0000;">'.bak'</span><span style="color: #FF0000;">' </span></li><li style="" class="li1"><span style="color: #FF0000;">WITH NOFORMAT, NOINIT, &nbsp;</span></li><li style="" class="li2"><span style="color: #FF0000;"> SKIP, NOREWIND, NOUNLOAD, COMPRESSION, &nbsp;STATS = 10'</span> + <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">13</span><span style="color: #808080;">&#41;</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">From</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">Where</span> <span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span> Not In <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb'</span><span style="color: #808080;">&#41;</span> and <span style="color: #FF00FF;">databasepropertyex</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span>,<span style="color: #FF0000;">'Status'</span><span style="color: #808080;">&#41;</span> = <span style="color: #FF0000;">'online'</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">Execute</span> <span style="color: #808080;">&#40;</span>@ToExecute<span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">--print @ToExecute</span></li></ol></div><div id="cb68128" style="display: none; color: red;"></div></div></div>

<p>Get all schema names in all databases:</p>
<h2 id="11">All Schema Names in All Databases</h2>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb34956'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb34956','cb2263'); 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="cb34956" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">declare</span> @<span style="color: #0000FF;">Sql</span> <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">create</span> <span style="color: #0000FF;">table</span> AllDBSchemas <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>DB Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">sysname</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Schema</span> Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">sysname</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">select</span> @<span style="color: #0000FF;">Sql</span> = <span style="color: #FF00FF;">coalesce</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">Sql</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'</span></li><li style="" class="li1"><span style="color: #FF0000;">insert into AllDBSchemas</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #FF0000;">select '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>name,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' as [DB Name], [Name] as [Schema Name] from '</span> + </li><li style="" class="li2"><span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Name<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'.sys.schemas order by [DB Name],[Name];'</span> <span style="color: #0000FF;">from</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span></li><li style="" class="li1"><span style="color: #0000FF;">order</span> <span style="color: #0000FF;">by</span> name</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">execute</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">select</span> * <span style="color: #0000FF;">from</span> AllDBSchemas <span style="color: #0000FF;">order</span> <span style="color: #0000FF;">by</span> <span style="color: #808080;">&#91;</span>DB Name<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">SCHEMA</span> NAME<span style="color: #808080;">&#93;</span></li></ol></div><div id="cb2263" style="display: none; color: red;"></div></div></div>

<p><br /></p>
<h2 id="12">List of All Tables in All Databases</h2>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb7725'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb7725','cb71096'); 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="cb7725" 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> AllTables <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>DB Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">sysname</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Schema</span> Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">sysname</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Table</span> Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">sysname</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;">SQL</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'</span></li><li style="" class="li2"><span style="color: #FF0000;">insert into AllTables</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #FF0000;">select '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>name,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' as [DB Name], [Table_Schema] as [Table Schema], [Table_Name] as [Table Name] from '</span> +</li><li style="" class="li1"><span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Name<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'.INFORMATION_SCHEMA.Tables;'</span> <span style="color: #0000FF;">FROM</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span></li><li style="" class="li2"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> name</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">EXECUTE</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</span> AllTables <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #808080;">&#91;</span>DB Name<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">SCHEMA</span> NAME<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Table</span> Name<span style="color: #808080;">&#93;</span></li></ol></div><div id="cb71096" style="display: none; color: red;"></div></div></div>

<p>Alternative way to get all tables in all databases:</p>

<h2 id="13">List of All Tables in All Databases</h2>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb9567'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb9567','cb73915'); 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="cb9567" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">if</span> <span style="color: #FF00FF;">object_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'TempDB..#AllTables'</span>,<span style="color: #FF0000;">'U'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> NOT NULL <span style="color: #0000FF;">drop</span> <span style="color: #0000FF;">table</span> #AllTables</li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #AllTables <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>DB Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">sysname</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Schema</span> Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> NULL, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Table</span> Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">sysname</span>, create_date <span style="color: #0000FF;">datetime</span>, modify_date <span style="color: #0000FF;">datetime</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">DECLARE</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'USE '</span> + <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span>name<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'</span></li><li style="" class="li1"><span style="color: #FF0000;">insert into #AllTables </span></li><li style="" class="li2"><span style="color: #FF0000;">select '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>name,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' as [DB Name], schema_name(schema_id) as [Table Schema], [Name] as [Table Name], Create_Date, Modify_Date</span></li><li style="" class="li1"><span style="color: #FF0000;"> from '</span> +</li><li style="" class="li2"><span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Name<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'.sys.Tables;'</span> <span style="color: #0000FF;">FROM</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span></li><li style="" class="li1"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> name</li><li style="" class="li2"><span style="color: #00AF00;">--print @SQL </span></li><li style="" class="li1"><span style="color: #0000FF;">EXECUTE</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb73915" style="display: none; color: red;"></div></div></div>
<p><br /><br /></p>
<h2 id="14">List of all Stored Procedures in All Databases</h2>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb92271'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb92271','cb69392'); 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="cb92271" 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> #SPList <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>DB Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">sysname</span>, <span style="color: #808080;">&#91;</span>SP Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">sysname</span>, create_date <span style="color: #0000FF;">datetime</span>, modify_date <span style="color: #0000FF;">datetime</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;">SQL</span> <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">set</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF0000;">''</span></li><li style="" class="li1"><span style="color: #0000FF;">select</span> @<span style="color: #0000FF;">SQL</span> = @<span style="color: #0000FF;">SQL</span> + <span style="color: #FF0000;">' insert into #SPList </span></li><li style="" class="li2"><span style="color: #FF0000;">select '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>name, <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">', name, create_date, modify_date</span></li><li style="" class="li1"><span style="color: #FF0000;">from '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>name<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'.sys.procedures'</span> <span style="color: #0000FF;">from</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">execute</span> <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">select</span> * <span style="color: #0000FF;">from</span> #SPList <span style="color: #0000FF;">order</span> <span style="color: #0000FF;">by</span> <span style="color: #808080;">&#91;</span>DB Name<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>SP Name<span style="color: #808080;">&#93;</span></li></ol></div><div id="cb69392" style="display: none; color: red;"></div></div></div>

<p>Database Files Growth:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb66741'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb66741','cb1893'); 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="cb66741" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #00AF00;">--select * from sys.sysfiles &nbsp;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">declare</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">select</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF00FF;">coalesce</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span> + <span style="color: #FF0000;">'</span></li><li style="" class="li1"><span style="color: #FF0000;">UNION ALL '</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> + </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #FF0000;">'SELECT CONVERT(varchar(100),</span></li><li style="" class="li2"><span style="color: #FF0000;">SERVERPROPERTY('</span><span style="color: #FF0000;">'Servername'</span><span style="color: #FF0000;">')) AS Server, '</span> + </li><li style="" class="li1"><span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span>name,<span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> +<span style="color: #FF0000;">' &nbsp;as DatabaseName,</span></li><li style="" class="li2"><span style="color: #FF0000;"> &nbsp; &nbsp;CAST(name as varchar(128)) COLLATE SQL_Latin1_General_CP1_CI_AS as name,</span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; &nbsp;CAST(filename as varchar(128)) COLLATE SQL_Latin1_General_CP1_CI_AS as FileName,</span></li><li style="" class="li2"><span style="color: #FF0000;"> &nbsp; &nbsp;Autogrowth = '</span><span style="color: #FF0000;">'Autogrowth: '</span><span style="color: #FF0000;">'</span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp;+</span></li><li style="" class="li2"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp;CASE</span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHEN (status &amp; 0x100000 = 0 AND CEILING((growth * 8192.0) / (1024.0 * 1024.0)) = 0.00) OR growth = 0 THEN '</span><span style="color: #FF0000;">'None'</span><span style="color: #FF0000;">'</span></li><li style="" class="li2"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHEN status &amp; 0x100000 = 0 THEN '</span><span style="color: #FF0000;">'By '</span><span style="color: #FF0000;">' + </span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CONVERT(VARCHAR,CEILING((growth * 8192.0) / (1024.0 * 1024.0))) + '</span><span style="color: #FF0000;">' MB'</span><span style="color: #FF0000;">'</span></li><li style="" class="li2"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ELSE '</span><span style="color: #FF0000;">'By '</span><span style="color: #FF0000;">' + CONVERT(VARCHAR,growth) + '</span><span style="color: #FF0000;">' percent'</span><span style="color: #FF0000;">'</span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp;END</span></li><li style="" class="li2"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp;+</span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp;CASE</span></li><li style="" class="li2"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHEN (status &amp; 0x100000 = 0 AND CEILING((growth * 8192.0) / (1024.0 * 1024.0)) = 0.00) OR growth = 0 THEN '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">'</span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHEN CAST([maxsize] * 8.0 / 1024 AS DEC(20,2)) &lt;= 0.00 THEN '</span><span style="color: #FF0000;">', unrestricted growth'</span><span style="color: #FF0000;">'</span></li><li style="" class="li2"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ELSE '</span><span style="color: #FF0000;">', restricted growth to '</span><span style="color: #FF0000;">' + CAST(CAST([maxsize] * 8.0 / 1024 AS DEC(20)) AS VARCHAR) + '</span><span style="color: #FF0000;">' MB'</span><span style="color: #FF0000;">'</span></li><li style="" class="li1"><span style="color: #FF0000;"> &nbsp; &nbsp; &nbsp; &nbsp;END</span></li><li style="" class="li2"><span style="color: #FF0000;">FROM '</span> &nbsp;+ <span style="color: #FF00FF;">quotename</span><span style="color: #808080;">&#40;</span>name<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'.sys.sysfiles &nbsp;s'</span></li><li style="" class="li1"><span style="color: #0000FF;">from</span> <span style="color: #00AF00;">sys</span>.<span style="color: #202020;">databases</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">set</span> @<span style="color: #0000FF;">SQL</span> = @<span style="color: #0000FF;">SQL</span> + <span style="color: #FF0000;">' </span></li><li style="" class="li2"><span style="color: #FF0000;">ORDER BY DatabaseName'</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">print</span> @<span style="color: #0000FF;">SQL</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">execute</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb1893" style="display: none; color: red;"></div></div></div>

<p>The possibilities are endless.</p>

<p><br /><br />
*** <strong>Remember, if you have a SQL related question, try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum.</strong><ins></ins></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas">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/how-to-get-information-about-all-databas#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=846</wfw:commentRss>
		</item>
				<item>
			<title>Aggregates with multiple tables</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/aggregates-with-multiple-tables</link>
			<pubDate>Sun, 25 Sep 2011 18:13:00 +0000</pubDate>			<dc:creator>Naomi Nosonovsky</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">1420@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;In this blog post I would like to touch a very common problem which often trips up newbies and even serious SQL Professionals. I wanted to write this blog post for quite some time, but always put it aside. However, just a few days ago I have been dealing with this problem at my work place, so I believe it&#039;s time to discuss the problem.&lt;/p&gt;

&lt;p&gt;For the discussion let&#039;s consider AdventureWorks database SalesOrderHeader and SalesOrderDetail tables. To illustrate the problem, let&#039;s assume that returns are handled in a different table with a structure very similar to SalesOrderDetail. And let&#039;s assume there are about 20 percent of returns (the business must not be doing too well!).&lt;/p&gt;

&lt;p&gt;Ok, to create the returns table in the Sales schema let&#039;s use the following code:&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;cb21841&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;IDENTITY&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: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; ReturnID, D.&lt;span style=&quot;color: #202020;&quot;&gt;LineTotal&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; ReturnTotal, D.&lt;span style=&quot;color: #202020;&quot;&gt;ProductID&lt;/span&gt;, D.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&lt;/span&gt;, D.&lt;span style=&quot;color: #202020;&quot;&gt;UnitPrice&lt;/span&gt;, D.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesReturns&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderDetail&lt;/span&gt; D TABLESAMPLE &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: #0000FF;&quot;&gt;PERCENT&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;cb26844&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, suppose we need to get quantity ordered and quantity returned as well as line total for both quantity and returned grouped by Customer ID. Let&#039;s write the most intuitive query:&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;cb28085&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;OD.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;OD.&lt;span style=&quot;color: #202020;&quot;&gt;LineTotal&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;R.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;R.&lt;span style=&quot;color: #202020;&quot;&gt;ReturnTotal&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt; OH &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderDetail&lt;/span&gt; OD &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt; = OD.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesReturns&lt;/span&gt; R &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt; = R.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&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; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb96177&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Which gives us these results&lt;/p&gt;
&lt;div class=&quot;tables&quot;&gt;
&lt;center&gt;
&lt;table border=&quot;1&quot; cellpadding =&quot;5&quot; cellspacing = &quot;5&quot;&gt; 
&lt;caption style=&quot;color:blue;font-weight:bold&quot;&gt;Aggregate Query Results - Ordered and Returned&lt;/caption&gt;
&lt;tr&gt;&lt;th align=&quot;right&quot;&gt;CustomerID&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Ordered quantity&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Ordered&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Returned quantity&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Returned&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;882&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;724610.142300&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;857&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;713619.368600&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;2&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;198&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;25199.499392&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;0&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;0.000000&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;3&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;20140&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;4084947.217763&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;18916&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;3814775.599788&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;4&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;8268&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;4493242.544792&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;7530&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;4036924.558800&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;5&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;1190&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;325894.561736&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;973&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;261739.811536&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/div&gt;

&lt;p&gt;Now, let&#039;s verify these results by only running Ordered amounts.&lt;/p&gt;

&lt;p&gt;Let&#039;s run this query (I re-wrote using CTE for simplicity):&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;cb41698&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;;with cte &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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;OD.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;OD.&lt;span style=&quot;color: #202020;&quot;&gt;LineTotal&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt; OH &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderDetail&lt;/span&gt; OD &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt; = OD.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&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; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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: #0000FF;&quot;&gt;FROM&lt;/span&gt; cte&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb95131&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;which produces the following results:&lt;/p&gt;

&lt;div class=&quot;tables&quot;&gt;&lt;p&gt; &lt;/p&gt;&lt;center&gt;
&lt;table border=&quot;1&quot; cellpadding =&quot;5&quot; cellspacing = &quot;5&quot;&gt; 
&lt;caption style=&quot;color:blue;font-weight:bold&quot;&gt;Aggregate Query Results - Ordered only&lt;/caption&gt;
&lt;tr&gt;&lt;th align=&quot;right&quot;&gt;CustomerID&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Ordered quantity&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Ordered&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;121&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;85177.081200&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;2&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;198&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;25199.499392&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;3&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;1695&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;361999.058170&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;4&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;980&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;586524.947384&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;5&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;299&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;86177.847024&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt; &lt;/center&gt;
&lt;/div&gt;

&lt;p&gt;We see that numbers differ dramatically. So, what did happen in our first query? Why did it produce such high numbers that have nothing to do with the real numbers?&lt;/p&gt;

&lt;p&gt;The reason is that for each row of Ordered results we get N Returned rows and so the total number of rows per one customer became M*N, where M is number of Ordered rows and N number of Returned rows. Therefore our results get much higher numbers.&lt;/p&gt;

&lt;p&gt;So, the rule I always employ when I need to do aggregates that will involve more than one table to JOIN is to produce the necessary results separately as derived tables or CTE and only then join them together to form final result.&lt;/p&gt;

&lt;p&gt;Here is how our query works when I re-write it using this idea:&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;cb30333&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;;WITH Ordered &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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;OD.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;OD.&lt;span style=&quot;color: #202020;&quot;&gt;LineTotal&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt; OH &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderDetail&lt;/span&gt; OD &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt; = OD.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&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; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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;Returned &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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;R.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;R.&lt;span style=&quot;color: #202020;&quot;&gt;ReturnTotal&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt; OH &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesReturns&lt;/span&gt; R &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt; = R.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&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; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, R.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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; CustomerID, &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;R.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;R.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Ordered O FULL JOIN Returned R &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; O.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt; = R.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, R.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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;cb16086&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;with these correct now results:&lt;/p&gt;

&lt;div class=&quot;tables&quot;&gt;&lt;p&gt; &lt;/p&gt;&lt;center&gt;
&lt;table border=&quot;1&quot; cellpadding =&quot;5&quot; cellspacing = &quot;5&quot;&gt;
&lt;caption style=&quot;color:blue;font-weight:bold&quot;&gt;Aggregate Query Results - Ordered and Returned&lt;/caption&gt;
&lt;tr&gt;&lt;th align=&quot;right&quot;&gt;CustomerID&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Ordered Quantity&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Ordered&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Returned Quantity&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Returned&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;121&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;85177.081200&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;44&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;37199.802700&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;2&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;198&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;25199.499392&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;0&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;0.000000&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;3&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;1695&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;361999.058170&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;471&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;91827.440195&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;4&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;980&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;586524.947384&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;242&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;130206.961392&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;5&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;299&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;86177.847024&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;82&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;22023.096824&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/div&gt;

&lt;p&gt;I used the FULL JOIN just in case we may have Ordered items and no returns or vs. versa for the customer.&lt;/p&gt;

&lt;p&gt;I received a question of how to write this code for SQL 2000. In SQL 2000 we can not use CTE (common table expressions), but we can use derived tables, so the code above will be re-written in this manner:&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;cb93024&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: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, R.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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; CustomerID,&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;R.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;R.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&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; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;OD.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;OD.&lt;span style=&quot;color: #202020;&quot;&gt;LineTotal&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt; OH &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderDetail&lt;/span&gt; OD &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt; = OD.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&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; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;O FULL JOIN &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; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;R.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;R.&lt;span style=&quot;color: #202020;&quot;&gt;ReturnTotal&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt; OH &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesReturns&lt;/span&gt; R &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt; = R.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&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; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; R &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; O.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt; = R.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, R.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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;cb89713&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, hopefully you can now easily recognize the problem and apply the same idea when dealing with a similar situation.&lt;/p&gt;

&lt;p&gt;Say, few days ago I looked into report. The number of joins used in the query made my head dizzy - I told right away that because of the JOINs the total results are not going to be correct.&lt;/p&gt;

&lt;p&gt;So, how did I solve the problem? I included the totals using WINDOW AGGREGATE function in the CTE first and then added the rest of the JOINs to get the data needed for report.&lt;/p&gt;

&lt;p&gt;Unfortunately, I am not sure if an easier method exists for reports in SSRS when we need to present information for customer orders, for example, but also display customer addresses (and there may be many addresses per customer). Obviously, we will not get correct totals if we will just sum the records - we need to apply some tricks, simplest of them will be to pre-calculate totals and include these fields as part of the returned dataset. I will be interested to know your solutions for this problem.&lt;/p&gt;

&lt;p&gt;I should also mention another way to solve this query (assuming we also want to return the Customer name) by using OUTER APPLY query. &lt;/p&gt;

&lt;p&gt;Here I am showing two different approaches for comparison. The first approach performs better.&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;cb66095&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;;WITH Ordered &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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;OD.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;OD.&lt;span style=&quot;color: #202020;&quot;&gt;LineTotal&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt; OH &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderDetail&lt;/span&gt; OD &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt; = OD.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&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; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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;Returned &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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;R.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;R.&lt;span style=&quot;color: #202020;&quot;&gt;ReturnTotal&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt; OH &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesReturns&lt;/span&gt; R &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt; = R.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&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; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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;&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: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; C.&lt;span style=&quot;color: #202020;&quot;&gt;FirstName&lt;/span&gt;, C.&lt;span style=&quot;color: #202020;&quot;&gt;LastName&lt;/span&gt;, &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;R.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;R.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--INTO dbo.QueryResults&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;vIndividualCustomer&lt;/span&gt; C&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Ordered O &lt;span style=&quot;color: #0000FF;&quot;&gt;On&lt;/span&gt; C.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt; = O.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Returned R &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; C.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt; = R.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&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; C.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt; &lt;br /&gt;&amp;nbsp;&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: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; C.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, C.&lt;span style=&quot;color: #202020;&quot;&gt;FirstName&lt;/span&gt;, C.&lt;span style=&quot;color: #202020;&quot;&gt;LastName&lt;/span&gt;, &amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;O.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;R.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;R.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;vIndividualCustomer&lt;/span&gt; C&lt;br /&gt;OUTER APPLY &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;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;OD.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&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;OD.&lt;span style=&quot;color: #202020;&quot;&gt;LineTotal&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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Ordered&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt; OH &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderDetail&lt;/span&gt; OD &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt; = OD.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt; = C.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; O&lt;br /&gt;&amp;nbsp;&lt;br /&gt;OUTER APPLY &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;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;R.&lt;span style=&quot;color: #202020;&quot;&gt;OrderQty&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned Quantity&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&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;R.&lt;span style=&quot;color: #202020;&quot;&gt;ReturnTotal&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: #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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Returned&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt; OH &lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesReturns&lt;/span&gt; R &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt; = R.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderID&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; OH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt; = C.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; R&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; C.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb88442&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;*** &lt;strong&gt;Remember, if you have a SQL related question, try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&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/aggregates-with-multiple-tables&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>In this blog post I would like to touch a very common problem which often trips up newbies and even serious SQL Professionals. I wanted to write this blog post for quite some time, but always put it aside. However, just a few days ago I have been dealing with this problem at my work place, so I believe it's time to discuss the problem.</p>

<p>For the discussion let's consider AdventureWorks database SalesOrderHeader and SalesOrderDetail tables. To illustrate the problem, let's assume that returns are handled in a different table with a structure very similar to SalesOrderDetail. And let's assume there are about 20 percent of returns (the business must not be doing too well!).</p>

<p>Ok, to create the returns table in the Sales schema let's use the following code:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb13846'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb13846','cb72241'); 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="cb13846" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> ReturnID, D.<span style="color: #202020;">LineTotal</span> <span style="color: #0000FF;">AS</span> ReturnTotal, D.<span style="color: #202020;">ProductID</span>, D.<span style="color: #202020;">OrderQty</span>, D.<span style="color: #202020;">UnitPrice</span>, D.<span style="color: #202020;">SalesOrderID</span></li><li style="" class="li2"><span style="color: #0000FF;">INTO</span> Sales.<span style="color: #202020;">SalesReturns</span></li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderDetail</span> D TABLESAMPLE <span style="color: #808080;">&#40;</span><span style="color: #000;">20</span> <span style="color: #0000FF;">PERCENT</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb72241" style="display: none; color: red;"></div></div></div>

<p>Now, suppose we need to get quantity ordered and quantity returned as well as line total for both quantity and returned grouped by Customer ID. Let's write the most intuitive query:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb50261'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb50261','cb70288'); 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="cb50261" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> OH.<span style="color: #202020;">CustomerID</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">LineTotal</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>R.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned quantity<span style="color: #808080;">&#93;</span>, &nbsp; &nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>R.<span style="color: #202020;">ReturnTotal</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span> OH <span style="color: #FF00FF;">LEFT</span> JOIN Sales.<span style="color: #202020;">SalesOrderDetail</span> OD <span style="color: #0000FF;">ON</span> OH.<span style="color: #202020;">SalesOrderID</span> = OD.<span style="color: #202020;">SalesOrderID</span></li><li style="" class="li2"><span style="color: #FF00FF;">LEFT</span> JOIN Sales.<span style="color: #202020;">SalesReturns</span> R <span style="color: #0000FF;">ON</span> OH.<span style="color: #202020;">SalesOrderID</span> = R.<span style="color: #202020;">SalesOrderID</span> </li><li style="" class="li1"><span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> OH.<span style="color: #202020;">CustomerID</span> </li><li style="" class="li2"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> OH.<span style="color: #202020;">CustomerID</span></li></ol></div><div id="cb70288" style="display: none; color: red;"></div></div></div>

<p>Which gives us these results</p>
<div class="tables">
<center>
<table border="1" cellpadding ="5" cellspacing = "5"> 
<caption style="color:blue;font-weight:bold">Aggregate Query Results - Ordered and Returned</caption>
<tr><th align="right">CustomerID</th><th align="right">Ordered quantity</th><th align="right">Ordered</th><th align="right">Returned quantity</th><th align="right">Returned</th></tr>
<tr><td align="right">1</td><td align="right">882</td><td align="right">724610.142300</td><td align="right">857</td><td align="right">713619.368600</td></tr>
<tr><td align="right">2</td><td align="right">198</td><td align="right">25199.499392</td><td align="right">0</td><td align="right">0.000000</td></tr>
<tr><td align="right">3</td><td align="right">20140</td><td align="right">4084947.217763</td><td align="right">18916</td><td align="right">3814775.599788</td></tr>
<tr><td align="right">4</td><td align="right">8268</td><td align="right">4493242.544792</td><td align="right">7530</td><td align="right">4036924.558800</td></tr>
<tr><td align="right">5</td><td align="right">1190</td><td align="right">325894.561736</td><td align="right">973</td><td align="right">261739.811536</td></tr>
</table></center>
</div>

<p>Now, let's verify these results by only running Ordered amounts.</p>

<p>Let's run this query (I re-wrote using CTE for simplicity):</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb6945'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb6945','cb80977'); 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="cb6945" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">;with cte <span style="color: #0000FF;">as</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> OH.<span style="color: #202020;">CustomerID</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">LineTotal</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span> OH <span style="color: #FF00FF;">LEFT</span> JOIN Sales.<span style="color: #202020;">SalesOrderDetail</span> OD <span style="color: #0000FF;">ON</span> OH.<span style="color: #202020;">SalesOrderID</span> = OD.<span style="color: #202020;">SalesOrderID</span></li><li style="" class="li2"><span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> OH.<span style="color: #202020;">CustomerID</span> </li><li style="" class="li1"><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</span> cte</li></ol></div><div id="cb80977" style="display: none; color: red;"></div></div></div>

<p>which produces the following results:</p>

<div class="tables"><p> </p><center>
<table border="1" cellpadding ="5" cellspacing = "5"> 
<caption style="color:blue;font-weight:bold">Aggregate Query Results - Ordered only</caption>
<tr><th align="right">CustomerID</th><th align="right">Ordered quantity</th><th align="right">Ordered</th></tr>
<tr><td align="right">1</td><td align="right">121</td><td align="right">85177.081200</td></tr>
<tr><td align="right">2</td><td align="right">198</td><td align="right">25199.499392</td></tr>
<tr><td align="right">3</td><td align="right">1695</td><td align="right">361999.058170</td></tr>
<tr><td align="right">4</td><td align="right">980</td><td align="right">586524.947384</td></tr>
<tr><td align="right">5</td><td align="right">299</td><td align="right">86177.847024</td></tr>
</table> </center>
</div>

<p>We see that numbers differ dramatically. So, what did happen in our first query? Why did it produce such high numbers that have nothing to do with the real numbers?</p>

<p>The reason is that for each row of Ordered results we get N Returned rows and so the total number of rows per one customer became M*N, where M is number of Ordered rows and N number of Returned rows. Therefore our results get much higher numbers.</p>

<p>So, the rule I always employ when I need to do aggregates that will involve more than one table to JOIN is to produce the necessary results separately as derived tables or CTE and only then join them together to form final result.</p>

<p>Here is how our query works when I re-write it using this idea:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb90374'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb90374','cb46242'); 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="cb90374" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">;WITH Ordered <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> OH.<span style="color: #202020;">CustomerID</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">LineTotal</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span> OH <span style="color: #FF00FF;">LEFT</span> JOIN Sales.<span style="color: #202020;">SalesOrderDetail</span> OD <span style="color: #0000FF;">ON</span> OH.<span style="color: #202020;">SalesOrderID</span> = OD.<span style="color: #202020;">SalesOrderID</span></li><li style="" class="li2"><span style="color: #0000FF;">Group</span> <span style="color: #0000FF;">BY</span> OH.<span style="color: #202020;">CustomerID</span> </li><li style="" class="li1"><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">Returned <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> OH.<span style="color: #202020;">CustomerID</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>R.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>R.<span style="color: #202020;">ReturnTotal</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span> OH <span style="color: #FF00FF;">LEFT</span> JOIN Sales.<span style="color: #202020;">SalesReturns</span> R <span style="color: #0000FF;">ON</span> OH.<span style="color: #202020;">SalesOrderID</span> = R.<span style="color: #202020;">SalesOrderID</span></li><li style="" class="li2"><span style="color: #0000FF;">Group</span> <span style="color: #0000FF;">BY</span> OH.<span style="color: #202020;">CustomerID</span> </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;">SELECT</span> <span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #202020;">CustomerID</span>, R.<span style="color: #202020;">CustomerID</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> CustomerID, </li><li style="" class="li2"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span>, </li><li style="" class="li2"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>R.<span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>R.<span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Ordered O FULL JOIN Returned R <span style="color: #0000FF;">ON</span> O.<span style="color: #202020;">CustomerID</span> = R.<span style="color: #202020;">CustomerID</span> </li><li style="" class="li2"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #202020;">CustomerID</span>, R.<span style="color: #202020;">CustomerID</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb46242" style="display: none; color: red;"></div></div></div>

<p>with these correct now results:</p>

<div class="tables"><p> </p><center>
<table border="1" cellpadding ="5" cellspacing = "5">
<caption style="color:blue;font-weight:bold">Aggregate Query Results - Ordered and Returned</caption>
<tr><th align="right">CustomerID</th><th align="right">Ordered Quantity</th><th align="right">Ordered</th><th align="right">Returned Quantity</th><th align="right">Returned</th></tr>
<tr><td align="right">1</td><td align="right">121</td><td align="right">85177.081200</td><td align="right">44</td><td align="right">37199.802700</td></tr>
<tr><td align="right">2</td><td align="right">198</td><td align="right">25199.499392</td><td align="right">0</td><td align="right">0.000000</td></tr>
<tr><td align="right">3</td><td align="right">1695</td><td align="right">361999.058170</td><td align="right">471</td><td align="right">91827.440195</td></tr>
<tr><td align="right">4</td><td align="right">980</td><td align="right">586524.947384</td><td align="right">242</td><td align="right">130206.961392</td></tr>
<tr><td align="right">5</td><td align="right">299</td><td align="right">86177.847024</td><td align="right">82</td><td align="right">22023.096824</td></tr>
</table></center>
</div>

<p>I used the FULL JOIN just in case we may have Ordered items and no returns or vs. versa for the customer.</p>

<p>I received a question of how to write this code for SQL 2000. In SQL 2000 we can not use CTE (common table expressions), but we can use derived tables, so the code above will be re-written in this manner:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb49105'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb49105','cb25331'); 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="cb49105" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #202020;">CustomerID</span>, R.<span style="color: #202020;">CustomerID</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> CustomerID,</li><li style="" class="li2"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span>,</li><li style="" class="li2"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>R.<span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>R.<span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> OH.<span style="color: #202020;">CustomerID</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">LineTotal</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span> OH <span style="color: #FF00FF;">LEFT</span> JOIN Sales.<span style="color: #202020;">SalesOrderDetail</span> OD <span style="color: #0000FF;">ON</span> OH.<span style="color: #202020;">SalesOrderID</span> = OD.<span style="color: #202020;">SalesOrderID</span></li><li style="" class="li2"><span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> OH.<span style="color: #202020;">CustomerID</span></li><li style="" class="li1"><span style="color: #808080;">&#41;</span>O FULL JOIN <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> OH.<span style="color: #202020;">CustomerID</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>R.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>R.<span style="color: #202020;">ReturnTotal</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span> OH <span style="color: #FF00FF;">LEFT</span> JOIN Sales.<span style="color: #202020;">SalesReturns</span> R <span style="color: #0000FF;">ON</span> OH.<span style="color: #202020;">SalesOrderID</span> = R.<span style="color: #202020;">SalesOrderID</span></li><li style="" class="li2"><span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> OH.<span style="color: #202020;">CustomerID</span></li><li style="" class="li1"><span style="color: #808080;">&#41;</span> R <span style="color: #0000FF;">ON</span> O.<span style="color: #202020;">CustomerID</span> = R.<span style="color: #202020;">CustomerID</span></li><li style="" class="li2"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #202020;">CustomerID</span>, R.<span style="color: #202020;">CustomerID</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb25331" style="display: none; color: red;"></div></div></div>

<p>So, hopefully you can now easily recognize the problem and apply the same idea when dealing with a similar situation.</p>

<p>Say, few days ago I looked into report. The number of joins used in the query made my head dizzy - I told right away that because of the JOINs the total results are not going to be correct.</p>

<p>So, how did I solve the problem? I included the totals using WINDOW AGGREGATE function in the CTE first and then added the rest of the JOINs to get the data needed for report.</p>

<p>Unfortunately, I am not sure if an easier method exists for reports in SSRS when we need to present information for customer orders, for example, but also display customer addresses (and there may be many addresses per customer). Obviously, we will not get correct totals if we will just sum the records - we need to apply some tricks, simplest of them will be to pre-calculate totals and include these fields as part of the returned dataset. I will be interested to know your solutions for this problem.</p>

<p>I should also mention another way to solve this query (assuming we also want to return the Customer name) by using OUTER APPLY query. </p>

<p>Here I am showing two different approaches for comparison. The first approach performs better.</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb48506'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb48506','cb56830'); 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="cb48506" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">;WITH Ordered <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> OH.<span style="color: #202020;">CustomerID</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">LineTotal</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span> OH <span style="color: #FF00FF;">LEFT</span> JOIN Sales.<span style="color: #202020;">SalesOrderDetail</span> OD <span style="color: #0000FF;">ON</span> OH.<span style="color: #202020;">SalesOrderID</span> = OD.<span style="color: #202020;">SalesOrderID</span></li><li style="" class="li2"><span style="color: #0000FF;">Group</span> <span style="color: #0000FF;">BY</span> OH.<span style="color: #202020;">CustomerID</span> </li><li style="" class="li1"><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">Returned <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> OH.<span style="color: #202020;">CustomerID</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>R.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>R.<span style="color: #202020;">ReturnTotal</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span> OH <span style="color: #FF00FF;">LEFT</span> JOIN Sales.<span style="color: #202020;">SalesReturns</span> R <span style="color: #0000FF;">ON</span> OH.<span style="color: #202020;">SalesOrderID</span> = R.<span style="color: #202020;">SalesOrderID</span></li><li style="" class="li2"><span style="color: #0000FF;">Group</span> <span style="color: #0000FF;">BY</span> OH.<span style="color: #202020;">CustomerID</span> </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;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span> C.<span style="color: #202020;">FirstName</span>, C.<span style="color: #202020;">LastName</span>, </li><li style="" class="li2"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span>, </li><li style="" class="li2"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>R.<span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>R.<span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span></li><li style="" class="li2"><span style="color: #00AF00;">--INTO dbo.QueryResults</span></li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">vIndividualCustomer</span> C</li><li style="" class="li2"><span style="color: #FF00FF;">LEFT</span> JOIN Ordered O <span style="color: #0000FF;">On</span> C.<span style="color: #202020;">CustomerID</span> = O.<span style="color: #202020;">CustomerID</span> </li><li style="" class="li1"><span style="color: #FF00FF;">LEFT</span> JOIN Returned R <span style="color: #0000FF;">ON</span> C.<span style="color: #202020;">CustomerID</span> = R.<span style="color: #202020;">CustomerID</span> </li><li style="" class="li2"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> C.<span style="color: #202020;">CustomerID</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span> C.<span style="color: #202020;">CustomerID</span>, C.<span style="color: #202020;">FirstName</span>, C.<span style="color: #202020;">LastName</span>, &nbsp;</li><li style="" class="li2"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>O.<span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span>, </li><li style="" class="li2"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>R.<span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1"><span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span>R.<span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span></li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">vIndividualCustomer</span> C</li><li style="" class="li1">OUTER APPLY <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li2"><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>OD.<span style="color: #202020;">LineTotal</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Ordered<span style="color: #808080;">&#93;</span></li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span> OH </li><li style="" class="li2"><span style="color: #FF00FF;">LEFT</span> JOIN Sales.<span style="color: #202020;">SalesOrderDetail</span> OD <span style="color: #0000FF;">ON</span> OH.<span style="color: #202020;">SalesOrderID</span> = OD.<span style="color: #202020;">SalesOrderID</span></li><li style="" class="li1"><span style="color: #0000FF;">WHERE</span> OH.<span style="color: #202020;">CustomerID</span> = C.<span style="color: #202020;">CustomerID</span><span style="color: #808080;">&#41;</span> O</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">OUTER APPLY <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>R.<span style="color: #202020;">OrderQty</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned Quantity<span style="color: #808080;">&#93;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>R.<span style="color: #202020;">ReturnTotal</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Returned<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span> OH </li><li style="" class="li2"><span style="color: #FF00FF;">LEFT</span> JOIN Sales.<span style="color: #202020;">SalesReturns</span> R <span style="color: #0000FF;">ON</span> OH.<span style="color: #202020;">SalesOrderID</span> = R.<span style="color: #202020;">SalesOrderID</span></li><li style="" class="li1"><span style="color: #0000FF;">WHERE</span> OH.<span style="color: #202020;">CustomerID</span> = C.<span style="color: #202020;">CustomerID</span><span style="color: #808080;">&#41;</span> R</li><li style="" class="li2"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> C.<span style="color: #202020;">CustomerID</span></li></ol></div><div id="cb56830" style="display: none; color: red;"></div></div></div>

<p>*** <strong>Remember, if you have a SQL related question, try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/aggregates-with-multiple-tables">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/aggregates-with-multiple-tables#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1420</wfw:commentRss>
		</item>
				<item>
			<title>Finding the Winning Streak</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/finding-the-winning-streak</link>
			<pubDate>Thu, 14 Jul 2011 00:32:00 +0000</pubDate>			<dc:creator>Naomi Nosonovsky</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">1337@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Ok, I hope I got your attention with this title &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;p&gt;I want to share my solution of an interesting problem I saw today at tek-tips forum &lt;a href=&quot;http://tek-tips.com/viewthread.cfm?qid=1654675&amp;amp;page=1&quot;&gt;SQL 2005: Find longest sequence of events within a table&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Given the following table:&lt;/p&gt;

&lt;div class=&quot;tables&quot;&gt;
&lt;table border=&quot;1&quot; cellpadding =&quot;5&quot; cellspacing = &quot;5&quot;&gt; 
&lt;caption style=&quot;color:blue;font-weight:bold&quot;&gt;Team Table&lt;/caption&gt;

&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Team&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Date&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;WiLose&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;A&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-01 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Win&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;A&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-07 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;A&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-13 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;A&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-19 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;A&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-25 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;A&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-31 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;A&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-02-06 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Win&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;A&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-02-12 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;A&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-02-18 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Win&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;B&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-01 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;B&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-07 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Win&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;B&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-13 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;B&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-19 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Win&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;B&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-25 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Win&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;B&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-31 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Win&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;B&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-02-06 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Win&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;B&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-02-12 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;B&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-02-18 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;C&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-01 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;C&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-07 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;C&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-13 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;C&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-19 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;C&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-25 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;C&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-01-31 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Win&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;C&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-02-06 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Win&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;C&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-02-12 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;C&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2010-02-18 00:00:00.000&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Win&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;find the biggest number of consecutive wins or loses regardless of the team.&lt;/p&gt;

&lt;p&gt;Here is the code that creates our team table:&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;cb96427&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; team &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;br /&gt;&amp;nbsp; Team &amp;nbsp; &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;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DATE&lt;/span&gt; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DATETIME&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; WiLose &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;10&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: #0000FF;&quot;&gt;SET&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DATEFORMAT&lt;/span&gt; &amp;nbsp;dmy &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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;01/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;07/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;13/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;19/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;25/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;31/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;06/02/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;12/02/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;18/02/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;B&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;01/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;B&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;07/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;B&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;13/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;B&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;19/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;B&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;25/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;B&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;31/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;B&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;06/02/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;B&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;12/02/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;B&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;18/02/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;C&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;01/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;C&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;07/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;C&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;13/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;C&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;19/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;C&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;25/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;C&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;31/01/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;C&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;06/02/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;C&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;12/02/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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; team &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;C&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;18/02/2010&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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;SET&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DATEFORMAT&lt;/span&gt; &amp;nbsp;mdy &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; * &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; team&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb58073&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;


&lt;p&gt;This problem is known as &#039;finding islands of data&#039;, and I always refer to this blog by Plamen Ratchev &lt;a href=&quot;http://pratchev.blogspot.com/2010/02/refactoring-ranges.html&quot;&gt;Refactoring Ranges&lt;/a&gt; when I am facing such scenario.&lt;/p&gt;

&lt;p&gt;The idea of a solution is to first identify the blocks of consecutive wins or loses by each team. Applying the idea from Plamen&#039;s blog first step will be&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;cb30746&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;;WITH cte &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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; *, &lt;br /&gt;&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;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&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; team &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: #0000FF;&quot;&gt;DATE&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; - &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; team, wilose &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: #0000FF;&quot;&gt;DATE&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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;GroupID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; team&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;cb88699&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;By assigning GroupID we identified blocks of winning or losing streaks.&lt;/p&gt;

&lt;p&gt;The last two steps are simple enough:&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;cb27466&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- Calculate Wins/Loses totals per each team/block&lt;/span&gt;&lt;br /&gt;cte1 &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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; team, &lt;br /&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; WiLose = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Win&#039;&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;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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Wins&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;br /&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; WiLose = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Lose&#039;&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;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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Loses&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; cte&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; Team, GroupID&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;-- Rank from highest to lowest wins / loses&lt;/span&gt;&lt;br /&gt;cteSummary &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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; *, RANK&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;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; Wins &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; WinsRank,&lt;br /&gt;RANK&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;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; Loses &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; LosesRank&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; cte1&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- Get final results&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; Team, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Wins&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Loses&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; cteSummary &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; WinsRank = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; OR LosesRank = &lt;span style=&quot;color: #000;&quot;&gt;1&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; Team&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb97487&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, correctly identifying a known pattern helps to solve such problems very quickly.&lt;/p&gt;

&lt;p&gt;*** &lt;strong&gt;Remember, if you have a SQL related question, try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&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/finding-the-winning-streak&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>Ok, I hope I got your attention with this title <img src="http://blogs.lessthandot.com/rsc/smilies/icon_smile.gif" title=":)" alt=":)" class="middle" width="15" height="15" /></p>

<p>I want to share my solution of an interesting problem I saw today at tek-tips forum <a href="http://tek-tips.com/viewthread.cfm?qid=1654675&amp;page=1">SQL 2005: Find longest sequence of events within a table</a></p>

<p>Given the following table:</p>

<div class="tables">
<table border="1" cellpadding ="5" cellspacing = "5"> 
<caption style="color:blue;font-weight:bold">Team Table</caption>

<tr><th align="left">Team</th><th align="left">Date</th><th align="left">WiLose</th></tr>
<tr><td align="left">A</td><td align="left">2010-01-01 00:00:00.000</td><td align="left">Win</td></tr>
<tr><td align="left">A</td><td align="left">2010-01-07 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">A</td><td align="left">2010-01-13 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">A</td><td align="left">2010-01-19 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">A</td><td align="left">2010-01-25 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">A</td><td align="left">2010-01-31 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">A</td><td align="left">2010-02-06 00:00:00.000</td><td align="left">Win</td></tr>
<tr><td align="left">A</td><td align="left">2010-02-12 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">A</td><td align="left">2010-02-18 00:00:00.000</td><td align="left">Win</td></tr>
<tr><td align="left">B</td><td align="left">2010-01-01 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">B</td><td align="left">2010-01-07 00:00:00.000</td><td align="left">Win</td></tr>
<tr><td align="left">B</td><td align="left">2010-01-13 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">B</td><td align="left">2010-01-19 00:00:00.000</td><td align="left">Win</td></tr>
<tr><td align="left">B</td><td align="left">2010-01-25 00:00:00.000</td><td align="left">Win</td></tr>
<tr><td align="left">B</td><td align="left">2010-01-31 00:00:00.000</td><td align="left">Win</td></tr>
<tr><td align="left">B</td><td align="left">2010-02-06 00:00:00.000</td><td align="left">Win</td></tr>
<tr><td align="left">B</td><td align="left">2010-02-12 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">B</td><td align="left">2010-02-18 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">C</td><td align="left">2010-01-01 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">C</td><td align="left">2010-01-07 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">C</td><td align="left">2010-01-13 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">C</td><td align="left">2010-01-19 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">C</td><td align="left">2010-01-25 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">C</td><td align="left">2010-01-31 00:00:00.000</td><td align="left">Win</td></tr>
<tr><td align="left">C</td><td align="left">2010-02-06 00:00:00.000</td><td align="left">Win</td></tr>
<tr><td align="left">C</td><td align="left">2010-02-12 00:00:00.000</td><td align="left">Lose</td></tr>
<tr><td align="left">C</td><td align="left">2010-02-18 00:00:00.000</td><td align="left">Win</td></tr>
</table></div>

<p>find the biggest number of consecutive wins or loses regardless of the team.</p>

<p>Here is the code that creates our team table:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb90592'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb90592','cb49308'); 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="cb90592" 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> team <span style="color: #808080;">&#40;</span> </li><li style="" class="li2">&nbsp; Team &nbsp; <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>, </li><li style="" class="li1">&nbsp; <span style="color: #0000FF;">DATE</span> &nbsp; <span style="color: #0000FF;">DATETIME</span>, </li><li style="" class="li2">&nbsp; WiLose <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</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: #0000FF;">SET</span> <span style="color: #0000FF;">DATEFORMAT</span> &nbsp;dmy </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'A'</span>,<span style="color: #FF0000;">'01/01/2010'</span>,<span style="color: #FF0000;">'Win'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'A'</span>,<span style="color: #FF0000;">'07/01/2010'</span>,<span style="color: #FF0000;">'Lose'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'A'</span>,<span style="color: #FF0000;">'13/01/2010'</span>,<span style="color: #FF0000;">'Lose'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'A'</span>,<span style="color: #FF0000;">'19/01/2010'</span>,<span style="color: #FF0000;">'Lose'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'A'</span>,<span style="color: #FF0000;">'25/01/2010'</span>,<span style="color: #FF0000;">'Lose'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'A'</span>,<span style="color: #FF0000;">'31/01/2010'</span>,<span style="color: #FF0000;">'Lose'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'A'</span>,<span style="color: #FF0000;">'06/02/2010'</span>,<span style="color: #FF0000;">'Win'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'A'</span>,<span style="color: #FF0000;">'12/02/2010'</span>,<span style="color: #FF0000;">'Lose'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'A'</span>,<span style="color: #FF0000;">'18/02/2010'</span>,<span style="color: #FF0000;">'Win'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'B'</span>,<span style="color: #FF0000;">'01/01/2010'</span>,<span style="color: #FF0000;">'Lose'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'B'</span>,<span style="color: #FF0000;">'07/01/2010'</span>,<span style="color: #FF0000;">'Win'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'B'</span>,<span style="color: #FF0000;">'13/01/2010'</span>,<span style="color: #FF0000;">'Lose'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'B'</span>,<span style="color: #FF0000;">'19/01/2010'</span>,<span style="color: #FF0000;">'Win'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'B'</span>,<span style="color: #FF0000;">'25/01/2010'</span>,<span style="color: #FF0000;">'Win'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'B'</span>,<span style="color: #FF0000;">'31/01/2010'</span>,<span style="color: #FF0000;">'Win'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'B'</span>,<span style="color: #FF0000;">'06/02/2010'</span>,<span style="color: #FF0000;">'Win'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'B'</span>,<span style="color: #FF0000;">'12/02/2010'</span>,<span style="color: #FF0000;">'Lose'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'B'</span>,<span style="color: #FF0000;">'18/02/2010'</span>,<span style="color: #FF0000;">'Lose'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'C'</span>,<span style="color: #FF0000;">'01/01/2010'</span>,<span style="color: #FF0000;">'Lose'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'C'</span>,<span style="color: #FF0000;">'07/01/2010'</span>,<span style="color: #FF0000;">'Lose'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'C'</span>,<span style="color: #FF0000;">'13/01/2010'</span>,<span style="color: #FF0000;">'Lose'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'C'</span>,<span style="color: #FF0000;">'19/01/2010'</span>,<span style="color: #FF0000;">'Lose'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'C'</span>,<span style="color: #FF0000;">'25/01/2010'</span>,<span style="color: #FF0000;">'Lose'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'C'</span>,<span style="color: #FF0000;">'31/01/2010'</span>,<span style="color: #FF0000;">'Win'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'C'</span>,<span style="color: #FF0000;">'06/02/2010'</span>,<span style="color: #FF0000;">'Win'</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> team </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'C'</span>,<span style="color: #FF0000;">'12/02/2010'</span>,<span style="color: #FF0000;">'Lose'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> team </li><li style="" class="li1"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'C'</span>,<span style="color: #FF0000;">'18/02/2010'</span>,<span style="color: #FF0000;">'Win'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">DATEFORMAT</span> &nbsp;mdy </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> * </li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> &nbsp; team</li></ol></div><div id="cb49308" style="display: none; color: red;"></div></div></div>


<p>This problem is known as 'finding islands of data', and I always refer to this blog by Plamen Ratchev <a href="http://pratchev.blogspot.com/2010/02/refactoring-ranges.html">Refactoring Ranges</a> when I am facing such scenario.</p>

<p>The idea of a solution is to first identify the blocks of consecutive wins or loses by each team. Applying the idea from Plamen's blog first step will be</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb24332'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb24332','cb18678'); 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="cb24332" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">;WITH cte <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> *, </li><li style="" class="li2"><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> </li><li style="" class="li1"><span style="color: #808080;">&#40;</span></li><li style="" class="li2"><span style="color: #0000FF;">PARTITION</span> <span style="color: #0000FF;">BY</span> team <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #0000FF;">DATE</span></li><li style="" class="li1"><span style="color: #808080;">&#41;</span> - <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> team, wilose <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>GroupID<span style="color: #808080;">&#93;</span></li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> team<span style="color: #808080;">&#41;</span>,</li></ol></div><div id="cb18678" style="display: none; color: red;"></div></div></div>

<p>By assigning GroupID we identified blocks of winning or losing streaks.</p>

<p>The last two steps are simple enough:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb45485'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb45485','cb66031'); 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="cb45485" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #00AF00;">-- Calculate Wins/Loses totals per each team/block</span></li><li style="" class="li2">cte1 <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> team, </li><li style="" class="li1"><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> WiLose = <span style="color: #FF0000;">'Win'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">1</span> <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: #808080;">&#91;</span>Wins<span style="color: #808080;">&#93;</span>,</li><li style="" class="li2"><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> WiLose = <span style="color: #FF0000;">'Lose'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">1</span> <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: #808080;">&#91;</span>Loses<span style="color: #808080;">&#93;</span></li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> cte</li><li style="" class="li2"><span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> Team, GroupID<span style="color: #808080;">&#41;</span>,</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">-- Rank from highest to lowest wins / loses</span></li><li style="" class="li1">cteSummary <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> *, RANK<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;">ORDER</span> <span style="color: #0000FF;">BY</span> Wins <span style="color: #0000FF;">DESC</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> WinsRank,</li><li style="" class="li2">RANK<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;">ORDER</span> <span style="color: #0000FF;">BY</span> Loses <span style="color: #0000FF;">DESC</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> LosesRank</li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> cte1<span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #00AF00;">-- Get final results</span></li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> Team, <span style="color: #808080;">&#91;</span>Wins<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>Loses<span style="color: #808080;">&#93;</span></li><li style="" class="li2">&nbsp;<span style="color: #0000FF;">FROM</span> cteSummary <span style="color: #0000FF;">WHERE</span> WinsRank = <span style="color: #000;">1</span> OR LosesRank = <span style="color: #000;">1</span></li><li style="" class="li1"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Team</li></ol></div><div id="cb66031" style="display: none; color: red;"></div></div></div>

<p>So, correctly identifying a known pattern helps to solve such problems very quickly.</p>

<p>*** <strong>Remember, if you have a SQL related question, try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/finding-the-winning-streak">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/finding-the-winning-streak#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1337</wfw:commentRss>
		</item>
				<item>
			<title>Finding Duplicates - Interesting twist</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/finding-duplicates-interesting-twist</link>
			<pubDate>Sun, 05 Jun 2011 23:48:00 +0000</pubDate>			<dc:creator>Naomi Nosonovsky</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">1287@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;A recent &lt;a href=&quot;http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/8ce895b9-11f1-494c-a63e-e25f08f93bd8&quot;&gt;MSDN thread&lt;/a&gt; presented a very interesting problem - find duplicates based on any 4 of the 5 columns and eliminate the duplicates.&lt;/p&gt;

&lt;p&gt;Here is the data table we will be working with:&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;cb13809&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; tblTEST &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;br /&gt;&amp;nbsp; ID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;UNIQUE&lt;/span&gt; &amp;nbsp; &amp;nbsp;NOT NULL, &lt;br /&gt;&amp;nbsp; FirstColumn &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;50&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;nbsp; &amp;nbsp;NOT NULL, &lt;br /&gt;&amp;nbsp; SecondColumn &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; &amp;nbsp; &amp;nbsp;NOT NULL, &lt;br /&gt;&amp;nbsp; ThirdColumn &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;50&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;nbsp; &amp;nbsp;NOT NULL, &lt;br /&gt;&amp;nbsp; FourthColumn &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; &amp;nbsp; &amp;nbsp;NOT NULL, &lt;br /&gt;&amp;nbsp; FifthColumn &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;50&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;nbsp; &amp;nbsp;NOT NULL &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; tblTEST &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ID, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FirstColumn, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SecondColumn, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ThirdColumn, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FourthColumn, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FifthColumn&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; &amp;nbsp; &amp;nbsp; &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: #FF0000;&quot;&gt;&#039;value1&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value2&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value3&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value4&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value5&#039;&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; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value2&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value3&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value4&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value5&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value6&#039;&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; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;3&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value1&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value4&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value5&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value8&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value9&#039;&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; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;4&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value1&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value2&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value7&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value8&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value9&#039;&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; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;5&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value3&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value4&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value5&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value6&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value7&#039;&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; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;6&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value1&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value2&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value9&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value4&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value5&#039;&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; &amp;nbsp; &amp;nbsp;&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: #FF0000;&quot;&gt;&#039;value2&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value3&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value4&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value5&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value11&#039;&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; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;8&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value4&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value5&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value3&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value9&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;value1&#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;cb75818&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Our test table:&lt;/p&gt;
&lt;div class=&quot;tables&quot;&gt;
&lt;table border=&quot;1&quot; cellpadding =&quot;5&quot; cellspacing = &quot;5&quot;&gt; 
&lt;caption style=&quot;color:blue;font-weight:bold&quot;&gt;Test Table with Duplicates&lt;/caption&gt;
&lt;tr&gt;&lt;th&gt;ID&lt;/th&gt;
     &lt;th&gt;FirstColumn&lt;/th&gt;
     &lt;th&gt;SecondColumn&lt;/th&gt;
     &lt;th&gt;ThirdColumn&lt;/th&gt;
     &lt;th&gt;FourthColumn&lt;/th&gt;
     &lt;th&gt;FifthColumn&lt;/th&gt;
&lt;/tr&gt;
 &lt;tr&gt;
                 &lt;td&gt;1&lt;/td&gt;
                 &lt;td&gt;value1&lt;/td&gt;
                 &lt;td&gt;value2&lt;/td&gt;
                 &lt;td&gt;value3&lt;/td&gt;
                 &lt;td&gt;value4&lt;/td&gt;
                 &lt;td&gt;value5&lt;/td&gt;
             &lt;/tr&gt;
             &lt;tr&gt;
                 &lt;td&gt;2&lt;/td&gt;
                 &lt;td&gt;value2&lt;/td&gt;
                 &lt;td&gt;value3&lt;/td&gt;
                 &lt;td&gt;value4&lt;/td&gt;
                 &lt;td&gt;value5&lt;/td&gt;
                 &lt;td&gt;value6&lt;/td&gt;
             &lt;/tr&gt;
             &lt;tr&gt;
                 &lt;td&gt;3&lt;/td&gt;
                 &lt;td&gt;value1&lt;/td&gt;
                 &lt;td&gt;value4&lt;/td&gt;
                 &lt;td&gt;value5&lt;/td&gt;
                 &lt;td&gt;value8&lt;/td&gt;
                 &lt;td&gt;value9&lt;/td&gt;
             &lt;/tr&gt;
             &lt;tr&gt;
                 &lt;td&gt;4&lt;/td&gt;
                 &lt;td&gt;value1&lt;/td&gt;
                 &lt;td&gt;value2&lt;/td&gt;
                 &lt;td&gt;value7&lt;/td&gt;
                 &lt;td&gt;value8&lt;/td&gt;
                 &lt;td&gt;value9&lt;/td&gt;
             &lt;/tr&gt;
             &lt;tr&gt;
                 &lt;td&gt;5&lt;/td&gt;
                 &lt;td&gt;value3&lt;/td&gt;
                 &lt;td&gt;value4&lt;/td&gt;
                 &lt;td&gt;value5&lt;/td&gt;
                 &lt;td&gt;value6&lt;/td&gt;
                 &lt;td&gt;value7&lt;/td&gt;
             &lt;/tr&gt;
             &lt;tr&gt;
                 &lt;td&gt;6&lt;/td&gt;
                 &lt;td&gt;value1&lt;/td&gt;
                 &lt;td&gt;value2&lt;/td&gt;
                 &lt;td&gt;value9&lt;/td&gt;
                 &lt;td&gt;value4&lt;/td&gt;
                 &lt;td&gt;value5&lt;/td&gt;
             &lt;/tr&gt;
             &lt;tr&gt;
                 &lt;td&gt;7&lt;/td&gt;
                 &lt;td&gt;value2&lt;/td&gt;
                 &lt;td&gt;value3&lt;/td&gt;
                 &lt;td&gt;value4&lt;/td&gt;
                 &lt;td&gt;value5&lt;/td&gt;
                 &lt;td&gt;value11&lt;/td&gt;
             &lt;/tr&gt;
             &lt;tr&gt;
                 &lt;td&gt;8&lt;/td&gt;
                 &lt;td&gt;value4&lt;/td&gt;
                 &lt;td&gt;value5&lt;/td&gt;
                 &lt;td&gt;value3&lt;/td&gt;
                 &lt;td&gt;value9&lt;/td&gt;
                 &lt;td&gt;value1&lt;/td&gt;
             &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This problem is different from the typical find the duplicates problem and at first does not even seem solvable.&lt;/p&gt;

&lt;p&gt;The first idea that comes to mind is to UNPIVOT values from these 5 columns into rows. This is simple enough with this code:&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;cb82118&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;;with UnPvt &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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; ID, ColValue &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; tblTEST &lt;br /&gt;UNPIVOT &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ColValue &lt;span style=&quot;color: #0000FF;&quot;&gt;FOR&lt;/span&gt; ColName IN &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;FirstColumn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;SecondColumn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ThirdColumn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;FourthColumn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;FifthColumn&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;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; unpvt&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;cb6834&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The second step is also more or less clear - find possible duplicates by counting IDs partitioned by ColValue&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;cb65532&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;SameVals &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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&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;ID&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; ColValue&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; cntSame &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; UnPvt&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; X &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; cntSame &amp;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;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb48213&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, what can we do next? The next step was a road block for me. But then, an Eureka moment - we can use CROSS APPLY and find all records that have more than 4 values matching the current record values&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;cb95276&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;DupRecs &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;span style=&quot;color: #0000FF;&quot;&gt;select&lt;/span&gt; T.&lt;span style=&quot;color: #202020;&quot;&gt;*&lt;/span&gt;,S.&lt;span style=&quot;color: #202020;&quot;&gt;cntDups&lt;/span&gt;, S.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; DupID&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; tblTEST T CROSS APPLY &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; S.&lt;span style=&quot;color: #202020;&quot;&gt;ID&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: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; cntDups &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; SameVals S&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; T.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &amp;lt; S.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; and S.&lt;span style=&quot;color: #202020;&quot;&gt;ColValue&lt;/span&gt; IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;T.&lt;span style=&quot;color: #202020;&quot;&gt;FirstColumn&lt;/span&gt;,T.&lt;span style=&quot;color: #202020;&quot;&gt;SecondColumn&lt;/span&gt;,T.&lt;span style=&quot;color: #202020;&quot;&gt;ThirdColumn&lt;/span&gt;,T.&lt;span style=&quot;color: #202020;&quot;&gt;FourthColumn&lt;/span&gt;, T.&lt;span style=&quot;color: #202020;&quot;&gt;FifthColumn&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&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; S.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; S&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Candidates &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;span style=&quot;color: #0000FF;&quot;&gt;select&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;distinct&lt;/span&gt; ID,DupID &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; DupRecs &lt;span style=&quot;color: #0000FF;&quot;&gt;where&lt;/span&gt; cntDups &amp;gt;=&lt;span style=&quot;color: #000;&quot;&gt;4&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;cb79379&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ok, but do we want to delete all records identified in DupID column? Turned out, that it&#039;s not even that simple. &lt;/p&gt;

&lt;p&gt;Say, in our sample rows 1 &amp;amp; 2 match. So, do we want to delete the row 2? Well, row 2 matches with the row 5. Which rows of 2/5 we delete and which to keep? That&#039;s not easy at all.&lt;/p&gt;

&lt;p&gt;So, my final select is&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;cb20455&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;Distinct&lt;/span&gt; DupID &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; Candidates &lt;span style=&quot;color: #0000FF;&quot;&gt;where&lt;/span&gt; DupID not IN &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; ID &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; Candidates&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;cb45538&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This select will only produce real duplicates.&lt;/p&gt;

&lt;p&gt;Now, this is the whole solution again as one 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;cb49668&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;;with UnPvt &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;span style=&quot;color: #0000FF;&quot;&gt;select&lt;/span&gt; ID, ColValue &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; tblTEST &lt;br /&gt;UNPIVOT &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ColValue &lt;span style=&quot;color: #0000FF;&quot;&gt;for&lt;/span&gt; ColName IN &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;FirstColumn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;SecondColumn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;ThirdColumn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;FourthColumn&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;FifthColumn&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;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; unpvt&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;SameVals &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;span style=&quot;color: #0000FF;&quot;&gt;select&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;from&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;ID&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; ColValue&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; cntSame &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; UnPvt&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; X &lt;span style=&quot;color: #0000FF;&quot;&gt;where&lt;/span&gt; cntSame &amp;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;br /&gt;DupRecs &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;span style=&quot;color: #0000FF;&quot;&gt;select&lt;/span&gt; T.&lt;span style=&quot;color: #202020;&quot;&gt;*&lt;/span&gt;,S.&lt;span style=&quot;color: #202020;&quot;&gt;cntDups&lt;/span&gt;, S.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; DupID&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; tblTEST T CROSS APPLY &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; S.&lt;span style=&quot;color: #202020;&quot;&gt;ID&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: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;as&lt;/span&gt; cntDups &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; SameVals S&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; T.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &amp;lt; S.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; and S.&lt;span style=&quot;color: #202020;&quot;&gt;ColValue&lt;/span&gt; IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;T.&lt;span style=&quot;color: #202020;&quot;&gt;FirstColumn&lt;/span&gt;,T.&lt;span style=&quot;color: #202020;&quot;&gt;SecondColumn&lt;/span&gt;,T.&lt;span style=&quot;color: #202020;&quot;&gt;ThirdColumn&lt;/span&gt;,T.&lt;span style=&quot;color: #202020;&quot;&gt;FourthColumn&lt;/span&gt;, T.&lt;span style=&quot;color: #202020;&quot;&gt;FifthColumn&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&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; S.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; S&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Candidates &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;span style=&quot;color: #0000FF;&quot;&gt;select&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;distinct&lt;/span&gt; ID,DupID &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; DupRecs &lt;span style=&quot;color: #0000FF;&quot;&gt;where&lt;/span&gt; cntDups &amp;gt;=&lt;span style=&quot;color: #000;&quot;&gt;4&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;select&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Distinct&lt;/span&gt; DupID &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; Candidates &lt;span style=&quot;color: #0000FF;&quot;&gt;where&lt;/span&gt; DupID not IN &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; ID &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; Candidates&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;cb27400&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Well, even the above solution is not quite right as now it doesn&#039;t delete all duplicates.&lt;/p&gt;

&lt;p&gt;After some more discussion in the mentioned thread and with the help of Peter Larsson, here is the solution that seems to work for the problem:&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;cb26515&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; &amp;nbsp;@DupLoop &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt; &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @DupLoop = &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; @DupLoop = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;BEGIN&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; Dups &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DISTINCT&lt;/span&gt; a.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; DuplicateID &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;tblTEST&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; t &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN dbo.&lt;span style=&quot;color: #202020;&quot;&gt;tblTEST&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; a &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; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; a.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &amp;gt; t.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &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;AND a.&lt;span style=&quot;color: #202020;&quot;&gt;FirstColumn&lt;/span&gt; IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;t.&lt;span style=&quot;color: #202020;&quot;&gt;FirstColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;SecondColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;ThirdColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;FourthColumn&lt;/span&gt;,&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;t.&lt;span style=&quot;color: #202020;&quot;&gt;FifthColumn&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN dbo.&lt;span style=&quot;color: #202020;&quot;&gt;tblTEST&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; b &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; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; b.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; = a.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &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;AND b.&lt;span style=&quot;color: #202020;&quot;&gt;SecondColumn&lt;/span&gt; IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;t.&lt;span style=&quot;color: #202020;&quot;&gt;FirstColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;SecondColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;ThirdColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;FourthColumn&lt;/span&gt;,&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t.&lt;span style=&quot;color: #202020;&quot;&gt;FifthColumn&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN dbo.&lt;span style=&quot;color: #202020;&quot;&gt;tblTEST&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; c &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; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; c.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; = a.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &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;AND c.&lt;span style=&quot;color: #202020;&quot;&gt;ThirdColumn&lt;/span&gt; IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;t.&lt;span style=&quot;color: #202020;&quot;&gt;FirstColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;SecondColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;ThirdColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;FourthColumn&lt;/span&gt;,&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;t.&lt;span style=&quot;color: #202020;&quot;&gt;FifthColumn&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN dbo.&lt;span style=&quot;color: #202020;&quot;&gt;tblTEST&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; d &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; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; d.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; = a.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &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;AND d.&lt;span style=&quot;color: #202020;&quot;&gt;FourthColumn&lt;/span&gt; IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;t.&lt;span style=&quot;color: #202020;&quot;&gt;FirstColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;SecondColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;ThirdColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;FourthColumn&lt;/span&gt;,&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t.&lt;span style=&quot;color: #202020;&quot;&gt;FifthColumn&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEFT&lt;/span&gt; JOIN dbo.&lt;span style=&quot;color: #202020;&quot;&gt;tblTEST&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; e &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; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; e.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; = a.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &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;AND e.&lt;span style=&quot;color: #202020;&quot;&gt;FifthColumn&lt;/span&gt; IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;t.&lt;span style=&quot;color: #202020;&quot;&gt;FirstColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;SecondColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;ThirdColumn&lt;/span&gt;,t.&lt;span style=&quot;color: #202020;&quot;&gt;FourthColumn&lt;/span&gt;,&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;t.&lt;span style=&quot;color: #202020;&quot;&gt;FifthColumn&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; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &amp;nbsp;&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHEN&lt;/span&gt; a.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL &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; &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;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt; + &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHEN&lt;/span&gt; b.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL &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; &amp;nbsp; &amp;nbsp; &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;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; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt; + &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHEN&lt;/span&gt; c.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ELSE&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt; + &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHEN&lt;/span&gt; d.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ELSE&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt; + &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHEN&lt;/span&gt; e.&lt;span style=&quot;color: #202020;&quot;&gt;ID&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ELSE&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt; &amp;gt;= &lt;span style=&quot;color: #000;&quot;&gt;4&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;DELETE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; tblTEST &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; ID IN &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; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&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; DuplicateID &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;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; &amp;nbsp; Dups &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;&lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; DuplicateID&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;SELECT&lt;/span&gt; @DupLoop = &lt;span style=&quot;color: #FF00FF;&quot;&gt;@@ROWCOUNT&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &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;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; tblTest&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb40043&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;


&lt;p&gt;Well, hopefully this was interesting to read as for me trying to solve such problem. Thanks for listening!&lt;/p&gt;

&lt;p&gt;*** &lt;strong&gt;Remember, if you have a SQL related question, try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&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/finding-duplicates-interesting-twist&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>A recent <a href="http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/8ce895b9-11f1-494c-a63e-e25f08f93bd8">MSDN thread</a> presented a very interesting problem - find duplicates based on any 4 of the 5 columns and eliminate the duplicates.</p>

<p>Here is the data table we will be working with:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb57369'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb57369','cb21281'); 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="cb57369" 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> tblTEST <span style="color: #808080;">&#40;</span> </li><li style="" class="li2">&nbsp; ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INT</span> &nbsp; &nbsp;<span style="color: #0000FF;">UNIQUE</span> &nbsp; &nbsp;NOT NULL, </li><li style="" class="li1">&nbsp; FirstColumn &nbsp;<span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> &nbsp; &nbsp;NOT NULL, </li><li style="" class="li2">&nbsp; SecondColumn <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> &nbsp; &nbsp;NOT NULL, </li><li style="" class="li1">&nbsp; ThirdColumn &nbsp;<span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> &nbsp; &nbsp;NOT NULL, </li><li style="" class="li2">&nbsp; FourthColumn <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> &nbsp; &nbsp;NOT NULL, </li><li style="" class="li1">&nbsp; FifthColumn &nbsp;<span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> &nbsp; &nbsp;NOT NULL <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> tblTEST </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080;">&#40;</span>ID, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FirstColumn, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SecondColumn, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ThirdColumn, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FourthColumn, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FifthColumn<span style="color: #808080;">&#41;</span> </li><li style="" class="li2"><span style="color: #0000FF;">VALUES</span> &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #FF0000;">'value1'</span>,<span style="color: #FF0000;">'value2'</span>,<span style="color: #FF0000;">'value3'</span>,<span style="color: #FF0000;">'value4'</span>,<span style="color: #FF0000;">'value5'</span><span style="color: #808080;">&#41;</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #000;">2</span>,<span style="color: #FF0000;">'value2'</span>,<span style="color: #FF0000;">'value3'</span>,<span style="color: #FF0000;">'value4'</span>,<span style="color: #FF0000;">'value5'</span>,<span style="color: #FF0000;">'value6'</span><span style="color: #808080;">&#41;</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #000;">3</span>,<span style="color: #FF0000;">'value1'</span>,<span style="color: #FF0000;">'value4'</span>,<span style="color: #FF0000;">'value5'</span>,<span style="color: #FF0000;">'value8'</span>,<span style="color: #FF0000;">'value9'</span><span style="color: #808080;">&#41;</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>,<span style="color: #FF0000;">'value1'</span>,<span style="color: #FF0000;">'value2'</span>,<span style="color: #FF0000;">'value7'</span>,<span style="color: #FF0000;">'value8'</span>,<span style="color: #FF0000;">'value9'</span><span style="color: #808080;">&#41;</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #000;">5</span>,<span style="color: #FF0000;">'value3'</span>,<span style="color: #FF0000;">'value4'</span>,<span style="color: #FF0000;">'value5'</span>,<span style="color: #FF0000;">'value6'</span>,<span style="color: #FF0000;">'value7'</span><span style="color: #808080;">&#41;</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #000;">6</span>,<span style="color: #FF0000;">'value1'</span>,<span style="color: #FF0000;">'value2'</span>,<span style="color: #FF0000;">'value9'</span>,<span style="color: #FF0000;">'value4'</span>,<span style="color: #FF0000;">'value5'</span><span style="color: #808080;">&#41;</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #000;">7</span>,<span style="color: #FF0000;">'value2'</span>,<span style="color: #FF0000;">'value3'</span>,<span style="color: #FF0000;">'value4'</span>,<span style="color: #FF0000;">'value5'</span>,<span style="color: #FF0000;">'value11'</span><span style="color: #808080;">&#41;</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #000;">8</span>,<span style="color: #FF0000;">'value4'</span>,<span style="color: #FF0000;">'value5'</span>,<span style="color: #FF0000;">'value3'</span>,<span style="color: #FF0000;">'value9'</span>,<span style="color: #FF0000;">'value1'</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb21281" style="display: none; color: red;"></div></div></div>

<p>Our test table:</p>
<div class="tables">
<table border="1" cellpadding ="5" cellspacing = "5"> 
<caption style="color:blue;font-weight:bold">Test Table with Duplicates</caption>
<tr><th>ID</th>
     <th>FirstColumn</th>
     <th>SecondColumn</th>
     <th>ThirdColumn</th>
     <th>FourthColumn</th>
     <th>FifthColumn</th>
</tr>
 <tr>
                 <td>1</td>
                 <td>value1</td>
                 <td>value2</td>
                 <td>value3</td>
                 <td>value4</td>
                 <td>value5</td>
             </tr>
             <tr>
                 <td>2</td>
                 <td>value2</td>
                 <td>value3</td>
                 <td>value4</td>
                 <td>value5</td>
                 <td>value6</td>
             </tr>
             <tr>
                 <td>3</td>
                 <td>value1</td>
                 <td>value4</td>
                 <td>value5</td>
                 <td>value8</td>
                 <td>value9</td>
             </tr>
             <tr>
                 <td>4</td>
                 <td>value1</td>
                 <td>value2</td>
                 <td>value7</td>
                 <td>value8</td>
                 <td>value9</td>
             </tr>
             <tr>
                 <td>5</td>
                 <td>value3</td>
                 <td>value4</td>
                 <td>value5</td>
                 <td>value6</td>
                 <td>value7</td>
             </tr>
             <tr>
                 <td>6</td>
                 <td>value1</td>
                 <td>value2</td>
                 <td>value9</td>
                 <td>value4</td>
                 <td>value5</td>
             </tr>
             <tr>
                 <td>7</td>
                 <td>value2</td>
                 <td>value3</td>
                 <td>value4</td>
                 <td>value5</td>
                 <td>value11</td>
             </tr>
             <tr>
                 <td>8</td>
                 <td>value4</td>
                 <td>value5</td>
                 <td>value3</td>
                 <td>value9</td>
                 <td>value1</td>
             </tr>
</table></div>

<p>This problem is different from the typical find the duplicates problem and at first does not even seem solvable.</p>

<p>The first idea that comes to mind is to UNPIVOT values from these 5 columns into rows. This is simple enough with this code:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb354'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb354','cb99252'); 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="cb354" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">;with UnPvt <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> ID, ColValue <span style="color: #0000FF;">FROM</span> tblTEST </li><li style="" class="li2">UNPIVOT <span style="color: #808080;">&#40;</span>ColValue <span style="color: #0000FF;">FOR</span> ColName IN <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>FirstColumn<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>SecondColumn<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>ThirdColumn<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>FourthColumn<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>FifthColumn<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> unpvt<span style="color: #808080;">&#41;</span>,</li></ol></div><div id="cb99252" style="display: none; color: red;"></div></div></div>

<p>The second step is also more or less clear - find possible duplicates by counting IDs partitioned by ColValue</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb77268'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb77268','cb14460'); 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="cb77268" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">SameVals <span style="color: #0000FF;">as</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</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>ID<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> ColValue<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> cntSame <span style="color: #0000FF;">from</span> UnPvt<span style="color: #808080;">&#41;</span> X <span style="color: #0000FF;">WHERE</span> cntSame &gt;=<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>,</li></ol></div><div id="cb14460" style="display: none; color: red;"></div></div></div>

<p>Now, what can we do next? The next step was a road block for me. But then, an Eureka moment - we can use CROSS APPLY and find all records that have more than 4 values matching the current record values</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb90820'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb90820','cb21670'); 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="cb90820" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">DupRecs <span style="color: #0000FF;">as</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> T.<span style="color: #202020;">*</span>,S.<span style="color: #202020;">cntDups</span>, S.<span style="color: #202020;">ID</span> <span style="color: #0000FF;">as</span> DupID</li><li style="" class="li2">&nbsp;<span style="color: #0000FF;">from</span> tblTEST T CROSS APPLY <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> S.<span style="color: #202020;">ID</span>, <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span>*<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> cntDups <span style="color: #0000FF;">from</span> SameVals S</li><li style="" class="li1"><span style="color: #0000FF;">WHERE</span> T.<span style="color: #202020;">ID</span> &lt; S.<span style="color: #202020;">ID</span> and S.<span style="color: #202020;">ColValue</span> IN <span style="color: #808080;">&#40;</span>T.<span style="color: #202020;">FirstColumn</span>,T.<span style="color: #202020;">SecondColumn</span>,T.<span style="color: #202020;">ThirdColumn</span>,T.<span style="color: #202020;">FourthColumn</span>, T.<span style="color: #202020;">FifthColumn</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> S.<span style="color: #202020;">ID</span><span style="color: #808080;">&#41;</span> S<span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">Candidates <span style="color: #0000FF;">as</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> <span style="color: #0000FF;">distinct</span> ID,DupID &nbsp;<span style="color: #0000FF;">from</span> DupRecs <span style="color: #0000FF;">where</span> cntDups &gt;=<span style="color: #000;">4</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb21670" style="display: none; color: red;"></div></div></div>

<p>Ok, but do we want to delete all records identified in DupID column? Turned out, that it's not even that simple. </p>

<p>Say, in our sample rows 1 &amp; 2 match. So, do we want to delete the row 2? Well, row 2 matches with the row 5. Which rows of 2/5 we delete and which to keep? That's not easy at all.</p>

<p>So, my final select is</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb93683'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb93683','cb10533'); 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="cb93683" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">select</span> <span style="color: #0000FF;">Distinct</span> DupID <span style="color: #0000FF;">from</span> Candidates <span style="color: #0000FF;">where</span> DupID not IN <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> ID <span style="color: #0000FF;">from</span> Candidates<span style="color: #808080;">&#41;</span></li></ol></div><div id="cb10533" style="display: none; color: red;"></div></div></div>

<p>This select will only produce real duplicates.</p>

<p>Now, this is the whole solution again as one statement:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb49916'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb49916','cb52458'); 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="cb49916" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">;with UnPvt <span style="color: #0000FF;">as</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> ID, ColValue <span style="color: #0000FF;">from</span> tblTEST </li><li style="" class="li2">UNPIVOT <span style="color: #808080;">&#40;</span>ColValue <span style="color: #0000FF;">for</span> ColName IN <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>FirstColumn<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>SecondColumn<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>ThirdColumn<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>FourthColumn<span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>FifthColumn<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> unpvt<span style="color: #808080;">&#41;</span>,</li><li style="" class="li1">SameVals <span style="color: #0000FF;">as</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> * <span style="color: #0000FF;">from</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>ID<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> ColValue<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> cntSame <span style="color: #0000FF;">from</span> UnPvt<span style="color: #808080;">&#41;</span> X <span style="color: #0000FF;">where</span> cntSame &gt;=<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">DupRecs <span style="color: #0000FF;">as</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> T.<span style="color: #202020;">*</span>,S.<span style="color: #202020;">cntDups</span>, S.<span style="color: #202020;">ID</span> <span style="color: #0000FF;">as</span> DupID</li><li style="" class="li1">&nbsp;<span style="color: #0000FF;">from</span> tblTEST T CROSS APPLY <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> S.<span style="color: #202020;">ID</span>, <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span>*<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">as</span> cntDups <span style="color: #0000FF;">from</span> SameVals S</li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> T.<span style="color: #202020;">ID</span> &lt; S.<span style="color: #202020;">ID</span> and S.<span style="color: #202020;">ColValue</span> IN <span style="color: #808080;">&#40;</span>T.<span style="color: #202020;">FirstColumn</span>,T.<span style="color: #202020;">SecondColumn</span>,T.<span style="color: #202020;">ThirdColumn</span>,T.<span style="color: #202020;">FourthColumn</span>, T.<span style="color: #202020;">FifthColumn</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> S.<span style="color: #202020;">ID</span><span style="color: #808080;">&#41;</span> S<span style="color: #808080;">&#41;</span>,</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">Candidates <span style="color: #0000FF;">as</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> <span style="color: #0000FF;">distinct</span> ID,DupID &nbsp;<span style="color: #0000FF;">from</span> DupRecs <span style="color: #0000FF;">where</span> cntDups &gt;=<span style="color: #000;">4</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">select</span> <span style="color: #0000FF;">Distinct</span> DupID <span style="color: #0000FF;">from</span> Candidates <span style="color: #0000FF;">where</span> DupID not IN <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> ID <span style="color: #0000FF;">from</span> Candidates<span style="color: #808080;">&#41;</span></li></ol></div><div id="cb52458" style="display: none; color: red;"></div></div></div>

<p>Well, even the above solution is not quite right as now it doesn't delete all duplicates.</p>

<p>After some more discussion in the mentioned thread and with the help of Peter Larsson, here is the solution that seems to work for the problem:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb44688'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb44688','cb31347'); 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="cb44688" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> &nbsp;@DupLoop <span style="color: #0000FF;">INT</span> </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SET</span> @DupLoop = <span style="color: #000;">1</span> </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">WHILE</span> @DupLoop = <span style="color: #000;">1</span> </li><li style="" class="li2">&nbsp; <span style="color: #0000FF;">BEGIN</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">WITH</span> Dups </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> a.<span style="color: #202020;">ID</span> <span style="color: #0000FF;">AS</span> DuplicateID </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">FROM</span> &nbsp; dbo.<span style="color: #202020;">tblTEST</span> <span style="color: #0000FF;">AS</span> t </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF00FF;">LEFT</span> JOIN dbo.<span style="color: #202020;">tblTEST</span> <span style="color: #0000FF;">AS</span> a </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">ID</span> &gt; t.<span style="color: #202020;">ID</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND a.<span style="color: #202020;">FirstColumn</span> IN <span style="color: #808080;">&#40;</span>t.<span style="color: #202020;">FirstColumn</span>,t.<span style="color: #202020;">SecondColumn</span>,t.<span style="color: #202020;">ThirdColumn</span>,t.<span style="color: #202020;">FourthColumn</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.<span style="color: #202020;">FifthColumn</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF00FF;">LEFT</span> JOIN dbo.<span style="color: #202020;">tblTEST</span> <span style="color: #0000FF;">AS</span> b </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> b.<span style="color: #202020;">ID</span> = a.<span style="color: #202020;">ID</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND b.<span style="color: #202020;">SecondColumn</span> IN <span style="color: #808080;">&#40;</span>t.<span style="color: #202020;">FirstColumn</span>,t.<span style="color: #202020;">SecondColumn</span>,t.<span style="color: #202020;">ThirdColumn</span>,t.<span style="color: #202020;">FourthColumn</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.<span style="color: #202020;">FifthColumn</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF00FF;">LEFT</span> JOIN dbo.<span style="color: #202020;">tblTEST</span> <span style="color: #0000FF;">AS</span> c </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> c.<span style="color: #202020;">ID</span> = a.<span style="color: #202020;">ID</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND c.<span style="color: #202020;">ThirdColumn</span> IN <span style="color: #808080;">&#40;</span>t.<span style="color: #202020;">FirstColumn</span>,t.<span style="color: #202020;">SecondColumn</span>,t.<span style="color: #202020;">ThirdColumn</span>,t.<span style="color: #202020;">FourthColumn</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.<span style="color: #202020;">FifthColumn</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF00FF;">LEFT</span> JOIN dbo.<span style="color: #202020;">tblTEST</span> <span style="color: #0000FF;">AS</span> d </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> d.<span style="color: #202020;">ID</span> = a.<span style="color: #202020;">ID</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND d.<span style="color: #202020;">FourthColumn</span> IN <span style="color: #808080;">&#40;</span>t.<span style="color: #202020;">FirstColumn</span>,t.<span style="color: #202020;">SecondColumn</span>,t.<span style="color: #202020;">ThirdColumn</span>,t.<span style="color: #202020;">FourthColumn</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.<span style="color: #202020;">FifthColumn</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF00FF;">LEFT</span> JOIN dbo.<span style="color: #202020;">tblTEST</span> <span style="color: #0000FF;">AS</span> e </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> e.<span style="color: #202020;">ID</span> = a.<span style="color: #202020;">ID</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND e.<span style="color: #202020;">FifthColumn</span> IN <span style="color: #808080;">&#40;</span>t.<span style="color: #202020;">FirstColumn</span>,t.<span style="color: #202020;">SecondColumn</span>,t.<span style="color: #202020;">ThirdColumn</span>,t.<span style="color: #202020;">FourthColumn</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.<span style="color: #202020;">FifthColumn</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">WHERE</span> &nbsp;<span style="color: #0000FF;">CASE</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> a.<span style="color: #202020;">ID</span> <span style="color: #0000FF;">IS</span> NULL <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">1</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> + <span style="color: #0000FF;">CASE</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> b.<span style="color: #202020;">ID</span> <span style="color: #0000FF;">IS</span> NULL <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">1</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> + <span style="color: #0000FF;">CASE</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> c.<span style="color: #202020;">ID</span> <span style="color: #0000FF;">IS</span> NULL <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">1</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> + <span style="color: #0000FF;">CASE</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> d.<span style="color: #202020;">ID</span> <span style="color: #0000FF;">IS</span> NULL <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">1</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> + <span style="color: #0000FF;">CASE</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> e.<span style="color: #202020;">ID</span> <span style="color: #0000FF;">IS</span> NULL <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">1</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> &gt;= <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> tblTEST </li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; &nbsp; &nbsp; ID IN <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> &nbsp; <span style="color: #0000FF;">TOP</span> <span style="color: #808080;">&#40;</span> <span style="color: #000;">1</span> <span style="color: #808080;">&#41;</span> DuplicateID </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">FROM</span> &nbsp; &nbsp; Dups </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> DuplicateID<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;">SELECT</span> @DupLoop = <span style="color: #FF00FF;">@@ROWCOUNT</span> </li><li style="" class="li2">&nbsp; <span style="color: #0000FF;">END</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> * </li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> &nbsp; tblTest</li></ol></div><div id="cb31347" style="display: none; color: red;"></div></div></div>


<p>Well, hopefully this was interesting to read as for me trying to solve such problem. Thanks for listening!</p>

<p>*** <strong>Remember, if you have a SQL related question, try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/finding-duplicates-interesting-twist">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/finding-duplicates-interesting-twist#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1287</wfw:commentRss>
		</item>
				<item>
			<title>Dynamic PIVOT on multiple columns</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/dynamic-pivot-on-multiple-columns</link>
			<pubDate>Sun, 29 May 2011 14:42:00 +0000</pubDate>			<dc:creator>Naomi Nosonovsky</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">1277@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;In this blog post I want to discuss a problem I encounter frequently in SQL related forums - how to perform dynamic PIVOT involving multiple columns using pure T-SQL solution. In SQL Server Reporting Services this functionality can be easily achieved using &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms157334(SQL.100).aspx&quot;&gt;Matrix template&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With the introduction of PIVOT operator in SQL 2005, it became very easy to write queries that transform rows into columns. There are numerous articles on the Web as how to perform PIVOT and even dynamic PIVOT. However, most of these articles explain how to perform such queries for just one column. I want to expand the transformation for multiple columns.&lt;/p&gt;

&lt;p&gt;The idea of a solution is quite simple - you need to generate the SQL dynamically. Since we want to use pivot on multiple columns, we need to use &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/understanding-sql-server-2000-pivot&quot;&gt;CASE based pivot&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You need to keep in mind, that resulting query should have less than 1024 columns. You may build the check for number of columns into the query.&lt;/p&gt;

&lt;p&gt;Now, every time I need to create a dynamic query, I need to know what is the final query I am going to arrive to. Having the idea in mind and printing the SQL command until I got it right helps to create the working query.&lt;/p&gt;

&lt;p&gt;Let&#039;s consider the first example based on the AdventureWorks SalesOrderHeader table.&lt;br /&gt;
This code creates the table we&#039;re going to transform - it gives us summary of orders and total due per each quarter:&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;cb73643&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; AdventureWorks &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &amp;nbsp; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;quarter&lt;/span&gt;,OrderDate&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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Quarter&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;&lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;YEAR&lt;/span&gt;,OrderDate&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Year&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;&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;SalesOrderID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &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;Orders &lt;span style=&quot;color: #FF00FF;&quot;&gt;Count&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;&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;TotalDue&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &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;Total Due&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; &amp;nbsp; &amp;nbsp; #SalesSummary &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; &amp;nbsp; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&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; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;quarter&lt;/span&gt;,OrderDate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;YEAR&lt;/span&gt;,OrderDate&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;SELECT&lt;/span&gt; &amp;nbsp; * &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; &amp;nbsp; #SalesSummary &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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Year&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;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Quarter&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb75621&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The #SalesSummary table lists count of orders and total due for each quarter and year.&lt;/p&gt;

&lt;center&gt;
&lt;div class=&quot;tables&quot;&gt;
&lt;table border=&quot;1&quot; cellpadding =&quot;5&quot; cellspacing = &quot;5&quot;&gt; 
&lt;caption style=&quot;color:blue;font-weight:bold&quot;&gt;Quarterly Orders Summary&lt;/caption&gt;
&lt;tr&gt;&lt;th&gt;Quarter&lt;/th&gt;&lt;th&gt;Year&lt;/th&gt;
&lt;th&gt;Orders Count&lt;/th&gt;&lt;th&gt;Total Due&lt;/th&gt;&lt;/tr&gt;


&lt;tr&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;3&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;2001&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	621&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	$5,850,932.9483&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;4&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2001&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	758&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$8,476,619.278&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2002&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	741&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$7,379,686.3091&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;2&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2002&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	825&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$8,210,285.1655&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;3&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2002&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	1054&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$13,458,206.13&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;4&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2002&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	1072&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$10,827,327.4904&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2003&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	1091&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$8,550,831.8702&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;2&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2003&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	1260&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$10,749,269.374&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;3&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2003&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	4152&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$18,220,131.5285&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;4&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2003&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	5940&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$16,787,382.3141&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2004&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	6087&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$14,170,982.5455&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;2&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2004&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	6888&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$17,969,750.9487&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;3&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2004&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	976&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$56,178.9223&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;&lt;/center&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Now, suppose we want to see these results horizontally. The following dynamic SQL produces the desired output:&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;cb65374&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; &amp;nbsp;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; &amp;nbsp;&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: #FF00FF;&quot;&gt;MAX&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; &amp;nbsp;@Cols &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: #FF00FF;&quot;&gt;MAX&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; &amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @Cols = &lt;span style=&quot;color: #FF00FF;&quot;&gt;STUFF&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;&lt;span style=&quot;color: #0000FF;&quot;&gt;select&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;, &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;SUM(CASE WHEN [Quarter]=&#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;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Quarter&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;3&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: #FF0000;&quot;&gt;&#039; AND [Year] = &#039;&lt;/span&gt; + &lt;br /&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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Year&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;4&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;span style=&quot;color: #FF0000;&quot;&gt;&#039; THEN [Orders Count] ELSE 0 END) AS [&#039;&lt;/span&gt; + &lt;br /&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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Year&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;4&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: #FF0000;&quot;&gt;&#039;-&#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;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Quarter&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;3&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;span style=&quot;color: #FF0000;&quot;&gt;&#039; Orders], &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;SUM(CASE WHEN [Quarter]=&#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;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Quarter&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;3&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;span style=&quot;color: #FF0000;&quot;&gt;&#039; AND [Year] = &#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;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Year&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;4&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;span style=&quot;color: #FF0000;&quot;&gt;&#039; THEN [Total Due] ELSE 0 END) AS [&#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;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Year&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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;4&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: #FF0000;&quot;&gt;&#039;-&#039;&lt;/span&gt; + &lt;br /&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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Quarter&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;3&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: #FF0000;&quot;&gt;&#039; Sales]&#039;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; #SalesSummary &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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Year&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;#91;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;Quarter&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FOR&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;XML&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;PATH&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;&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #0000FF;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;value&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;.&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;varchar(max)&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&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;SET&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;SELECT &#039;&lt;/span&gt; + @Cols + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; &amp;nbsp;FROM #SalesSummary&#039;&lt;/span&gt; &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--print @SQL &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;cb22150&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It produces the transformed result (I show only few first columns):&lt;/p&gt;

&lt;div class=&quot;tables&quot;&gt;
&lt;table border=&quot;1&quot; cellpadding =&quot;6&quot; cellspacing = &quot;4&quot;&gt; 
&lt;caption style=&quot;color:blue;font-weight:bold&quot;&gt;Transformed result&lt;/caption&gt;
&lt;tr&gt;&lt;th&gt;2001-3 Orders&lt;/th&gt;&lt;th&gt;2001-3 Sales&lt;/th&gt;
&lt;th&gt;2001-4 Orders&lt;/th&gt;&lt;th&gt;2001-4 Sales&lt;/th&gt;
&lt;th&gt;2002-1 Orders&lt;/th&gt;&lt;th&gt;2002-1 Sales&lt;/th&gt;
&lt;th&gt;2002-2 Orders&lt;/th&gt;&lt;th&gt;2002-2 Sales&lt;/th&gt;
&lt;/tr&gt;

&lt;tr&gt;&lt;td align=&quot;right&quot;&gt;
621&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;$5,850,932.9483&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;758&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;$8,476,619.278&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;741&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	$7,379,686.3091&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;825&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;$8,210,285.1655&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;You may want to un-comment PRINT @SQL statement if you want to see the generated command. I used XML PATH to concatenate rows into a string based on this blog post by Brad Schulz &lt;a href=&quot;http://bradsruminations.blogspot.com/2009/10/making-list-and-checking-it-twice.html&quot;&gt;Making a list and checking it twice&lt;/a&gt;.&lt;/p&gt;



&lt;p&gt;Another interesting problem was presented in this &lt;a href=&quot;http://social.msdn.microsoft.com/Forums/hu-HU/transactsql/thread/f64ca6b6-9d7b-4863-86fa-591af1ec2b9f&quot;&gt;MSDN Transact-SQL forum thread&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;For the claims table that has many integer columns, transform rows into columns. I demonstrate just the solution with the table creation 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;cb32451&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; tempdb &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;OBJECT_ID&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;Claims&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;U&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NOT NULL &lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DROP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; Claims &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: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; Claims &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;br /&gt;&amp;nbsp; Claim &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; HCPC &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;mod&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;, &lt;br /&gt;&amp;nbsp; charge &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; paid &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; Qty &amp;nbsp; &amp;nbsp;&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; Claims &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;12345&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;99245&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;90&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt; ALL &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;12345&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;99112&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NULL, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;30&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt; ALL &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;12345&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;99111&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;80&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;50&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;25&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt; ALL &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;11112&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;99911&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;60&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;50&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt; ALL &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;12222&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;99454&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NULL, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;50&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #000;&quot;&gt;20&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&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;SELECT&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Claims&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb61802&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The output is:&lt;/p&gt;
&lt;center&gt;
&lt;div class=&quot;tables&quot;&gt;
&lt;table border=&quot;1&quot; cellpadding =&quot;5&quot; cellspacing = &quot;5&quot;&gt; 
&lt;caption style=&quot;color:blue;font-weight:bold&quot;&gt;Claims table&lt;/caption&gt;
&lt;tr&gt;&lt;th&gt;Claim&lt;/th&gt;&lt;th&gt;HCPC&lt;/th&gt;
&lt;th&gt;mod&lt;/th&gt;&lt;th&gt;Charge&lt;/th&gt;&lt;th&gt;Paid&lt;/th&gt;&lt;th&gt;Qty&lt;/th&gt;&lt;/tr&gt;

&lt;tr&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;12345&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	99245&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	90&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	20&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	10&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;12345&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	99112&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	30&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	20&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;12345&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	99111&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	80&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	50&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	25&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	2&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;11112&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	99911&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	60&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	50&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	20&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;12222&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	99454&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	50&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	20&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;	1&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;&lt;/center&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;And the following code transforms the result (as you see, I am using a loop and ROW_NUMBER() approach):&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;cb80223&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; &amp;nbsp;@&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; &amp;nbsp; &amp;nbsp; &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: #FF00FF;&quot;&gt;MAX&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; &amp;nbsp;@&lt;span style=&quot;color: #0000FF;&quot;&gt;Loop&lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;@MaxRows &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&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;Sql&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt; &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @MaxRows = &lt;span style=&quot;color: #FF00FF;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;MaxRow&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; &amp;nbsp; &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; &amp;nbsp; &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: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; MaxRow, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Claim &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; &amp;nbsp; Claims &lt;br /&gt;&amp;nbsp; &amp;nbsp; &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; Claim&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; X &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;Loop&lt;/span&gt; = &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: #0000FF;&quot;&gt;Loop&lt;/span&gt; &amp;lt;= @MaxRows &lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;BEGIN&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;SQL&lt;/span&gt; = @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;, &amp;nbsp; &amp;nbsp; SUM(CASE WHEN Row = &#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;@&lt;span style=&quot;color: #0000FF;&quot;&gt;Loop&lt;/span&gt; &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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&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: #FF0000;&quot;&gt;&#039; THEN &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;QUOTENAME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Column_Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; END) AS [&#039;&lt;/span&gt; + COLUMN_NAME + &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;Loop&lt;/span&gt; &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;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&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: #FF0000;&quot;&gt;&#039;]&#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; &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; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &amp;nbsp;TABLE_Name = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Claims&#039;&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AND COLUMN_NAME NOT IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Row&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Claim&#039;&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;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;Loop&lt;/span&gt; = @&lt;span style=&quot;color: #0000FF;&quot;&gt;Loop&lt;/span&gt; + &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &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: #00AF00;&quot;&gt;--PRINT @SQL &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;SELECT Claim&#039;&lt;/span&gt; + @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; FROM (select *, &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;row_number() over (partition by Claim ORDER BY Claim) as Row &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FROM Claims) X GROUP BY Claim &#039;&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: #0000FF;&quot;&gt;SQL&lt;/span&gt; &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;SQL&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;cb63198&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;tables&quot;&gt;
&lt;table border=&quot;1&quot; cellpadding =&quot;5&quot; cellspacing = &quot;5&quot;&gt; 
&lt;caption style=&quot;color:blue;font-weight:bold&quot;&gt;Transformed Output&lt;/caption&gt;
&lt;tr&gt;&lt;th&gt;Claim&lt;/th&gt;
&lt;th&gt;HCPC1&lt;/th&gt;&lt;th&gt;mod1&lt;/th&gt;&lt;th&gt;Charge1&lt;/th&gt;&lt;th&gt;Paid1&lt;/th&gt;&lt;th&gt;Qty1&lt;/th&gt;
&lt;th&gt;HCPC2&lt;/th&gt;&lt;th&gt;mod2&lt;/th&gt;&lt;th&gt;Charge2&lt;/th&gt;&lt;th&gt;Paid2&lt;/th&gt;&lt;th&gt;Qty2&lt;/th&gt;
&lt;th&gt;HCPC3&lt;/th&gt;&lt;th&gt;mod3&lt;/th&gt;&lt;th&gt;Charge3&lt;/th&gt;&lt;th&gt;Paid3&lt;/th&gt;&lt;th&gt;Qty3&lt;/th&gt;
&lt;/tr&gt;

&lt;tr&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;11112&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 10%&quot;&gt;	99911&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 10%&quot;&gt;	60&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 10%&quot;&gt;	50&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 10%&quot;&gt;	20&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	1&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL	&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;12222&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	99454&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	50&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	20&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	1	&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;12345&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	99245&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	90&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	20&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	10&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	1	&lt;/td&gt;&lt;td align=&quot;right&quot; style=&quot;width: 20%&quot;&gt;99112&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	NULL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	30&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	20&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	1&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	99111&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	80&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	50&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	25&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;	2&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I&#039;ll post another recent problem found in the following MSDN thread &lt;a href=&quot;http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/89d5954f-d518-4eb4-8c44-c124c6e1ee6d&quot;&gt;Basic Crosstab Query&lt;/a&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;cb50501&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&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;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; tempdb;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&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; #T &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;StoreID &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;5&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL,&lt;br /&gt;CriteriaNo &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Result&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL, &lt;br /&gt;Position &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; NOT NULL,&lt;br /&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;#40;&lt;/span&gt;StoreID, CriteriaNo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&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;GO&lt;/span&gt;&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; #T&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;StoreID, CriteriaNo, &lt;span style=&quot;color: #0000FF;&quot;&gt;Result&lt;/span&gt;, Position&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;0001&#039;&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;9&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt; ALL &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;0002&#039;&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;9&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;12&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt; ALL &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;0001&#039;&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;5&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt; ALL&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;0002&#039;&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;6&lt;/span&gt;, &lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;-- dynamic&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;sql&lt;/span&gt; &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: #FF00FF;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, @Cols &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: #FF00FF;&quot;&gt;max&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;SELECT&lt;/span&gt; @Cols = &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: #FF0000;&quot;&gt;&#039;, &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;MAX(case when CriteriaNo = &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&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;, CriteriaNo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; then Result else 0 end) AS CriteriaNo&#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&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;, CriteriaNo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;, MAX(case when CriteriaNo = &#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&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;, CriteriaNo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; then Position else 0 end) AS CriteriaPosition&#039;&lt;/span&gt; + &lt;span style=&quot;color: #FF00FF;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&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;, CriteriaNo&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; &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: #0000FF;&quot;&gt;distinct&lt;/span&gt; CriteriaNo &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; #T&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; X &lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;By&lt;/span&gt; CriteriaNo &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FOR&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;XML&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;PATH&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;&#039;&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: #0000FF;&quot;&gt;SET&lt;/span&gt; @&lt;span style=&quot;color: #0000FF;&quot;&gt;sql&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;SELECT StoreID&#039;&lt;/span&gt; + @Cols + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;, SUM(Result) as PerformancePrint&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;FROM #T&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;GROUP BY StoreID&#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXECUTE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@&lt;span style=&quot;color: #0000FF;&quot;&gt;sql&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;cb7690&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;*** &lt;strong&gt;Remember, if you have a SQL related question, try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&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/dynamic-pivot-on-multiple-columns&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>In this blog post I want to discuss a problem I encounter frequently in SQL related forums - how to perform dynamic PIVOT involving multiple columns using pure T-SQL solution. In SQL Server Reporting Services this functionality can be easily achieved using <a href="http://msdn.microsoft.com/en-us/library/ms157334(SQL.100).aspx">Matrix template</a>.</p>

<p>With the introduction of PIVOT operator in SQL 2005, it became very easy to write queries that transform rows into columns. There are numerous articles on the Web as how to perform PIVOT and even dynamic PIVOT. However, most of these articles explain how to perform such queries for just one column. I want to expand the transformation for multiple columns.</p>

<p>The idea of a solution is quite simple - you need to generate the SQL dynamically. Since we want to use pivot on multiple columns, we need to use <a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/understanding-sql-server-2000-pivot">CASE based pivot</a>.</p>

<p>You need to keep in mind, that resulting query should have less than 1024 columns. You may build the check for number of columns into the query.</p>

<p>Now, every time I need to create a dynamic query, I need to know what is the final query I am going to arrive to. Having the idea in mind and printing the SQL command until I got it right helps to create the working query.</p>

<p>Let's consider the first example based on the AdventureWorks SalesOrderHeader table.<br />
This code creates the table we're going to transform - it gives us summary of orders and total due per each quarter:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb60686'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb60686','cb41094'); 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="cb60686" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">USE</span> AdventureWorks </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> &nbsp; <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">quarter</span>,OrderDate<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Quarter</span><span style="color: #808080;">&#93;</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">YEAR</span>,OrderDate<span style="color: #808080;">&#41;</span> &nbsp; &nbsp;<span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Year</span><span style="color: #808080;">&#93;</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span>SalesOrderID<span style="color: #808080;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Orders <span style="color: #FF00FF;">Count</span><span style="color: #808080;">&#93;</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>TotalDue<span style="color: #808080;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Total Due<span style="color: #808080;">&#93;</span> </li><li style="" class="li1"><span style="color: #0000FF;">INTO</span> &nbsp; &nbsp; #SalesSummary </li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> &nbsp; &nbsp; Sales.<span style="color: #202020;">SalesOrderHeader</span> </li><li style="" class="li1"><span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">quarter</span>,OrderDate<span style="color: #808080;">&#41;</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">YEAR</span>,OrderDate<span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> &nbsp; * </li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> &nbsp; &nbsp; #SalesSummary </li><li style="" class="li2"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Year</span><span style="color: #808080;">&#93;</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Quarter</span><span style="color: #808080;">&#93;</span></li></ol></div><div id="cb41094" style="display: none; color: red;"></div></div></div>

<p>The #SalesSummary table lists count of orders and total due for each quarter and year.</p>

<center>
<div class="tables">
<table border="1" cellpadding ="5" cellspacing = "5"> 
<caption style="color:blue;font-weight:bold">Quarterly Orders Summary</caption>
<tr><th>Quarter</th><th>Year</th>
<th>Orders Count</th><th>Total Due</th></tr>


<tr><td align="right" style="width: 20%">3</td><td align="right" style="width: 20%">2001</td><td align="right" style="width: 20%">	621</td><td align="right" style="width: 20%">	$5,850,932.9483</td></tr>
<tr><td align="right">4</td><td align="right">	2001</td><td align="right">	758</td><td align="right">	$8,476,619.278</td></tr>
<tr><td align="right">1</td><td align="right">	2002</td><td align="right">	741</td><td align="right">	$7,379,686.3091</td></tr>
<tr><td align="right">2</td><td align="right">	2002</td><td align="right">	825</td><td align="right">	$8,210,285.1655</td></tr>
<tr><td align="right">3</td><td align="right">	2002</td><td align="right">	1054</td><td align="right">	$13,458,206.13</td></tr>
<tr><td align="right">4</td><td align="right">	2002</td><td align="right">	1072</td><td align="right">	$10,827,327.4904</td></tr>
<tr><td align="right">1</td><td align="right">	2003</td><td align="right">	1091</td><td align="right">	$8,550,831.8702</td></tr>
<tr><td align="right">2</td><td align="right">	2003</td><td align="right">	1260</td><td align="right">	$10,749,269.374</td></tr>
<tr><td align="right">3</td><td align="right">	2003</td><td align="right">	4152</td><td align="right">	$18,220,131.5285</td></tr>
<tr><td align="right">4</td><td align="right">	2003</td><td align="right">	5940</td><td align="right">	$16,787,382.3141</td></tr>
<tr><td align="right">1</td><td align="right">	2004</td><td align="right">	6087</td><td align="right">	$14,170,982.5455</td></tr>
<tr><td align="right">2</td><td align="right">	2004</td><td align="right">	6888</td><td align="right">	$17,969,750.9487</td></tr>
<tr><td align="right">3</td><td align="right">	2004</td><td align="right">	976</td><td align="right">	$56,178.9223</td></tr>
</table></div></center><p></p>

<p>Now, suppose we want to see these results horizontally. The following dynamic SQL produces the desired output:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb56998'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb56998','cb85018'); 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="cb56998" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> &nbsp;@<span style="color: #0000FF;">SQL</span> &nbsp;<span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Cols <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @Cols = <span style="color: #FF00FF;">STUFF</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> <span style="color: #FF0000;">', </span></li><li style="" class="li1"><span style="color: #FF0000;">SUM(CASE WHEN [Quarter]='</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Quarter</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' AND [Year] = '</span> + </li><li style="" class="li2"><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Year</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + </li><li style="" class="li1"><span style="color: #FF0000;">' THEN [Orders Count] ELSE 0 END) AS ['</span> + </li><li style="" class="li2"><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Year</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'-'</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Quarter</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + </li><li style="" class="li1"><span style="color: #FF0000;">' Orders], </span></li><li style="" class="li2"><span style="color: #FF0000;">SUM(CASE WHEN [Quarter]='</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Quarter</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + </li><li style="" class="li1"><span style="color: #FF0000;">' AND [Year] = '</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Year</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + </li><li style="" class="li2"><span style="color: #FF0000;">' THEN [Total Due] ELSE 0 END) AS ['</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Year</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">'-'</span> + </li><li style="" class="li1"><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Quarter</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">as</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' Sales]'</span></li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> #SalesSummary </li><li style="" class="li1"><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Year</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">Quarter</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FOR</span> <span style="color: #0000FF;">XML</span> <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>,<span style="color: #0000FF;">type</span><span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>,<span style="color: #FF0000;">'varchar(max)'</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">1</span>,<span style="color: #000;">2</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF0000;">'SELECT '</span> + @Cols + <span style="color: #FF0000;">' &nbsp;FROM #SalesSummary'</span> </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">--print @SQL </span></li><li style="" class="li2"><span style="color: #0000FF;">EXECUTE</span><span style="color: #808080;">&#40;</span> @<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb85018" style="display: none; color: red;"></div></div></div>

<p>It produces the transformed result (I show only few first columns):</p>

<div class="tables">
<table border="1" cellpadding ="6" cellspacing = "4"> 
<caption style="color:blue;font-weight:bold">Transformed result</caption>
<tr><th>2001-3 Orders</th><th>2001-3 Sales</th>
<th>2001-4 Orders</th><th>2001-4 Sales</th>
<th>2002-1 Orders</th><th>2002-1 Sales</th>
<th>2002-2 Orders</th><th>2002-2 Sales</th>
</tr>

<tr><td align="right">
621</td><td align="right">$5,850,932.9483</td><td align="right">758</td><td align="right">$8,476,619.278</td><td align="right">741</td><td align="right">	$7,379,686.3091</td><td align="right">825</td><td align="right">$8,210,285.1655</td></tr>
</table></div>
<p>You may want to un-comment PRINT @SQL statement if you want to see the generated command. I used XML PATH to concatenate rows into a string based on this blog post by Brad Schulz <a href="http://bradsruminations.blogspot.com/2009/10/making-list-and-checking-it-twice.html">Making a list and checking it twice</a>.</p>



<p>Another interesting problem was presented in this <a href="http://social.msdn.microsoft.com/Forums/hu-HU/transactsql/thread/f64ca6b6-9d7b-4863-86fa-591af1ec2b9f">MSDN Transact-SQL forum thread</a>:</p>

<p>For the claims table that has many integer columns, transform rows into columns. I demonstrate just the solution with the table creation script:</p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb59772'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb59772','cb2483'); 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="cb59772" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">USE</span> tempdb </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Claims'</span>,<span style="color: #FF0000;">'U'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> NOT NULL </li><li style="" class="li2">&nbsp; <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> Claims </li><li style="" class="li1">&nbsp;</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;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Claims <span style="color: #808080;">&#40;</span> </li><li style="" class="li1">&nbsp; Claim &nbsp;<span style="color: #0000FF;">INT</span>, </li><li style="" class="li2">&nbsp; HCPC &nbsp; <span style="color: #0000FF;">INT</span>, </li><li style="" class="li1">&nbsp; <span style="color: #808080;">&#91;</span>mod<span style="color: #808080;">&#93;</span> &nbsp;<span style="color: #0000FF;">INT</span>, </li><li style="" class="li2">&nbsp; charge <span style="color: #0000FF;">INT</span>, </li><li style="" class="li1">&nbsp; paid &nbsp; <span style="color: #0000FF;">INT</span>, </li><li style="" class="li2">&nbsp; Qty &nbsp; &nbsp;<span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> Claims </li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">12345</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">99245</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">90</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">20</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">10</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">1</span> </li><li style="" class="li1"><span style="color: #0000FF;">UNION</span> ALL </li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">12345</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">99112</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;NULL, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">30</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">20</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">1</span> </li><li style="" class="li2"><span style="color: #0000FF;">UNION</span> ALL </li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">12345</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">99111</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">80</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">50</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">25</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">2</span> </li><li style="" class="li1"><span style="color: #0000FF;">UNION</span> ALL </li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">11112</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">99911</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">60</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">50</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">20</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">1</span> </li><li style="" class="li2"><span style="color: #0000FF;">UNION</span> ALL </li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">12222</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">99454</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;NULL, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">50</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">20</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000;">1</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</span> Claims</li></ol></div><div id="cb2483" style="display: none; color: red;"></div></div></div>

<p>The output is:</p>
<center>
<div class="tables">
<table border="1" cellpadding ="5" cellspacing = "5"> 
<caption style="color:blue;font-weight:bold">Claims table</caption>
<tr><th>Claim</th><th>HCPC</th>
<th>mod</th><th>Charge</th><th>Paid</th><th>Qty</th></tr>

<tr><td align="right" style="width: 20%">12345</td><td align="right" style="width: 20%">	99245</td><td align="right" style="width: 20%">	90</td><td align="right" style="width: 20%">	20</td><td align="right" style="width: 20%">	10</td><td align="right" style="width: 20%">	1</td></tr>
<tr><td align="right" style="width: 20%">12345</td><td align="right" style="width: 20%">	99112</td><td align="right" style="width: 20%">	NULL</td><td align="right" style="width: 20%">	30</td><td align="right" style="width: 20%">	20</td><td align="right" style="width: 20%">	1</td></tr>
<tr><td align="right" style="width: 20%">12345</td><td align="right" style="width: 20%">	99111</td><td align="right" style="width: 20%">	80</td><td align="right" style="width: 20%">	50</td><td align="right" style="width: 20%">	25</td><td align="right" style="width: 20%">	2</td></tr>
<tr><td align="right" style="width: 20%">11112</td><td align="right" style="width: 20%">	99911</td><td align="right" style="width: 20%">	60</td><td align="right" style="width: 20%">	50</td><td align="right" style="width: 20%">	20</td><td align="right" style="width: 20%">	1</td></tr>
<tr><td align="right" style="width: 20%">12222</td><td align="right" style="width: 20%">	99454</td><td align="right" style="width: 20%">	NULL</td><td align="right" style="width: 20%">	50</td><td align="right" style="width: 20%">	20</td><td align="right" style="width: 20%">	1</td></tr>
</table></div></center><p></p>

<p>And the following code transforms the result (as you see, I am using a loop and ROW_NUMBER() approach):</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb51049'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb51049','cb73582'); 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="cb51049" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> &nbsp;@<span style="color: #0000FF;">SQL</span> &nbsp; &nbsp; <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@<span style="color: #0000FF;">Loop</span> &nbsp; &nbsp;<span style="color: #0000FF;">INT</span>, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@MaxRows <span style="color: #0000FF;">INT</span> </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">Sql</span> = <span style="color: #FF0000;">''</span> </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> @MaxRows = <span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span>MaxRow<span style="color: #808080;">&#41;</span> </li><li style="" class="li2"><span style="color: #0000FF;">FROM</span> &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> &nbsp; <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span>* <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> MaxRow, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Claim </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> &nbsp; &nbsp; Claims </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> Claim<span style="color: #808080;">&#41;</span> X </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">Loop</span> = <span style="color: #000;">1</span> </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">WHILE</span> @<span style="color: #0000FF;">Loop</span> &lt;= @MaxRows </li><li style="" class="li2">&nbsp; <span style="color: #0000FF;">BEGIN</span> </li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">SQL</span> = @<span style="color: #0000FF;">SQL</span> + <span style="color: #FF0000;">', &nbsp; &nbsp; SUM(CASE WHEN Row = '</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">Loop</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' THEN '</span> + <span style="color: #FF00FF;">QUOTENAME</span><span style="color: #808080;">&#40;</span>Column_Name<span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' END) AS ['</span> + COLUMN_NAME + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">Loop</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">']'</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> &nbsp; <span style="color: #00AF00;">INFORMATION_SCHEMA</span>.<span style="color: #00AF00;">COLUMNS</span> </li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp;TABLE_Name = <span style="color: #FF0000;">'Claims'</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND COLUMN_NAME NOT IN <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Row'</span>,<span style="color: #FF0000;">'Claim'</span><span style="color: #808080;">&#41;</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">Loop</span> = @<span style="color: #0000FF;">Loop</span> + <span style="color: #000;">1</span> </li><li style="" class="li1">&nbsp; <span style="color: #0000FF;">END</span> </li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">--PRINT @SQL </span></li><li style="" class="li2"><span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF0000;">'SELECT Claim'</span> + @<span style="color: #0000FF;">SQL</span> + <span style="color: #FF0000;">' FROM (select *, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;row_number() over (partition by Claim ORDER BY Claim) as Row &nbsp; &nbsp; &nbsp; &nbsp; FROM Claims) X GROUP BY Claim '</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">PRINT</span> @<span style="color: #0000FF;">SQL</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">EXECUTE</span><span style="color: #808080;">&#40;</span> @<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb73582" style="display: none; color: red;"></div></div></div>

<div class="tables">
<table border="1" cellpadding ="5" cellspacing = "5"> 
<caption style="color:blue;font-weight:bold">Transformed Output</caption>
<tr><th>Claim</th>
<th>HCPC1</th><th>mod1</th><th>Charge1</th><th>Paid1</th><th>Qty1</th>
<th>HCPC2</th><th>mod2</th><th>Charge2</th><th>Paid2</th><th>Qty2</th>
<th>HCPC3</th><th>mod3</th><th>Charge3</th><th>Paid3</th><th>Qty3</th>
</tr>

<tr><td align="right" style="width: 20%">11112</td><td align="right" style="width: 10%">	99911</td><td align="right" style="width: 10%">	60</td><td align="right" style="width: 10%">	50</td><td align="right" style="width: 10%">	20</td><td align="right">	1</td><td align="right" style="width: 20%">NULL</td><td align="right">	NULL</td><td align="right">	NULL</td><td align="right">	NULL</td><td align="right">	NULL</td><td align="right">	NULL	</td><td align="right" style="width: 20%">NULL</td><td align="right">	NULL</td><td align="right">	NULL</td><td align="right">	NULL</td></tr>

<tr><td align="right" style="width: 20%">12222</td><td align="right">	99454</td><td align="right">	NULL</td><td align="right">	50</td><td align="right">	20</td><td align="right">	1	</td><td align="right" style="width: 20%">NULL</td><td align="right">	NULL</td><td align="right">	NULL</td><td align="right">	NULL</td><td align="right">	NULL</td><td align="right">	NULL</td><td align="right">	NULL</td><td align="right">	NULL</td><td align="right">	NULL</td><td align="right">	NULL</td></tr>

<tr><td align="right" style="width: 20%">12345</td><td align="right">	99245</td><td align="right">	90</td><td align="right">	20</td><td align="right">	10</td><td align="right">	1	</td><td align="right" style="width: 20%">99112</td><td align="right">	NULL</td><td align="right">	30</td><td align="right">	20</td><td align="right">	1</td><td align="right">	99111</td><td align="right">	80</td><td align="right">	50</td><td align="right">	25</td><td align="right">	2</td></tr>
</table></div>

<p>I'll post another recent problem found in the following MSDN thread <a href="http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/89d5954f-d518-4eb4-8c44-c124c6e1ee6d">Basic Crosstab Query</a></p>

<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb78302'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb78302','cb33168'); 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="cb78302" style="display: block; color: rgb(0, 0, 0);"><ol><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;">GO</span></li><li style="" class="li1"><span style="color: #0000FF;">USE</span> tempdb;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #T <span style="color: #808080;">&#40;</span></li><li style="" class="li2">StoreID <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">5</span><span style="color: #808080;">&#41;</span> NOT NULL,</li><li style="" class="li1">CriteriaNo <span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li2"><span style="color: #0000FF;">Result</span> <span style="color: #0000FF;">int</span> NOT NULL, </li><li style="" class="li1">Position <span style="color: #0000FF;">int</span> NOT NULL,</li><li style="" class="li2"><span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #808080;">&#40;</span>StoreID, CriteriaNo<span style="color: #808080;">&#41;</span></li><li style="" class="li1"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #T<span style="color: #808080;">&#40;</span>StoreID, CriteriaNo, <span style="color: #0000FF;">Result</span>, Position<span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'0001'</span>, <span style="color: #000;">9</span>, <span style="color: #000;">10</span>, <span style="color: #000;">1</span> <span style="color: #0000FF;">UNION</span> ALL </li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'0002'</span>, <span style="color: #000;">9</span>, <span style="color: #000;">12</span>, <span style="color: #000;">2</span> <span style="color: #0000FF;">UNION</span> ALL </li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'0001'</span>, <span style="color: #000;">10</span>, <span style="color: #000;">5</span>, <span style="color: #000;">1</span> <span style="color: #0000FF;">UNION</span> ALL</li><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'0002'</span>, <span style="color: #000;">10</span>, <span style="color: #000;">6</span>, <span style="color: #000;">2</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1"><span style="color: #00AF00;">-- dynamic</span></li><li style="" class="li2"><span style="color: #0000FF;">DECLARE</span> @<span style="color: #0000FF;">sql</span> <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span>, @Cols <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @Cols = <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> <span style="color: #FF0000;">', '</span> + <span style="color: #FF0000;">'MAX(case when CriteriaNo = '</span> + <span style="color: #FF00FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>, CriteriaNo<span style="color: #808080;">&#41;</span> + </li><li style="" class="li1"><span style="color: #FF0000;">' then Result else 0 end) AS CriteriaNo'</span> + <span style="color: #FF00FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>, CriteriaNo<span style="color: #808080;">&#41;</span> + </li><li style="" class="li2"><span style="color: #FF0000;">', MAX(case when CriteriaNo = '</span> + <span style="color: #FF00FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>, CriteriaNo<span style="color: #808080;">&#41;</span> + </li><li style="" class="li1"><span style="color: #FF0000;">' then Position else 0 end) AS CriteriaPosition'</span> + <span style="color: #FF00FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>, CriteriaNo<span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">from</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">select</span> <span style="color: #0000FF;">distinct</span> CriteriaNo <span style="color: #0000FF;">from</span> #T<span style="color: #808080;">&#41;</span> X <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">By</span> CriteriaNo </li><li style="" class="li1"><span style="color: #0000FF;">FOR</span> <span style="color: #0000FF;">XML</span> <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</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: #0000FF;">SET</span> @<span style="color: #0000FF;">sql</span> = <span style="color: #FF0000;">'SELECT StoreID'</span> + @Cols + <span style="color: #FF0000;">', SUM(Result) as PerformancePrint</span></li><li style="" class="li2"><span style="color: #FF0000;">FROM #T</span></li><li style="" class="li1"><span style="color: #FF0000;">GROUP BY StoreID'</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">EXECUTE</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">sql</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb33168" style="display: none; color: red;"></div></div></div>

<p>*** <strong>Remember, if you have a SQL related question, try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/dynamic-pivot-on-multiple-columns">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/dynamic-pivot-on-multiple-columns#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1277</wfw:commentRss>
		</item>
				<item>
			<title>Get weekly transactions showing Monday's date</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/get-weekly-transactions-showing-monday</link>
			<pubDate>Mon, 11 Apr 2011 11:25:00 +0000</pubDate>			<dc:creator>Naomi Nosonovsky</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>			<guid isPermaLink="false">1190@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;This &lt;a href=&quot;http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/5442bbd7-ef89-47f8-8ba9-eef04b6b5fe4/#aa2f0695-501b-43b2-a559-290f6c50b0c7&quot;&gt;recent MSDN thread&lt;/a&gt; presented an interesting, I believe, question:&lt;/p&gt;

&lt;p&gt;For a predefined set of the Application IDs show the weekly transactions and display Monday&#039;s day for each week. The interesting twist in this common PIVOT query is to display a Monday&#039;s date for a week.&lt;/p&gt;

&lt;p&gt;Rather than showing a solution for that particular thread, I show an AdventureWorks sample query. This query will return top 5 total amounts for each week by Customer (using SalesOrderHeader table).&lt;/p&gt;

&lt;p&gt;Here is the query&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;cb80416&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; &amp;nbsp;@First_Bow &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;DATETIME&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;@Week_Start_Day &lt;span style=&quot;color: #0000FF;&quot;&gt;TINYINT&lt;/span&gt; &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @Week_Start_Day = &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;SELECT&lt;/span&gt; @First_Bow = &lt;span style=&quot;color: #FF00FF;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DATETIME&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;-53690&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;@Week_Start_Day + &lt;span style=&quot;color: #000;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;%&lt;span style=&quot;color: #000;&quot;&gt;7&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: #0000FF;&quot;&gt;WITH&lt;/span&gt; cte &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; SH.&lt;span style=&quot;color: #202020;&quot;&gt;CustomerID&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TotalDue &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; Total, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEADD&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;DAY&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;DAY&lt;/span&gt;,@FIRST_BOW,OrderDate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; * &lt;span style=&quot;color: #000;&quot;&gt;7&lt;/span&gt;, &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; @FIRST_BOW&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: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;MondayDate&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;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt; SH&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;cte2 &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&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;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; Total, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; MondayDate, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &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;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &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; MondayDate &lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; Total &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;, CustomerID&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;Row&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; cte&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;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; cte2 &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PIVOT &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&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;Total&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;FOR&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;Row&lt;/span&gt; IN &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: #000;&quot;&gt;1&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;#91;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;2&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;#91;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;3&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;#91;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;4&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;#91;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;5&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;#91;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;6&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;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; pvt&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb43835&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt; &lt;br /&gt;
The interesting part here is in determining the Monday&#039;s day for each week. I decided to use a solution presented in SQL Server Forums &lt;a href=&quot;http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47307&quot;&gt;topic&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;*** &lt;strong&gt;Remember, if you have a SQL related question, try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&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/get-weekly-transactions-showing-monday&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 <a href="http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/5442bbd7-ef89-47f8-8ba9-eef04b6b5fe4/#aa2f0695-501b-43b2-a559-290f6c50b0c7">recent MSDN thread</a> presented an interesting, I believe, question:</p>

<p>For a predefined set of the Application IDs show the weekly transactions and display Monday's day for each week. The interesting twist in this common PIVOT query is to display a Monday's date for a week.</p>

<p>Rather than showing a solution for that particular thread, I show an AdventureWorks sample query. This query will return top 5 total amounts for each week by Customer (using SalesOrderHeader table).</p>

<p>Here is the query</p><div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb81382'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb81382','cb75693'); 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="cb81382" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> &nbsp;@First_Bow &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">DATETIME</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Week_Start_Day <span style="color: #0000FF;">TINYINT</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SET</span> @Week_Start_Day = <span style="color: #000;">2</span> </li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> @First_Bow = <span style="color: #FF00FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">DATETIME</span>,<span style="color: #000;">-53690</span> + <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>@Week_Start_Day + <span style="color: #000;">5</span><span style="color: #808080;">&#41;</span>%<span style="color: #000;">7</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: #0000FF;">WITH</span> cte </li><li style="" class="li1">&nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> SH.<span style="color: #202020;">CustomerID</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TotalDue &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">AS</span> Total, </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF00FF;">DATEADD</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DAY</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEDIFF</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DAY</span>,@FIRST_BOW,OrderDate<span style="color: #808080;">&#41;</span> / <span style="color: #000;">7</span><span style="color: #808080;">&#41;</span> * <span style="color: #000;">7</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @FIRST_BOW<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>MondayDate<span style="color: #808080;">&#93;</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">FROM</span> &nbsp; Sales.<span style="color: #202020;">SalesOrderHeader</span> SH<span style="color: #808080;">&#41;</span>, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp;cte2 </li><li style="" class="li1">&nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> Total, </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MondayDate, </li><li style="" class="li1">&nbsp; &nbsp; &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> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">OVER</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">PARTITION</span> <span style="color: #0000FF;">BY</span> MondayDate <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Total <span style="color: #0000FF;">DESC</span>, CustomerID<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">Row</span> </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">FROM</span> &nbsp; cte<span style="color: #808080;">&#41;</span> </li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> * </li><li style="" class="li1"><span style="color: #0000FF;">FROM</span> &nbsp; cte2 </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp;PIVOT </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>Total<span style="color: #808080;">&#41;</span> </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FOR</span> <span style="color: #0000FF;">Row</span> IN <span style="color: #808080;">&#40;</span> <span style="color: #808080;">&#91;</span><span style="color: #000;">1</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">2</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">3</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">4</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">5</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">6</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#41;</span> <span style="color: #808080;">&#41;</span> pvt</li></ol></div><div id="cb75693" style="display: none; color: red;"></div></div></div>
<p> <br />
The interesting part here is in determining the Monday's day for each week. I decided to use a solution presented in SQL Server Forums <a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47307">topic</a>.</p>

<p>*** <strong>Remember, if you have a SQL related question, try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/get-weekly-transactions-showing-monday">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/get-weekly-transactions-showing-monday#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1190</wfw:commentRss>
		</item>
			</channel>
</rss>
