<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jon Galloway : SQL</title><link>http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx</link><description>Tags: SQL</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>The real reason SELECT * queries are bad: index coverage</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/18/the-real-reason-select-queries-are-bad-index-coverage.aspx</link><pubDate>Thu, 19 Jul 2007 06:56:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:3217421</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>18</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=3217421</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2007/07/18/the-real-reason-select-queries-are-bad-index-coverage.aspx#comments</comments><description>&lt;P&gt;Are SELECT * queries bad? Sure,&amp;nbsp;everyone know that. But, why? 
&lt;H3&gt;It's returning too much data, right?&lt;/H3&gt;
&lt;P&gt;That's the common answer, but I don't think it's the right one. If you're working with a reasonably normalized database, the actual network traffic difference is pretty small. 
&lt;P&gt;Let's take a look at a sample. The following two queries select 326 rows from the TransactionHistoryArchive table in the AdventureWorks database (which has a total of 89K rows). The first uses a SELECT * query, the second selects a specific column: 
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d918d8f5-d3ce-4bc9-918e-f3d505475e9c contentEditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;PRE style="BACKGROUND-COLOR: #ecf0f2"&gt;&lt;DIV&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;*&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.TransactionHistoryArchive 
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;WHERE&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ReferenceOrderID &lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-WEIGHT: bold; COLOR: #800000"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ReferenceOrderLineID &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.TransactionHistoryArchive 
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;WHERE&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ReferenceOrderID &lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-WEIGHT: bold; COLOR: #800000"&gt;100&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;

