<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Open Source U &#187; SQL Server</title>
	<atom:link href="http://blog.theopensourceu.com/category/technologies/sql-server-technologies/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.theopensourceu.com</link>
	<description>An Open-Source Discovery/Education Blog</description>
	<lastBuildDate>Fri, 07 May 2010 18:39:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using COALESCE to Build Comma-Delimited String directly in SQL</title>
		<link>http://blog.theopensourceu.com/2009/10/using-coalesce-to-build-comma-delimited-string/</link>
		<comments>http://blog.theopensourceu.com/2009/10/using-coalesce-to-build-comma-delimited-string/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 18:46:50 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://theopensourceu.com/?p=488</guid>
		<description><![CDATA[The following article covers a great should-know piece of information for any SQL Server developer. Though, comma delimited strings are not usually desired in database, it can be nice to receive data this way for things like embedding in to a query string or passing over a web service*. http://www.sqlteam.com/article/using-coalesce-to-build-comma-delimited-string *I don&#8217;t necessarily suggest the [...]]]></description>
			<content:encoded><![CDATA[<p>The following article covers a great should-know piece of information for any SQL Server developer. Though, comma delimited strings are not usually desired in database, it can be nice to receive data this way for things like embedding in to a query string or passing over a web service*.</p>
<p><a title="Build a Comma-Delimited string on SQL Server with SQL!" href="http://www.sqlteam.com/article/using-coalesce-to-build-comma-delimited-string">http://www.sqlteam.com/article/using-coalesce-to-build-comma-delimited-string</a></p>
<p>*<small>I don&#8217;t necessarily suggest the web service idea. I was just saying for illustrative purposes&#8230;</small></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.theopensourceu.com/2009/10/using-coalesce-to-build-comma-delimited-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Great SQL Server Tip</title>
		<link>http://blog.theopensourceu.com/2009/05/great-sql-server-tip/</link>
		<comments>http://blog.theopensourceu.com/2009/05/great-sql-server-tip/#comments</comments>
		<pubDate>Fri, 08 May 2009 21:40:30 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://theopensourceu.com/?p=286</guid>
		<description><![CDATA[A couple of great blog articles on sp_msforeachtable. This is an undocumented stored procedure.]]></description>
			<content:encoded><![CDATA[<p>A couple of great blog articles on sp_ms<strong>ForEachTable</strong>. This is an undocumented stored procedure.</p>
<p><a title="http://www.shanedevane.net/msforeachtable" href="http://www.shanedevane.net/msforeachtable">http://www.shanedevane.net/msforeachtable</a><br />
<a href="http://blog.sqlauthority.com/2007/01/31/sql-server-reindexing-database-tables-and-update-statistics-on-tables/">http://blog.sqlauthority.com/2007/01/31/sql-server-reindexing-database-tables-and-update-statistics-on-tables/</a></p>
<p><a title="Undocumented System Procedure 'sp_MSforeachtable' and the @whereand parameter - Stack Overflow" href="http://stackoverflow.com/questions/977400/undocumented-system-procedure-spmsforeachtable-and-the-whereand-parameter">http://stackoverflow.com/questions/977400/undocumented-system-procedure-spmsforeachtable-and-the-whereand-parameter</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.theopensourceu.com/2009/05/great-sql-server-tip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>@@SERVERNAME</title>
		<link>http://blog.theopensourceu.com/2009/01/servername/</link>
		<comments>http://blog.theopensourceu.com/2009/01/servername/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 00:46:11 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[Coding Tip]]></category>
		<category><![CDATA[Design Tip]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Code Example]]></category>
		<category><![CDATA[logical statements]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server 2000]]></category>
		<category><![CDATA[SS200]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://theopensourceu.com/?p=83</guid>
		<description><![CDATA[I was working on a SQL Server project and came across a piece of code that I wanted to only execute if the code was running in production. This is a fairly common problem &#8212; code that should only execute if on a specific environment. Most of the time in my career, I&#8217;ve seen people [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on a SQL Server project and came across a piece of code that I wanted to <strong>only</strong> execute if the code was running in production. This is a fairly common problem &#8212; code that should only execute if on a specific environment.</p>
<p>Most of the time in my career, I&#8217;ve seen people just comment out the code in a given environment. [<em>And yes, I've seen bad mistakes with this method.</em>] While this has obvious potential side effects I didn&#8217;t have time to give it much thought&#8230; Until recently.</p>
<p>I realized that SQL Server 2000 has a system variable called @@ServerName that will always give you the server name for the active connection.</p>
<p>Using this system variable, you can write some very simple code that will only execute other code in a given environment.</p>
<h2>How can you use the variable?</h2>
<p>Usage of the system variable (and any system variable) is easy. You can use either Select, Print or  any logical statements (such as If or While loops).</p>
<p>All of the following are validate:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> @@ServerName<br />
PRINT @@ServerName<br />
<br />
<span style="color: #993333; font-weight: bold;">IF</span> @@Servername <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Me'</span> BEGIN<br />
END</div></td></tr></tbody></table></div>
<h2>How to Implement It</h2>
<p>How I implemented it was a bit unique; I will first start with a very simple example.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Exec ValidateData<br />
<br />
<span style="color: #993333; font-weight: bold;">IF</span> @@ServerName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'ProductionSrvr'</span> BEGIN<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">UPDATE</span> Users<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SET</span> Processed <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> UserID <span style="color: #66cc66;">=</span> @UserID<br />
END</div></td></tr></tbody></table></div>
<p>To quickly walk though this code, it is executeing a stored procedure call ValidateData and then we check the @@ServerName variable to find if it matches &#8220;ProductionSrvr&#8221;. If @@ServerName does, it will execute the update; otherwise the code would go right over the if. This would be great if you didn&#8217;t want to update the Users table in the staging or development environments but you did in production.</p>
<h2>My twist on implementation</h2>
<p>My primary employer does a thing where they lease their servers (and desktop stations) from a provider. The leases expire every 4 years. This means that every four years we migrate databases and web servers to new machines (or virtual machines, as of recent). To my knowledge, we have hundreds of Servers, serving all sorts of software and information (Web sites, databases, Cubes, etc)</p>
<p>Since our SQL Servers are no exception, I wanted to be prepared for such a switch but I still wanted to implement this because of it&#8217;s obvious benefits.</p>
<p>I obviously didn&#8217;t want to do a find and replace on all of my Stored Procedures and DTS packages (don&#8217;t ask) and so I used a function to check the @@ServerName variable against. The function was simple and an example definition follows:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> dbo<span style="color: #66cc66;">.</span>fn_ExampleServer <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
RETURNS VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
BEGIN<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #ff0000;">'ExampleServer'</span> <span style="color: #808080; font-style: italic;">--Simply return a constant with the server's name</span><br />
END</div></td></tr></tbody></table></div>
<p>My @@ServerName example above would simply become:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">EXEC ValidateData<br />
<br />
<span style="color: #993333; font-weight: bold;">IF</span> @@ServerName <span style="color: #66cc66;">=</span> dbo<span style="color: #66cc66;">.</span>fn_ExampleServer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> BEGIN<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">UPDATE</span> Users<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SET</span> Processed <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> UserID <span style="color: #66cc66;">=</span> @UserID<br />
END</div></td></tr></tbody></table></div>
<p>That&#8217;s it! I hope others will find this useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.theopensourceu.com/2009/01/servername/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
