<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:thr="http://purl.org/syndication/thread/1.0"><id>tag:blogger.com,1999:blog-9150837343934029699</id><updated>2024-09-16T19:02:22.585-04:00</updated><category term="SQL Server 2008"/><category term="SQL Server 2005"/><category term="T-SQL"/><category term="ASP.NET"/><category term="MOSS 2007"/><category term="ADO.NET"/><category term="LINQ"/><category term="LINQ to SQL"/><category term="AJAX"/><category term="ASMX"/><category term="CRM"/><category term="Dynamics"/><category term="LINQ to XML"/><category term="SQL Server 2000"/><category term="SharePoint Designer 2007"/><category term="Silverlight"/><category term="Silverlight 2.0"/><category term="Tools"/><category term="Trainings"/><category term="VPC"/><category term="Web Services"/><title type="text">Syed A. Rizvi - Developing Web-based Architectures</title><subtitle type="html"/><link href="http://syedarizvi.blogspot.com/feeds/posts/default" rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/" rel="alternate" type="text/html"/><link href="http://pubsubhubbub.appspot.com/" rel="hub"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><generator uri="http://www.blogger.com" version="7.00">Blogger</generator><openSearch:totalResults>13</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><xhtml:meta content="noindex" name="robots" xmlns:xhtml="http://www.w3.org/1999/xhtml"/><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-1361101524438207721</id><published>2010-01-26T11:53:00.007-05:00</published><updated>2010-01-26T12:47:03.996-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AJAX"/><category scheme="http://www.blogger.com/atom/ns#" term="MOSS 2007"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="VPC"/><title type="text">Microsoft Virtual PC 2007 - Performance</title><content type="html">&lt;span style="font-family:georgia;"&gt;Microsoft provides free download for Virtual PC which is a helpful asset when developing applications in team environment as it provides an exclusive environment to develop/debug code without interfering other developers.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Virtual PC environment is big plus for Microsoft SharePoint development which requires regular &lt;span style="color:#ff6600;"&gt;&lt;strong&gt;iisreset&lt;/strong&gt;&lt;/span&gt; command to reflect the &lt;span style="color:#ff6600;"&gt;&lt;strong&gt;GAC&lt;/strong&gt;&lt;/span&gt; changes. This causes other team members to lose productivity if everyone is working on a shared server.&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;For more details on Microsoft Virtual PC visit: &lt;/span&gt;&lt;a href="http://www.microsoft.com/windows/virtual-pc/" target="_blank"&gt;&lt;span style="font-family:georgia;"&gt;Microsoft Virtual PC Overview&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:georgia;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;There are numerous blog postings regarding performance of VPC: &lt;a href="http://www.andrewconnell.com/blog/articles/SqeezePerformanceOutOfVirtualPCs.aspx"&gt;Andrew Connell&lt;/a&gt;, &lt;a href="http://blah.winsmarts.com/2007-2-10_tips_to_help_your_Virtual_PC_performance.aspx"&gt;Sahil Malik&lt;/a&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;After following all these tips my VPC running on Lenovo laptop (host) was still dragging in performance. &lt;/span&gt;&lt;span style="font-family:georgia;"&gt;The VPC was running with Visual Studio 2008 and SQL Server 2005. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;Two things did the trick for me:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;a) Turn off SQL Server service and other non-important ones like Error reporting service, Wireless service, Windows Update Service etc. I was mainly doing AJAX UI development on this VPC and therefore disabling SQL Server service was safe.&lt;br /&gt;&lt;br /&gt;b) I was initially using 1.5GB on VPC as my host machine running on Vista 64 (Intel dual processors) had 4GB of RAM but this did not help in any way regarding performance.&lt;br /&gt;&lt;br /&gt;The trick which worked for me was to set the VPC memory to 1GB, this gave me an immediate performance boost!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2010%2f01%2fmicrosoft-virtual-pc-2007-performance.html" target="_blank"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2010%2f01%2fmicrosoft-virtual-pc-2007-performance.html&amp;amp;bgcolor=336688" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/1361101524438207721/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/1361101524438207721" rel="replies" title="0 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/1361101524438207721" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/1361101524438207721" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2010/01/microsoft-virtual-pc-2007-performance.html" rel="alternate" title="Microsoft Virtual PC 2007 - Performance" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-2002283265708113557</id><published>2009-06-09T12:26:00.006-04:00</published><updated>2009-06-09T13:08:44.916-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CRM"/><category scheme="http://www.blogger.com/atom/ns#" term="Dynamics"/><title type="text">Dynamics - Virtual Machine Num Lock</title><content type="html">&lt;span style="font-family:georgia;"&gt;Microsoft provides free download for Dynamics CRM 4.0 virtual demo machine. Microsoft Dynamics is a business management offering for financial, customer relationship management and supply chain.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;For more details on Microsoft Dynamics visit: &lt;/span&gt;&lt;a href="http://www.microsoft.com/dynamics/overview.mspx" target="_blank"&gt;&lt;span style="font-family:georgia;"&gt;Microsoft Dynamics Overview&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:georgia;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;Demo virtual machine can be downloaded from the &lt;/span&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=dd939ed9-87a5-4c13-b212-a922cc02b469&amp;amp;displaylang=en" target="_blank"&gt;&lt;span style="font-family:georgia;"&gt;following location&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:georgia;"&gt;. &lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;The virtual machine has Num Lock key enabled by default, which requires the user to manually disable everytime VM starts.&lt;br /&gt;&lt;br /&gt;To disable the Num Lock permanently go to the registry [&lt;span style="color:#ff6600;"&gt;regedit&lt;/span&gt;] and navigate to [&lt;span style="color:#ff6600;"&gt;Hkey_Current_User\Control Panel\Keyboard&lt;/span&gt;] and change the [&lt;span style="color:#ff6600;"&gt;InitialKeyboardIndcators&lt;/span&gt;] key to '&lt;strong&gt;0&lt;/strong&gt;'.&lt;br /&gt;&lt;br /&gt;If the above step does not work as often when rebooting machine registry key is reverted back to original state, to fix this the other way is using standard Windows command go to [&lt;span style="color:#ff6600;"&gt;Start\All programs\Accessories\Accessibility\On-Screen Keyboard&lt;/span&gt;] and then un-check the '&lt;strong&gt;nlk&lt;/strong&gt;' key.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2009%2f06%2fdynamics-virtual-machine-num-lock.html" target="_blank"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2009%2f06%2fdynamics-virtual-machine-num-lock.html&amp;amp;bgcolor=336688" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/2002283265708113557/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/2002283265708113557" rel="replies" title="0 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/2002283265708113557" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/2002283265708113557" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2009/06/dynamics-virtual-machine-num-lock.html" rel="alternate" title="Dynamics - Virtual Machine Num Lock" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-5912066732408550925</id><published>2009-05-25T19:44:00.017-04:00</published><updated>2009-06-01T23:00:39.553-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="MOSS 2007"/><category scheme="http://www.blogger.com/atom/ns#" term="SharePoint Designer 2007"/><category scheme="http://www.blogger.com/atom/ns#" term="Tools"/><category scheme="http://www.blogger.com/atom/ns#" term="Trainings"/><title type="text">Free SharePoint Designer 2007 Training</title><content type="html">&lt;span style="font-family:georgia;"&gt;Microsoft made SharePoint Designer 2007 a free download as of April 1, 2009. SharePoint Designer 2007 is based on Microsoft FrontPage 2003 with more emphasis towards SharePoint enabled web-sites.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;For more details on SharePoint Designer 2007 visit: &lt;/span&gt;&lt;a href="http://office.microsoft.com/en-us/sharepointdesigner/HA101656311033.aspx" target="_blank"&gt;&lt;span style="font-family:georgia;"&gt;SharePoint Designer 2007 Overview&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:georgia;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;Microsoft is offering free downloadable training on SharePoint Designer 2007, you can download the training from the &lt;/span&gt;&lt;a href="http://office.microsoft.com/en-us/sharepointdesigner/HA102632321033.aspx" target="_blank"&gt;&lt;span style="font-family:georgia;"&gt;following location&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:georgia;"&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2009%2f05%2fsharepoint-designer-2007-training-free.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2009%2f05%2fsharepoint-designer-2007-training-free.html&amp;bgcolor=336688" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/5912066732408550925/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/5912066732408550925" rel="replies" title="0 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/5912066732408550925" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/5912066732408550925" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2009/05/sharepoint-designer-2007-training-free.html" rel="alternate" title="Free SharePoint Designer 2007 Training" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-3341344083845694871</id><published>2008-11-02T10:33:00.103-05:00</published><updated>2008-11-02T21:16:08.188-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ASMX"/><category scheme="http://www.blogger.com/atom/ns#" term="LINQ"/><category scheme="http://www.blogger.com/atom/ns#" term="LINQ to SQL"/><category scheme="http://www.blogger.com/atom/ns#" term="LINQ to XML"/><category scheme="http://www.blogger.com/atom/ns#" term="Silverlight"/><category scheme="http://www.blogger.com/atom/ns#" term="Silverlight 2.0"/><category scheme="http://www.blogger.com/atom/ns#" term="Web Services"/><title type="text">Silverlight 2.0 - Using Silverlight DataGrid to consume ASMX Web Service</title><content type="html">&lt;span style=";font-family:georgia;font-size:100%;"  &gt;Microsoft released Silverlight 2.0 on October 13, 2008.  Silverlight 2 provides cross-browser rich UI experience which developers can use to author media rich applications using .NET language (C#, Visual Basic etc) of their choice.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;For more details on setting up your Visual Studio 2008 development environment and Silverlight 2.0 visit: &lt;a target="_blank" href="http://silverlight.net/GetStarted/"&gt;Silverlight web-site&lt;/a&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt; &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;The example uses NorthWind database. You can download and install NorthWind database from the &lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&amp;amp;displaylang=en"&gt;following location&lt;/a&gt;.&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="line-height: 115%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-size:100%;"&gt;The article will demonstrate working of &lt;span style="color: rgb(255, 102, 0);"&gt;Silverlight 2.0&lt;/span&gt; enabled UI by creating an ASP.NET web-form with &lt;span style="color: rgb(255, 102, 0);"&gt;Silverlight DataGrid&lt;/span&gt; control&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt; consuming &lt;span style="color: rgb(255, 102, 0);"&gt;ASMX Web Service&lt;/span&gt;.&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;Silverlight DataGrid control allows structured data to be displayed without requiring any major programming effort, some of the rich UI features like column resizing, column reordering, frozen columns, sorting for data which supports &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 51); font-weight: bold;font-family:georgia;font-size:100%;"  &gt;ILIST&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt; interface are provided Out-of-the box.&lt;/span&gt;  &lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;br /&gt;Let us start with our example, we are going to build an ASMX Web Service which will extract data from NorthWind database using LINQ to SQL (&lt;/span&gt;&lt;a style="font-family: georgia;" target="_blank" href="http://syedarizvi.blogspot.com/2008/10/aspnetlinq-to-sql-building-ui-layer.html"&gt;see my earlier post&lt;/a&gt;&lt;span style="font-family:georgia;"&gt;), later we will build Silverlight client with DataGrid which will consume this Web Service using LINQ to XML for parsing the Web Service response.&lt;/span&gt; &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="line-height: 115%;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ASMX Web Service Setup:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="font-weight: bold;"&gt;a.&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;Use O/R designer to map the Database, Tables and Stored procedures, I have added 'Ten_Most_Expensive_Products' Stored procedure  to the designer, this will be exposed as a Web Method by the ASMX Web Service:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj32IN9EhjmynGO0TMLGUD5za-yCLkv2uP4h0TVwQSHKE8FekgYAAnjWuwb-uPL_ppcOHDEumwqgxeV0KuzzsFIDs66hxQ5DPENzwv3lX78oi0bwIO2raUW-tKmsn3KhyphenhyphenKvCt3cFZFohiI/s1600-h/WS1.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 139px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj32IN9EhjmynGO0TMLGUD5za-yCLkv2uP4h0TVwQSHKE8FekgYAAnjWuwb-uPL_ppcOHDEumwqgxeV0KuzzsFIDs66hxQ5DPENzwv3lX78oi0bwIO2raUW-tKmsn3KhyphenhyphenKvCt3cFZFohiI/s400/WS1.jpg" alt="" id="BLOGGER_PHOTO_ID_5264217475096535218" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;b.&lt;/span&gt;&lt;span style="font-family:georgia;"&gt; Web Method which exposes Stored procedure to be consumed by Silverlight client application&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Web;&lt;br /&gt;using System.Web.Services;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Xml.Linq;&lt;br /&gt;&lt;br /&gt;namespace NorthWindWS&lt;br /&gt;{&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Summary description for Service1&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;[WebService(Namespace = "http://tempuri.org/")]&lt;br /&gt;[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]&lt;br /&gt;[System.ComponentModel.ToolboxItem(false)]&lt;br /&gt;// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.&lt;br /&gt;// [System.Web.Script.Services.ScriptService]&lt;br /&gt;public class DBWebService : System.Web.Services.WebService&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;[WebMethod]&lt;br /&gt;public string GetTenMostExpensiveProducts()&lt;br /&gt;{&lt;br /&gt;//use DataContext&lt;br /&gt;using (NorthWindDataContext DBContext = new NorthWindDataContext())&lt;br /&gt;{&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;//LINQ to SQL query&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;var Categories = from cat in DBContext.Ten_Most_Expensive_Products()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                      where cat.UnitPrice &amp;gt; 0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                      select cat;        &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;//LINQ to XML query&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;XDocument xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                 new XElement("Categories",&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                               from cat in Categories&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                               select new XElement("Cat",&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                               new XAttribute("ProductName", cat.TenMostExpensiveProducts),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                               new XAttribute("UnitPrice", cat.UnitPrice.ToString()))));&lt;/span&gt;&lt;br /&gt;//return&lt;br /&gt;return xDoc.ToString();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;c.&lt;/span&gt; &lt;/span&gt;Consideration to make Web Service available across Domain Boundaries&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimG3rOo6PPIFeGaR2l5a5hz0uAlsr6Ct_IeGFpa3TuuEM98QZ0MwcpL_fZLGHrBO0YhbjpOdctZ8cFxiK8OkO6KHqCTkQnMB4YyRsw-A9_-9iUNZjsb7p138wmd9S60WSeBSvnlgGDxDM/s1600-h/WS2.jpg"&gt;&lt;img style="cursor: pointer; width: 365px; height: 400px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimG3rOo6PPIFeGaR2l5a5hz0uAlsr6Ct_IeGFpa3TuuEM98QZ0MwcpL_fZLGHrBO0YhbjpOdctZ8cFxiK8OkO6KHqCTkQnMB4YyRsw-A9_-9iUNZjsb7p138wmd9S60WSeBSvnlgGDxDM/s400/WS2.jpg" alt="" id="BLOGGER_PHOTO_ID_5264218996630557874" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;Notice  &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="color: rgb(255, 102, 0);"&gt;clientaccesspolicy.xml &amp;amp; crossdomain.xml&lt;/span&gt; files, these 2 files are required &lt;/span&gt;for Silverlight client application to access Web Service, for more details refer this &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/cc197955%28VS.95%29.aspx"&gt;article&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="line-height: 115%;"&gt;&lt;span style="font-weight: bold;"&gt;Silverlight Client Application:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;"&gt;a. &lt;/span&gt;Create New project -&gt; Visual C# -&gt; Silverlight&lt;/span&gt; -&gt; Silverlight Application&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijAw0rg9pd8iib9hP3Y2mLNJSan6jzncPDCOGq9MorzTw7wvlF3tlWHMOXsq4J8B7LHjRigst9XHTE9adL-EEs9m7-PA_LhW75f7PwtWpnS0W29NvgdwL1y_Jy0Rt6wXgMiX4BzQ6HG0M/s1600-h/silver1.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 272px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijAw0rg9pd8iib9hP3Y2mLNJSan6jzncPDCOGq9MorzTw7wvlF3tlWHMOXsq4J8B7LHjRigst9XHTE9adL-EEs9m7-PA_LhW75f7PwtWpnS0W29NvgdwL1y_Jy0Rt6wXgMiX4BzQ6HG0M/s400/silver1.jpg" alt="" id="BLOGGER_PHOTO_ID_5264226235605030258" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;"&gt;b. &lt;/span&gt;&lt;span&gt;Choose&lt;/span&gt;&lt;/span&gt; 'Add a new ASP.NET Web project to the solution to host Silverlight', this will give us an ASP.NET application to host and test Silverlight client&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0uZtYpxb8tHZTSh5XjOI57OOJpAeB-JcoLD5hq6Ogy8zVWbuUzqK3PPfycRvo6hPySVOz5tH-BlmEFS6N0a29AFS0hPwHpCrwSGLUFpVwkQLByhIwidsUMCl_iakvwUlhMi2gfwW0Vo4/s1600-h/silver2.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 357px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0uZtYpxb8tHZTSh5XjOI57OOJpAeB-JcoLD5hq6Ogy8zVWbuUzqK3PPfycRvo6hPySVOz5tH-BlmEFS6N0a29AFS0hPwHpCrwSGLUFpVwkQLByhIwidsUMCl_iakvwUlhMi2gfwW0Vo4/s400/silver2.jpg" alt="" id="BLOGGER_PHOTO_ID_5264226844993118114" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;"&gt;c. &lt;/span&gt;&lt;span&gt;ASP.NET Web project added to the solution&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1DJth86RYYh_kS7224s_nH6j-w6rjKb-_8ZrLaq-VG8l5iFueChiDvV2axRStRwQUrCvZF9htfvcbd65MVZsJk-2ACHIw6UFdhvoq4Dw6-2dKakt0lL2M_3y9Ut7rG_LtLhcA3jMcv0A/s1600-h/silver3.jpg"&gt;&lt;img style="cursor: pointer; width: 138px; height: 400px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1DJth86RYYh_kS7224s_nH6j-w6rjKb-_8ZrLaq-VG8l5iFueChiDvV2axRStRwQUrCvZF9htfvcbd65MVZsJk-2ACHIw6UFdhvoq4Dw6-2dKakt0lL2M_3y9Ut7rG_LtLhcA3jMcv0A/s400/silver3.jpg" alt="" id="BLOGGER_PHOTO_ID_5264227226686496098" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;d. &lt;/span&gt;&lt;span&gt;&lt;span style="font-family:georgia;"&gt;Add ASMX reference to the project&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgx1Z8pPsLw-rLb41eAZQ2IgrqD-ldPzyfNdmZapHJq4WrZchaSlDjb6FRG5f-ML9jjo1p6hWPHH6jt6Pt1N8PI0QlZUeNS_QDHIsmTcWLIn0rJKn7X-JggraaMlpPZCowEe6YkskCJr_M/s1600-h/silver4.jpg"&gt;&lt;img style="cursor: pointer; width: 305px; height: 361px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgx1Z8pPsLw-rLb41eAZQ2IgrqD-ldPzyfNdmZapHJq4WrZchaSlDjb6FRG5f-ML9jjo1p6hWPHH6jt6Pt1N8PI0QlZUeNS_QDHIsmTcWLIn0rJKn7X-JggraaMlpPZCowEe6YkskCJr_M/s400/silver4.jpg" alt="" id="BLOGGER_PHOTO_ID_5264227462286488370" border="0" /&gt;&lt;/a&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;&lt;br /&gt;e. &lt;/span&gt;&lt;span&gt;&lt;span style="font-family:georgia;"&gt;Web Service reference added to the DBWebService&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheLB8VVDBNZtCbvqt6Ej1JL9N0nfTQsginxlx3NLHdnaGyE0r6zoQQzVIK53JH93jf6kYsoQCpy9dLe8xpGgIHbLIhNcymUTiphhF1RyXm61RbjXfiqx9YtlTCvnYZEEoFDquCDDeGxTw/s1600-h/silver5.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 327px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheLB8VVDBNZtCbvqt6Ej1JL9N0nfTQsginxlx3NLHdnaGyE0r6zoQQzVIK53JH93jf6kYsoQCpy9dLe8xpGgIHbLIhNcymUTiphhF1RyXm61RbjXfiqx9YtlTCvnYZEEoFDquCDDeGxTw/s400/silver5.jpg" alt="" id="BLOGGER_PHOTO_ID_5264227890150984722" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;f. &lt;/span&gt;&lt;span&gt;&lt;span style="font-family:georgia;"&gt;Add StackPanel, DataGrid &amp;amp; Button controls to the Page.xaml file&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;&amp;lt;UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="MySilver.Page"&lt;br /&gt;xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;br /&gt;xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&lt;br /&gt;Width="800" Height="800"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;StackPanel Background="AliceBlue"  Width="Auto" Height="Auto"&amp;gt;&lt;br /&gt;&amp;lt;data:DataGrid Name="myGrid" AutoGenerateColumns="False" GridLinesVisibility="Horizontal"  HeadersVisibility="Column"&lt;br /&gt;RowBackground="Cornsilk" AlternatingRowBackground="LemonChiffon"&lt;br /&gt;Width="500" Height="250" CanUserReorderColumns="True" CanUserSortColumns="True"&lt;br /&gt;IsReadOnly="True" CanUserResizeColumns="True" Visibility="Collapsed"&lt;br /&gt;&amp;gt;&lt;br /&gt;&amp;lt;data:DataGrid.Columns&amp;gt;&lt;br /&gt;&amp;lt;data:DataGridTextColumn Binding="{Binding ProductName}"&lt;br /&gt;Width="300" Header="Product Name"/&amp;gt;&lt;br /&gt;&amp;lt;data:DataGridTextColumn Binding="{Binding UnitPrice}"&lt;br /&gt;Width="200" Header="Unit Price"/&amp;gt;&lt;br /&gt;&amp;lt;/data:DataGrid.Columns&amp;gt;&lt;br /&gt;&amp;lt;/data:DataGrid&amp;gt;&lt;br /&gt;&amp;lt;TextBlock Text=" "&amp;gt;&amp;lt;/TextBlock&amp;gt;&lt;br /&gt;&amp;lt;Button Name="myButton" Content="Call Web Service" Click="Button_Click" Width="100" Height="25"&amp;gt;&amp;lt;/Button&amp;gt;&lt;br /&gt;&amp;lt;/StackPanel&amp;gt;&lt;br /&gt;&amp;lt;/UserControl&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;g. &lt;/span&gt;&lt;span style="font-family:georgia;"&gt;Wire the&lt;/span&gt;&lt;/span&gt; Button event handler to call the Web Service&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Net;&lt;br /&gt;using System.Windows;&lt;br /&gt;using System.Windows.Controls;&lt;br /&gt;using System.Windows.Documents;&lt;br /&gt;using System.Windows.Input;&lt;br /&gt;using System.Windows.Media;&lt;br /&gt;using System.Windows.Media.Animation;&lt;br /&gt;using System.Windows.Shapes;&lt;br /&gt;using System.Xml.Linq;&lt;br /&gt;&lt;br /&gt;namespace MySilver&lt;br /&gt;{&lt;br /&gt;public partial class Page : UserControl&lt;br /&gt;{&lt;br /&gt;public Page()&lt;br /&gt;{&lt;br /&gt;InitializeComponent();&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void Button_Click(object sender, RoutedEventArgs e)&lt;br /&gt;{&lt;br /&gt;MyDBWebService.DBWebServiceSoapClient myService =  new MySilver.MyDBWebService.DBWebServiceSoapClient();&lt;br /&gt;myService.GetTenMostExpensiveProductsCompleted  += new EventHandler&amp;lt;MySilver.MyDBWebService.GetTenMostExpensiveProductsCompletedEventArgs&amp;gt;(myService_GetTenMostExpensiveProductsCompleted);&lt;br /&gt;myService.GetTenMostExpensiveProductsAsync();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void myService_GetTenMostExpensiveProductsCompleted( object sender, MyDBWebService.GetTenMostExpensiveProductsCompletedEventArgs e )&lt;br /&gt;{&lt;br /&gt;//show data&lt;br /&gt;ShowData(e.Result);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void ShowData(string xmlData)&lt;br /&gt;{&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;XDocument xmlCategories = XDocument.Parse(xmlData);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;       //LINQ to XML query, to extract response from Web Service&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;       var categories          = from cat in xmlCategories.Descendants("Cat")&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                                 where cat.Attribute("ProductName") != null&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                                 select new &lt;span style="font-weight: bold;"&gt;Product&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                                 {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                                    ProductName    = (string)cat.Attribute("ProductName"),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                                    UnitPrice      = (string)cat.Attribute("UnitPrice")&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;                                 };&lt;/span&gt;&lt;br /&gt;//bind to DataGrid&lt;br /&gt;myGrid.Visibility         = Visibility.Visible;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;//attach to DataGrid, ToList() is used to enable DataGrid sorting which needs ILIST interface&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;myGrid.ItemsSource        = categories.ToList();&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;h. &lt;/span&gt;&lt;span style="font-family:georgia;"&gt;Product.cs class which uses&lt;/span&gt;&lt;/span&gt; C# 'Automatic properties' to map the Web Service response&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace MySilver&lt;br /&gt;{&lt;br /&gt;public class Product&lt;br /&gt;{&lt;br /&gt;public string  ProductName { get; set; }&lt;br /&gt;public string  UnitPrice { get; set; }&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="font-weight: bold;"&gt;i.&lt;/span&gt;&lt;span&gt; Silverlight &lt;/span&gt;&lt;span&gt;DataGrid output&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_aeQ6EyzH0zUSSFGavWHI-2ifl9wwq2vGyIaXiptXdkhnfZaRrE2etLiLAveFm8yLojO4HxFPJYAJOJ1LYiDWNQk_yGcnOuZqRG3hpJFh6mdR-KWf3fhcv6sa2Fll-Do1C_W5Mf_a3Xc/s1600-h/Silver6.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 277px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_aeQ6EyzH0zUSSFGavWHI-2ifl9wwq2vGyIaXiptXdkhnfZaRrE2etLiLAveFm8yLojO4HxFPJYAJOJ1LYiDWNQk_yGcnOuZqRG3hpJFh6mdR-KWf3fhcv6sa2Fll-Do1C_W5Mf_a3Xc/s400/Silver6.jpg" alt="" id="BLOGGER_PHOTO_ID_5264249285997555426" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;Consideration:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Silverlight is a Client-side technology, when calling ASMX Web Service make sure it is invoked 'Asynchronously' and not 'Synchronously'. This is the reason 'ShowData' method &lt;span style="font-style: italic;"&gt;(method which binds DataGrid to the Web Service response)&lt;/span&gt; is called from 'Completed' event handler.&lt;br /&gt;&lt;br /&gt;I  will try to provide more working examples of Silverlight controls in the future posts!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f11%2fsilverlight-20linq-to-sql-building-ui.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f11%2fsilverlight-20linq-to-sql-building-ui.html&amp;amp;bgcolor=336688" alt="kick it on DotNetKicks.com" border="0" /&gt;&lt;/a&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/3341344083845694871/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/3341344083845694871" rel="replies" title="0 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/3341344083845694871" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/3341344083845694871" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2008/11/silverlight-20linq-to-sql-building-ui.html" rel="alternate" title="Silverlight 2.0 - Using Silverlight DataGrid to consume ASMX Web Service" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj32IN9EhjmynGO0TMLGUD5za-yCLkv2uP4h0TVwQSHKE8FekgYAAnjWuwb-uPL_ppcOHDEumwqgxeV0KuzzsFIDs66hxQ5DPENzwv3lX78oi0bwIO2raUW-tKmsn3KhyphenhyphenKvCt3cFZFohiI/s72-c/WS1.jpg" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-9117015971258249934</id><published>2008-10-27T15:45:00.150-04:00</published><updated>2008-11-02T16:14:32.289-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET"/><category scheme="http://www.blogger.com/atom/ns#" term="LINQ"/><category scheme="http://www.blogger.com/atom/ns#" term="LINQ to SQL"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2008"/><title type="text">LINQ to SQL - Building UI Layer using ASP:LinqDataSource Control</title><content type="html">&lt;span style=";font-family:georgia;font-size:100%;"  &gt;LINQ &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;stands for Language Integrated Query which was introduced with &lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?familyid=333325FD-AE52-4E35-B531-508D977D32A6&amp;amp;displaylang=en"&gt;.NET 3.5&lt;/a&gt;&lt;/span&gt;&lt;span style=";font-family:arial;font-size:100%;"  &gt;.&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt; LINQ provides the ability to write query as a first-class language construct using modern programming languages like C# and Visual Basic.&lt;br /&gt;&lt;br /&gt;LINQ acts as a bridge between Objects and Data, classic data driven applications expected the programmer to be proficient in both the programming language (C#, Visual Basic etc) and the data source native language (SQL, XQuery etc)  to extract the data.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;LINQ is designed to work against all data sources possible which includes objects, relational, XML etc.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;LINQ allows the developer to write type-safe queries against data sources with their choice of programming languages (C#, Visual Basic). LINQ enjoys full compile-time and intellisense&lt;/span&gt; support.&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;.NET 3.5 framework supports &lt;span style="font-style: italic;"&gt;three&lt;/span&gt; flavors of LINQ supported by Microsoft:&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;.&lt;/span&gt; LINQ to Object&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;s&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;.&lt;/span&gt; LINQ to XML&lt;/span&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;.&lt;/span&gt; LINQ to ADO.NET&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt; &lt;span&gt;[LINQ to SQL, LINQ to Entities and LINQ to DataSet]&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt; &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt; &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;For more details on LINQ visit: &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx"&gt;MSDN&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;The example uses NorthWind database. You can download and install NorthWind database from the &lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&amp;amp;displaylang=en"&gt;following location&lt;/a&gt;.&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="line-height: 115%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-size:100%;"&gt;The article will demonstrate working of &lt;span style="color: rgb(255, 102, 0);"&gt;LINQ to SQL&lt;/span&gt; enabled UI by creating an ASP.NET web-form with GridView control bound to the newly introduced &lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;LinqDataSource&lt;/span&gt; control&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-size:100%;"&gt; (&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?familyid=333325FD-AE52-4E35-B531-508D977D32A6&amp;amp;displaylang=en"&gt;.NET 3.5&lt;/a&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-size:100%;"&gt; required).&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="line-height: 115%;"&gt;LinqDataSource control allows the ability to connect data from wide variety of sources like database, collections etc, while maintaining a uniform programming model for data access.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;In our example we will see how LinqDataSource control interacts with Database, when &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;LinqDataSource control is used to interact with database it communicates with the database through Entity classes which are generated by &lt;span style="color: rgb(255, 102, 0); font-style: italic;"&gt;O/R Designer (Object Relational Designer)&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;O/R Designer (Object Relational Designer) can be accessed by adding 'LINQ to SQL classes&lt;/span&gt;' from Visual Studio 2008 IDE, the designer usually creates one class mapping the database and one class each for all the tables in the database. The generated classes are typically placed in the &lt;span style="font-style: italic;"&gt;App_Code&lt;/span&gt; folder of the project.&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;Let us see the steps involved in adding a LINQ to SQL enabled UI.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;a) &lt;/span&gt;Use O/R designer to map the database and tables, I have added 'Categories' table to the designer, the sample web-form will use 'Categories' table to demonstrate display/edit/delete functionality:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXi1sb5kZP9nERfUFCiolaynKfKuCxGmEMlcaxJO0bX8RLZYZL70vkslMaclDxSOFQk2DYGMPkTtXry8mTx5aRhu5GOyTNW4dkwi9Qd-fUGsDBFkHXpXA5suK3V4JNjHi36V5QqchqiKc/s1600-h/ORD1.jpg"&gt;&lt;img style="cursor: pointer; width: 355px; height: 386px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXi1sb5kZP9nERfUFCiolaynKfKuCxGmEMlcaxJO0bX8RLZYZL70vkslMaclDxSOFQk2DYGMPkTtXry8mTx5aRhu5GOyTNW4dkwi9Qd-fUGsDBFkHXpXA5suK3V4JNjHi36V5QqchqiKc/s400/ORD1.jpg" alt="" id="BLOGGER_PHOTO_ID_5262269812689735906" border="0" /&gt;&lt;/a&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;b) &lt;/span&gt;Add a GridView control and click on 'Choose Data Source':&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiI6eH_RTT0mVldmxpvkDgTUNwdvOKFZve-30-SmUfOgfHmvai6xi-ABLD87prjLVbAfTbWb7DPB24MOPxByuypt8qjHxfC6RNl-IvlCtaUS1MnMYzqIWa7PE2RaxtVoqDcU45XhbYomPw/s1600-h/PickDataSource2.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 166px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiI6eH_RTT0mVldmxpvkDgTUNwdvOKFZve-30-SmUfOgfHmvai6xi-ABLD87prjLVbAfTbWb7DPB24MOPxByuypt8qjHxfC6RNl-IvlCtaUS1MnMYzqIWa7PE2RaxtVoqDcU45XhbYomPw/s400/PickDataSource2.jpg" alt="" id="BLOGGER_PHOTO_ID_5262270456070665234" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;c) &lt;/span&gt;Select LINQ from Data Source Type and then provide an ID for the DataSource selected:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_jdX1CiUQQgr_VT8Ob9ssZdx_9xLYQFzIFYi2WRdjTsKyuQFkgX0idvyW1-S8bUUUeHeKx6Q1xx5n9Z37IHstqN9PQciQAtasYFwb_Xy8S6bF52U4V3ybfZSf6PP2Fnj67vmuhX_V3lc/s1600-h/LINQDataSource3.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 335px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_jdX1CiUQQgr_VT8Ob9ssZdx_9xLYQFzIFYi2WRdjTsKyuQFkgX0idvyW1-S8bUUUeHeKx6Q1xx5n9Z37IHstqN9PQciQAtasYFwb_Xy8S6bF52U4V3ybfZSf6PP2Fnj67vmuhX_V3lc/s400/LINQDataSource3.jpg" alt="" id="BLOGGER_PHOTO_ID_5262271235640533266" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;d) &lt;/span&gt;Choose the DataContext from the drop-down&lt;/span&gt; list, this is the name for your O/R designer generated class:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrSboXyDuCTktgGES89dgOR2-QqN9C9H5mDd4YDBf6koMOZaOKn1YyzYF6W6xxLYmXRnDDN-68Jq6vOzLQX9gzbplXVU06XRC94id7Z_zUyMsMcHH2TBqZFb_zD7UdJr8kE65sTZhGKeY/s1600-h/DataContext4.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 336px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrSboXyDuCTktgGES89dgOR2-QqN9C9H5mDd4YDBf6koMOZaOKn1YyzYF6W6xxLYmXRnDDN-68Jq6vOzLQX9gzbplXVU06XRC94id7Z_zUyMsMcHH2TBqZFb_zD7UdJr8kE65sTZhGKeY/s400/DataContext4.jpg" alt="" id="BLOGGER_PHOTO_ID_5262271964809094114" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;e) &lt;/span&gt;Configure Data Source, Pick the 'Categories' table from the Table list, Select '*' for the Columns and then Click 'Advanced':&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOkvFwLqUPTbE3BRN0Se3Y4-HYcwJXLTa_GF7VlRO-6zwi4G6J4BgvWgSa9o5JQaUBYac8qSR1oAeFD0P8snq9OECwwnyVo7yWtISu8nVx3k1CxnFMGjNiMHirsvYReCTPMC13hnGqtn8/s1600-h/ConfigureData5.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 221px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOkvFwLqUPTbE3BRN0Se3Y4-HYcwJXLTa_GF7VlRO-6zwi4G6J4BgvWgSa9o5JQaUBYac8qSR1oAeFD0P8snq9OECwwnyVo7yWtISu8nVx3k1CxnFMGjNiMHirsvYReCTPMC13hnGqtn8/s400/ConfigureData5.jpg" alt="" id="BLOGGER_PHOTO_ID_5262272681790879202" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;f) &lt;/span&gt;Select all three options to enable Insert, Update &amp;amp; Delete functionality:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixu8ykNWkNTSZH8K3_TbEv9z3uaZLxOjR0xbByQFgMxdLmu4SQhAm4BDz-vBHHdVWui2gUtLjyXPDDl-dWUiDloXiOB-C4eyhpyXEVyqgMxcBKKhPvKag1uPPY_Rj3LZ121cNWE3OPL5U/s1600-h/ConfigureAdvancedOptions6.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 278px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixu8ykNWkNTSZH8K3_TbEv9z3uaZLxOjR0xbByQFgMxdLmu4SQhAm4BDz-vBHHdVWui2gUtLjyXPDDl-dWUiDloXiOB-C4eyhpyXEVyqgMxcBKKhPvKag1uPPY_Rj3LZ121cNWE3OPL5U/s400/ConfigureAdvancedOptions6.jpg" alt="" id="BLOGGER_PHOTO_ID_5262273181537568850" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;g) &lt;/span&gt;Select options to Enable Insert, Update &amp;amp; Delete functionality in the GridView control:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxHwCcmAYZlSWIby1orQtouC3SWLqaPu0TTDeMkHr8ILjWoHJRYbLX49QsDuXDiL_37-Xcsjn8cpGPgHA2ZOU6ziv4R4b-ZqfaXA0g3vHiJEUlmV6AEJ7fdQiBkjp8iD-2Qwa-dikPiqs/s1600-h/ConfigureAdvancedOptionsGridView7.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 158px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxHwCcmAYZlSWIby1orQtouC3SWLqaPu0TTDeMkHr8ILjWoHJRYbLX49QsDuXDiL_37-Xcsjn8cpGPgHA2ZOU6ziv4R4b-ZqfaXA0g3vHiJEUlmV6AEJ7fdQiBkjp8iD-2Qwa-dikPiqs/s400/ConfigureAdvancedOptionsGridView7.jpg" alt="" id="BLOGGER_PHOTO_ID_5262273055883983842" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;h) &lt;/span&gt;Web-form is wired with all the controls and is being displayed in 'Display' mode:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicpD_x1DzfJJqqy54KB9ItNLev46kbPPju1W69nJUa6qDcr8CEOZ4L7fy4P_mtZNvgraDuuMVuDLc4NROmn4vQb2uVHO5kQ4iEsp2px5zKyqTqaR6V6yHkEniUiX_AIKzfEXyEzMpmRX8/s1600-h/WebFormDisplay7.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 164px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicpD_x1DzfJJqqy54KB9ItNLev46kbPPju1W69nJUa6qDcr8CEOZ4L7fy4P_mtZNvgraDuuMVuDLc4NROmn4vQb2uVHO5kQ4iEsp2px5zKyqTqaR6V6yHkEniUiX_AIKzfEXyEzMpmRX8/s400/WebFormDisplay7.jpg" alt="" id="BLOGGER_PHOTO_ID_5262297039958615218" border="0" /&gt;&lt;/a&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;i) &lt;/span&gt;Web-form is wired with all the controls and is being displayed in 'Edit' mode:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEga_ci8CcpejZSQsdC6ywji40qQZEwTYP3mn9gb4WnjhZH6DzEfugpd3bqDFC8FQrooO_T3dIrrh7d8jVxxwGDlC8TchK_ugSOMI99uLxXctxVhOBiieCE58Mv3OzYdxeDGdyNVA9-Qq_4/s1600-h/WebFormEdit8.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 139px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEga_ci8CcpejZSQsdC6ywji40qQZEwTYP3mn9gb4WnjhZH6DzEfugpd3bqDFC8FQrooO_T3dIrrh7d8jVxxwGDlC8TchK_ugSOMI99uLxXctxVhOBiieCE58Mv3OzYdxeDGdyNVA9-Qq_4/s400/WebFormEdit8.jpg" alt="" id="BLOGGER_PHOTO_ID_5262274006078999858" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;j) &lt;/span&gt;Web-form is displayed with warning message showing 'Category Name cannot be blank', we will visit this section in the code walk-through:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrpH3ybOlqT_9xaA5v1M_taX1Io0LvcvipBFO7FfEGKphoe0VTS-UggKqN4rVx4rZ28divymkzq9Qp0qEy5ExSBbrQwlMLq3dk__WJJXFWzEee8C9rxWi1HqhitRynGzCol8QEFCSS7EQ/s1600-h/WebFormEdit9.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 177px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrpH3ybOlqT_9xaA5v1M_taX1Io0LvcvipBFO7FfEGKphoe0VTS-UggKqN4rVx4rZ28divymkzq9Qp0qEy5ExSBbrQwlMLq3dk__WJJXFWzEee8C9rxWi1HqhitRynGzCol8QEFCSS7EQ/s400/WebFormEdit9.jpg" alt="" id="BLOGGER_PHOTO_ID_5262274512180192834" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;k) &lt;/span&gt;Web-form is displayed with successful update:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinOQM-CzQKy8zfERXpCjjqEFq618w5c1pNUaOHn7GihE00RublC3OHb9c0qlKK-6Xdzo-wp8XtJ7hvEBai9FxieYsODPkPWidfVcr_PVDb-e5goi-W1pNYsh7cNKnGgJw-aabOl5pUULI/s1600-h/WebFormDisplay10.jpg"&gt;&lt;img style="cursor: pointer; width: 400px; height: 145px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinOQM-CzQKy8zfERXpCjjqEFq618w5c1pNUaOHn7GihE00RublC3OHb9c0qlKK-6Xdzo-wp8XtJ7hvEBai9FxieYsODPkPWidfVcr_PVDb-e5goi-W1pNYsh7cNKnGgJw-aabOl5pUULI/s400/WebFormDisplay10.jpg" alt="" id="BLOGGER_PHOTO_ID_5262274832595437490" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;Code Walk-through&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;a) Web-form &lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;declarative Markup:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre   style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;font-size:12px;"&gt;&lt;code&gt;&amp;lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="myGrid.aspx.cs" Inherits="myGrid" %&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;html xmlns="http://www.w3.org/1999/xhtml"&amp;gt;&lt;br /&gt;&amp;lt;head runat="server"&amp;gt;&lt;br /&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;lt;form id="form1" runat="server"&amp;gt;&lt;br /&gt;&amp;lt;asp:Literal runat=server ID="ErrMsg"&amp;gt;&amp;lt;/asp:Literal&amp;gt;&lt;br /&gt;&amp;lt;asp:GridView ID="GridView1" runat="server" &lt;span style="font-weight: bold; color: rgb(255, 102, 0);"&gt;DataSourceID="DBLINQ"&lt;/span&gt; CellPadding="4" ForeColor="#333333"&lt;br /&gt;GridLines="None" Width="700px" AllowPaging="True" PagerSettings-Mode=NextPreviousFirstLast PageSize="5" AllowSorting="True"&lt;br /&gt;Height="112px" AutoGenerateColumns="False" DataKeyNames="CategoryID"&amp;gt;&lt;br /&gt;&amp;lt;PagerSettings Mode="NumericFirstLast" /&amp;gt;&lt;br /&gt;&amp;lt;RowStyle BackColor="#EFF3FB"  Font-Names="Verdana" Font-Size=Small/&amp;gt;&lt;br /&gt;&amp;lt;Columns&amp;gt;&lt;br /&gt;&amp;lt;asp:BoundField DataField="CategoryID"  HeaderStyle-Wrap=false HeaderText="Category ID"&lt;br /&gt;InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" &amp;gt;&lt;br /&gt;&amp;lt;/asp:BoundField&amp;gt;&lt;br /&gt;&amp;lt;asp:BoundField DataField="CategoryName" HeaderStyle-Wrap=false HeaderText="Category Name"&lt;br /&gt;SortExpression="CategoryName" &amp;gt;&lt;br /&gt;&amp;lt;/asp:BoundField&amp;gt;&lt;br /&gt;&amp;lt;asp:BoundField DataField="Description"  HeaderStyle-Wrap=false HeaderText="Description"&lt;br /&gt;SortExpression="Description" &amp;gt;&lt;br /&gt;&amp;lt;/asp:BoundField&amp;gt;&lt;br /&gt;&amp;lt;asp:CommandField HeaderText="Action" ShowDeleteButton="True" ShowEditButton="True" /&amp;gt;&lt;br /&gt;&amp;lt;/Columns&amp;gt;&lt;br /&gt;&amp;lt;FooterStyle BackColor="#507CD1"  Font-Names="Verdana" Font-Size=Small Font-Bold="True" ForeColor="White" /&amp;gt;&lt;br /&gt;&amp;lt;PagerStyle BackColor="#2461BF"   Font-Names="Verdana" Font-Size=Small ForeColor="White" HorizontalAlign="Center" /&amp;gt;&lt;br /&gt;&amp;lt;SelectedRowStyle BackColor="#D1DDF1"    Font-Names="Verdana" Font-Size=Small Font-Bold="True" ForeColor="#333333" /&amp;gt;&lt;br /&gt;&amp;lt;HeaderStyle BackColor="#507CD1"  Font-Names="Verdana" Font-Size=Small Font-Bold="True" ForeColor="White" /&amp;gt;&lt;br /&gt;&amp;lt;EditRowStyle BackColor="#FFFFFF"  Font-Names="Verdana" Font-Size=Small/&amp;gt;&lt;br /&gt;&amp;lt;AlternatingRowStyle BackColor="White"  Font-Names="Verdana" Font-Size=Small/&amp;gt;&lt;br /&gt;&amp;lt;/asp:GridView&amp;gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;&amp;lt;asp:LinqDataSource ID="DBLINQ" runat="server"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;        ContextTypeName="DataClassesDataContext" EnableDelete="True"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;        EnableInsert="True" EnableUpdate="True" TableName="Categories"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;    &amp;lt;/asp:LinqDataSource&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;&lt;span style="font-family:georgia;"&gt;b) Web-form Code-behind &lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre face="Andale Mono,Lucida Console,Monaco,fixed,monospace" size="12px" style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); line-height: 14px; width: 100%;"&gt;&lt;code&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Web;&lt;br /&gt;using System.Web.UI;&lt;br /&gt;using System.Web.UI.WebControls;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public partial class myGrid : System.Web.UI.Page&lt;br /&gt;{&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(255, 102, 0);"&gt;private Boolean IsError;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;//wire event handlers&lt;br /&gt;GridView1.RowUpdating      += new&lt;br /&gt;GridViewUpdateEventHandler(GridView1_RowUpdating);&lt;br /&gt;DBLINQ.Updating            += new&lt;br /&gt;EventHandler&amp;lt;LinqDataSourceUpdateEventArgs&amp;gt;(DBLINQ_Updating);&lt;br /&gt;//ini&lt;br /&gt;IsError                    = false;&lt;br /&gt;ErrMsg.Visible             = false;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;protected void &lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;GridView1_RowUpdating&lt;/span&gt;(object sender, GridViewUpdateEventArgs e)&lt;br /&gt;{&lt;br /&gt;//get the row being updated&lt;br /&gt;GridViewRow currRow  = GridView1.Rows[e.RowIndex];&lt;br /&gt;//check for null or empty value&lt;br /&gt;string txtCatName    = ((TextBox)(currRow.Cells[1].Controls[0])).Text;&lt;br /&gt;if (string.IsNullOrEmpty(txtCatName))&lt;br /&gt;{&lt;br /&gt;ErrMsg.Text            = "&lt;br /&gt;&amp;lt;div style='font-family:Verdana;font-size:smaller;font-weight:bolder;&lt;br /&gt;color:red'&amp;gt;* Category Name cannot be blank&amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;&amp;lt;/div&amp;gt;";&lt;br /&gt;ErrMsg.Visible         = true;&lt;br /&gt;e.Cancel               = true;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;//set the IsError variable to cancel LINQDataSource control update&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;IsError                = true;&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected void &lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;DBLINQ_Updating&lt;/span&gt;(object sender, LinqDataSourceUpdateEventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;//check if error occurred&lt;/span&gt;&lt;span style="color: rgb(255, 102, 0); font-weight: bold;"&gt;&lt;br /&gt;if (IsError) e.Cancel = true;&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;Code highlighted with &lt;/span&gt;&lt;span style="color: rgb(255, 102, 0);font-family:georgia;font-size:100%;"  &gt;orange&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt; shows how we can add data validation logic to the update routine. This is just a basic example ideally we would have used ASP.NET RequiredFieldValidation control.&lt;br /&gt;&lt;br /&gt;Key thing to notice is we need to add custom validation to both the &lt;/span&gt;&lt;span style="font-style: italic;font-family:georgia;font-size:100%;"  &gt;GridView's RowUpdating and LinqDataSource Updating event handlers&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;, as this gives us more granular control towards the validation.&lt;br /&gt;&lt;br /&gt;Once the GridView's RowUpdating event handler detects an exception it sets the global variable &lt;/span&gt;&lt;span style="font-style: italic;font-family:georgia;font-size:100%;"  &gt;'IsError'&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt; to &lt;/span&gt;&lt;span style="font-style: italic;font-family:georgia;font-size:100%;"  &gt;True&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;, this variable is then checked by the LinqDataSource Updating event handler before performing the Database update. If the variable is &lt;/span&gt;&lt;span style="font-style: italic;font-family:georgia;font-size:100%;"  &gt;False&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt; then the changes are saved to the Database.&lt;br /&gt;&lt;br /&gt;&lt;a target="_blank" href="http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx"&gt;Another way of handling&lt;/a&gt; the validation logic is to handle it in the &lt;/span&gt;&lt;span style="font-style: italic;font-family:georgia;font-size:100%;"  &gt;LinqDataSource RowUpdated event handler&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt; but my personal choice is to use the approach as described above since this approach gives us more flexibility in both handling validation and providing custom feedback to the User.&lt;br /&gt;&lt;br /&gt;I will try to provide more working examples of LINQ in the future posts!&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f10%2faspnetlinq-to-sql-building-ui-layer.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f10%2faspnetlinq-to-sql-building-ui-layer.html&amp;amp;bgcolor=336688" alt="kick it on DotNetKicks.com" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/9117015971258249934/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/9117015971258249934" rel="replies" title="1 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/9117015971258249934" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/9117015971258249934" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2008/10/aspnetlinq-to-sql-building-ui-layer.html" rel="alternate" title="LINQ to SQL - Building UI Layer using ASP:LinqDataSource Control" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXi1sb5kZP9nERfUFCiolaynKfKuCxGmEMlcaxJO0bX8RLZYZL70vkslMaclDxSOFQk2DYGMPkTtXry8mTx5aRhu5GOyTNW4dkwi9Qd-fUGsDBFkHXpXA5suK3V4JNjHi36V5QqchqiKc/s72-c/ORD1.jpg" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-188360569362018484</id><published>2008-10-17T15:00:00.016-04:00</published><updated>2008-11-02T16:14:53.592-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ADO.NET"/><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="T-SQL"/><title type="text">ASP.NET/SQL Server 2008 – Using Table-Valued Parameters with .NET Application</title><content type="html">&lt;span style=";font-family:georgia;font-size:100%;"  &gt;With the release of SQL Server 2008, Microsoft introduced a new parameter type called 'Table-Valued Parameters', which provided the flexibility for the SQL developers to send multiple rows of data to a stored procedure/function or inline T-SQL code.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;Table-Valued parameters can be used to transfer multiple rows of data without requiring the developer to construct custom string parsing routines, temporary table or multiple parameters which for a complex stored procedure can become difficult to maintain.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;Table-Valued parameters are declared in series of steps:&lt;br /&gt;&lt;/span&gt;&lt;ul  style="font-family:georgia;"&gt;&lt;li&gt;&lt;span style="font-size:100%;"&gt;Create a Table type and define the table structure based on the requirement&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:100%;"&gt;Declare a local variable to use this Table type&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:100%;"&gt;Initialize the local variable with the data&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:100%;"&gt;Pass the local variable to the stored procedure/function to accept this table parameter, make sure the input parameter is 'READONLY'&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;Table-Valued parameter &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;goes out of scope once the stored procedure/function/T-SQL is executed. &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;Table-Valued parameter &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;can only be modified in the scope it was created,  due to this criteria when the parameter is passed to a different stored procedure/function it is passed as READONLY.&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;br /&gt;For more details on Table-Valued parameters visit: &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/bb510489.aspx"&gt;MSDN&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;Let us consider a real-world example using NorthWind database. You can download and install NorthWind database from the &lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&amp;amp;displaylang=en"&gt;following location&lt;/a&gt;.&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="line-height: 115%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-size:100%;"&gt;The article will demonstrate working of Table-Valued Parameters by creating a custom stored-procedure and calling the stored-procedure code from the .NET application (&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?familyid=333325FD-AE52-4E35-B531-508D977D32A6&amp;amp;displaylang=en"&gt;.NET 3.5&lt;/a&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-size:100%;"&gt; required).&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="line-height: 115%;"&gt;&lt;br /&gt;NorthWind database contains 'Shippers' table&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;, this table stores shipping company contact details used in the Order fulfillment operation.&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;We are going to insert 2 new Shippers record in the table, using Table-Valued Parameter enabled stored procedure, the stored procedure will be invoked from .NET code.&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-family:georgia;font-size:100%;"  &gt;a) Create User-Defined Table type&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="color: rgb(51, 51, 255);"&gt;CREATE TYPE Shippers AS TABLE (CompanyName nvarchar(80), phone nvarchar(48))&lt;/span&gt;  &lt;/span&gt;&lt;span style="font-weight: bold;font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;br /&gt;b) Creating the &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;Table-Valued Parameter enabled Stored procedure&lt;/span&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;&lt;br /&gt;&lt;br /&gt;CREATE Procedure CreateShippers(@Shippers Shippers &lt;span style="font-weight: bold;"&gt;READONLY&lt;/span&gt;)&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;&lt;br /&gt;AS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;        INSERT INTO Shippers&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;    SELECT CompanyName, Phone FROM @Shippers&lt;/span&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;&lt;br /&gt;END&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;  Notice @Shippers variable of Shippers type is declared with READONLY qualifier.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;c) Calling the Stored procedure through .NET application&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I have created a test web form to demonstrate how we can call the Table-Valued Parameter enable stored procedure from .NET application.&lt;br /&gt;&lt;br /&gt;Web form displays original data in the Shippers table and a 'Load Shippers' button, once the button is clicked 2 new rows of data are inserted into the Shippers table and the web form reloads to display the newly inserted data (I am using hard coded values in the OnClick event handler of the button to initialize data, the code can be easily modified to accept user keyed data by adding text box controls to the web form).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Screen shot showing Web-form with Original and Inserted data:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV7aHMglt-SVwlm8sra3XcrI32wlMSd6DQMEYbLm4lawN1tXaUVYghyphenhyphenZwThICmN_xeuYZ7kqe4v7WqxUM-rPkZGxIw-_bd2T19EqrQRq0u48Ysfc2co-qsqzbMNG6Rre0evcJa1CvTan8/s1600-h/webform.jpg"&gt;&lt;img style="cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV7aHMglt-SVwlm8sra3XcrI32wlMSd6DQMEYbLm4lawN1tXaUVYghyphenhyphenZwThICmN_xeuYZ7kqe4v7WqxUM-rPkZGxIw-_bd2T19EqrQRq0u48Ysfc2co-qsqzbMNG6Rre0evcJa1CvTan8/s400/webform.jpg" alt="" id="BLOGGER_PHOTO_ID_5258193592005897234" border="0" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;ASP.NET Web form Code (C#):&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;protected void Button1_Click(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;string path  = Request.CurrentExecutionFilePath;&lt;br /&gt;path              = path.Substring(0, path.LastIndexOf('/'));&lt;br /&gt;Configuration rootWebConfig  =    WebConfigurationManager.OpenWebConfiguration(path);&lt;br /&gt;ConnectionStringSettings connString = rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div  style="text-align: left;font-family:georgia;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;        //create DataTable&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;        DataTable dt        = new DataTable("MyData");&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;        DataColumn dbCol1    = new DataColumn("CompanyName", System.Type.GetType("System.String"));&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;        DataColumn dbCol2    = new DataColumn("Phone", System.Type.GetType("System.String"));&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;        //add columns&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;        dt.Columns.Add(dbCol1);&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;        dt.Columns.Add(dbCol2);&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;        &lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="color: rgb(51, 51, 255);"&gt;        //create data&lt;br /&gt;DataRow dr;&lt;br /&gt;dr                    = dt.NewRow();&lt;br /&gt;dr["CompanyName"]    = "ACME Express";&lt;br /&gt;dr["Phone"]            = "(503) 111-1111";&lt;br /&gt;dt.Rows.Add(dr);&lt;br /&gt;//new row&lt;br /&gt;dr                    = dt.NewRow();&lt;br /&gt;dr["CompanyName"]    = "Courier Express";&lt;br /&gt;dr["Phone"]            = "(503) 222-2222";&lt;br /&gt;dt.Rows.Add(dr);&lt;br /&gt;//save&lt;br /&gt;dt.AcceptChanges();&lt;br /&gt;&lt;br /&gt;//open Database connection&lt;br /&gt;using (SqlConnection dbConn = new SqlConnection(connString.ConnectionString))&lt;br /&gt;{&lt;br /&gt;   dbConn.Open();&lt;br /&gt;   using (SqlCommand dbCommand    = new SqlCommand("CreateShippers", dbConn))&lt;br /&gt;   {&lt;br /&gt;       dbCommand.CommandType    = CommandType.StoredProcedure;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;           //Create a parameter using the new type&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;           SqlParameter param        = dbCommand.Parameters.Add("@Shippers", SqlDbType.Structured);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;           //Set the value of the parameter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;           param.Value                = dt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;           &lt;/span&gt;//Execute the query&lt;br /&gt;       dbCommand.ExecuteNonQuery();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//get new inserted data&lt;br /&gt;SqlDataSource1.ConnectionString        = connString.ConnectionString;&lt;br /&gt;SqlDataSource1.SelectCommand        = "Select * from Shippers";&lt;br /&gt;SqlDataSource1.SelectCommandType    = SqlDataSourceCommandType.Text;&lt;br /&gt;GridView1.DataSourceID                = "SqlDataSource1";&lt;br /&gt;//show confirmation&lt;br /&gt;Msg.Visible        = true;&lt;br /&gt;//hide button&lt;br /&gt;Button1.Visible    = false;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2fsql-server-table-valued-parameters.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2fsql-server-table-valued-parameters.html&amp;amp;bgcolor=336688" alt="kick it on DotNetKicks.com" border="0" /&gt;&lt;/a&gt;&lt;/span&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/188360569362018484/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/188360569362018484" rel="replies" title="2 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/188360569362018484" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/188360569362018484" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2008/08/sql-server-table-valued-parameters.html" rel="alternate" title="ASP.NET/SQL Server 2008 – Using Table-Valued Parameters with .NET Application" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV7aHMglt-SVwlm8sra3XcrI32wlMSd6DQMEYbLm4lawN1tXaUVYghyphenhyphenZwThICmN_xeuYZ7kqe4v7WqxUM-rPkZGxIw-_bd2T19EqrQRq0u48Ysfc2co-qsqzbMNG6Rre0evcJa1CvTan8/s72-c/webform.jpg" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-815194855698372291</id><published>2008-10-08T17:31:00.033-04:00</published><updated>2008-11-02T16:06:56.988-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ADO.NET"/><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET"/><title type="text">ASP.NET - Efficiently Importing DataTable</title><content type="html">&lt;span style=";font-family:georgia;font-size:100%;"  &gt;ASP.NET developers often use DataTable to store data received from SQL stored procedure, web-service, data access layer (DAL) etc.&lt;br /&gt;&lt;br /&gt;DataTable acts like a temporary container where the data is stored, refined (trimming white-spaces, number formatting), transformed (calculating a running total) and then consumed.&lt;br /&gt;&lt;br /&gt;For more details on DataTable visit: &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx"&gt;MSDN&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The article will demonstrate how to efficiently create a DataTable from an existing DataTable based on row by row comparison.&lt;br /&gt;&lt;br /&gt;Suppose DataTable (A) has 1000 rows of Order data placed by multiple customers of the web-site, our goal is to efficiently create a new DataTable (B) using DataTable (A) to extract Orders having customer ID of 101 &amp;amp; 102.&lt;br /&gt;&lt;br /&gt;DataTable (A) is populated by making a call to the DAL, which returns all the Orders placed to date [ideally DAL would provide a method which will return filtered data but for now we are going to go with the assumption of filtering data out of DataTable (A)].&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnEknIHC0gxPV6si5J6RGc2fQfel18lqm0Bc8HdCxCqPWF10wQzEj2ToOpSd2sLBn-N3Z5M0_zja2St6hNV_xIJh47ThNhML5vrRt_U9A5tHQFMCqfUo-gcDwoMoQ5m4YJAtXlG5CkAds/s1600-h/DataTable_A.jpg"&gt;&lt;img style="cursor: pointer; width: 335px; height: 221px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnEknIHC0gxPV6si5J6RGc2fQfel18lqm0Bc8HdCxCqPWF10wQzEj2ToOpSd2sLBn-N3Z5M0_zja2St6hNV_xIJh47ThNhML5vrRt_U9A5tHQFMCqfUo-gcDwoMoQ5m4YJAtXlG5CkAds/s400/DataTable_A.jpg" alt="" id="BLOGGER_PHOTO_ID_5254977249288955746" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZXmPNrxgXaEJuPblnwTyUaRD8j-drn9hMJqmgc7VC1fKjYcdJFkRuYjPaB0L2gVROOprXbJQ6Kr5_CKyqs03L8aDpRoWl8n7fyfdRnF0R8N0kk55yyHxKYs08C6rIdb47tCoQTRCGhQI/s1600-h/DataTable_B.jpg"&gt;&lt;img style="cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZXmPNrxgXaEJuPblnwTyUaRD8j-drn9hMJqmgc7VC1fKjYcdJFkRuYjPaB0L2gVROOprXbJQ6Kr5_CKyqs03L8aDpRoWl8n7fyfdRnF0R8N0kk55yyHxKYs08C6rIdb47tCoQTRCGhQI/s400/DataTable_B.jpg" alt="" id="BLOGGER_PHOTO_ID_5254977464947850226" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;Code:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;//DataTable (B) is cloned from DataTable (A)&lt;/span&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;DataTable B  = A.Clone();&lt;/span&gt;&lt;/span&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;&lt;br /&gt;//clear DataTable&lt;/span&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;&lt;br /&gt;B.Clear();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;//save changes&lt;/span&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;&lt;br /&gt;B.AcceptChanges();&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Note we are using DataTable &lt;span style="font-weight: bold;"&gt;'Clone'&lt;/span&gt; method instead of &lt;span style="font-weight: bold;"&gt;'Copy'&lt;/span&gt; method as our objective is to only retain the &lt;span style="font-style: italic;"&gt;structure&lt;/span&gt; of DataTable (A) &lt;span style="font-style: italic;"&gt;and not the data&lt;/span&gt;.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Now we need to populate DataTable (B) with Orders having customer ID of 101 &amp;amp; 102.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;//iterate&lt;/span&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;&lt;br /&gt;foreach(DataRow dr in A.Rows)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;     &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;int customerID = Convert.ToDouble(dr["CustomerID"]);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;   //match customer ID &lt;/span&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;&lt;br /&gt;if (customerID == 101 || customerID == 102)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;     {&lt;/span&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;&lt;br /&gt;//customerID matched add it to DataTable (B)&lt;/span&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;B.ImportRow(dr);&lt;/span&gt;&lt;/span&gt; &lt;span style="color: rgb(51, 102, 255);"&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;//save&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;B.AcceptChanges(); &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;DataTable’s &lt;span style="font-weight: bold;"&gt;ImportRow&lt;/span&gt; method copies the row from DataTable (A) to DataTable (B), performance wise ImportRow is faster than individually adding rows specially when dealing with large amount of data.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f10%2faspnet-efficiently-importing-datatable.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f10%2faspnet-efficiently-importing-datatable.html&amp;amp;bgcolor=336688" alt="kick it on DotNetKicks.com" border="0" /&gt;&lt;/a&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/815194855698372291/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/815194855698372291" rel="replies" title="0 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/815194855698372291" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/815194855698372291" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2008/10/aspnet-efficiently-importing-datatable.html" rel="alternate" title="ASP.NET - Efficiently Importing DataTable" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnEknIHC0gxPV6si5J6RGc2fQfel18lqm0Bc8HdCxCqPWF10wQzEj2ToOpSd2sLBn-N3Z5M0_zja2St6hNV_xIJh47ThNhML5vrRt_U9A5tHQFMCqfUo-gcDwoMoQ5m4YJAtXlG5CkAds/s72-c/DataTable_A.jpg" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-8380104260460686452</id><published>2008-08-27T15:00:00.006-04:00</published><updated>2008-11-02T16:07:30.474-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="T-SQL"/><title type="text">SQL Server - Get Nth Highest using ROW_NUMBER() Function</title><content type="html">&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="font-style: italic;font-size:78%;" &gt;* Compatibility SQL Server 2005 onwards&lt;/span&gt;
&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;ROW_NUMBER&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt; function was introduced in SQL Server 2005. &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;ROW_NUMBER&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;as the name suggests is used to calculate row numbers in the query result set.
&lt;br /&gt;
&lt;br /&gt;Prior to &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;ROW_NUMBER&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt; developers relied on temporary tables and co-related sub-queries to calculate sequential output, this often resulted in spaghetti code and overall bad query performance.
&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;
&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;For more details on ROW_NUMBER&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;visit: &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms186734.aspx"&gt;&lt;span style="font-size:100%;"&gt;&lt;/span&gt;&lt;/a&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms186734.aspx" style="font-family: georgia;" target="_blank"&gt;MSDN&lt;/a&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;
&lt;br /&gt;
&lt;br /&gt;Let us consider a real-world ROW_NUMBER &lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;example using NorthWind database. You can download and install NorthWind database from the &lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&amp;amp;displaylang=en"&gt;following location&lt;/a&gt;.&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;Consider this scenario, we want to run a report which gives us Top Order by sales.&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;We are not calculating ties here i.e. what if more than one order generated exactly same sales amount, ties can be handled using using &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms176102.aspx"&gt;RANK()&lt;/a&gt; function but for now we will assume sequential result set.
&lt;br /&gt;
&lt;br /&gt;The stored procedure which we are going to see will provide the flexibility to return exact result set i.e. Order with highest sales or range i.e. Top 10 Orders by sales
&lt;br /&gt;
&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;strong&gt;Stored procedure code:&lt;/strong&gt;&lt;/span&gt;
&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-size:100%;" &gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;/span&gt;
&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;USE [Northwind]
&lt;br /&gt;GO
&lt;br /&gt;IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetHighestSales_sp]') AND type in (N'P', N'PC'))
&lt;br /&gt;DROP PROCEDURE [dbo].[GetHighestSales_sp]
&lt;br /&gt;GO
&lt;br /&gt;SET ANSI_NULLS OFF
&lt;br /&gt;GO
&lt;br /&gt;SET QUOTED_IDENTIFIER ON
&lt;br /&gt;GO
&lt;br /&gt;
&lt;br /&gt;CREATE PROCEDURE dbo.GetHighestSales_sp
&lt;br /&gt;(
&lt;br /&gt;@Rank       int = NULL,
&lt;br /&gt;@FROM    int = 1,
&lt;br /&gt;@TO          int = 1
&lt;br /&gt;)
&lt;br /&gt;AS
&lt;br /&gt;BEGIN
&lt;br /&gt;--no extra columns
&lt;br /&gt;SET NOCOUNT ON;
&lt;br /&gt;
&lt;br /&gt;/*CTE Declaration block    */
&lt;br /&gt;WITH CustomerOrders (OrderID, CustomerID, EmployeeID, ContactName, CompanyName, Phone, TotalPurchasePrice, SalesRank) AS
&lt;br /&gt;(
&lt;br /&gt;SELECT  OrderTotal.OrderID As OrderID, C.CustomerID As CustomerID, E.EmployeeID As EmployeeID,
&lt;br /&gt;   C.ContactName, C.CompanyName, C.Phone, OrderTotal.TotalPurchasePrice,
&lt;br /&gt;   OrderTotal.SalesRank
&lt;br /&gt;FROM
&lt;br /&gt;Customers C
&lt;br /&gt;INNER JOIN
&lt;br /&gt;(
&lt;br /&gt;SELECT ORD.OrderID As OrderID, O.CustomerID, O.EmployeeID,
&lt;br /&gt;Sum(CAST((UnitPrice * Quantity) * (1-Discount)/100 * 100 AS Money)) AS  TotalPurchasePrice,
&lt;br /&gt;Row_Number() OVER(ORDER BY (Sum(CAST((UnitPrice * Quantity) * (1-Discount)/100 * 100 AS Money))) DESC) AS SalesRank
&lt;br /&gt;FROM
&lt;br /&gt;[Order Details] ORD
&lt;br /&gt;INNER JOIN ORDERS O ON O.OrderID = ORD.OrderID
&lt;br /&gt;GROUP BY ORD.ORDERID, O.CustomerID, O.EmployeeID
&lt;br /&gt;) AS OrderTotal  ON OrderTotal.CustomerID = C.CustomerID
&lt;br /&gt;INNER JOIN Employees E ON OrderTotal.EmployeeID = E.EmployeeID
&lt;br /&gt;)
&lt;br /&gt;
&lt;br /&gt;--determine which result to extract
&lt;br /&gt;SELECT
&lt;br /&gt;  OrderID, E.LastName + ', ' + E.FirstName AS EmployeeName, ContactName,
&lt;br /&gt; CompanyName, Phone, TotalPurchasePrice, SalesRank
&lt;br /&gt;FROM
&lt;br /&gt;  Employees E
&lt;br /&gt;INNER JOIN CustomerOrders CO ON CO.EmployeeID = E.EmployeeID
&lt;br /&gt;WHERE
&lt;br /&gt;  CO.SalesRank between @From AND @To
&lt;br /&gt;
&lt;br /&gt;--reset
&lt;br /&gt;SET ROWCOUNT 0
&lt;br /&gt;END
&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;
&lt;br /&gt;
&lt;br /&gt;(a) Top Order by sa&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;les&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;EXEC dbo.GetHighestSales_sp 1
&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;&lt;/span&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNoPe6qq3a7nI7udPvtoMiIwxDOVt1ZaN00iAtpsxfC0aNn1gD74e5_qTz5snWiH-lZgU9ALIGo5OKEbwqTsVrjLxTmI28NIXvkCjZSV4WaqOhgcb27PcV-jIyXrx1tr6uHEPAdQSg2Ac/s1600-h/toporder.jpg"&gt;&lt;img style="cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNoPe6qq3a7nI7udPvtoMiIwxDOVt1ZaN00iAtpsxfC0aNn1gD74e5_qTz5snWiH-lZgU9ALIGo5OKEbwqTsVrjLxTmI28NIXvkCjZSV4WaqOhgcb27PcV-jIyXrx1tr6uHEPAdQSg2Ac/s400/toporder.jpg" alt="" id="BLOGGER_PHOTO_ID_5239273768062973474" border="0" /&gt;&lt;/a&gt;
&lt;br /&gt;&lt;meta equiv="Content-Type" content="text/html; charset=utf-8"&gt;&lt;meta name="ProgId" content="Word.Document"&gt;&lt;meta name="Generator" content="Microsoft Word 12"&gt;&lt;meta name="Originator" content="Microsoft Word 12"&gt;&lt;link rel="File-List" href="file:///C:%5CUsers%5Csyedr%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"&gt;&lt;link rel="themeData" href="file:///C:%5CUsers%5Csyedr%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"&gt;&lt;link rel="colorSchemeMapping" href="file:///C:%5CUsers%5Csyedr%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml"&gt;&lt;!--[if gte mso 9]&gt;&lt;xml&gt;  &lt;w:worddocument&gt;   &lt;w:view&gt;Normal&lt;/w:View&gt;   &lt;w:zoom&gt;0&lt;/w:Zoom&gt;   &lt;w:trackmoves/&gt;   &lt;w:trackformatting/&gt;   &lt;w:punctuationkerning/&gt;   &lt;w:validateagainstschemas/&gt;   &lt;w:saveifxmlinvalid&gt;false&lt;/w:SaveIfXMLInvalid&gt;   &lt;w:ignoremixedcontent&gt;false&lt;/w:IgnoreMixedContent&gt;   &lt;w:alwaysshowplaceholdertext&gt;false&lt;/w:AlwaysShowPlaceholderText&gt;   &lt;w:donotpromoteqf/&gt;   &lt;w:lidthemeother&gt;EN-US&lt;/w:LidThemeOther&gt;   &lt;w:lidthemeasian&gt;X-NONE&lt;/w:LidThemeAsian&gt;   &lt;w:lidthemecomplexscript&gt;X-NONE&lt;/w:LidThemeComplexScript&gt;   &lt;w:compatibility&gt;    &lt;w:breakwrappedtables/&gt;    &lt;w:snaptogridincell/&gt;    &lt;w:wraptextwithpunct/&gt;    &lt;w:useasianbreakrules/&gt;    &lt;w:dontgrowautofit/&gt;    &lt;w:splitpgbreakandparamark/&gt;    &lt;w:dontvertaligncellwithsp/&gt;    &lt;w:dontbreakconstrainedforcedtables/&gt;    &lt;w:dontvertalignintxbx/&gt;    &lt;w:word11kerningpairs/&gt;    &lt;w:cachedcolbalance/&gt;   &lt;/w:Compatibility&gt;   &lt;w:browserlevel&gt;MicrosoftInternetExplorer4&lt;/w:BrowserLevel&gt;   &lt;m:mathpr&gt;    &lt;m:mathfont val="Cambria Math"&gt;    &lt;m:brkbin val="before"&gt;    &lt;m:brkbinsub val="&amp;#45;-"&gt;    &lt;m:smallfrac val="off"&gt;    &lt;m:dispdef/&gt;    &lt;m:lmargin val="0"&gt;    &lt;m:rmargin val="0"&gt;    &lt;m:defjc val="centerGroup"&gt;    &lt;m:wrapindent val="1440"&gt;    &lt;m:intlim val="subSup"&gt;    &lt;m:narylim val="undOvr"&gt;   &lt;/m:mathPr&gt;&lt;/w:WordDocument&gt; &lt;/xml&gt;&lt;![endif]--&gt;&lt;!--[if gte mso 9]&gt;&lt;xml&gt;  &lt;w:latentstyles deflockedstate="false" defunhidewhenused="true" defsemihidden="true" defqformat="false" defpriority="99" latentstylecount="267"&gt;   &lt;w:lsdexception locked="false" priority="0" semihidden="false" unhidewhenused="false" qformat="true" name="Normal"&gt;   &lt;w:lsdexception locked="false" priority="9" semihidden="false" unhidewhenused="false" qformat="true" name="heading 1"&gt;   &lt;w:lsdexception locked="false" priority="9" qformat="true" name="heading 2"&gt;   &lt;w:lsdexception locked="false" priority="9" qformat="true" name="heading 3"&gt;   &lt;w:lsdexception locked="false" priority="9" qformat="true" name="heading 4"&gt;   &lt;w:lsdexception locked="false" priority="9" qformat="true" name="heading 5"&gt;   &lt;w:lsdexception locked="false" priority="9" qformat="true" name="heading 6"&gt;   &lt;w:lsdexception locked="false" priority="9" qformat="true" name="heading 7"&gt;   &lt;w:lsdexception locked="false" priority="9" qformat="true" name="heading 8"&gt;   &lt;w:lsdexception locked="false" priority="9" qformat="true" name="heading 9"&gt;   &lt;w:lsdexception locked="false" priority="39" name="toc 1"&gt;   &lt;w:lsdexception locked="false" priority="39" name="toc 2"&gt;   &lt;w:lsdexception locked="false" priority="39" name="toc 3"&gt;   &lt;w:lsdexception locked="false" priority="39" name="toc 4"&gt;   &lt;w:lsdexception locked="false" priority="39" name="toc 5"&gt;   &lt;w:lsdexception locked="false" priority="39" name="toc 6"&gt;   &lt;w:lsdexception locked="false" priority="39" name="toc 7"&gt;   &lt;w:lsdexception locked="false" priority="39" name="toc 8"&gt;   &lt;w:lsdexception locked="false" priority="39" name="toc 9"&gt;   &lt;w:lsdexception locked="false" priority="35" qformat="true" name="caption"&gt;   &lt;w:lsdexception locked="false" priority="10" semihidden="false" unhidewhenused="false" qformat="true" name="Title"&gt;   &lt;w:lsdexception locked="false" priority="1" name="Default Paragraph Font"&gt;   &lt;w:lsdexception locked="false" priority="11" semihidden="false" unhidewhenused="false" qformat="true" name="Subtitle"&gt;   &lt;w:lsdexception locked="false" priority="22" semihidden="false" unhidewhenused="false" qformat="true" name="Strong"&gt;   &lt;w:lsdexception locked="false" priority="20" semihidden="false" unhidewhenused="false" qformat="true" name="Emphasis"&gt;   &lt;w:lsdexception locked="false" priority="59" semihidden="false" unhidewhenused="false" name="Table Grid"&gt;   &lt;w:lsdexception locked="false" unhidewhenused="false" name="Placeholder Text"&gt;   &lt;w:lsdexception locked="false" priority="1" semihidden="false" unhidewhenused="false" qformat="true" name="No Spacing"&gt;   &lt;w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading"&gt;   &lt;w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List"&gt;   &lt;w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid"&gt;   &lt;w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1"&gt;   &lt;w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2"&gt;   &lt;w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1"&gt;   &lt;w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2"&gt;   &lt;w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1"&gt;   &lt;w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2"&gt;   &lt;w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3"&gt;   &lt;w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List"&gt;   &lt;w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading"&gt;   &lt;w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List"&gt;   &lt;w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid"&gt;   &lt;w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 1"&gt;   &lt;w:lsdexception locked="false" unhidewhenused="false" name="Revision"&gt;   &lt;w:lsdexception locked="false" priority="34" semihidden="false" unhidewhenused="false" qformat="true" name="List Paragraph"&gt;   &lt;w:lsdexception locked="false" priority="29" semihidden="false" unhidewhenused="false" qformat="true" name="Quote"&gt;   &lt;w:lsdexception locked="false" priority="30" semihidden="false" unhidewhenused="false" qformat="true" name="Intense Quote"&gt;   &lt;w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 1"&gt;   &lt;w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 2"&gt;   &lt;w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 3"&gt;   &lt;w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 4"&gt;   &lt;w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 5"&gt;   &lt;w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 6"&gt;   &lt;w:lsdexception locked="false" priority="19" semihidden="false" unhidewhenused="false" qformat="true" name="Subtle Emphasis"&gt;   &lt;w:lsdexception locked="false" priority="21" semihidden="false" unhidewhenused="false" qformat="true" name="Intense Emphasis"&gt;   &lt;w:lsdexception locked="false" priority="31" semihidden="false" unhidewhenused="false" qformat="true" name="Subtle Reference"&gt;   &lt;w:lsdexception locked="false" priority="32" semihidden="false" unhidewhenused="false" qformat="true" name="Intense Reference"&gt;   &lt;w:lsdexception locked="false" priority="33" semihidden="false" unhidewhenused="false" qformat="true" name="Book Title"&gt;   &lt;w:lsdexception locked="false" priority="37" name="Bibliography"&gt;   &lt;w:lsdexception locked="false" priority="39" qformat="true" name="TOC Heading"&gt;  &lt;/w:LatentStyles&gt; &lt;/xml&gt;&lt;![endif]--&gt;&lt;style&gt; &lt;!--  /* Font Definitions */  @font-face 	{font-family:"Cambria Math"; 	panose-1:2 4 5 3 5 4 6 3 2 4; 	mso-font-charset:0; 	mso-generic-font-family:roman; 	mso-font-pitch:variable; 	mso-font-signature:-1610611985 1107304683 0 0 159 0;} @font-face 	{font-family:Calibri; 	panose-1:2 15 5 2 2 2 4 3 2 4; 	mso-font-charset:0; 	mso-generic-font-family:swiss; 	mso-font-pitch:variable; 	mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face 	{font-family:Georgia; 	panose-1:2 4 5 2 5 4 5 2 3 3; 	mso-font-charset:0; 	mso-generic-font-family:roman; 	mso-font-pitch:variable; 	mso-font-signature:647 0 0 0 159 0;}  /* Style Definitions */  p.MsoNormal, li.MsoNormal, div.MsoNormal 	{mso-style-unhide:no; 	mso-style-qformat:yes; 	mso-style-parent:""; 	margin-top:0in; 	margin-right:0in; 	margin-bottom:10.0pt; 	margin-left:0in; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:"Calibri","sans-serif"; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:Calibri; 	mso-fareast-theme-font:minor-latin; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin; 	mso-bidi-font-family:"Times New Roman"; 	mso-bidi-theme-font:minor-bidi;} .MsoChpDefault 	{mso-style-type:export-only; 	mso-default-props:yes; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:Calibri; 	mso-fareast-theme-font:minor-latin; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin; 	mso-bidi-font-family:"Times New Roman"; 	mso-bidi-theme-font:minor-bidi;} .MsoPapDefault 	{mso-style-type:export-only; 	margin-bottom:10.0pt; 	line-height:115%;} @page Section1 	{size:8.5in 11.0in; 	margin:1.0in 1.0in 1.0in 1.0in; 	mso-header-margin:.5in; 	mso-footer-margin:.5in; 	mso-paper-source:0;} div.Section1 	{page:Section1;} --&gt; &lt;/style&gt;&lt;!--[if gte mso 10]&gt; &lt;style&gt;  /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:"Table Normal"; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-priority:99; 	mso-style-qformat:yes; 	mso-style-parent:""; 	mso-padding-alt:0in 5.4pt 0in 5.4pt; 	mso-para-margin-top:0in; 	mso-para-margin-right:0in; 	mso-para-margin-bottom:10.0pt; 	mso-para-margin-left:0in; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:"Calibri","sans-serif"; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:"Times New Roman"; 	mso-fareast-theme-font:minor-fareast; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin;} &lt;/style&gt; &lt;![endif]--&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;
&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;Query returns Order with highest sales amount&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;.
&lt;br /&gt;
&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;(b)    &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;Top 10 Order by sa&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;les&lt;/span&gt;&lt;/span&gt;
&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;
&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;EXEC dbo.GetHighestSales_sp NULL, 1, 10&lt;/span&gt;
&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWO-jdu22k9Cu1hUiFDsqczFu3Ys0_MPUtBX-rJ8gX-dYyOXMS531oaTjHEAZ-5uUpCSQHiuiFCP7DFyUdfqxP3ZT_qTxCHNaV4mOIzceFWHtayjn_iA87xQZN4lvDnAq8fPFyoSNvqEo/s1600-h/top10order.jpg"&gt;&lt;img style="cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWO-jdu22k9Cu1hUiFDsqczFu3Ys0_MPUtBX-rJ8gX-dYyOXMS531oaTjHEAZ-5uUpCSQHiuiFCP7DFyUdfqxP3ZT_qTxCHNaV4mOIzceFWHtayjn_iA87xQZN4lvDnAq8fPFyoSNvqEo/s400/top10order.jpg" alt="" id="BLOGGER_PHOTO_ID_5239274001080039634" border="0" /&gt;&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;Query returns Top 10 Orders by sales&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt; amount.
&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2fsql-server-get-nth-highest-using.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2fsql-server-get-nth-highest-using.html&amp;amp;bgcolor=336688" alt="kick it on DotNetKicks.com" border="0" /&gt;&lt;/a&gt;
&lt;br /&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/8380104260460686452/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/8380104260460686452" rel="replies" title="0 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/8380104260460686452" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/8380104260460686452" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2008/08/sql-server-get-nth-highest-using.html" rel="alternate" title="SQL Server - Get Nth Highest using ROW_NUMBER() Function" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNoPe6qq3a7nI7udPvtoMiIwxDOVt1ZaN00iAtpsxfC0aNn1gD74e5_qTz5snWiH-lZgU9ALIGo5OKEbwqTsVrjLxTmI28NIXvkCjZSV4WaqOhgcb27PcV-jIyXrx1tr6uHEPAdQSg2Ac/s72-c/toporder.jpg" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-8222225292749309608</id><published>2008-08-12T18:16:00.116-04:00</published><updated>2008-11-02T16:07:57.277-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="MOSS 2007"/><title type="text">MOSS 2007 - Data Caching Vs List Iteration</title><content type="html">&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;MOSS developers most of the time need to retrieve data from SharePoint Lists.  In one of the MOSS project I worked there were several provisioned .ASPX Pages which lacked in performance.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: georgia;"&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt; The pages took more than usual time to render, on average a page displaying 1000+ List records (we are not discussing Pagination here) was taking approximately 2 minutes! Coming from ASP.NET/SQL background this was just unacceptable.&lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;The code was nicely structured with N-Tier architecture, all the MOSS specific code was residing in Data Access Layer (DAL) and .ASPX pages were consumers.&lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;DAL consisted of function which was returning a DataTable with all the relevant data to be displayed on the .ASPX page. &lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;The problem code was in this function, below is an excerpt of code from this function.  &lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:100%;" &gt;//load datatable&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;DataTable dt = objDB.GetAllData();&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;//this happens for 1000+ rows&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;foreach (DataRowView dr in pages.DefaultView)&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;....&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;....&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;....&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(255, 102, 0); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;DataRow viewRow            = null;&lt;br /&gt;//problem code &lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(255, 102, 0); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;viewRow["MyRow"]       = objDB.GetNameByID(dr["ID"].ToString())["Name"].ToString();&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;....&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;....&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;....&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;//gets Name by ID&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;public SPListItem GetNameById(string id)&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;int itemId = -1;&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;if (Int32.TryParse(id, out itemId))&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;using (SPSite site = new SPSite(SiteUrl))&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;using (SPWeb web = site.OpenWeb())&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;SPList list = web.Lists["ListData"];&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;return list.GetItemById(itemId);&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;return null;&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;Code highlighted with &lt;span style="color: rgb(255, 102, 0);"&gt;orange&lt;/span&gt; shows the problem area, if the outer loop is executing 1000+ times then this lookup function is called 1000+ times along with expensive SPSite, SPWeb and individual lookup which works satisfactorily if you have 100-200 rows but the performance starts to deteriorate exponentially with the increase in number of calls.&lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;b&gt;Fix:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;After the reviewing  the code, I recognized the need to avoid calling lookup function 1000+ times and instead have an inline cache to do lookup.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;I am not talking about .NET cache here or MOSS 2007 object cache but DataView which can be used as a temporary cache.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:100%;" &gt;//gets Name by ID revised to return DataView&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;public DataView GetAllNameById()&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;using (SPSite site = new SPSite(SiteUrl))&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;using (SPWeb web = site.OpenWeb())&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;SPList oList                        = web.Lists["ListData"];&lt;/span&gt;&lt;/div&gt;&lt;div  style="color: rgb(51, 51, 255); font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;SPQuery oQuery       = new SPQuery();&lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;oQuery.Query                = "&lt;/span&gt;&lt;orderby&gt;&lt;fieldref name="ID" style="color: rgb(0, 0, 255);"&gt;&lt;/fieldref&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;";&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;                     oQuery.RowLimit     = 0;&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;          &lt;br /&gt;DataView dv                     = new DataView();&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;          &lt;br /&gt;dv                                                       = oList.GetItems(oQuery).GetDataTable().DefaultView;&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;          &lt;br /&gt;//indexes, helps in faster lookup&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;          &lt;br /&gt;dv.Sort                                        = "ID";&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;          &lt;br /&gt;//return sorted dataview&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;          &lt;br /&gt;return dv;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;                 }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;/orderby&gt;&lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;orderby&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Let us see the revised code excerpt once again:&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;/orderby&gt;&lt;/span&gt;&lt;/div&gt;&lt;div  style="font-family: georgia;font-family:Georgia,&amp;quot;;"&gt;&lt;span style="font-size:100%;"&gt;&lt;orderby&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;//cache all data to avoid query MOSS on every request&lt;/span&gt;&lt;/span&gt; &lt;span style="color: rgb(255, 102, 0);"&gt;&lt;br /&gt;DataView dv  = objDB.GetAllNameById();&lt;br /&gt;&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;//load datatable&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; DataTable dt = objDB.GetAllData();&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;&lt;br /&gt;//this happens for 1000+ rows&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;&lt;br /&gt;foreach (DataRowView dr in pages.DefaultView)&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;&lt;br /&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; ....&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; ....&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; ....&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; DataRow viewRow          = null;&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;//revised code&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt; &lt;span style="color: rgb(255, 102, 0);"&gt; DataRowView[] drv       = dv.FindRows(dr["Name"].ToString());&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;if (null != drv)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;viewRow["MyRow"] = drv[0]["Name"].ToString();&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/orderby&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;....&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;orderby&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;  &lt;/span&gt;&lt;/orderby&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;orderby&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;....&lt;/span&gt;&lt;/orderby&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;orderby&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;....&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; }&lt;/span&gt;  &lt;/orderby&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;orderby&gt;&lt;br /&gt;With this change we are not using expensive SPSite, SPWeb objects and lookup on every iteration but caching the entire lookup List in an indexed DataView, when we are doing the lookups we are not querying a List but querying a DataView which is much Faster!&lt;/orderby&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt; With this code change the Page rendering time changed from 2 minutes to 7 seconds! &lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;orderby&gt;&lt;br /&gt;&lt;/orderby&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: georgia;font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2fmoss-2007-data-caching-vs-list.html"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2fmoss-2007-data-caching-vs-list.html&amp;amp;bgcolor=336688" border="0" /&gt;&lt;/a&gt;&lt;/span&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/8222225292749309608/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/8222225292749309608" rel="replies" title="0 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/8222225292749309608" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/8222225292749309608" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2008/08/moss-2007-data-caching-vs-list.html" rel="alternate" title="MOSS 2007 - Data Caching Vs List Iteration" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-8792265053472479182</id><published>2008-08-08T16:00:00.003-04:00</published><updated>2008-08-08T16:06:10.544-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="T-SQL"/><title type="text">SQL Server - Using EXCEPT Operator</title><content type="html">&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="font-style: italic;font-size:78%;" &gt;* Compatibility SQL Server 2005 onwards&lt;/span&gt;&lt;br /&gt;EXCEPT operator was introduced in SQL Server 2005. &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;EXCEPT &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;can be used to return &lt;span style="color: rgb(0, 0, 0);"&gt;distinct&lt;/span&gt; records from the left side of a query and which are not located on the right side.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;EXCEPT operator has the following behavior&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;• &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;EXCEPT &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;by default returns a DISTINCT result set&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;• &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;EXCEPT &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;requires same column number and order on both side of the query with comparable data type&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;For more details on &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;EXCEPT &lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;visit: &lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;a style="font-family: georgia;" href="http://msdn.microsoft.com/en-us/library/ms188055.aspx" target="_blank"&gt;MSDN&lt;/a&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;br /&gt;Let us consider a real-world &lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;EXCEPT &lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;example using NorthWind database. You can download and install NorthWind database from the &lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&amp;amp;displaylang=en"&gt;following location&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;(a)    Customers who never placed any Order&lt;/span&gt;&lt;span style="font-weight: bold;"&gt; (Using EXCEPT)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;SELECT&lt;br /&gt;   CustomerID&lt;br /&gt;FROM&lt;br /&gt;   Customers&lt;br /&gt;EXCEPT&lt;br /&gt;SELECT&lt;br /&gt;   CustomerID&lt;br /&gt;FROM&lt;br /&gt;   Orders&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;CustomerID&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;--------------&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;FISSA&lt;br /&gt;PARIS&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;….&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;--------------&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;Query returns 2 rows&lt;/span&gt;, which are unique CustomerID. The above query demonstrated that we can extract unique CustomerID for all the Customers who never placed an Order using &lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;EXCEPT &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;operator instead of writing a LEFT OUTER JOIN syntax to accomplish the same output.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;(b)    Customers who never placed any Order&lt;/span&gt;&lt;span style="font-weight: bold;"&gt; (Using LEFT OUTER JOIN)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;SELECT&lt;br /&gt;   DISTINCT C.CustomerID&lt;br /&gt;FROM&lt;br /&gt;   Customers C&lt;br /&gt;LEFT OUTER JOIN Orders O ON C.CustomerID = O.CustomerID  &lt;br /&gt;WHERE&lt;br /&gt;   O.CustomerID IS NULL&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;CustomerID&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;--------------&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;FISSA&lt;br /&gt;PARIS&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;….&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;--------------&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;Query returns same output as &lt;span style="font-weight: bold;"&gt;(a)&lt;/span&gt; using LEFT OUTER JOIN&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt; syntax&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2fsql-server-using-except-operator.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2fsql-server-using-except-operator.html&amp;bgcolor=336688" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/8792265053472479182/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/8792265053472479182" rel="replies" title="1 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/8792265053472479182" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/8792265053472479182" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2008/08/sql-server-using-except-operator.html" rel="alternate" title="SQL Server - Using EXCEPT Operator" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-5847745930985998893</id><published>2008-08-07T15:14:00.028-04:00</published><updated>2008-08-08T15:43:56.720-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="T-SQL"/><title type="text">SQL Server – Using INTERSECT Operator</title><content type="html">&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="font-style: italic;font-size:78%;" &gt;* Compatibility SQL Server 2005 onwards&lt;/span&gt;&lt;br /&gt;INTERSECT operator was introduced in SQL Server 2005. INTERSECT can be used to return &lt;span style="color: rgb(0, 0, 0);"&gt;distinct&lt;/span&gt; records from the left and right side of a query.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;INTERSECT is often compared with INNER JOINS, it can be said that INTERSECT is almost same as INNER JOIN with the following key differences:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;• INTERSECT by default returns a DISTINCT result set whereas INNER JOIN return duplicates unless  it is instructed not to do so by using DISTINCT clause to the SELECT statement&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;• INTERSECT requires same column number and order on both side of the query with comparable data type, INNER JOIN does not require same column number and order condition&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;For more details on INTERSECT &lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;visit: &lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;a style="font-family: georgia;" href="http://msdn.microsoft.com/en-us/library/ms188055.aspx" target="_blank"&gt;MSDN&lt;/a&gt;&lt;/span&gt;&lt;span style=";font-family:georgia;font-size:100%;"  &gt;&lt;br /&gt;&lt;br /&gt;Let us consider a real-world INTERSECT example using NorthWind database. You can download and install NorthWind database from the &lt;a target="_blank" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&amp;amp;displaylang=en"&gt;following location&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;(a)    How many Products have been at least ordered once&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;SELECT &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;      ProductID&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;FROM&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;    Products&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;INTERSECT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;SELECT &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;     ProductID&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;FROM&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;     [Order Details]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;ProductID&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;--------------&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;3&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;4&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;5&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;….&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;….&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;Query returns 77 rows&lt;/span&gt;, which are unique ProductID. The above query demonstrated that we can extract unique ProductID for all the Products purchased using INTERSECT operator instead of writing an INNER JOIN to accomplish the same output&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-family:georgia;" &gt;(b)    INTERSECT Vs INNER JOIN&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;SELECT &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt; ProductID&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;FROM&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;     Products&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;INTERSECT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;SELECT &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;     ProductID&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;FROM&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;     [Order Details]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;ProductID&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;--------------&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;3&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;4&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;5&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;….&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;….&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;Returns 77 rows&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;SELECT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;     ORD.OrderID&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;FROM&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;     [Order Details] ORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;INNER JOIN  Orders O ON O.OrderID = ORD.OrderID&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;ProductID&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;------------&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;3&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;4&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;5&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;……&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;……&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;Query returns 2155 rows&lt;/span&gt;, which are non-unique ProductID. The above query demonstrated  the default behavior of INNER JOIN&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;SELECT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;    DISTINCT ORD.OrderID&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;FROM&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;    [Order Details] ORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:georgia;" &gt;INNER JOIN  Orders O ON O.OrderID = ORD.OrderID&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;ProductID&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;------------&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;3&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;4&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;5&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;……&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;……&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:georgia;"&gt;Query returns 77 rows&lt;/span&gt;, which are unique ProductID as we used DISTINCT keyword to filter out the duplicates.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2fsql-server-using-intersect_07.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2fsql-server-using-intersect_07.html&amp;amp;bgcolor=336688" alt="kick it on DotNetKicks.com" border="0" /&gt;&lt;/a&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/5847745930985998893/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/5847745930985998893" rel="replies" title="0 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/5847745930985998893" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/5847745930985998893" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2008/08/sql-server-using-intersect_07.html" rel="alternate" title="SQL Server – Using INTERSECT Operator" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-7476067538732976574</id><published>2008-08-05T20:12:00.040-04:00</published><updated>2008-08-21T16:48:27.516-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="T-SQL"/><title type="text">SQL Server - Common Table Expressions (CTE)</title><content type="html">&lt;div style="text-align: left;"&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 0);font-family:georgia;font-size:78%;"  &gt;&lt;span style="color: rgb(0, 0, 0);"&gt;* Compatibility: SQL Server 2005 onwards&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Common Table Expressions (CTE) construct was introduced with Microsoft SQL Server 2005. CTE behaves like a derived table but unlike derived table which has statement level scope CTE offers a procedure level.&lt;br /&gt;&lt;/div&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;CTE helps in writing more modular code, which helps in better readability and reducing complexity of query.&lt;br /&gt;&lt;br /&gt;CTE provides following benefits:&lt;br /&gt;. Ability to call block of query (CTE declaration) multiple times like a function call in modern&lt;br /&gt;programming language&lt;br /&gt;. Ability to provide recursion logic (with robust exit condition)&lt;br /&gt;. Ability to use result set (CTE call) in multiple statements or single statement (self join)&lt;br /&gt;&lt;br /&gt;CTE is described with the following syntax:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;WITH MyCTE1 (OrderID, CustomerID) AS&lt;br /&gt;(&lt;br /&gt;SELECT&lt;br /&gt;OrderID, CustomerID&lt;br /&gt;FROM&lt;br /&gt;MyTable1&lt;br /&gt;)&lt;br /&gt;, MyCTE2 (FirstName, LastName)&lt;br /&gt;(&lt;br /&gt;SELECT&lt;br /&gt;FirstName, LastName&lt;br /&gt;FROM&lt;br /&gt;MyTable2&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;)&lt;br /&gt;/*Multiple CTE’s can be declared with ‘,’*/&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;Where&lt;br /&gt;. MyCTE1 is CTE Name used to reference CTE in queries&lt;br /&gt;. Column list (optional), good practice to declare them to avoid confusion&lt;br /&gt;. Query declaration after the AS keyword&lt;br /&gt;. Multiple CTE’s can be declared putting ‘,’ after previous ending declaration&lt;br /&gt;&lt;br /&gt;CTE can then be used as following (we will see detailed example later):&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT * FROM MyCTE&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;CTE can be declared in stored procedures, user-defined functions (UDF), triggers, or views.&lt;br /&gt;&lt;br /&gt;For more details on CTE’s visit: &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms190766.aspx" target="_blank"&gt;&lt;span style="font-family:georgia;"&gt;MSDN&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;br /&gt;Let us consider a real-world CTE example using NorthWind database. You can download and install NorthWind database from the &lt;/span&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&amp;amp;displaylang=en" target="_blank"&gt;&lt;span style="font-family:georgia;"&gt;following location&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:georgia;"&gt;.&lt;br /&gt;&lt;br /&gt;Consider this scenario; we want to run a report which gives us Order detail by OrderID, EmployeeID or CustomerID, our sample query will only return Employee name, Contact name, Company name, Phone, Fax and Total purchase price for the order (the objective is to make the stored procedure flexible enough to fetch data for all the input parameters or some).&lt;br /&gt;&lt;br /&gt;If we follow conventional T-SQL approach we would have to write multiple T-SQL statements to achieve our objective, but as you can see with one stored procedure using combination of CTE, Dynamic tables and Coalesce statement we made the entire stored procedure multi-functional, we can use the stored procedure to run reports like Sales by Employee, Sales by Customer, Overall Sales total and many more cases depending on how you use the data, of course we can change the query criteria to make our stored procedure even more flexible or extendable.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Stored procedure code:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-size:100%;" &gt;&lt;span style="font-family:georgia;"&gt;USE [Northwind]&lt;br /&gt;GO&lt;br /&gt;/****** Object:  StoredProcedure [dbo].[GetCustomerOrders_sp]    Script Date: 08/17/2008 21:37:16 ******/&lt;br /&gt;SET ANSI_NULLS OFF&lt;br /&gt;GO&lt;br /&gt;SET QUOTED_IDENTIFIER ON&lt;br /&gt;GO&lt;br /&gt;/*&lt;br /&gt;--all&lt;br /&gt;EXEC dbo.GetCustomerOrders_sp null, null, null&lt;br /&gt;--by OrderID&lt;br /&gt;EXEC dbo.GetCustomerOrders_sp 10682, null, null&lt;br /&gt;--by CustomerID&lt;br /&gt;EXEC dbo.GetCustomerOrders_sp null, null, 'ANTON'&lt;br /&gt;--by EmployeeID&lt;br /&gt;EXEC dbo.GetCustomerOrders_sp null, 9, null&lt;br /&gt;--exact match&lt;br /&gt;EXEC dbo.GetCustomerOrders_sp 10263, 9, 'ERNSH'&lt;br /&gt;*/&lt;br /&gt;ALTER PROCEDURE [dbo].[GetCustomerOrders_sp]&lt;br /&gt;(&lt;br /&gt;@OrderID            int                = null,&lt;br /&gt;@EmployeeID            int                = null,&lt;br /&gt;@CustomerID            nVarchar(5)        = null&lt;br /&gt;)&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;--no extra columns&lt;br /&gt;SET NOCOUNT ON;&lt;br /&gt;&lt;br /&gt;/*CTE Declaration block    */&lt;br /&gt;WITH CustomerOrders (OrderID, CustomerID, EmployeeID, ContactName, CompanyName, Phone, Fax, TotalPurchasePrice) AS&lt;br /&gt;(&lt;br /&gt;  SELECT  OrderTotal.OrderID As OrderID, C.CustomerID As CustomerID, E.EmployeeID As EmployeeID,&lt;br /&gt;          C.ContactName, C.CompanyName, C.Phone, C.Fax, OrderTotal.TotalPurchasePrice                             &lt;br /&gt;  FROM&lt;br /&gt;      Customers C&lt;br /&gt;  INNER JOIN&lt;br /&gt;  (&lt;br /&gt;  SELECT ORD.OrderID As OrderID, O.CustomerID, O.EmployeeID,&lt;br /&gt;  Sum(CAST((UnitPrice * Quantity) * (1-Discount)/100 * 100 AS Money)) AS  TotalPurchasePrice&lt;br /&gt;  FROM&lt;br /&gt;  [Order Details] ORD&lt;br /&gt;  INNER JOIN ORDERS O ON O.OrderID = ORD.OrderID&lt;br /&gt;  GROUP BY ORD.ORDERID, O.CustomerID, O.EmployeeID&lt;br /&gt;  ) AS OrderTotal  ON OrderTotal.CustomerID = C.CustomerID&lt;br /&gt;  INNER JOIN Employees E ON OrderTotal.EmployeeID = E.EmployeeID&lt;br /&gt;),&lt;br /&gt;CustomerDetail (CustomerID, ContactName, CompanyName, Phone, Fax) AS&lt;br /&gt;(&lt;br /&gt;  SELECT CustomerID, ContactName, CompanyName, Phone, Fax&lt;br /&gt;  FROM Customers&lt;br /&gt;)&lt;br /&gt;/*Note Multiple CTE's can be declared in declaration block*/&lt;br /&gt;--determine which result to extract&lt;br /&gt;SELECT&lt;br /&gt;  OrderID, E.LastName + ', ' + E.FirstName AS EmployeeName, E.EmployeeID, ContactName, CompanyName, Phone, Fax, TotalPurchasePrice&lt;br /&gt;FROM&lt;br /&gt;  Employees E&lt;br /&gt;INNER JOIN CustomerOrders CO ON CO.EmployeeID = E.EmployeeID&lt;br /&gt;WHERE&lt;br /&gt;  CO.EmployeeID LIKE Coalesce(@EmployeeID, CO.EmployeeID)&lt;br /&gt;  AND CO.OrderID LIKE Coalesce(@OrderID, CO.OrderID)&lt;br /&gt;  AND CO.CustomerID LIKE Coalesce(@CustomerID, CO.CustomerID) &lt;br /&gt;ORDER BY E.EmployeeID ASC&lt;br /&gt;END &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;strong&gt;Test Scripts result: &lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Note: Result sets images are adjusted to show the first few records and rest are omitted&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;--all&lt;/strong&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;EXEC dbo.GetCustomerOrders_sp null, null, null&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEisDMwW7Pn5Z4F_H9EtU-M-sUj4wc21X7TipJYKL6Lyoyl5lazFrW06VAAwyADLtsps4qWoZOkxrtQoiwmEdrOEBtF4Fd2BNdqzyOUO9bbKoUTWwbOOKdQQ5nUqGg90t2EMhiDXoFpRjgw/s1600-h/All.jpg"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEisDMwW7Pn5Z4F_H9EtU-M-sUj4wc21X7TipJYKL6Lyoyl5lazFrW06VAAwyADLtsps4qWoZOkxrtQoiwmEdrOEBtF4Fd2BNdqzyOUO9bbKoUTWwbOOKdQQ5nUqGg90t2EMhiDXoFpRjgw/s400/All.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5237074579382032514" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;--&lt;strong&gt;by OrderID &lt;/strong&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;EXEC dbo.GetCustomerOrders_sp 10682, null, null&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZtp14bbHNK24B3c1KbjRhIHanEKWC6mI0AloZ32BJv7-iuwHH8a9fXI3MnUVU8mhGgj0cEGX-cNs_lYZqZtkjIa7EgGW8AVRndpQfl4bAtsqP-ix9gUd62Hu67e1-G71T8hXU1CqA-aU/s1600-h/ByOrderID.jpg"&gt;&lt;img style="cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZtp14bbHNK24B3c1KbjRhIHanEKWC6mI0AloZ32BJv7-iuwHH8a9fXI3MnUVU8mhGgj0cEGX-cNs_lYZqZtkjIa7EgGW8AVRndpQfl4bAtsqP-ix9gUd62Hu67e1-G71T8hXU1CqA-aU/s400/ByOrderID.jpg" alt="" id="BLOGGER_PHOTO_ID_5235694076650540018" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;strong&gt;--by CustomerID&lt;/strong&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;EXEC dbo.GetCustomerOrders_sp null, null, 'ANTON'&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgn-gC4z8XT9RmK5diozm6mcIVyv0VHRWgOVpHbhVeEhWiad7Vxdjq4q9Sr9EhuuIf_SmWypODSXqHVsNx7_Lrwr4YF35jyeVtMwVSKfqaVGa21sMWNTME7BANII5bn5xjzqZqleZYdFdU/s1600-h/ByCustomerID.jpg"&gt;&lt;img style="cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgn-gC4z8XT9RmK5diozm6mcIVyv0VHRWgOVpHbhVeEhWiad7Vxdjq4q9Sr9EhuuIf_SmWypODSXqHVsNx7_Lrwr4YF35jyeVtMwVSKfqaVGa21sMWNTME7BANII5bn5xjzqZqleZYdFdU/s400/ByCustomerID.jpg" alt="" id="BLOGGER_PHOTO_ID_5235695196219097522" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;strong&gt;--by EmployeeID&lt;/strong&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;EXEC dbo.GetCustomerOrders_sp null, 9, null&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhO8Q0eCWLrDZyy7kSA2-q8IQMUk14rt5_UCyhlZOicy-g7jP8XPQQPbAK2NFv-huChGwiINKjzDOjyvqTpxj6IbynWhUPdLUrlLeCEYwopg0O5rbElTdhMiHts3Pw8LRP9eZ-aAT_tX0w/s1600-h/ByEmployeeID.jpg"&gt;&lt;img style="cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhO8Q0eCWLrDZyy7kSA2-q8IQMUk14rt5_UCyhlZOicy-g7jP8XPQQPbAK2NFv-huChGwiINKjzDOjyvqTpxj6IbynWhUPdLUrlLeCEYwopg0O5rbElTdhMiHts3Pw8LRP9eZ-aAT_tX0w/s400/ByEmployeeID.jpg" alt="" id="BLOGGER_PHOTO_ID_5235696944802478002" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;strong&gt;--exact match&lt;/strong&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;EXEC dbo.GetCustomerOrders_sp 10263, 9, 'ERNSH'&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6cCmxNuaBHtf2Z5esa5yazXFZMSOZW7eXSqAOqh0Eg7hLgnZGS4ZkDD7I7aU4swliVMhNPdDZnYePO_gsqikbpkBP-wErlxUWucVw9yv-TrrxqBGQrTWGMNZXq8ruD6Xr1h16RjNR4ig/s1600-h/ByExact.jpg"&gt;&lt;img style="cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6cCmxNuaBHtf2Z5esa5yazXFZMSOZW7eXSqAOqh0Eg7hLgnZGS4ZkDD7I7aU4swliVMhNPdDZnYePO_gsqikbpkBP-wErlxUWucVw9yv-TrrxqBGQrTWGMNZXq8ruD6Xr1h16RjNR4ig/s400/ByExact.jpg" alt="" id="BLOGGER_PHOTO_ID_5235700869277845970" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f07%2fcommon-table-expressions-cte.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f07%2fcommon-table-expressions-cte.html&amp;amp;bgcolor=336688" alt="kick it on DotNetKicks.com" border="0" /&gt;&lt;/a&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/7476067538732976574/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/7476067538732976574" rel="replies" title="0 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/7476067538732976574" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/7476067538732976574" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2008/07/common-table-expressions-cte.html" rel="alternate" title="SQL Server - Common Table Expressions (CTE)" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEisDMwW7Pn5Z4F_H9EtU-M-sUj4wc21X7TipJYKL6Lyoyl5lazFrW06VAAwyADLtsps4qWoZOkxrtQoiwmEdrOEBtF4Fd2BNdqzyOUO9bbKoUTWwbOOKdQQ5nUqGg90t2EMhiDXoFpRjgw/s72-c/All.jpg" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9150837343934029699.post-4887108598849564850</id><published>2008-08-01T17:00:00.017-04:00</published><updated>2008-08-27T15:40:06.926-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2000"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="T-SQL"/><title type="text">SQL Server – Power of CASE Function</title><content type="html">&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(0, 0, 0); font-style: italic;font-size:78%;" &gt;&lt;strong&gt;&lt;span style="font-weight: normal;"&gt;* Compatibility SQL Server 2000 onwards&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;CASE statement can be used to introduce conditional logic. CASE can be used to evaluate multiple statements and return one possible output.&lt;br /&gt;&lt;br /&gt;CASE can be compared to Switch statement in modern programming languages like C#.&lt;br /&gt;&lt;br /&gt;CASE statement provides following benefits:&lt;br /&gt;&lt;/span&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-family:georgia;"&gt;Ability to format output&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family:georgia;"&gt;Ability to perform conditional logic on a Column&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family:georgia;"&gt;Ability to optimize query by emitting the need to write multiple queries to&lt;br /&gt;return a result set&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family:georgia;"&gt;Ability to perform conditional Update&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family:georgia;"&gt;Ability to provide dynamic Order By in single query&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;CASE functions are of 2 types: &lt;/span&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-family:georgia;"&gt;&lt;strong&gt;Simple&lt;/strong&gt; [Comparison is done with substituted inline values]&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family:georgia;"&gt;&lt;strong&gt;Searched&lt;/strong&gt; [Comparison is done on Column level]&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;For more details on CASE syntax visit: &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx" target="_blamk"&gt;&lt;span style="font-family:georgia;"&gt;MSDN&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:georgia;"&gt; &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;Let us see CASE statement in action for the different type of scenarios:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-size:100%;"&gt;&lt;strong&gt;(a) Formatted output using Simple CASE function:&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT&lt;br /&gt;OrderID, OrderDate, ShipVia,&lt;br /&gt;Shipper =&lt;br /&gt;(CASE SHIPVIA&lt;br /&gt;WHEN 1 THEN 'Speedy Express'&lt;br /&gt;WHEN 2 THEN 'United Package'&lt;br /&gt;WHEN 3 THEN 'Federal Shipping'&lt;br /&gt;ELSE 'Unknown'&lt;br /&gt;END)&lt;br /&gt;FROM&lt;br /&gt;Orders&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNHwYFWeKF8HGumZBPF1vxrMXHMbpcpjUjYmxzRPKZOZyphoaLoUFM45eSFzOc0prGLlJ5keML0IB69suk3ix-dbQHG77lyeaDTC5a188ETTRPRk9HUGeilvf4olnohDz9qZQzYOYL9J0/s1600-h/formatted.jpg"&gt;&lt;span style="font-family:georgia;"&gt;&lt;img id="BLOGGER_PHOTO_ID_5230779553214718386" style="" alt="" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNHwYFWeKF8HGumZBPF1vxrMXHMbpcpjUjYmxzRPKZOZyphoaLoUFM45eSFzOc0prGLlJ5keML0IB69suk3ix-dbQHG77lyeaDTC5a188ETTRPRk9HUGeilvf4olnohDz9qZQzYOYL9J0/s400/formatted.jpg" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;(b) Column level comparison using Searched CASE function: &lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;SELECT&lt;br /&gt;OrderID, OrderDate, Freight,&lt;br /&gt;FreightCategory =&lt;br /&gt;(CASE&lt;br /&gt;WHEN Freight &lt;= 0 THEN 'No Freight' &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;WHEN (Freight &gt;= 1 AND Freight &lt;= 100) THEN 'Low Freight' &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;WHEN (Freight &gt;= 100 ) THEN 'High Freight'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;ELSE 'Unknown Freight'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;END)&lt;br /&gt;FROM &lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;Orders &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjE-IabVL_KfzimZvmR4zY-qveD3w4DKO460yNM_Fiu7iNyYnJfunMQsj1YhfOrTUhz9LLDwGhD66rOTrhMBuPyy7MHTcajElm7SRPHpvQhHUEv8b2P3oQ20QuL3FCiyE0Ws0qXMCSAdLo/s1600-h/ColumnLevel.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5230795895561775906" style="" alt="" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjE-IabVL_KfzimZvmR4zY-qveD3w4DKO460yNM_Fiu7iNyYnJfunMQsj1YhfOrTUhz9LLDwGhD66rOTrhMBuPyy7MHTcajElm7SRPHpvQhHUEv8b2P3oQ20QuL3FCiyE0Ws0qXMCSAdLo/s400/ColumnLevel.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;div&gt;&lt;span style="font-family:georgia;"&gt;&lt;strong&gt;(c) Dynamic Order By functionality using CASE function: &lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;This is helpful in the scenario when query needs to return the output by multiple columns. CASE function can be used to accomplish this by using Column names as variable which can be evaluated and substituted in the order by clause criteria.&lt;br /&gt;&lt;br /&gt;Sample example below shows a query which can be dynamically sorted by LastName, FirstName and EmployeeID (default), the same query can be packaged into a stored procedure where it can take column names as a parameter and return the output accordingly:&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;--dynamic Order By&lt;br /&gt;Declare @OrderBy Varchar(100)&lt;br /&gt;--order by LastName&lt;br /&gt;SELECT @OrderBy = 'LastName'&lt;br /&gt;&lt;br /&gt;SELECT&lt;br /&gt;EmployeeID, LastName, FirstName, Title, BirthDate, HireDate&lt;br /&gt;FROM&lt;br /&gt;Employees&lt;br /&gt;ORDER BY&lt;br /&gt;CASE&lt;br /&gt;WHEN @OrderBy = 'LastName' THEN LastName&lt;br /&gt;WHEN @OrderBy = 'FirstName' THEN FirstName&lt;br /&gt;--default&lt;br /&gt;ELSE Cast(EmployeeID As Varchar)&lt;br /&gt;END&lt;br /&gt;&lt;br /&gt;--dynamic Order By&lt;br /&gt;Declare @OrderBy Varchar(100)&lt;br /&gt;--order by FirstName&lt;br /&gt;SELECT @OrderBy = 'FirstName'&lt;br /&gt;&lt;br /&gt;SELECT&lt;br /&gt;EmployeeID, LastName, FirstName, Title, BirthDate, HireDate&lt;br /&gt;FROM&lt;br /&gt;Employees&lt;br /&gt;ORDER BY&lt;br /&gt;CASE&lt;br /&gt;WHEN @OrderBy = 'LastName' THEN LastName&lt;br /&gt;WHEN @OrderBy = 'FirstName' THEN FirstName&lt;br /&gt;--default&lt;br /&gt;ELSE Cast(EmployeeID As Varchar)&lt;br /&gt;END &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxfOpH7Z4HsJ_I97434qV27XUJsT85-n92frvL-x-9cAHhftNTQ0r7UPCDcE95WP4nRIGLvgFNWDSPyZEGPMQDnfuWVnkm3_vHFIPL0ylJvqyBJXigPPhnZVhv8Yk-Po21MdOwtVdbeYU/s1600-h/DynamicOrderBy.jpg"&gt;&lt;span style="font-family:georgia;"&gt;&lt;img id="BLOGGER_PHOTO_ID_5230781423587893682" style="" alt="" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxfOpH7Z4HsJ_I97434qV27XUJsT85-n92frvL-x-9cAHhftNTQ0r7UPCDcE95WP4nRIGLvgFNWDSPyZEGPMQDnfuWVnkm3_vHFIPL0ylJvqyBJXigPPhnZVhv8Yk-Po21MdOwtVdbeYU/s400/DynamicOrderBy.jpg" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;strong&gt;(d) Conditional Update functionality using CASE function:&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;This is helpful in the scenario when multiple updates are needed for a particular table. CASE function can be used to accomplish the same operation using single update statement instead of writing multiple update statements for each criteria.&lt;br /&gt;&lt;br /&gt;Sample example below adds $8 to all the Products which have UnitPrice less than 50, adds $5 to Products having UnitPrice greater than 50 and less than 100, adds $3 to Products having UnitPrice greater than 100 and finally for conditions which are falling in any of the criteria it puts the same UnitPrice (unchanged):&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;--put in temporary transaction to perform rollback&lt;br /&gt;BEGIN TRAN&lt;br /&gt;UPDATE Products SET UnitPrice =&lt;br /&gt;CASE&lt;br /&gt;WHEN (UnitPrice &gt;= 0 AND UnitPrice &lt;= 50)     THEN (UnitPrice + 8) &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;WHEN (UnitPrice &gt;= 50 AND UnitPrice &lt;= 100) THEN (UnitPrice + 5) &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;WHEN (UnitPrice &gt;= 100)                                        THEN (UnitPrice + 3)&lt;br /&gt;ELSE UnitPrice&lt;br /&gt;END&lt;br /&gt;--roll back the changes&lt;br /&gt;ROLLBACK&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwPKgKRdCb22czqUKRFbkAPZUQVcGX9R6qEbxbNAsYpdOk7lb00OAQYCNeUGunyDUJ_L8NvxsjMFY4BBMc7Uk2V3gdGUvkNxct1U4I3x-xyy9hY7PZoo2e-pl_spcmkfowRTKuwuXhcD0/s1600-h/ConditionalUpdate.jpg"&gt;&lt;span style="font-family:georgia;"&gt;&lt;img id="BLOGGER_PHOTO_ID_5230781790269670674" style="" alt="" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwPKgKRdCb22czqUKRFbkAPZUQVcGX9R6qEbxbNAsYpdOk7lb00OAQYCNeUGunyDUJ_L8NvxsjMFY4BBMc7Uk2V3gdGUvkNxct1U4I3x-xyy9hY7PZoo2e-pl_spcmkfowRTKuwuXhcD0/s400/ConditionalUpdate.jpg" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2ft-sql-power-of-case-function.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsyedarizvi.blogspot.com%2f2008%2f08%2ft-sql-power-of-case-function.html&amp;amp;bgcolor=336688" alt="kick it on DotNetKicks.com" border="0" /&gt;&lt;/a&gt;</content><link href="http://syedarizvi.blogspot.com/feeds/4887108598849564850/comments/default" rel="replies" title="Post Comments" type="application/atom+xml"/><link href="http://www.blogger.com/comment/fullpage/post/9150837343934029699/4887108598849564850" rel="replies" title="0 Comments" type="text/html"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/4887108598849564850" rel="edit" type="application/atom+xml"/><link href="http://www.blogger.com/feeds/9150837343934029699/posts/default/4887108598849564850" rel="self" type="application/atom+xml"/><link href="http://syedarizvi.blogspot.com/2008/08/t-sql-power-of-case-function.html" rel="alternate" title="SQL Server – Power of CASE Function" type="text/html"/><author><name>Syed A. Rizvi</name><uri>http://www.blogger.com/profile/16524399726573653900</uri><email>noreply@blogger.com</email><gd:image height="24" rel="http://schemas.google.com/g/2005#thumbnail" src="//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXiS2nEbI7-57o07fao1m_dDgCDP80YZAfth8H64nleolfDFDCtyFwlzhX-hOlzP9j2LXYSETIbLrhzD_88UqsZbfsjzZnsHYmtr7Q1yViADUSQFN85flk1bPKfY5wjg/s220/Snap1.JPG" width="32"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNHwYFWeKF8HGumZBPF1vxrMXHMbpcpjUjYmxzRPKZOZyphoaLoUFM45eSFzOc0prGLlJ5keML0IB69suk3ix-dbQHG77lyeaDTC5a188ETTRPRk9HUGeilvf4olnohDz9qZQzYOYL9J0/s72-c/formatted.jpg" width="72"/><thr:total>0</thr:total></entry></feed>