&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case, the difference in network traffic is only 15K, roughly a 10% difference (180K vs. 165K). It's worth fixing, but not a huge difference.&lt;/P&gt;
&lt;H3&gt;SELECT * makes the Table / Index Scan Monster come&lt;/H3&gt;
&lt;P&gt;Often, the bigger problem with SELECT * is the effect it will have on the execution plan. While SQL Server primarily uses indexes to look up your data, if the index contains all the columns you’re requesting it doesn’t even need to look in the table. That concept is known as &lt;I&gt;index coverage. &lt;/I&gt;In the above example, the first query results in a Clustered Index Scan, whereas the second query uses a much more efficient Index Seek. In this case, the Index seek is&amp;nbsp;&lt;STRONG&gt;one hundred times&lt;/STRONG&gt;&amp;nbsp;more efficient than the Clustered Index Scan.&lt;/P&gt;
&lt;P&gt;&lt;A title=SelectStarQueryPlan href="http://www.flickr.com/photos/36836555@N00/849940280/" mce_href="http://www.flickr.com/photos/36836555@N00/849940280/"&gt;&lt;IMG alt=SelectStarQueryPlan src="http://farm2.static.flickr.com/1198/849940280_b55bd2970d_o.jpg" border=0 mce_src="http://farm2.static.flickr.com/1198/849940280_b55bd2970d_o.jpg"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Unless you've indexed every single column in a table (which is almost never a good idea), a SELECT * query can't take advantage of index coverage, and you're likely to get (extremely inefficient) scan operations. &lt;/P&gt;
&lt;P&gt;If you just query the rows you'll actually be using, it's more likely they'll be covered by indexes. And I think that's the biggest performance advantage of ignoring SELECT * queries.&lt;/P&gt;
&lt;H3&gt;The Stability Aspect&lt;/H3&gt;
&lt;P&gt;SELECT * queries are also bad from an application maintenance point of view as well, since it introduces another outside variable to your code. If a column is added to a table, the results returned to your application will change in structure. Well programmed applications should be referring to columns by name and shouldn't be affected, but well programmed applications should also minimize the ways in which they are vulnerable to external changes.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Shameless Plug&lt;/STRONG&gt;: I go into this (and&amp;nbsp;a lot&amp;nbsp;other important performance tips) in more detail in a soon-to-be-released book for SitePoint.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=3217421" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>[T-SQL] Getting distinct, current entries from tables with timestamp / datetime fields</title><link>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx</link><pubDate>Fri, 13 Jul 2007 05:50:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:3127789</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=3127789</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2007/07/12/t-sql-getting-distinct-current-entries-from-tables-with-timestamp-datetime-fields.aspx#comments</comments><description>&lt;P&gt;It's relatively easy to store data with a time dimension, but querying it is another matter. If you select from a temporal tables (one which includes historical information indicated by a timestamp or datetime column) based on your ID, you'll get a lot of duplicate records; only one or a few of those records will be applicable to a given time or timespan.&lt;/P&gt;
&lt;P&gt;For example, the following query (from the AdventureWorks sample database) returns 746 records:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class=wlWriterEditableSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ae9a5660-72fe-4705-9c6c-78f86f69dcb1 contentEditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;PRE style="BACKGROUND-COLOR: #eef1f2"&gt;&lt;DIV&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ActualCost 
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.TransactionHistory 
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;WHERE&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ProductID &lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-WEIGHT: bold; COLOR: #800000"&gt;784&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A table with History in the name is expected to work that way, but I've run into plenty of tables which allow for duplicates of every column but the primary key, differentiated by a datetime column. I'm starting to use these more now, partly due to project requirements, and partly because &lt;A href="http://www.subsonicproject.com/view/conventions.aspx" mce_href="http://www.subsonicproject.com/view/conventions.aspx"&gt;SubSonic&amp;nbsp;has built-in support for&amp;nbsp;CreatedOn and ModifiedOn columns&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;In the past, I've used stacked views or nested subqueries to handle this madness.&amp;nbsp;It's a pain in the neck, and often leads to frustrations with the GROUP BY clause&amp;nbsp;since queries with an aggregate term can't include a column in a resultset that's not in the GROUP BY clause (&lt;A href="http://geekswithblogs.net/bsherwin/archive/2007/03/11/108456.aspx" mce_href="http://geekswithblogs.net/bsherwin/archive/2007/03/11/108456.aspx"&gt;but adding those columns to the GROUP BY clause isn't the right solution, as it alters the rows you're returning&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;The general idea is to write a query which gets the latest update date for a unique ID combination, then join against it. It works, but it's a pain in the neck, and it's error prone. &lt;STRONG&gt;Fortunately, &lt;/STRONG&gt;&lt;A href="http://weblogs.sqlteam.com/jeffs/archive/2007/03/28/60146.aspx" mce_href="http://weblogs.sqlteam.com/jeffs/archive/2007/03/28/60146.aspx"&gt;&lt;STRONG&gt;SQL Server 2005's RANK / PARTITION features make this a lot easier&lt;/STRONG&gt;&lt;/A&gt;&lt;STRONG&gt;.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Let's talk specifics -&amp;nbsp;a query against the AdventureWorks Production.TransactionHistory table which returns the latest record Product Name and Number by Transaction Date. There are 113K rows in that table, but if we only want the latest entry for each Product we're down to 490 rows.&lt;/P&gt;
&lt;P&gt;Unlike the traditional GROUP BY based clause, the RANK / POSITION based queries are a bit more intuitive once you've got the hang of them. The important thing is to use PARTITION in the same way you'd think of a GROUP BY - the column(s) you want unique should show up in the PARTITION clause.&lt;/P&gt;
&lt;P&gt;One limitation on RANK queries is that you can't use the RANK value directly in a where clause. That's easy to work around by placing the RANK value in a subquery and the WHERE clause filtering on the RANK in the outer query.&lt;/P&gt;
&lt;P&gt;UPDATE: Based on comments, my original example was a little too simple. I've added a few joins and return columns; the point is that we can continue to add other columns to the result set without having to worry about how the grouping is handled. Yes, you can do this with subqueries, but in many cases those will continue to grow more complicated as you add tables and columns; this syntax doesn't.&lt;/P&gt;
&lt;DIV class=wlWriterEditableSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e56776f5-8bcf-4b0c-b2d5-299fa4e175d3 contentEditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;PRE style="BACKGROUND-COLOR: #eef1f2"&gt;&lt;DIV&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    Product.Name,
    Product.ProductNumber,
    TransactionDate,
    TransactionID,
    ProductCategory.Name,
    ProductSubcategory.Name,
    ProductSubcategory.ModifiedDate
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
(
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;DISTINCT&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    Production.TransactionHistory.ProductID,
    Production.TransactionHistory.TransactionDate,
    Production.TransactionHistory.TransactionID,
    RANK() &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;OVER&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (
        PARTITION &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;BY&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.TransactionHistory.ProductID 
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;ORDER&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;BY&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.TransactionHistory.TransactionDate &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;DESC&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;) 
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;AS&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; DateOrder
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.TransactionHistory
) CurrentTransactionHistory
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;INNER&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;JOIN&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.Product 
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;ON&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.Product.ProductID &lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CurrentTransactionHistory.ProductID
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;INNER&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;JOIN&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.ProductSubcategory &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;ON&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    Production.Product.ProductSubcategoryID &lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.ProductSubcategory.ProductSubcategoryID
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;INNER&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;JOIN&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.ProductCategory &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;ON&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    Production.ProductSubcategory.ProductCategoryID &lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Production.ProductCategory.ProductCategoryID
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;WHERE&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; DateOrder &lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-WEIGHT: bold; COLOR: #800000"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; 
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;ORDER&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;BY&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; ProductSubcategory.ModifiedDate
&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=3127789" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>[SQL] Force the protocol (TCP, Named Pipes, etc.) in your connection string</title><link>http://weblogs.asp.net/jgalloway/archive/2007/02/24/sql-force-the-protocol-tcp-named-pipes-etc-in-your-connection-string.aspx</link><pubDate>Sun, 25 Feb 2007 03:09:42 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:1790984</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=1790984</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2007/02/24/sql-force-the-protocol-tcp-named-pipes-etc-in-your-connection-string.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://idunno.org/archive/2007/01/24/Using-SQL-Trusted-Connections-from-a-machine-not-in-a.aspx"&gt;Barry Dorrans&lt;/a&gt; recently mentioned that you can &lt;a href="http://idunno.org/archive/2007/01/24/Using-SQL-Trusted-Connections-from-a-machine-not-in-a.aspx"&gt;force the database connection protocol by specifying np: or tcp: before the server name in your connection string&lt;/a&gt;. I've jumped through some hoops before &lt;a href="http://weblogs.asp.net/jgalloway/archive/2005/12/02/432062.aspx"&gt;using localhost to target tcp and (local) to target named pipes,&lt;/a&gt; but it looks like there's a much better way to do this (since MDAC 2.6).&lt;/p&gt; &lt;p&gt;There's more info in &lt;a href="http://support.microsoft.com/kb/313295"&gt;MS KB Article 313295&lt;/a&gt;:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;b&gt;TCP/IP&lt;/b&gt;:&lt;/p&gt;&lt;pre&gt;server=tcp:hostname&lt;/pre&gt;You can optionally specify a specific port number. By default, the port is 1433.&lt;code&gt;&lt;/code&gt;&lt;pre&gt;server=tcp:hostname, portNumber&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Named Pipes&lt;/b&gt;:&lt;pre&gt;server=np:hostname&lt;/pre&gt;You can optionally specify a specific named pipe.&lt;br&gt;&lt;code&gt;&lt;/code&gt;&lt;pre&gt;server=np:\\hostname\pipe\pipeName&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;They recommend that you always use (local) to specify the local machine, and then specify a protocol. Sounds like &lt;strong&gt;the right way to connect to the local machine over TCP is to specify server=tcp:(local)&lt;/strong&gt;, and to use named pipes on the local machine you'd use np:(local). To specify a protocol when pointing to a server, you'd use server=tcp:DBSERVERNAME.&lt;/p&gt;
&lt;p&gt;To quote&amp;nbsp;my previous post:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;There are many differences between TCP and Named Pipe connection, but if you're on localhost you're mostly concerned with simple access. &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The default ASPNET account generally has an easier time with TCP, since the &amp;nbsp;ASPNET user doesn't have access to named pipes by default (&lt;a href="http://support.microsoft.com/Default.aspx?id=315159"&gt;http://support.microsoft.com/Default.aspx?id=315159&lt;/a&gt;). 
&lt;li&gt;If you're using impersonation, Named Pipes is usually simpler. If you're using impersonation with no username specified, you're&amp;nbsp;running under the IIS Authenticating user. This defaults to IUSR_MACHINENAME if you're allowing annonymous access, which generally has access to the IPC$ share required for named pipe communications.&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;
&lt;p&gt;You can target other supported protocols, too:&lt;/p&gt;
&lt;p&gt;Multiprotocol = rpc&lt;br&gt;Shared Memory = lpc&lt;br&gt;NWlink&amp;nbsp; IPX / SPX = spx&lt;br&gt;Banyan VINES =&amp;nbsp;vines&lt;br&gt;Apple Talk = adsp&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=1790984" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/Tips+_2F00_+Tricks/default.aspx">Tips / Tricks</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Passing lists to SQL Server 2005 with XML Parameters</title><link>http://weblogs.asp.net/jgalloway/archive/2007/02/16/passing-lists-to-sql-server-2005-with-xml-parameters.aspx</link><pubDate>Fri, 16 Feb 2007 09:01:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:1685883</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>19</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=1685883</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2007/02/16/passing-lists-to-sql-server-2005-with-xml-parameters.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h3&gt;Overview&lt;/h3&gt; &lt;p&gt;SQL Server 2005&amp;#39;s XML capabilities make it a easier to pass lists to SQL Server procedures.  &lt;/p&gt;&lt;h3&gt;Background&lt;/h3&gt; &lt;p&gt;I recently needed to write a stored procedure which took a list of ID&amp;#39;s as a parameter. That&amp;#39;s one of those things that seems like it would be really simple, but isn&amp;#39;t. You&amp;#39;d think you could just pass in a comma delimited string of id&amp;#39;s: @ids = &amp;#39;3,5,7,8&amp;#39; and use something like &amp;#39;SELECT * FROM Products WHERE ID IN (@ids)&amp;#39;.&amp;nbsp;Nope, it doesn&amp;#39;t work. I still remember my surprise when I ran into that six or seven years ago.  &lt;/p&gt;&lt;p&gt;There are a huge variety of workarounds for this issue - see &lt;a href="http://www.sommarskog.se/arrays-in-sql.html"&gt;Erland&amp;#39;s comprehensive list ranging form SQL Server 6.5 to 2000&lt;/a&gt;. I&amp;#39;ve used several of these, and while they worked I never liked them. Probably the best method is to just use a &lt;a href="http://www.sommarskog.se/arrays-in-sql.html#tblnum-core"&gt;SPLIT table valued function which splits your string and returns a table&lt;/a&gt;. It&amp;#39;s clean, but all of your procedures depend on the existence of that function.  &lt;/p&gt;&lt;p&gt;It was also possible to use&amp;nbsp;&lt;a href="http://www.sommarskog.se/arrays-in-sql.html#OPENXML"&gt;OPENXML in SQL Server 2000&lt;/a&gt;. The syntax was obviously put together by C++ programmers (you have to prepare a document and work with an integer handle, which feels a lot like a pointer), and there were some limitations to be aware of, but it pretty much worked.  &lt;/p&gt;&lt;p&gt;This time around, I decided to try this with SQL Server 2005&amp;#39;s XML capabilities and see if it was any easier. It is.  &lt;/p&gt;&lt;h3&gt;Getting started with SQL Server 2005&amp;#39;s XML Syntax&lt;/h3&gt; &lt;p&gt;XML variables in SQL Server 2005 make it easy to &amp;quot;shred&amp;quot; XML strings into relational data. The main new methods you&amp;#39;ll need to use are value() and nodes() which allow us to select values from XML documents.  &lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:eef6d414-6cca-4b1a-bca6-52bd21c9853f" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;&lt;pre style="background-color: White"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;DECLARE&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@productIds&lt;/span&gt;&lt;span style="color: #000000"&gt; xml&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;SET&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@productIds&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;lt;Products&amp;gt;&amp;lt;id&amp;gt;3&amp;lt;/id&amp;gt;&amp;lt;id&amp;gt;6&amp;lt;/id&amp;gt;&amp;lt;id&amp;gt;15&amp;lt;/id&amp;gt;&amp;lt;/Products&amp;gt;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;SELECT&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;ParamValues.ID.value(&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;.&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;VARCHAR(20)&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;FROM&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@productIds&lt;/span&gt;&lt;span style="color: #000000"&gt;.nodes(&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;/Products/id&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="color: #000000"&gt; ParamValues(ID) &lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Which gives us the following three rows:&lt;/p&gt;
&lt;p&gt;3&lt;br /&gt;6&lt;br /&gt;15&lt;/p&gt;
&lt;h3&gt;Alright, just show me how to pass a list in a procedure parameter already!&lt;/h3&gt;
&lt;p&gt;Here&amp;#39;s a proc which takes a single XML parameter. We first declare a table variable (@Products) and load the XML values into it. Once that&amp;#39;s done, we can join against the @Products table as if it were any other table in the database.&lt;br /&gt;&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:0e3eda99-2aea-41dc-9e56-61d7224e0787" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;&lt;pre style="background-color: White"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;CREATE&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;PROCEDURE&lt;/span&gt;&lt;span style="color: #000000"&gt; SelectByIdList(&lt;/span&gt;&lt;span style="color: #008000"&gt;@productIds&lt;/span&gt;&lt;span style="color: #000000"&gt; xml) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;AS&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;DECLARE&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@Products&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;TABLE&lt;/span&gt;&lt;span style="color: #000000"&gt; (ID &lt;/span&gt;&lt;strong&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;/strong&gt;&lt;span style="color: #000000"&gt;) &lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;INSERT&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;INTO&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@Products&lt;/span&gt;&lt;span style="color: #000000"&gt; (ID) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;SELECT&lt;/span&gt;&lt;span style="color: #000000"&gt; ParamValues.ID.value(&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;.&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;VARCHAR(20)&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;FROM&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@productIds&lt;/span&gt;&lt;span style="color: #000000"&gt;.nodes(&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;/Products/id&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="color: #000000"&gt; ParamValues(ID) &lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;SELECT&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;FROM&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;br /&gt;    Products&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;INNER&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;JOIN&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #008000"&gt;@Products&lt;/span&gt;&lt;span style="color: #000000"&gt; p&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;ON&lt;/span&gt;&lt;span style="color: #000000"&gt;    Products.ProductID &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; p.ID&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can call it as follows:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ac5a2761-ed37-477e-9342-ad4bfb4c0e41" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;&lt;pre style="background-color: White"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;EXEC&lt;/span&gt;&lt;span style="color: #000000"&gt; SelectByIdList &lt;/span&gt;&lt;span style="color: #008000"&gt;@productIds&lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;lt;Products&amp;gt;&amp;lt;id&amp;gt;3&amp;lt;/id&amp;gt;&amp;lt;id&amp;gt;6&amp;lt;/id&amp;gt;&amp;lt;id&amp;gt;15&amp;lt;/id&amp;gt;&amp;lt;/Products&amp;gt;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Which gives us the following:&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr style="height: 15pt"&gt;
&lt;td height="20" style="border-style: solid none solid solid; border-color: -moz-use-text-color; border-width: 0.5pt medium 0.5pt 0.5pt; background: #f79646 none repeat scroll 0% 50%; font-weight: 700; font-size: 11pt; width: 62pt; color: white; font-family: calibri; height: 15pt; text-decoration: none; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" width="83"&gt;ProductID&lt;/td&gt;
&lt;td style="border-style: solid none; border-color: -moz-use-text-color; border-width: 0.5pt medium; background: #f79646 none repeat scroll 0% 50%; font-weight: 700; font-size: 11pt; width: 80pt; color: white; font-family: calibri; text-decoration: none; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" width="107"&gt;ProductName&lt;/td&gt;
&lt;td style="border-style: solid none; border-color: -moz-use-text-color; border-width: 0.5pt medium; background: #f79646 none repeat scroll 0% 50%; font-weight: 700; font-size: 11pt; width: 65pt; color: white; font-family: calibri; text-decoration: none; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" width="87"&gt;SupplierID&lt;/td&gt;
&lt;td style="border-style: solid none; border-color: -moz-use-text-color; border-width: 0.5pt medium; background: #f79646 none repeat scroll 0% 50%; font-weight: 700; font-size: 11pt; width: 68pt; color: white; font-family: calibri; text-decoration: none; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" width="90"&gt;CategoryID&lt;/td&gt;
&lt;td style="border-style: solid none; border-color: -moz-use-text-color; border-width: 0.5pt medium; background: #f79646 none repeat scroll 0% 50%; font-weight: 700; font-size: 11pt; width: 92pt; color: white; font-family: calibri; text-decoration: none; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" width="123"&gt;QuantityPerUnit&lt;/td&gt;
&lt;td style="border-style: solid none; border-color: -moz-use-text-color; border-width: 0.5pt medium; background: #f79646 none repeat scroll 0% 50%; font-weight: 700; font-size: 11pt; width: 59pt; color: white; font-family: calibri; text-decoration: none; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" width="79"&gt;UnitPrice&lt;/td&gt;
&lt;td style="border-style: solid none; border-color: -moz-use-text-color; border-width: 0.5pt medium; background: #f79646 none repeat scroll 0% 50%; font-weight: 700; font-size: 11pt; width: 74pt; color: white; font-family: calibri; text-decoration: none; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" width="99"&gt;UnitsInStock&lt;/td&gt;
&lt;td style="border-style: solid none; border-color: -moz-use-text-color; border-width: 0.5pt medium; background: #f79646 none repeat scroll 0% 50%; font-weight: 700; font-size: 11pt; width: 81pt; color: white; font-family: calibri; text-decoration: none; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" width="108"&gt;UnitsOnOrder&lt;/td&gt;
&lt;td style="border-style: solid none; border-color: -moz-use-text-color; border-width: 0.5pt medium; background: #f79646 none repeat scroll 0% 50%; font-weight: 700; font-size: 11pt; width: 79pt; color: white; font-family: calibri; text-decoration: none; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" width="105"&gt;ReorderLevel&lt;/td&gt;
&lt;td style="border-style: solid none; border-color: -moz-use-text-color; border-width: 0.5pt medium; background: #f79646 none repeat scroll 0% 50%; font-weight: 700; font-size: 11pt; width: 78pt; color: white; font-family: calibri; text-decoration: none; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" width="104"&gt;Discontinued&lt;/td&gt;
&lt;td style="border-style: solid solid solid none; border-color: -moz-use-text-color; border-width: 0.5pt 0.5pt 0.5pt medium; background: #f79646 none repeat scroll 0% 50%; font-weight: 700; font-size: 11pt; width: 48pt; color: white; font-family: calibri; text-decoration: none; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" width="64"&gt;ID&lt;/td&gt;&lt;/tr&gt;
&lt;tr style="height: 15pt"&gt;
&lt;td align="right" height="20" style="border-style: none none solid solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; height: 15pt; text-decoration: none"&gt;3&lt;/td&gt;
&lt;td style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;Aniseed Syrup&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;1&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;2&lt;/td&gt;
&lt;td style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;12 - 550 ml bottles&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;10&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;13&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;100&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;25&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;0&lt;/td&gt;
&lt;td align="right" style="border-style: none solid solid none; border-color: -moz-use-text-color; border-width: medium 0.5pt 0.5pt medium; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;3&lt;/td&gt;&lt;/tr&gt;
&lt;tr style="height: 15pt"&gt;
&lt;td align="right" height="20" style="border-style: none none solid solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; height: 15pt; text-decoration: none"&gt;6&lt;/td&gt;
&lt;td style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;Grandma&amp;#39;s Boyse&lt;span style="display: none"&gt;nberry Spread&lt;/span&gt;&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;3&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;2&lt;/td&gt;
&lt;td style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;12 - 8 oz jars&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;25&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;120&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;0&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;25&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;0&lt;/td&gt;
&lt;td align="right" style="border-style: none solid solid none; border-color: -moz-use-text-color; border-width: medium 0.5pt 0.5pt medium; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;6&lt;/td&gt;&lt;/tr&gt;
&lt;tr style="height: 15pt"&gt;
&lt;td align="right" height="20" style="border-style: none none solid solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; height: 15pt; text-decoration: none"&gt;15&lt;/td&gt;
&lt;td style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;Genen Shouyu&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;6&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;2&lt;/td&gt;
&lt;td style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;24 - 250 ml bottles&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;15.5&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;39&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;0&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;5&lt;/td&gt;
&lt;td align="right" style="border-style: none none solid; border-color: -moz-use-text-color; border-width: medium medium 0.5pt; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;0&lt;/td&gt;
&lt;td align="right" style="border-style: none solid solid none; border-color: -moz-use-text-color; border-width: medium 0.5pt 0.5pt medium; font-weight: 400; font-size: 11pt; color: black; font-family: calibri; text-decoration: none"&gt;15&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;In order to use this, you&amp;#39;ll need to an XML string with your ID&amp;#39;s. In our application, &lt;a href="http://www.stevenharman.net"&gt;Steve&lt;/a&gt; was handling the application code, and I talked him into doing this via quick and dirty string concatenation. His method worked great:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:7e593f78-2cfa-488d-8b5e-537df0e7706c" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;&lt;pre style="background-color: White"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; BuildXmlString(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; xmlRootName, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;[] values)&lt;br /&gt;{&lt;br /&gt;    StringBuilder xmlString &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; StringBuilder();&lt;br /&gt;&lt;br /&gt;    xmlString.AppendFormat(&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;{0}&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;, xmlRootName);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;for&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; i &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;; i &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt; values.Length; i&lt;/span&gt;&lt;span style="color: #000000"&gt;++&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;    {&lt;br /&gt;    xmlString.AppendFormat(&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;value&amp;gt;{0}&amp;lt;/value&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;, values[i]);&lt;br /&gt;    }&lt;br /&gt;    xmlString.AppendFormat(&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;/{0}&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;, xmlRootName);&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; xmlString.ToString();&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;What&amp;#39;s next?&lt;/h3&gt;
&lt;p&gt;This is a very simple use of XML in SQL Server. You can pass complex XML documents containing business objects to insert and update in your relational tables, for instance. If you&amp;#39;re going to do that with a large amount of data, have a look at &lt;a href="http://www.ayende.com/Blog/archive/2006/04/18/7661.aspx"&gt;Ayende&amp;#39;s clever use of SqlBulkCopy&lt;/a&gt;&amp;nbsp;to handle that more efficiently.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=1685883" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>sysobjects, sys.objects... what about INFORMATION_SCHEMA?</title><link>http://weblogs.asp.net/jgalloway/archive/2006/12/31/sysobjects-sys-objects-what-about-information-schema.aspx</link><pubDate>Sun, 31 Dec 2006 23:53:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:1342738</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=1342738</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2006/12/31/sysobjects-sys-objects-what-about-information-schema.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/ms162169.aspx"&gt;SQL Server Management Objects (&lt;strong&gt;SMO&lt;/strong&gt;)&lt;/a&gt; is one of the coolest parts of SQL Server 2005, in my opinion. I&amp;#39;ve &lt;a href="http://weblogs.asp.net/jgalloway/archive/2006/07/07/455797.aspx"&gt;written about SMO&lt;/a&gt; &lt;a href="http://weblogs.asp.net/jgalloway/archive/2006/12/03/notes-for-11-28-talk-smo-and-information-schema.aspx"&gt;before&lt;/a&gt;, and used it&amp;nbsp;extensively in writing&amp;nbsp;&lt;a href="http://www.codeplex.com/datadictionary"&gt;Data Dictionary Creator&lt;/a&gt;. In a nutshell, SMO&amp;nbsp;is &lt;em&gt;a collection of objects that are designed for programming all aspects of managing Microsoft SQL Server&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;One thing about SMO puzzles me, though - it uses the system tables (sysobjects and sys.objects) instead of INFORMATION_SCHEMA when it checks if tables, views, or procedures exist.&lt;/strong&gt; There are at least three ways that&amp;#39;s evident:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Scripting database objects&amp;nbsp;from SSMS  &lt;/li&gt;&lt;li&gt;Scripting database object using SMO in CaptureSQL mode  &lt;/li&gt;&lt;li&gt;The new SQL Server Database Publishing Wizard&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;All the above check for object existence using system table checks rather than INFORMATION_SCHEMA checks. I&amp;#39;m not just guessing how they work, though - take a look at Microsoft.SqlServer.Management.Smo.Scripts..ctor() in Reflector:&lt;/p&gt; &lt;p&gt;&lt;font size="1"&gt;Scripts.INCLUDE_EXISTS_TABLE80 = &amp;quot;IF {0} EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N&amp;#39;{1}&amp;#39;) AND OBJECTPROPERTY(id, N&amp;#39;IsUserTable&amp;#39;) = 1)&amp;quot;&lt;br /&gt;Scripts.INCLUDE_EXISTS_TABLE90 = &amp;quot;IF {0} EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N&amp;#39;{1}&amp;#39;) AND type in (N&amp;#39;U&amp;#39;))&amp;quot;&lt;br /&gt;Scripts.INCLUDE_EXISTS_VIEW90 = &amp;quot;IF {0} EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N&amp;#39;{1}&amp;#39;))&amp;quot;&lt;br /&gt;Scripts.INCLUDE_EXISTS_VIEW80 = &amp;quot;IF {0} EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N&amp;#39;{1}&amp;#39;) AND OBJECTPROPERTY(id, N&amp;#39;IsView&amp;#39;) = 1)&amp;quot;&lt;br /&gt;Scripts.INCLUDE_EXISTS_PROCEDURE80 = &amp;quot;IF {0} EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N&amp;#39;{1}&amp;#39;) AND OBJECTPROPERTY(id,N&amp;#39;IsProcedure&amp;#39;) = 1)&amp;quot;&lt;br /&gt;Scripts.INCLUDE_EXISTS_PROCEDURE90 = &amp;quot;IF {0} EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N&amp;#39;{1}&amp;#39;) AND type in (N&amp;#39;P&amp;#39;, N&amp;#39;PC&amp;#39;))&amp;quot;&lt;/font&gt;&lt;/p&gt; &lt;h3&gt;INFORMATION_SCHEMA views good, System Tables bad&lt;/h3&gt; &lt;p&gt;&lt;strong&gt;The biggest problem is that internal system tables change between versions, so scripts are generated for one specific SQL Server version.&lt;/strong&gt; The INFORMATION_SCHEMA views, on the other hand, are part of the SQL92 standard, so they don&amp;#39;t change between versions.[1] Here&amp;#39;s &lt;a href="http://www.dbazine.com/db2/db2-disarticles/pelzer4"&gt;a pretty good article on INFORMATION_SCHEMA&lt;/a&gt; if you&amp;#39;d like to read further.&lt;/p&gt; &lt;p&gt;For example, the following works on both SQL 2000 and SQL 2005:&lt;/p&gt; &lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3353c37b-5be3-4f04-9f6e-60e7e68e10d8" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;&lt;pre style="background-color: White"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;use&lt;/span&gt;&lt;span style="color: #000000"&gt; northwind&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;go&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;exists&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;select&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;from&lt;/span&gt;&lt;span style="color: #000000"&gt; information_schema.tables &lt;/span&gt;&lt;span style="color: #0000ff"&gt;where&lt;/span&gt;&lt;span style="color: #000000"&gt; table_name &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Products&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;and&lt;/span&gt;&lt;span style="color: #000000"&gt; table_type &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;BASE TABLE&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Products table exists&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;exists&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;select&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;from&lt;/span&gt;&lt;span style="color: #000000"&gt; information_schema.tables &lt;/span&gt;&lt;span style="color: #0000ff"&gt;where&lt;/span&gt;&lt;span style="color: #000000"&gt; table_name &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Invoices&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;and&lt;/span&gt;&lt;span style="color: #000000"&gt; table_type &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;VIEW&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Invoices view exists&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;exists&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;select&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;from&lt;/span&gt;&lt;span style="color: #000000"&gt; information_schema.routines &lt;/span&gt;&lt;span style="color: #0000ff"&gt;where&lt;/span&gt;&lt;span style="color: #000000"&gt; specific_name &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;SalesByCategory&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;print&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;SalesByCategory stored procedure exists&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Disclaimer&lt;/h3&gt;
&lt;p&gt;I know there are some cases where the INFORMATION_SCHEMA won&amp;#39;t cut it. INFO SCHEMA is a cross database standard, so it&amp;#39;s got no concept of some advanced and proprietary features. In Data Dictionary Creator, I had to support different syntax for extended property lookup, since SQL Server 2000 uses&amp;nbsp; &lt;strong&gt;::fn_listextendedproperty&lt;/strong&gt; and SQL Server 2005 uses &lt;strong&gt;sys.fn_listextendedproperty&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Fine. That&amp;#39;s no reason to drop INFORMATION_SCHEMA - use it for the 90% of the time it will do the job, and drop down to the system tables when you have to.&lt;/p&gt;
&lt;h3&gt;Workaround: Use Regular Expression replacements on the scripts&lt;/h3&gt;
&lt;p&gt;You can use regular expression replacements to &amp;quot;fix&amp;quot; SMO generated scripts to run against INFORMATION_SCHEMA.&lt;/p&gt;
&lt;p&gt;This regular expression matches a table existence check in SQL 2000 specific syntax:&lt;/p&gt;
&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;^IF\ EXISTS\ \(SELECT\ \*\ FROM\ dbo\.sysobjects\ WHERE\ id\ =\ OBJECT_ID\(N&amp;#39;(?&amp;lt;tablename&amp;gt;.+?)&amp;#39;\)\ AND\ OBJECTPROPERTY\(id,\ N&amp;#39;IsUserTable&amp;#39;\)\ =\ 1\)$&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s the same thing, in SQL 2005 syntax:&lt;/p&gt;
&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;^IF\ NOT\ EXISTS\ \(SELECT\ \*\ FROM\ sys\.objects\ WHERE\ object_id\ =\ OBJECT_ID\(N&amp;#39;\[dbo]\.\[(?&amp;lt;tablename&amp;gt;.+?)]&amp;#39;\)\ AND\ type\ in\ \(N&amp;#39;U&amp;#39;\)\)$&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;We&amp;#39;re using a named capture group so we can use it in the replacement. Then we replace either with the following:&lt;/p&gt;
&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N&amp;#39;${TABLENAME}&amp;#39; AND TABLE_TYPE = &amp;#39;BASE TABLE&amp;#39;)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m resisting the urge to write a utility to handle the script conversion since I need to be working on my book. Here&amp;#39;s a rough outline&amp;nbsp; for a console application that would read the console input, convert table existence checks to use INFORMATION_SCHEMA, and write the result to the console output. The reason for using console input and output is to allow piping &lt;a href="http://www.codeplex.com/sqlhost/Wiki/View.aspx?title=DPW%20Command%20Line%20Interface"&gt;SqlPubWiz&lt;/a&gt; output through it. It&amp;#39;s not tested, and it doesn&amp;#39;t include checks for views, procedures, and indices; they&amp;#39;d follow the exact same pattern. This isn&amp;#39;t the optimal way to do regular expression replacements - given time, I&amp;#39;d do these with MatchEvaluators.&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:a587f10c-5d3a-431a-8b74-3fd843c6a7de" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;&lt;pre style="background-color: White"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Text;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Text.RegularExpressions;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; SqlScriptCleaner&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; Main(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;[] args)&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; script &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Console.In.ToString();&lt;br /&gt;        Regex.Replace(script,&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #000000"&gt;@&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;^IF\ EXISTS\ \(SELECT\ \*\ FROM\ dbo\.sysobjects\ WHERE\ id\ =\ OBJECT_ID\(N&amp;#39;(?&amp;lt;tablename&amp;gt;.+?)&amp;#39;\)\ AND\ OBJECTPROPERTY\(id,\ N&amp;#39;IsUserTable&amp;#39;\)\ =\ 1\)$&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #000000"&gt;@&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N&amp;#39;${TABLENAME}&amp;#39; AND TABLE_TYPE = &amp;#39;BASE TABLE&amp;#39;)&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;);&lt;br /&gt;        Regex.Replace(script,&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #000000"&gt;@&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;^IF\ NOT\ EXISTS\ \(SELECT\ \*\ FROM\ sys\.objects\ WHERE\ object_id\ =\ OBJECT_ID\(N&amp;#39;\[dbo]\.\[(?&amp;lt;tablename&amp;gt;.+?)]&amp;#39;\)\ AND\ type\ in\ \(N&amp;#39;U&amp;#39;\)\)$&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #000000"&gt;@&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N&amp;#39;${TABLENAME}&amp;#39; AND TABLE_TYPE = &amp;#39;BASE TABLE&amp;#39;)&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;);&lt;br /&gt;        System.Console.Out.Write(script);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Come to think of it, this (theoretical) utility could also take care of &lt;a href="http://weblogs.asp.net/jgalloway/archive/2006/12/29/tip-use-the-database-publishing-wizard-to-script-your-table-data.aspx"&gt;my other gripe with SqlPubWiz&lt;/a&gt;, which is that it doesn&amp;#39;t allow you to select which objects to script. It&amp;#39;s inelegant, but it would work - let SqlPubWiz script everything, then clean the output to remove scripts for tables we don&amp;#39;t want included.&lt;/p&gt;
&lt;h3&gt;A more practical workaround: Regex Replacements in SSMS&lt;/h3&gt;
&lt;p&gt;For now, though, I&amp;#39;m fine with just handling this manually in an editor that supports Regular Expression replacement. Visual Studio does it, as do most of the top Notepad replacement tools (Notepad++, Notepad2, etc.). Now that SQL Server Management Studio uses the Visual Studio IDE, you can do Regex replacements in SSMS as well.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Generate your script and open it in a new query window - if you&amp;#39;re just scripting schema, you can right click on the database, select Tasks, and pick the Generate Scripts task (selecting a new query window as the output method). If you&amp;#39;re scripting schema and data with SqlPubWiz, output to a SQL file and open it in SSMS.&lt;/li&gt;
&lt;li&gt;Bring up the Quick Replace dialog (Edit -&amp;gt; Find / Replace -&amp;gt; Quick Replace, or just hit ctrl-h).&lt;/li&gt;
&lt;li&gt;Expand the &amp;quot;Find Options&amp;quot; section and check the &amp;quot;Use&amp;quot; checkbox, then ensure &amp;quot;Regular expressions&amp;quot; is selected.&lt;/li&gt;
&lt;li&gt;Enter the correct Regex find and replacements as shown below. Note that &lt;a href="http://www.codinghorror.com/blog/archives/000633.html"&gt;Visual Studio has its own bastardized Regex syntax&lt;/a&gt;, so you&amp;#39;ll need to tweak the regexes.&lt;br /&gt;Revised Find expression: &lt;strong&gt;^IF\ NOT\ EXISTS\ \(SELECT\ \*\ FROM\ sys\.objects\ WHERE\ object_id\ =\ OBJECT_ID\(N&amp;#39;\[dbo\]\.\[{.+}\]&amp;#39;\)\ AND\ type\ in\ \(N&amp;#39;U&amp;#39;\)\)&lt;/strong&gt;&lt;br /&gt;Revised Replace expression: &lt;strong&gt;IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N&amp;#39;\1&amp;#39; AND TABLE_TYPE = &amp;#39;BASE TABLE&amp;#39;)&amp;nbsp;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Hit the Replace All button.&lt;/li&gt;
&lt;li&gt;Make sure the script still compiles by hitting CTRL-F5.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;&lt;img src="http://farm1.static.flickr.com/153/340160954_4c65117b60_o.png" /&gt; &lt;/p&gt;
&lt;p&gt;[1] &lt;a href="http://blogs.conchango.com/jamiethomson/archive/2006/09/06/SSIS_3A00_-Changing-system-tables-and-INFORMATION_5F00_SCHEMA-views.aspx"&gt;Jamie Thomson reported one issue with INFORMATION_SCHEMA behavior changing between 2000 and 2005&lt;/a&gt; due to an implementation bug in INFORMATION_SCHEMA.COLUMNS.NUMERIC.PRECISION for smalldatetime and datetime.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=1342738" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>[tip] Use the Database Publishing Wizard to script your table data</title><link>http://weblogs.asp.net/jgalloway/archive/2006/12/29/tip-use-the-database-publishing-wizard-to-script-your-table-data.aspx</link><pubDate>Sat, 30 Dec 2006 07:53:06 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:1335179</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=1335179</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2006/12/29/tip-use-the-database-publishing-wizard-to-script-your-table-data.aspx#comments</comments><description>&lt;p&gt;Jeff Atwood recently asked "&lt;a href="http://www.codinghorror.com/blog/archives/000743.html"&gt;Is your database under version control?&lt;/a&gt;" &amp;nbsp;Well, is it? It's not as easy as it sounds. Until now, there wasn't a good, scriptable solution to the problem. I'll run through a few of the options I've looked at in the past, but let's jump to to the punchline:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;The &lt;/strong&gt;&lt;a href="http://www.codeplex.com/sqlhost/Wiki/View.aspx?title=Database%20Publishing%20Wizard"&gt;&lt;strong&gt;Database Publishing Wizard&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; can script data as schema, and it's got a command line mode.&lt;/strong&gt;&lt;/p&gt; &lt;h3&gt;The Problem&lt;/h3&gt; &lt;p&gt;Most of the applications I work on run against SQL Server databases. &lt;a href="http://www.codinghorror.com/blog/archives/000743.html"&gt;Jeff&lt;/a&gt; hit the nail on the head when he said that &lt;strong&gt;your database&lt;/strong&gt; is an integral part of your application. I'll take it a step further and say that &lt;strong&gt;your data&lt;/strong&gt; is often an integral part of your application. Many databases include signifcant amounts of &lt;strong&gt;reference data which are as much a part of your software release as your config files&lt;/strong&gt; - for instance, a DotNetNuke site without the correct version of the database is just plain shot.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;I'm not talking about database backups,&lt;/strong&gt; which are an important part of your production environment. Database backups are a more appropriate solution to safeguarding your transactional data, but they don't integrate&amp;nbsp;at all&amp;nbsp;with the rest of your application's version control.&lt;/p&gt; &lt;p align="left"&gt;I previously posted &lt;a href="http://weblogs.asp.net/jgalloway/archive/2006/10/28/Batch-files-to-check-SQL-2005-_2800_MDF_2900_-files-in-and-out-of-Subversion-source-control.aspx"&gt;a script to check a database (MDF) file into Subversion&lt;/a&gt;. It works, but it's far from ideal. It's just a binary file, so while you've got history, you can't track differences. No good.&lt;/p&gt; &lt;p align="left"&gt;I've used &lt;a href="http://www.sqlscripter.com/"&gt;SQL-Scripter&lt;/a&gt;, &lt;a href="http://www.codeproject.com/dotnet/ScriptDatabase.asp"&gt;GenDbScripts&lt;/a&gt;&amp;nbsp;(had to fix a few bugs), and &lt;a href="http://www.codeproject.com/useritems/enisey.asp"&gt;Enisey Data Scripter&lt;/a&gt;. They all work, but none of them&amp;nbsp;was scriptable.&amp;nbsp;&lt;/p&gt; &lt;p align="left"&gt;I set out to see how easy it would be to write&amp;nbsp;something in&amp;nbsp;&lt;a href="http://www.pluralsight.com/blogs/dan/archive/2006/11/07/41936.aspx"&gt;PowerSMO&lt;/a&gt; that would script out the table schema and data. It should have been easy, since SMO has a special "execution mode" that&amp;nbsp;records all commands and outputs&amp;nbsp;the resulting&amp;nbsp;script. Heck, &lt;a href="http://davidhayden.com/blog/dave/archive/2006/11/09/ScriptDatabaseUsingSQLServerManagementObjects.aspx"&gt;David Hayden has one post showing C# sample code to script a database schema&lt;/a&gt;, and another&amp;nbsp;&lt;a href="http://davidhayden.com/blog/dave/archive/2006/11/09/CopyDatabaseSchemaDataSQLServerManagementObjects.aspx"&gt;using SMO's Transfer.TransferData() to copy all data from one database to another&lt;/a&gt;.&amp;nbsp;No problem, I'll just mash them together to enable CaptureSql and TransferData, and I'm set...&lt;/p&gt; &lt;p align="left"&gt;Yeah, not really. I converted it to a PowerShell script. It worked great... but it only scripted the schema, not the data. It looks like TransferData doesn't go through script, so it's not picked up by that cool CaptureSql thing. (In case you care, that PowerShell script is in the footnotes.)&lt;/p&gt; &lt;h3&gt;The Solution (well, pretty much...)&lt;/h3&gt; &lt;p align="left"&gt;Then &lt;a href="http://weblogs.asp.net/scottgu/archive/2006/12/22/recipe-deploying-a-sql-database-to-a-remote-hosting-environment-part-1.aspx"&gt;Scott Guthrie&amp;nbsp;mentioned the new Database Publishing Wizard on&amp;nbsp;an extensive&amp;nbsp;post on deploying a SQL database to a remote hosting environment&lt;/a&gt;. Scott's post was pretty beefy and&amp;nbsp;covered the SQL Server Hosting Toolkit, so you might have missed the beautiful screenshots below the fold. The Database Publishing Wizard makes it easy to script your schema and data from within Visual Studio:&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;img src="http://farm1.static.flickr.com/145/334852364_5852f7fdb9_o.gif"&gt;&lt;/p&gt; &lt;p&gt;The more exciting thing to me is that the Database Publishing Wizard can be run from the commandline:&lt;/p&gt; &lt;p&gt;&lt;pre&gt;sqlpubwiz script ( -C connection_string | -d local_database_name ) (output_file) [switches]

switches:

 Output options:

  [-f] :                       Overwrite existing files
  [-noschemaqualify] :         Output script does not qualify object names with
                             schema name.
  [-schemaonly] :              Only script schema
  [-dataonly] :                Only script data
  [-targetserver] :            Specifies what version of SQL Server the script
                             should target.  Valid versions are "2000", "2005".
                             Default Value: 2005
  [-q] :                       Suppress output
  [-nodropexisting] :           Default Value: False

 Local connection related:

  [-C connection_string] :     Connection string
  [-d local_database_name] :   Database name to script
  [-U local_user_name] :       Username
  [-P local_password] :        Password
  [-S local_server_name] :     Server name
&lt;/pre&gt;
&lt;p&gt;Perfect!!!&lt;/p&gt;
&lt;p&gt;Oops, we're missing two things before my quest is ended:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We need an option to specify which tables to script. I know that complicates things a bit, but we gotta have it. Maybe a parameter that points to a text file containing a list of tables to be scripted. That's important because we want to script and version our reference data, but we don't want to script our transactional data for a lot of reasons (most importantly we don't want to overwrite transactional data, but also the transactional data will usually be the most subject to large growth). 
&lt;li&gt;Why do the drop scripts need to check sys.objects for SQL 2005 databases and sysobjects for SQL 2000 databases? Why not use the INFORMATION_SCHEMA views so the same script works on both? That's the subject of another post, I guess...&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;Oh, and that PowerSMO I mentioned. It'll script the schema, but not the data:&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:36fad74a-629e-45b3-80c7-347f35bed29d" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color:White;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000; "&gt;$databaseToScript &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;AdventureWorks&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;
$serverName &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;(local)\SQL2005&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;
#USES POWERSMO &lt;/span&gt;&lt;span style="color: #000000; "&gt;-&lt;/span&gt;&lt;span style="color: #000000; "&gt; http:&lt;/span&gt;&lt;span style="color: #008000; "&gt;//&lt;/span&gt;&lt;span style="color: #008000; "&gt;www.pluralsight.com/dan/samples/PowerSMO.zip&lt;/span&gt;&lt;span style="color: #008000; "&gt;
&lt;/span&gt;&lt;span style="color: #000000; "&gt;
#SETUP VOODOO
$executionPolicy &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;get&lt;/span&gt;&lt;span style="color: #000000; "&gt;-&lt;/span&gt;&lt;span style="color: #000000; "&gt;ExecutionPolicy 
&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;set&lt;/span&gt;&lt;span style="color: #000000; "&gt;-&lt;/span&gt;&lt;span style="color: #000000; "&gt;ExecutionPolicy &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Unrestricted&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;
.\InitPowerSMO.ps1
cls

#CONNECT TO DATABASE
$server &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; SMO_Server $serverName
$server.ConnectionContext.SqlExecutionModes &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; [Microsoft.SqlServer.Management.Common.SqlExecutionModes]&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;CaptureSql&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt; #ExecuteAndCaptureSql
$database &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; $server.Databases[$databaseToScript]

$t &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; SMO_Transfer($database)
$t.CopyAllObjects &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; $&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;true&lt;/span&gt;&lt;span style="color: #000000; "&gt;
$t.DropDestinationObjectsFirst &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; $&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;true&lt;/span&gt;&lt;span style="color: #000000; "&gt;
$t.CopySchema &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; $&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;true&lt;/span&gt;&lt;span style="color: #000000; "&gt;
$t.CopyData &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; $&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;true&lt;/span&gt;&lt;span style="color: #000000; "&gt; #Doesn&lt;/span&gt;&lt;span style="color: #000000; "&gt;'&lt;/span&gt;&lt;span style="color: #000000; "&gt;t get scripted&lt;/span&gt;&lt;span style="color: #000000; "&gt;
&lt;/span&gt;&lt;span style="color: #000000; "&gt;$t.DestinationServer &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; $serverName
$t.DestinationDatabase &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;just_a_dummy_value&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;
$t.Options.IncludeIfNotExists &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; $&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;true&lt;/span&gt;&lt;span style="color: #000000; "&gt;
$t.CreateTargetDatabase &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; $&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;true&lt;/span&gt;&lt;span style="color: #000000; "&gt;

#Doesn&lt;/span&gt;&lt;span style="color: #000000; "&gt;'&lt;/span&gt;&lt;span style="color: #000000; "&gt;t help&lt;/span&gt;&lt;span style="color: #000000; "&gt;
&lt;/span&gt;&lt;span style="color: #000000; "&gt;#$t.TransferData()

&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Generating script&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;
$commands &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; $t.ScriptTransfer()
&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Writing to file&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;
$scriptFile &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;{0}_Script.sql&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;-&lt;/span&gt;&lt;span style="color: #000000; "&gt;f $databaseToScript
$commands &lt;/span&gt;&lt;span style="color: #000000; "&gt;|&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;out&lt;/span&gt;&lt;span style="color: #000000; "&gt;-&lt;/span&gt;&lt;span style="color: #000000; "&gt;File $scriptFile
&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Done&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;

#CLEANUP
$server &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;''&lt;/span&gt;&lt;span style="color: #000000; "&gt;
&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;set&lt;/span&gt;&lt;span style="color: #000000; "&gt;-&lt;/span&gt;&lt;span style="color: #000000; "&gt;ExecutionPolicy &lt;/span&gt;&lt;span style="color: #000000; "&gt;-&lt;/span&gt;&lt;span style="color: #000000; "&gt;executionPolicy $executionPolicy&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=1335179" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/Tools/default.aspx">Tools</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/Tips+_2F00_+Tricks/default.aspx">Tips / Tricks</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>SQL Server recovery model selection (or, what's this 3GB LDF file?!)</title><link>http://weblogs.asp.net/jgalloway/archive/2006/12/17/sql-server-recovery-model-selection-or-what-s-this-3gb-ldf-file.aspx</link><pubDate>Sun, 17 Dec 2006 09:22:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:1259799</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=1259799</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2006/12/17/sql-server-recovery-model-selection-or-what-s-this-3gb-ldf-file.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;a href="http://searchsqlserver.techtarget.com/tip/0,289483,sid87_gci1069109,00.html"&gt;SQL Server recovery models&lt;/a&gt; can be a bit tricky. By default, SQL Server databases are set to Full Recovery mode, which means they maintain sufficient logs to restore to any specific point in time. These logs can grow to several GB for a small, lightly used database over time. For a development workstation, especially &lt;a href="http://www.codinghorror.com/blog/archives/000743.html"&gt;if your database is under version control&lt;/a&gt;, I like to change the recovery mode to Simple (most cases) or Bulk-Logged at most. Then you can shrink the database, which will clear out the log file in Simple mode - you can do this in SQL Server Management Studio by right clicking on the database, selecting Shrink, then Database. Keep in mind that &lt;a href="http://www.karaszi.com/SQLServer/info_dont_shrink.asp"&gt;shrinking databases can be a little more complex&lt;/a&gt; that I&amp;#39;ve pointed out here, but that&amp;#39;s not normally important on a developer workstation.&lt;/p&gt; &lt;p&gt;&lt;img src="http://static.flickr.com/141/324667781_421c6861cd.jpg?v=0" /&gt; &lt;/p&gt; &lt;p&gt;Now, that solves the problem for this one database, but it will crop again the next time you create a database. A good way to deal with this once and for all is to change the recovery mode of your Model database to Simple or Bulk Logged, since that setting will apply to all future databases you create. Just make sure to update the recovery mode when you database is deployed to a production environment.&lt;/p&gt; &lt;p&gt;This is just a simplistic solution for SQL Server running on a developer machine - see some &lt;a href="http://support.microsoft.com/kb/317375"&gt;more in depth information on the MS Support site&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=1259799" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Notes for 11/28 Talk - SMO and INFORMATION_SCHEMA</title><link>http://weblogs.asp.net/jgalloway/archive/2006/12/03/notes-for-11-28-talk-smo-and-information-schema.aspx</link><pubDate>Sun, 03 Dec 2006 08:26:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:1115623</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=1115623</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2006/12/03/notes-for-11-28-talk-smo-and-information-schema.aspx#comments</comments><description>&lt;p&gt;I spoke at the San Diego .NET User Group meetin on 11/28 on SQL Server Metadata (SMO and INFORMATION_SCHEMA). The complete notes should be available on the&amp;nbsp;&lt;a href="http://www.sandiegodotnet.com/Downloads/tabid/74/Default.aspx"&gt;user group downloads page&lt;/a&gt;, but here are the rough notes:&lt;/p&gt; &lt;h3&gt;Extract of the PowerPoint outline:&lt;/h3&gt; &lt;ul&gt; &lt;li&gt;What is Metadata?&lt;/li&gt; &lt;li&gt;SMO&lt;/li&gt; &lt;ul&gt; &lt;li&gt;What is it?&lt;/li&gt; &lt;li&gt;Simple sample - C#&lt;/li&gt; &lt;li&gt;Data Dictionary Creator&lt;/li&gt; &lt;li&gt;PowerSMO&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;INFORMATION_SCHEMA views&lt;/li&gt; &lt;ul&gt; &lt;li&gt;What are they?&lt;/li&gt; &lt;li&gt;Sample SQL utility scripts&lt;/li&gt; &lt;li&gt;Unpivot example&lt;/li&gt; &lt;li&gt;SubSonic demo&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;References&lt;/li&gt; &lt;ul&gt; &lt;li&gt;SMO &amp;ndash; David Hayden &lt;a href="http://davidhayden.com/blog/dave/category/51.aspx"&gt;http://davidhayden.com/blog/dave/category/51.aspx&lt;/a&gt;&lt;/li&gt; &lt;li&gt;INFORMATION_SCHEMA &lt;a href="http://haacked.com/archive/2006/07/05/BulletproofSqlChangeScriptsUsingINFORMATION_SCHEMAViews.aspx"&gt;http://haacked.com/archive/2006/07/05/BulletproofSqlChangeScriptsUsingINFORMATION_SCHEMAViews.aspx&lt;/a&gt;&lt;/li&gt; &lt;li&gt;PowerSMO &amp;ndash; Dan Sullivan &lt;a href="http://pluralsight.com/blogs/dan/"&gt;http://pluralsight.com/blogs/dan/&lt;/a&gt;&lt;/li&gt; &lt;li&gt;DataDictionaryCreator &lt;a href="http://codeplex.com/datadictionary"&gt;http://codeplex.com/datadictionary&lt;/a&gt;&lt;/li&gt; &lt;li&gt;SubSonic &lt;a href="http://codeplex.com/actionpack"&gt;http://codeplex.com/actionpack&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h3&gt;Here&amp;#39;s my PowerSMO (SMO in PowerShell) script:&lt;/h3&gt; &lt;p&gt;&lt;font color="#0000ff" face="Lucida Console" size="2"&gt;#POWERSMO DEMO&lt;br /&gt;#USES POWERSMO - &lt;/font&gt;&lt;a href="http://www.pluralsight.com/dan/samples/PowerSMO.zip"&gt;&lt;font color="#0000ff" face="Lucida Console" size="2"&gt;http://www.pluralsight.com/dan/samples/PowerSMO.zip&lt;/font&gt;&lt;/a&gt; &lt;/p&gt;&lt;p&gt;&lt;font color="#0000ff" face="Lucida Console" size="2"&gt;#SETUP VOODOO&lt;br /&gt;$executionPolicy = get-ExecutionPolicy &lt;br /&gt;set-ExecutionPolicy &amp;quot;Unrestricted&amp;quot;&lt;br /&gt;.\InitPowerSMO.ps1&lt;br /&gt;cls &lt;/font&gt; &lt;/p&gt;&lt;p&gt;&lt;font color="#0000ff" face="Lucida Console" size="2"&gt;#CONNECT TO DATABASE&lt;br /&gt;$server = SMO_Server &amp;quot;(local)\SQL2005&amp;quot;&lt;br /&gt;$server.ConnectionContext.SqlExecutionModes = 3 #ExecuteAndCaptureSql&lt;br /&gt;$database = $server.Databases[&amp;quot;AdventureWorks&amp;quot;]&lt;br /&gt;$database.FileGroups[0].Files[0].FileName &lt;/font&gt; &lt;/p&gt;&lt;p&gt;&lt;font color="#0000ff" face="Lucida Console" size="2"&gt;#LIST TABLES&lt;br /&gt;$database.Tables | %{$_.name} &lt;/font&gt; &lt;/p&gt;&lt;p&gt;&lt;font color="#0000ff" face="Lucida Console" size="2"&gt;#CREATE A NEW TABLE&lt;br /&gt;$orders = SMO_Table $database &amp;quot;Orders&amp;quot;&lt;br /&gt;$order_number = SMO_Column $orders &amp;quot;Order Number&amp;quot; (SMO_DataType &amp;quot;Int&amp;quot;)&lt;br /&gt;$orders.Columns.Add($order_number)&lt;br /&gt;$customer_name = SMO_Column $orders &amp;quot;Customer Name&amp;quot; (SMO_DataType &amp;quot;Varchar&amp;quot;)&lt;br /&gt;$orders.Columns.Add($customer_name)&lt;br /&gt;$value = SMO_Column $orders &amp;quot;Value&amp;quot; (SMO_DataType &amp;quot;Int&amp;quot;)&lt;br /&gt;$orders.Columns.Add($value)&lt;br /&gt;$orders.Create() &lt;/font&gt; &lt;/p&gt;&lt;p&gt;&lt;font color="#0000ff" face="Lucida Console" size="2"&gt;#SHOW SCHEMA&lt;br /&gt;$orders.Columns | format-Table Name, DataType -auto&lt;br /&gt;$orders.Script() &lt;/font&gt; &lt;/p&gt;&lt;p&gt;&lt;font color="#0000ff" face="Lucida Console" size="2"&gt;#CHANGE SOME COLUMNS&lt;br /&gt;$orders.Columns[&amp;quot;Value&amp;quot;].DataType = (SMO_DataType &amp;quot;Money&amp;quot;)&lt;br /&gt;$orders.Alter()&lt;br /&gt;$orders.Script() &lt;/font&gt; &lt;/p&gt;&lt;p&gt;&lt;font color="#0000ff" face="Lucida Console" size="2"&gt;#DROP A COLUMN, THEN THE TABLE&lt;br /&gt;$orders.Columns[&amp;quot;Customer Name&amp;quot;].Drop()&lt;br /&gt;$orders.Drop() &lt;/font&gt; &lt;/p&gt;&lt;p&gt;&lt;font color="#0000ff" face="Lucida Console" size="2"&gt;cls&lt;br /&gt;$server.ConnectionContext.CapturedSql.Text&lt;br /&gt;#CLEANUP&lt;br /&gt;$server = &amp;#39;&amp;#39;&lt;br /&gt;set-ExecutionPolicy -executionPolicy $executionPolicy&lt;/font&gt; &lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h3&gt;Here&amp;#39;s the code from my winform sample&lt;/h3&gt; &lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:09399a0c-6e59-4dc5-afd1-f35244177afe" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;&lt;pre style="background-color: White"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Collections.Generic;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.ComponentModel;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Data;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Drawing;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Text;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Windows.Forms;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Data.SqlClient;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; Microsoft.SqlServer.Management.Smo;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; Microsoft.SqlServer.Management.Common;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt;&lt;span style="color: #000000"&gt; SMO_Overview&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;partial&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; Main : Form&lt;br /&gt;    {&lt;br /&gt;        Database db;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; Main()&lt;br /&gt;        {&lt;br /&gt;            InitializeComponent();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; btnConnect_Click(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; sender, EventArgs e)&lt;br /&gt;        {&lt;br /&gt;            Cursor.Current &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; Cursors.WaitCursor;&lt;br /&gt;            GetTables(connectionTextbox1.SqlConnectionString);&lt;br /&gt;            Cursor.Current &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; Cursors.Default;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; GetTables(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; connectionString)&lt;br /&gt;        {&lt;br /&gt;            ddlTables.Items.Clear();&lt;br /&gt;            SqlConnection connection &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; SqlConnection(connectionString);&lt;br /&gt;            Server server &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; Server(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; ServerConnection(connection));&lt;br /&gt;            db &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; server.Databases[connection.Database];&lt;br /&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000"&gt; (Table table &lt;/span&gt;&lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;span style="color: #000000"&gt; db.Tables)&lt;br /&gt;            {&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #000000"&gt;!&lt;/span&gt;&lt;span style="color: #000000"&gt;table.IsSystemObject)&lt;br /&gt;                {&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;!&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;.IsNullOrEmpty(table.Schema))&lt;br /&gt;                        ddlTables.Items.Add(table.Schema &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; table.Name);&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;                        ddlTables.Items.Add(table.Name);&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (ddlTables.Items.Count &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;                ddlTables.SelectedIndex &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; ddlTables_SelectedIndexChanged(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; sender, EventArgs e)&lt;br /&gt;        {&lt;br /&gt;            Cursor.Current &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; Cursors.WaitCursor;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;try&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;            {&lt;br /&gt;                &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (ddlTables.SelectedIndex &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;                {&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;[] tableSelection &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; ddlTables.Text.Split(&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;);&lt;br /&gt;                    Table table &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; db.Tables[tableSelection[&lt;/span&gt;&lt;span style="color: #000000"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;],tableSelection[&lt;/span&gt;&lt;span style="color: #000000"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;]];&lt;br /&gt;                    DataTable columnList &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; DataTable();&lt;br /&gt;                    columnList.Columns.Add(&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;Column&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;);&lt;br /&gt;                    columnList.Columns.Add(&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;Datatype&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;);&lt;br /&gt;&lt;br /&gt;                    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000"&gt; (Column column &lt;/span&gt;&lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;span style="color: #000000"&gt; table.Columns)&lt;br /&gt;                    {&lt;br /&gt;                        DataRow row &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; columnList.NewRow();&lt;br /&gt;                        row[&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;Column&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;] &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; column.Name;&lt;br /&gt;                        row[&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;Datatype&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;] &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; SmoUtil.GetDatatypeString(column);&lt;br /&gt;                        columnList.Rows.Add(row);&lt;br /&gt;                    }&lt;br /&gt;                    dgvColumns.DataSource &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; columnList;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;catch&lt;/span&gt;&lt;span style="color: #000000"&gt; { }&lt;br /&gt;            Cursor.Current &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; Cursors.Default;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I previously posted most of the INFORMATION_SCHEMA SQL Scripts: &lt;a href="http://weblogs.asp.net/jgalloway/archive/2006/07/07/455797.aspx"&gt;Some of my favorite INFORMATION_SCHEMA utility queries&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Thanks to &lt;a href="http://justinangel.net/"&gt;Justin Angel&lt;/a&gt;, who sent me hundreds of slides about SQL Server 2005 for his upcoming talk on December 4th at the Israeli SQL Server Usergroup. I&amp;#39;m sorry I won&amp;#39;t be able to see his talk, it looks like it will be incredible.&lt;br /&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=1115623" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Speaking: 11/28 - San Diego .NET User Group</title><link>http://weblogs.asp.net/jgalloway/archive/2006/11/24/speaking-11-28-san-diego-net-user-group.aspx</link><pubDate>Sat, 25 Nov 2006 00:29:54 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:1038675</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=1038675</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2006/11/24/speaking-11-28-san-diego-net-user-group.aspx#comments</comments><description>&lt;p&gt;I'll be speaking at the &lt;a href="http://www.sandiegodotnet.com/Home/tabid/36/ctl/Details/Mid/386/ItemID/19/Default.aspx?selecteddate=11/28/2006"&gt;San Diego .NET User Group on 11/28&lt;/a&gt;:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Jon has been putting some new&amp;nbsp;and lesser used features in SQL Server to work to solve some tough business problems lately. He will review SQL Server Management Objects (SMO), INFORMATION_SCHEMA queries, and some of the T-SQL language enhancements; and then will show how he used them in several recent projects. We'll look at SMO code in a freeware tool Jon recently released, &lt;a href="http://www.codeplex.com/datadictionary"&gt;Data Dictionary Creator&lt;/a&gt;, as well as INFORMATION_SCHEMA queries used to drive code generation in the &lt;a href="http://www.codeplex.com/actionpack"&gt;SubSonic&lt;/a&gt; Data Access Layer system.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;I'm splitting the&amp;nbsp;evening with Zoiner Tejada, who will be speaking on SSIS.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=1038675" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Data Dictionary Creator 1.2 is out on CodePlex</title><link>http://weblogs.asp.net/jgalloway/archive/2006/11/15/Data-Dictionary-Creator-1.2-is-out-on-CodePlex.aspx</link><pubDate>Wed, 15 Nov 2006 08:59:59 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:932386</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=932386</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2006/11/15/Data-Dictionary-Creator-1.2-is-out-on-CodePlex.aspx#comments</comments><description>&lt;p&gt;We just released &lt;a href="http://www.codeplex.com/datadictionary"&gt;Data Dictionary Creator&lt;/a&gt; version&amp;nbsp;1.2 to &lt;a href="http://www.codeplex.com/datadictionary"&gt;CodePlex&lt;/a&gt;. Version 1.2 adds a lot of great features:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Added documentation of tables as well as columns  &lt;li&gt;Changed Excel export from HTML based to XMLSpreadsheet to support separate worksheets for Table and Column documentation &lt;li&gt;Improved error handling - detection of non-DBO logins, etc.  &lt;li&gt;(UI) - tab reorganization to fit workflow a little better  &lt;li&gt;(UI) - moved feedback and progress bars to statusbar for consistency  &lt;li&gt;Support for SQL 2000 and 2005 export scripts (there were several breaking changes from 2000 to 2005)  &lt;li&gt;Limited import functionality (SQL and XML) &lt;/li&gt; &lt;li&gt;Added an Installer&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;DDC is a nice utility to help you document your databases. Even if that kind of thing doesn't appeal to you, you might want to &lt;a href="http://www.codeplex.com/SourceControl/ListDownloadableCommits.aspx?ProjectName=datadictionary"&gt;check out the code&lt;/a&gt; if you're at all interested in using SQL Server Managment Objects (SMO) or saving data to multiple formats (Word via WordML, Excel SpreadsheetML, HTML) using XSLT.&lt;/p&gt; &lt;p&gt;Thanks to Ben Griswold (a.k.a. &lt;a href="http://johnnycoder.com/blog/"&gt;JohnnyCoder&lt;/a&gt;)&amp;nbsp;who made this release possible, or at least kept it from being really crappy. Ben's been cleaning up my coding messes at three different jobs over the past nine years, and I was very happy to have his help&amp;nbsp;for this release. Thanks, Ben!&lt;/p&gt; &lt;p&gt;Let me know if you'd like to contribute, or if you've got any ideas for making DDC better (I think the next big feature to be added is an import from Excel, but what do I know?).&lt;/p&gt; &lt;p&gt;I set up a walkthrough on the DDC Codeplex site, &lt;a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=datadictionary&amp;amp;title=DDC%20Walkthrough"&gt;check it out&lt;/a&gt;!&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;You didn't check it out, did you? I thought you might not. No matter, I'm copying it below:&lt;/p&gt; &lt;h2&gt;Documenting databases is fun again! &lt;/h2&gt;No, not really. But a least it's not a complete waste of time. Unlike those data documents you've written in the past, this one lives with your database (in SQL Server Extended Properties) so it stays up to date. Follow along and we'll show you how easy it is to create useful documentation for that good old Northwind database...&lt;br&gt;&lt;br&gt; &lt;h2&gt;Make a connection &lt;/h2&gt;The first step is to connect to a database. You can enter a connection string or click on the "..." button to view the standard data connection dialog.&lt;br&gt;&lt;img alt="DDC_ConnnectTab.png" src="http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=datadictionary&amp;amp;CountDownload=false&amp;amp;DownloadId=3914"&gt;&lt;br&gt;&lt;br&gt; &lt;h2&gt;Hit the ground running - get advanced! &lt;/h2&gt;You can breeze right by the next tab (Advanced Settings) if you'd like, but it's got some neat gizmos that could save you some time. The two big features:&lt;br&gt; &lt;ul&gt; &lt;li&gt;Auto-Fill - this looks at you schema and adds descriptions for primary and foreign keys. You can customize the text these use. &lt;li&gt;Additional Properties - By default, you're only filling in a simple "Description" tab. If you'd like add other columns to your documentation, you're going to want to look at Additional Properties. &lt;/li&gt;&lt;/ul&gt;&lt;img alt="DDC_AdvancedTab.png" src="http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=datadictionary&amp;amp;CountDownload=false&amp;amp;DownloadId=3913"&gt;&lt;br&gt;&lt;br&gt; &lt;h2&gt;Document that DB &lt;/h2&gt;Did you think we were going to do it for you? Nope! But we'll help:&lt;br&gt; &lt;ol&gt; &lt;li&gt;The user interface shows you column names and datatypes to help you remember what all those columns are used for. &lt;li&gt;The datagrid lets you move around pretty quickly &lt;li&gt;Your work is saved immediately, so you can work on it as time permits. Your edits are saved with the database. &lt;/li&gt;&lt;/ol&gt;&lt;img alt="DDC_DocumentDbTab.png" src="http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=datadictionary&amp;amp;CountDownload=false&amp;amp;DownloadId=3912"&gt;&lt;br&gt;&lt;br&gt; &lt;h2&gt;Show off! &lt;/h2&gt;Now that you've documented your database, you can export it to share with others. There are two ways to share it:&lt;br&gt; &lt;ol&gt; &lt;li&gt;Documentation formats (Word, Excel, HTML) &lt;li&gt;Importable formats (T-SQL, XML) - these let other users import your documentation into their copy of the database &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;&lt;img alt="DDC_ExportTab.png" src="http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=datadictionary&amp;amp;CountDownload=false&amp;amp;DownloadId=3911"&gt;&lt;br&gt;&lt;br&gt;Here's what the export looks like in Excel. Note that the Table and Column documentation are on separate worksheets. Feel free to pick your favorite AutoFormat to pretty this up.&lt;/p&gt; &lt;p&gt;&lt;br&gt;&lt;img alt="DDC_Export_Excel.png" src="http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=datadictionary&amp;amp;CountDownload=false&amp;amp;DownloadId=3908"&gt;&lt;br&gt;&lt;br&gt;Here's the HTML export:&lt;/p&gt; &lt;p&gt;&lt;br&gt;&lt;img alt="DDC_Export_HTML.png" src="http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=datadictionary&amp;amp;CountDownload=false&amp;amp;DownloadId=3909"&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;The next two formats are importable. The T-SQL export can be executed via Query Analyzer / SSMS, or it can be loaded by DDC.&lt;/p&gt; &lt;p&gt;&lt;br&gt;&lt;img alt="DDC_Export_TSQL.png" src="http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=datadictionary&amp;amp;CountDownload=false&amp;amp;DownloadId=3907"&gt;&lt;br&gt;&lt;br&gt;Here's the XML format. The only reasons you'd want to use this are to import into another copy of the database via DDC, or to export to another format. All file exports actually start with XML and go through an XSL transform. You can check out the XSL folder and modify any of these, or create your own.&lt;/p&gt; &lt;p&gt;&lt;br&gt;&lt;img alt="DDC_Export_XML.png" src="http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=datadictionary&amp;amp;CountDownload=false&amp;amp;DownloadId=3906"&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt; &lt;h2&gt;Copy that documentation with Import &lt;/h2&gt;You can use the Import tab to load previously exported documentation. Browser for a T-SQL or XML export from DDC. We do a few checks to make sure the file was created by DDC, then load the documentation into the currently connected database.&lt;br&gt;&lt;img alt="DDC_ImportTab.png" src="http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=datadictionary&amp;amp;CountDownload=false&amp;amp;DownloadId=3910"&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=932386" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/Tools/default.aspx">Tools</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/Links/default.aspx">Links</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/General+Software+Development/default.aspx">General Software Development</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Handling "GO" Separators in SQL Scripts - the easy way</title><link>http://weblogs.asp.net/jgalloway/archive/2006/11/07/Handling-_2200_GO_2200_-Separators-in-SQL-Scripts-_2D00_-the-easy-way.aspx</link><pubDate>Tue, 07 Nov 2006 09:32:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:863821</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>19</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=863821</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2006/11/07/Handling-_2200_GO_2200_-Separators-in-SQL-Scripts-_2D00_-the-easy-way.aspx#comments</comments><description>&lt;p&gt;If you&amp;#39;ve ever had to execute one or more SQL scripts from ADO.NET, you&amp;#39;ve likely run into the GO batch terminator issue. Any SQL script that does anything worthwhile has more than one batch, separated by a GO terminator. &lt;strong&gt;The problem is that &amp;quot;GO&amp;quot; isn&amp;#39;t valid T-SQL, it&amp;#39;s just &lt;/strong&gt;&lt;a href="http://www.sqlservercentral.com/columnists/kpowers/thegocommandandthesemicolonterminator.asp"&gt;&lt;strong&gt;a command used by the SQLCMD, OSQL and ISQL utilities that can also be used within Query Analyzer and the Query Editor window&lt;/strong&gt;&lt;/a&gt;. If you try to execute T-SQL scripts with GO commands in them via ADO.NET SqlCommand.ExecuteNonQuery, you&amp;#39;ll get an error that says something like:&lt;/p&gt; &lt;p&gt;&amp;#39;CREATE/ALTER PROCEDURE&amp;#39; must be the first statement in a query batch&lt;/p&gt; &lt;p&gt;Until recently, there have been two ways to handle this problem - &lt;a href="http://www.mattberther.com/?p=422"&gt;execute SQL scripts by shelling to OSQL&lt;/a&gt;, or &lt;a href="http://www.mattberther.com/?p=619"&gt;splitting the script on GO separators and running them in sequence&lt;/a&gt;. Both solutions &lt;em&gt;kind of worked&lt;/em&gt;, but &lt;a href="http://msdn2.microsoft.com/en-gb/library/ms162169.aspx"&gt;SQL Server Management Objects (SMO)&lt;/a&gt; has a better solution for us: &lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sqlserver.management.common.serverconnection.executenonquery.aspx"&gt;Server.ConnectionContext.ExecuteNonQuery()&lt;/a&gt;, which parses T-SQL statements and &amp;quot;gets&amp;quot; the GO statement as a batch separator. &lt;strong&gt;And the crowd goes wild!!!&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;I&amp;#39;m telling you, if you&amp;#39;re doing anything with SQL Server from .NET code, you really have to look at SMO.&amp;nbsp;&lt;/p&gt; &lt;p&gt;Here&amp;#39;s a simple sample app that iterates SQL scripts in a directory and executes them with that fancy ConnectionContext.ExecuteNonQuery - the one that rocks a house party at the drop of a hat, while retaining the ability to&amp;nbsp;beat a biter down with an aluminum &lt;a href="http://www.lyricsdepot.com/beastie-boys/shake-your-rump.html"&gt;bat&lt;/a&gt;:&lt;/p&gt; &lt;div id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:e95c94c8-a48f-4b8b-be0f-a7f40d4d3f07" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;&lt;pre style="background-color: White"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.IO;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Data.SqlClient;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Collections.Generic;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Microsoft.SqlServer.Smo.dll&lt;/span&gt;&lt;span style="color: #008000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; Microsoft.SqlServer.Management.Smo;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Microsoft.SqlServer.ConnectionInfo.dll&lt;/span&gt;&lt;span style="color: #008000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; Microsoft.SqlServer.Management.Common;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; RunAllSqlSriptsInDirectory&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; Main()&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; scriptDirectory &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;c:\\temp\\sqltest\\&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; sqlConnectionString &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;Integrated Security=SSPI;&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;Persist Security Info=True;Initial Catalog=Northwind;Data Source=(local)&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;;&lt;br /&gt;        DirectoryInfo di &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; DirectoryInfo(scriptDirectory);&lt;br /&gt;        FileInfo[] rgFiles &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; di.GetFiles(&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;*.sql&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;);&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000"&gt; (FileInfo fi &lt;/span&gt;&lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;span style="color: #000000"&gt; rgFiles)&lt;br /&gt;        {&lt;br /&gt;            FileInfo fileInfo &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; FileInfo(fi.FullName);&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; script &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; fileInfo.OpenText().ReadToEnd();&lt;br /&gt;            SqlConnection connection &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; SqlConnection(sqlConnectionString);&lt;br /&gt;            Server server &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; Server(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; ServerConnection(connection));&lt;br /&gt;            &lt;strong&gt;server.ConnectionContext.ExecuteNonQuery(script);&lt;/strong&gt;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=863821" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/Tips+_2F00_+Tricks/default.aspx">Tips / Tricks</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/.NET+code/default.aspx">.NET code</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>SQL Puzzle #1 - Answer</title><link>http://weblogs.asp.net/jgalloway/archive/2006/11/02/SQL-Puzzle-_2300_1-_2D00_-Answer.aspx</link><pubDate>Thu, 02 Nov 2006 07:22:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:788166</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=788166</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2006/11/02/SQL-Puzzle-_2300_1-_2D00_-Answer.aspx#comments</comments><description>&lt;p&gt;Here&amp;#39;s my&amp;nbsp;solution to &lt;a href="http://weblogs.asp.net/jgalloway/archive/2006/11/02/SQL-Puzzle-_2300_1.aspx"&gt;the puzzle in my previous post&lt;/a&gt;. If you haven&amp;#39;t seen the challenge, go back and read that first.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Are you reading this before trying to solve the puzzle on your own? Unacceptable! Your IP is being logged! C&amp;#39;mon, give it a try!&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;h3&gt;My Solution&lt;/h3&gt; &lt;p&gt;I used 29 lines and 3 temp tables. It&amp;#39;s reasonably straightforward - I set up a rank table (#temp), cross join it with the Student table to get ten rows for each student (a cartesian product), then join it to the grades table in order and select out the result. &lt;/p&gt; &lt;p&gt;The one little trick is the ranking in the selct for #temp3 - I used the GradeID as the tie-breaker if the student gets the same grade more than once. Without a tie-breaker, a duplicate score would result duplicating one Row number and&amp;nbsp;&amp;nbsp;skipping the next (e.g. 1,2,3,4,5,5,7,8,9,10).&lt;/p&gt; &lt;p&gt;How would you solve this?&amp;nbsp;&amp;nbsp;&lt;/p&gt; &lt;div id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:bd19b5fa-9ecb-4330-bcd3-190b09199c07" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;&lt;pre style="background-color: White; white-space: -moz-pre-wrap"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;use&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000"&gt;test&lt;/span&gt;&lt;span style="color: #ff0000"&gt;]&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;go&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;IF&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff00ff"&gt;OBJECT_ID&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;tempdb..#temp&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;IS&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;NOT&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;NULL&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;drop&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt; #&lt;/span&gt;&lt;span style="color: #0000ff"&gt;temp&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;create&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt; #&lt;/span&gt;&lt;span style="color: #0000ff"&gt;temp&lt;/span&gt;&lt;span style="color: #000000"&gt; (Row &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;declare&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;while&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;10&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;begin&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color: #0000ff"&gt;insert&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;into&lt;/span&gt;&lt;span style="color: #000000"&gt; #&lt;/span&gt;&lt;span style="color: #0000ff"&gt;temp&lt;/span&gt;&lt;span style="color: #000000"&gt; (Row) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;values&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;end&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;IF&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff00ff"&gt;OBJECT_ID&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;tempdb..#temp2&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;IS&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;NOT&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;NULL&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;drop&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt; #temp2&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;create&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt; #temp2 (Row &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt;, StudentID &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;insert&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;into&lt;/span&gt;&lt;span style="color: #000000"&gt; #temp2 &lt;/span&gt;&lt;span style="color: #0000ff"&gt;select&lt;/span&gt;&lt;span style="color: #000000"&gt; Row, StudentID &lt;/span&gt;&lt;span style="color: #0000ff"&gt;from&lt;/span&gt;&lt;span style="color: #000000"&gt; Student, #&lt;/span&gt;&lt;span style="color: #0000ff"&gt;temp&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;order&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;by&lt;/span&gt;&lt;span style="color: #000000"&gt; StudentID, Row&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;IF&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff00ff"&gt;OBJECT_ID&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;tempdb..#temp3&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;IS&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;NOT&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;NULL&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;drop&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt;  #temp3&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;select&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #000000"&gt;, (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;SELECT&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff00ff"&gt;COUNT&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;FROM&lt;/span&gt;&lt;span style="color: #000000"&gt; Grade pt2 &lt;/span&gt;&lt;span style="color: #0000ff"&gt;WHERE&lt;/span&gt;&lt;span style="color: #000000"&gt; pt2.Grade &lt;/span&gt;&lt;span style="color: #808080"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; (.&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;001&lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #000000"&gt;pt2.GradeID) &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; pt.Grade &lt;/span&gt;&lt;span style="color: #808080"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; (.&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;001&lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #000000"&gt;pt.GradeID) &lt;/span&gt;&lt;span style="color: #808080"&gt;AND&lt;/span&gt;&lt;span style="color: #000000"&gt; pt2.StudentID &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; pt.StudentID) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;AS&lt;/span&gt;&lt;span style="color: #000000"&gt; Row&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;into&lt;/span&gt;&lt;span style="color: #000000"&gt; #temp3&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;from&lt;/span&gt;&lt;span style="color: #000000"&gt; Grade pt &lt;/span&gt;&lt;span style="color: #0000ff"&gt;order&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;by&lt;/span&gt;&lt;span style="color: #000000"&gt; StudentID, Grade &lt;/span&gt;&lt;span style="color: #0000ff"&gt;desc&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;select&lt;/span&gt;&lt;span style="color: #000000"&gt; a.Row, a.StudentID, s.FirstName, s.LastName, b.Grade &lt;/span&gt;&lt;span style="color: #0000ff"&gt;from&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;br /&gt;#temp2 a &lt;/span&gt;&lt;span style="color: #ff00ff"&gt;left&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;join&lt;/span&gt;&lt;span style="color: #000000"&gt; #temp3 b &lt;/span&gt;&lt;span style="color: #0000ff"&gt;on&lt;/span&gt;&lt;span style="color: #000000"&gt; a.StudentID &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; b.StudentID &lt;/span&gt;&lt;span style="color: #808080"&gt;and&lt;/span&gt;&lt;span style="color: #000000"&gt; a.Row &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; b.Row&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;inner&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;join&lt;/span&gt;&lt;span style="color: #000000"&gt; Student s &lt;/span&gt;&lt;span style="color: #0000ff"&gt;on&lt;/span&gt;&lt;span style="color: #000000"&gt; a.StudentID &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; s.StudentID&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;drop&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt; #&lt;/span&gt;&lt;span style="color: #0000ff"&gt;temp&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;drop&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt; #temp2&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;drop&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt; #temp3&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=788166" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>SQL Puzzle #1</title><link>http://weblogs.asp.net/jgalloway/archive/2006/11/02/SQL-Puzzle-_2300_1.aspx</link><pubDate>Thu, 02 Nov 2006 07:05:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:788125</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=788125</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2006/11/02/SQL-Puzzle-_2300_1.aspx#comments</comments><description>&lt;h2&gt;Background&lt;/h2&gt; &lt;p&gt;I had to write a semi-interesting SQL query this past week and thought it might make for a fun SQL puzzle (for very small values of &amp;quot;fun&amp;quot;). &lt;/p&gt; &lt;p&gt;I&amp;#39;m working on a bio-tech business intelligence application, but I simplified things way down to two&amp;nbsp;tables in a SQL Server 2000 database:&amp;nbsp;Student and Grade. The student may have any number of grades, and may of course may get the same grade several times.&lt;/p&gt; &lt;table border="0" cellpadding="5" style="border-collapse: collapse"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign="top"&gt; &lt;table border="1" style="border-collapse: collapse"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;strong&gt;StudentID&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;FirstName&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;LastName&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Bill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;Bobby&lt;/td&gt; &lt;td&gt;Brown&lt;/td&gt;&lt;/tr&gt; &lt;tr height="18" style="height: 13.5pt"&gt; &lt;td height="18" style="height: 13.5pt"&gt;3&lt;/td&gt; &lt;td&gt;Derek&lt;/td&gt; &lt;td&gt;Zoolander&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/td&gt; &lt;td valign="top"&gt; &lt;table border="1" style="border-collapse: collapse"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;strong&gt;GradeID&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;StudentID&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;Grade&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;61&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;98&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;87&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;...&lt;/td&gt; &lt;td&gt;...&lt;/td&gt; &lt;td&gt;...&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;18&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;58&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;19&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;82&lt;/td&gt;&lt;/tr&gt; &lt;tr height="18" style="height: 13.5pt"&gt; &lt;td height="18" style="height: 13.5pt"&gt;20&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;68&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;h2&gt;Task&lt;/h2&gt; &lt;p&gt;Prepare a report that shows the top ten grades for each student.&amp;nbsp;You must show exactly ten rows for each student, so if they have less than ten grades you should show a null. Remember that you need to handle the case where the same student gets the same score more than once (e.g.- in my results below, check out Bobby Brown&amp;#39;s top two grades are both 82).&lt;br /&gt;&lt;/p&gt; &lt;table border="1" cellspacing="0" cellpadding="0" style="border-collapse: collapse"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class="xl25" width="35" style="width: 26pt; height: 12.75pt"&gt;&lt;strong&gt;Row&lt;/strong&gt;&lt;/td&gt; &lt;td class="xl25" width="69" style="width: 52pt"&gt;&lt;strong&gt;StudentID&lt;/strong&gt;&lt;/td&gt; &lt;td class="xl25" width="71" style="width: 53pt"&gt;&lt;strong&gt;FirstName&lt;/strong&gt;&lt;/td&gt; &lt;td class="xl25" width="70" style="width: 53pt"&gt;&lt;strong&gt;LastName&lt;/strong&gt;&lt;/td&gt; &lt;td class="xl25" width="46" style="width: 35pt"&gt;&lt;strong&gt;Grade&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Bill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;94&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Bill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;87&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Bill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;82&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;4&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Bill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;67&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Bill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;62&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;6&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Bill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;58&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;7&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Bill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;8&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Bill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;9&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Bill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;10&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Bill&lt;/td&gt; &lt;td&gt;Smith&lt;/td&gt; &lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;Bobby&lt;/td&gt; &lt;td&gt;Brown&lt;/td&gt; &lt;td&gt;82&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;Bobby&lt;/td&gt; &lt;td&gt;Brown&lt;/td&gt; &lt;td&gt;82&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;Bobby&lt;/td&gt; &lt;td&gt;Brown&lt;/td&gt; &lt;td&gt;72&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;hellip;&lt;/td&gt; &lt;td&gt;&amp;hellip;&lt;/td&gt; &lt;td&gt;&amp;hellip;&lt;/td&gt; &lt;td&gt;&amp;hellip;&lt;/td&gt; &lt;td&gt;&amp;hellip;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;7&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;Derek&lt;/td&gt; &lt;td&gt;Zoolander&lt;/td&gt; &lt;td&gt;53&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;8&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;Derek&lt;/td&gt; &lt;td&gt;Zoolander&lt;/td&gt; &lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;9&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;Derek&lt;/td&gt; &lt;td&gt;Zoolander&lt;/td&gt; &lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;10&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;Derek&lt;/td&gt; &lt;td&gt;Zoolander&lt;/td&gt; &lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;My answer will&amp;nbsp;not use cursors and will run on standard SQL 2000 T-SQL, but if you&amp;#39;d like to submit answers using the SQL 2005 ranking functions, go for it. Post your answers&amp;nbsp;as comments;&amp;nbsp;my answer will be in the next post.&lt;/p&gt; &lt;hr /&gt;  &lt;p&gt;Here&amp;#39;s a SQL script&amp;nbsp;with some&amp;nbsp;sample&amp;nbsp;data.&amp;nbsp;You&amp;#39;ll need to&amp;nbsp;create a &amp;quot;test&amp;quot; database to run this.&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;div id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:a2607eee-c3ff-41e6-be03-a4f8ba3aaeff" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;&lt;pre style="background-color: White"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;use&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000"&gt;test&lt;/span&gt;&lt;span style="color: #ff0000"&gt;]&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;go&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;exists&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;select&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;from&lt;/span&gt;&lt;span style="color: #000000"&gt; information_schema.tables &lt;/span&gt;&lt;span style="color: #0000ff"&gt;where&lt;/span&gt;&lt;span style="color: #000000"&gt; table_name &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Grade&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;drop&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt; Grade&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;exists&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;select&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;from&lt;/span&gt;&lt;span style="color: #000000"&gt; information_schema.tables &lt;/span&gt;&lt;span style="color: #0000ff"&gt;where&lt;/span&gt;&lt;span style="color: #000000"&gt; table_name &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Student&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;drop&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt; student&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;create&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt; Student(&lt;br /&gt;StudentID &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff00ff"&gt;identity&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;primary&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;key&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;br /&gt;FirstName &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;varchar&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;50&lt;/span&gt;&lt;span style="color: #000000"&gt;),&lt;br /&gt;LastName &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;varchar&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;50&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;insert&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;into&lt;/span&gt;&lt;span style="color: #000000"&gt; student(firstname, lastname) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;values&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Bill&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Smith&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;insert&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;into&lt;/span&gt;&lt;span style="color: #000000"&gt; student(firstname, lastname) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;values&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Bobby&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Brown&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;insert&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;into&lt;/span&gt;&lt;span style="color: #000000"&gt; student(firstname, lastname) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;values&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Derek&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;Zoolander&lt;/span&gt;&lt;span style="color: #ff0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;create&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;table&lt;/span&gt;&lt;span style="color: #000000"&gt; Grade(&lt;br /&gt;GradeID &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff00ff"&gt;identity&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;),&lt;br /&gt;StudentID &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;br /&gt;Grade &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;float&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;ALTER&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;TABLE&lt;/span&gt;&lt;span style="color: #000000"&gt; Grade &lt;/span&gt;&lt;span style="color: #0000ff"&gt;WITH&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;CHECK&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;ADD&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;CONSTRAINT&lt;/span&gt;&lt;span style="color: #000000"&gt; FK_Grade_Student &lt;/span&gt;&lt;span style="color: #0000ff"&gt;FOREIGN&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;KEY&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #ff0000"&gt;[&lt;/span&gt;&lt;span style="color: #ff0000"&gt;StudentID&lt;/span&gt;&lt;span style="color: #ff0000"&gt;]&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;REFERENCES&lt;/span&gt;&lt;span style="color: #000000"&gt; Student(StudentID)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;declare&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;declare&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@grade&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;while&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;20&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;begin&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #008000"&gt;@grade&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #ff00ff"&gt;cast&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;50&lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #ff00ff"&gt;rand&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #ff00ff"&gt;cast&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #ff00ff"&gt;cast&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #ff00ff"&gt;newid&lt;/span&gt;&lt;span style="color: #000000"&gt;() &lt;/span&gt;&lt;span style="color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;binary&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;8&lt;/span&gt;&lt;span style="color: #000000"&gt;)) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt;)) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000; font-weight: bold"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt;) &lt;/span&gt;&lt;span style="color: #808080"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;50&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color: #0000ff"&gt;insert&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;into&lt;/span&gt;&lt;span style="color: #000000"&gt; Grade (StudentID,Grade) &lt;/span&gt;&lt;span style="color: #0000ff"&gt;values&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #008000"&gt;@counter&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;%&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;3&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;,&lt;/span&gt;&lt;span style="color: #008000"&gt;@grade&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;end&lt;/span&gt;&lt;span style="color: #000000"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;select&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;*&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;from&lt;/span&gt;&lt;span style="color: #000000"&gt; Grade&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;go&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=788125" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Showing a Connection String prompt in a WinForm application</title><link>http://weblogs.asp.net/jgalloway/archive/2006/10/29/Showing-a-Connection-String-prompt-in-a-WinForm-application.aspx</link><pubDate>Mon, 30 Oct 2006 05:45:03 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:761372</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=761372</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2006/10/29/Showing-a-Connection-String-prompt-in-a-WinForm-application.aspx#comments</comments><description>&lt;a title="Photo Sharing" href="http://www.flickr.com/photos/36836555@N00/283236748/"&gt;&lt;img style="float: left; margin: 0px 10px 0px 0px" height="240" alt="datalink" src="http://static.flickr.com/113/283236748_e268296f30_m.jpg" width="189" align="right" border="0"&gt;&lt;/a&gt;  &lt;p&gt;When I was putting together the &lt;a href="http://weblogs.asp.net/jgalloway/archive/2006/09/28/_5B00_Tool_5D00_-Data-Dictionary-Creator-_2D00_-Rapidly-database-documentation.aspx"&gt;Data Dictionary Creator&lt;/a&gt; program, I needed to allow users to input a connection string. &lt;strong&gt;A lot of winform applications that require data connections give you a textbox and tell you to figure it out yourself, but I really wanted to show a prompt that let you test your connections, select databases on a server, etc.&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;I was surprised that I couldn't find a nice, free, winform control to build a connection string. I found something &lt;a href="http://www.codeproject.com/cs/database/DataLinks.asp"&gt;on CodeProject with a smart solution - it pops up the standard Data Link Properties dialog&lt;/a&gt;. The code mostly worked, but had some problems (passwords were lost if user didn't check "Allow Saving Password", problems with integrated security). I found some general suggestions in the comments and newsgroup postings and threw in some exception handling, and came up with something that worked pretty well for me. It's a little trickier than it looks, since setting certain properties in just the right order shifts the dialog into modes which hide tabs and stuff.&lt;/p&gt; &lt;p&gt;This requires references to a few COM objects:&lt;/p&gt; &lt;p&gt;%PROGRAMFILES%\Microsoft.NET\Primary Interop Assemblies\adodb.dll&lt;br&gt;%PROGRAMFILES%\Common Files\System\Ole DB\OLEDB32.DLL  &lt;p&gt;I found &lt;a href="http://www.developerland.com/VisualBasic/VBGeneral/379.aspx"&gt;an article about using CreateObject or GetTypeByProgID to avoid the interop references&lt;/a&gt;, I think I'd rather ship the interop stubs than worry about dynamic object calls failing when I'm shipping an application, but it's interesting. &lt;p&gt;The next step for this would be bundle this up in a user control (textbox with an ellipses button to launch the dialog, maybe a balloon tip to show connection errors), but I'm not sure I'll get to that any time soon. Here's the code as a simple function that pops the dialog and returns the selected connection string:&amp;nbsp;&lt;/p&gt; &lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:86c07355-6483-4734-8cc1-99bbb0079b53" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color:White;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; &lt;/span&gt;&lt;span style="color: #808080; "&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000; "&gt;
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; Displays a Connection String Builder (DataLinks) dialog.
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; 
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; Credits:
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; &lt;/span&gt;&lt;span style="color: #008000; text-decoration: underline; "&gt;http://www.codeproject.com/cs/database/DataLinks.asp&lt;/span&gt;&lt;span style="color: #008000; "&gt;
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; &lt;/span&gt;&lt;span style="color: #008000; text-decoration: underline; "&gt;http://www.codeproject.com/cs/database/DataLinks.asp?df=100&lt;/span&gt;&lt;span style="color: #008000; "&gt;&amp;amp;forumid=33457&amp;amp;select=1560237#xx1560237xx
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; 
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; Required COM references:
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; %PROGRAMFILES%\Microsoft.NET\Primary Interop Assemblies\adodb.dll
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; %PROGRAMFILES%\Common Files\System\Ole DB\OLEDB32.DLL
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; &lt;/span&gt;&lt;span style="color: #808080; "&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000; "&gt;
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; &lt;/span&gt;&lt;span style="color: #808080; "&gt;&amp;lt;param name=&amp;quot;currentConnectionString&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: #008000; "&gt;Previous database connection string&lt;/span&gt;&lt;span style="color: #808080; "&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000; "&gt;
&lt;/span&gt;&lt;span style="color: #808080; "&gt;///&lt;/span&gt;&lt;span style="color: #008000; "&gt; &lt;/span&gt;&lt;span style="color: #808080; "&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: #008000; "&gt;Selected connection string&lt;/span&gt;&lt;span style="color: #808080; "&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080; "&gt;
&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;private&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;string&lt;/span&gt;&lt;span style="color: #000000; "&gt; PromptForConnectionString(&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;string&lt;/span&gt;&lt;span style="color: #000000; "&gt; currentConnectionString)
{
    MSDASC.DataLinks dataLinks &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;new&lt;/span&gt;&lt;span style="color: #000000; "&gt; MSDASC.DataLinksClass();
    ADODB.Connection dialogConnection;
    &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;string&lt;/span&gt;&lt;span style="color: #000000; "&gt; generatedConnectionString &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;string&lt;/span&gt;&lt;span style="color: #000000; "&gt;.Empty;

    &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;if&lt;/span&gt;&lt;span style="color: #000000; "&gt; (currentConnectionString &lt;/span&gt;&lt;span style="color: #000000; "&gt;==&lt;/span&gt;&lt;span style="color: #000000; "&gt; String.Empty)
    {
        dialogConnection &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; (ADODB.Connection)dataLinks.PromptNew();
        generatedConnectionString &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; dialogConnection.ConnectionString.ToString();
    }
    &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;else&lt;/span&gt;&lt;span style="color: #000000; "&gt;
    {
        dialogConnection &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;new&lt;/span&gt;&lt;span style="color: #000000; "&gt; ADODB.Connection();
        dialogConnection.Provider &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;SQLOLEDB.1&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;;
        ADODB.Property persistProperty &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; dialogConnection.Properties[&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Persist Security Info&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;];
        persistProperty.Value &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;true&lt;/span&gt;&lt;span style="color: #000000; "&gt;;

        dialogConnection.ConnectionString &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; currentConnectionString;
        dataLinks &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;new&lt;/span&gt;&lt;span style="color: #000000; "&gt; MSDASC.DataLinks();

        &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;object&lt;/span&gt;&lt;span style="color: #000000; "&gt; objConn &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; dialogConnection;
        &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;if&lt;/span&gt;&lt;span style="color: #000000; "&gt; (dataLinks.PromptEdit(&lt;/span&gt;&lt;span style="color: #0000FF; "&gt;ref&lt;/span&gt;&lt;span style="color: #000000; "&gt; objConn))
        {
            generatedConnectionString &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; dialogConnection.ConnectionString.ToString();
        }
    }
    generatedConnectionString &lt;/span&gt;&lt;span style="color: #000000; "&gt;=&lt;/span&gt;&lt;span style="color: #000000; "&gt; generatedConnectionString.Replace(&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Provider=SQLOLEDB.1;&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;, &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;string&lt;/span&gt;&lt;span style="color: #000000; "&gt;.Empty);
    &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;if&lt;/span&gt;&lt;span style="color: #000000; "&gt; (
            &lt;/span&gt;&lt;span style="color: #000000; "&gt;!&lt;/span&gt;&lt;span style="color: #000000; "&gt;generatedConnectionString.Contains(&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Integrated Security=SSPI&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;)
            &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;!&lt;/span&gt;&lt;span style="color: #000000; "&gt;generatedConnectionString.Contains(&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Trusted_Connection=True&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;)
            &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;!&lt;/span&gt;&lt;span style="color: #000000; "&gt;generatedConnectionString.Contains(&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Password=&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;)
            &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;!&lt;/span&gt;&lt;span style="color: #000000; "&gt;generatedConnectionString.Contains(&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Pwd=&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;)
        )
        &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;if&lt;/span&gt;&lt;span style="color: #000000; "&gt;(dialogConnection.Properties[&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Password&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;] &lt;/span&gt;&lt;span style="color: #000000; "&gt;!=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;null&lt;/span&gt;&lt;span style="color: #000000; "&gt;)
            generatedConnectionString &lt;/span&gt;&lt;span style="color: #000000; "&gt;+=&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;;Password=&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #000000; "&gt;+&lt;/span&gt;&lt;span style="color: #000000; "&gt; dialogConnection.Properties[&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;Password&lt;/span&gt;&lt;span style="color: #000000; "&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; "&gt;].Value.ToString();

    &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;return&lt;/span&gt;&lt;span style="color: #000000; "&gt; generatedConnectionString;
}
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=761372" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/Tips+_2F00_+Tricks/default.aspx">Tips / Tricks</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/.NET+code/default.aspx">.NET code</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Reading the SQL 2005 instance and installation directory from a DOS batch file</title><link>http://weblogs.asp.net/jgalloway/archive/2006/10/28/Reading-the-SQL-2005-instance-and-installation-directory-from-a-DOS-batch-file.aspx</link><pubDate>Sun, 29 Oct 2006 05:19:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:751900</guid><dc:creator>Jon Galloway</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jgalloway/rsscomments.aspx?PostID=751900</wfw:commentRss><comments>http://weblogs.asp.net/jgalloway/archive/2006/10/28/Reading-the-SQL-2005-instance-and-installation-directory-from-a-DOS-batch-file.aspx#comments</comments><description>&lt;p&gt;SQL 2005 installs each instance to a numbered directory based on the order installed: MSSQL.1, MSSQL.2, etc. That makes scripting a little tricky. Fortunately, you&amp;nbsp;can find them in the registry. &lt;/p&gt;
 
&lt;p&gt;The install path for the SQL 2005 database server is here:&lt;/p&gt;
 
&lt;p&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\SQL2005\Setup\SQLPath&lt;/p&gt;
 
&lt;p&gt;You can look up specific instances in the the Instance Names registry node. For instance, to find the SQL2005 instance, you&amp;#39;d look at this key:&lt;/p&gt;
 
&lt;p&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL\SQL2005&lt;/p&gt;
 
&lt;p&gt;Looking up registry values in a DOS batch file is a little tricky. Fortunately, &lt;a href="http://www.robvanderwoude.com/ntregistry.html"&gt;Robert VanderWoude has a great tip for reading registry values via creative use of the FOR /F command&lt;/a&gt;. &lt;/p&gt;
 
&lt;p&gt;So, with all that out of the way, we can read the SQL Server instance with this line:&lt;/p&gt;
 &lt;div id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:7d5d8111-32f0-4341-98ed-7c924f7a79cb" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;
&lt;pre style="background-color: White"&gt;&lt;div&gt;&lt;!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--&gt;&lt;span style="color: #0000ff"&gt;FOR&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;/&lt;/span&gt;&lt;span style="color: #000000"&gt;F &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;tokens=2* delims=     &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt; %%A IN &lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;#39;REG QUERY &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;/&lt;/span&gt;&lt;span style="color: #000000"&gt;v SQL2005&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;DO&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;SET&lt;/span&gt;&lt;span style="color: #000000"&gt; SQLINSTANCE&lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt;%%B&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and the SQL Server base installation directory with this:&lt;/p&gt;
&lt;div id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:31071eab-097e-4e84-b5af-9ee34af249b8" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline; float: none"&gt;
&lt;pre style="background-color: White"&gt;&lt;div&gt;&lt;!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--&gt;&lt;span style="color: #0000ff"&gt;FOR&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;/&lt;/span&gt;&lt;span style="color: #000000"&gt;F &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;tokens=2* delims=     &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt; %%A IN &lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;#39;REG QUERY &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\SQL2005\Setup&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;/&lt;/span&gt;&lt;span style="color: #000000"&gt;v SQLPath&amp;#39;&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;DO&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;SET&lt;/span&gt;&lt;span style="color: #000000"&gt; DATADIRECTORY&lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt;%%B&lt;/span&gt;&lt;span style="color: #000000"&gt;\&lt;/span&gt;&lt;span style="color: #000000"&gt;Data&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Why would you want to read that kind of stuff in a batch file? Well, to write a &lt;a href="http://weblogs.asp.net/jgalloway/archive/2006/10/28/Batch-files-to-check-SQL-2005-_2800_MDF_2900_-files-in-and-out-of-Subversion-source-control.aspx"&gt;Batch files to check SQL 2005 (MDF) files in and out of Subversion source control&lt;/a&gt;, or course.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=751900" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/jgalloway/archive/tags/Tips+_2F00_+Tricks/default.aspx">Tips / Tricks</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL/default.aspx">SQL</category><category domain="http://weblogs.asp.net/jgalloway/archive/tags/SQL+Server/default.aspx">SQL Server</category></item></channel></rss>