<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ray Wampler's Database Blog</title>
	<atom:link href="http://raywampler.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://raywampler.wordpress.com</link>
	<description>Databases are my thing, you know?</description>
	<lastBuildDate>Wed, 24 Aug 2011 01:03:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='raywampler.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ray Wampler's Database Blog</title>
		<link>http://raywampler.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://raywampler.wordpress.com/osd.xml" title="Ray Wampler&#039;s Database Blog" />
	<atom:link rel='hub' href='http://raywampler.wordpress.com/?pushpress=hub'/>
		<item>
		<title>How to add a video</title>
		<link>http://raywampler.wordpress.com/2011/08/24/63/</link>
		<comments>http://raywampler.wordpress.com/2011/08/24/63/#comments</comments>
		<pubDate>Wed, 24 Aug 2011 00:56:11 +0000</pubDate>
		<dc:creator>raywampler</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://raywampler.wordpress.com/?p=63</guid>
		<description><![CDATA[Here is how you add a video: Enjoy, Ray<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=63&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here is how you add a video:</p>
<span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='500' height='312' src='http://www.youtube.com/embed/8IzWa1o3K1Q?version=3&amp;rel=1&amp;fs=1&amp;showsearch=0&amp;showinfo=1&amp;iv_load_policy=1&amp;wmode=transparent' frameborder='0'></iframe></span>
<p>Enjoy,<br />
Ray</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raywampler.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raywampler.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raywampler.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raywampler.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raywampler.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raywampler.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raywampler.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raywampler.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raywampler.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raywampler.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raywampler.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raywampler.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raywampler.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raywampler.wordpress.com/63/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=63&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raywampler.wordpress.com/2011/08/24/63/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4f3b05a2cc79da12d35be08929d988cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raywampler</media:title>
		</media:content>
	</item>
		<item>
		<title>Dynamic output columns from a SQL stored proc</title>
		<link>http://raywampler.wordpress.com/2009/06/23/dynamic-output-columns-from-a-sql-stored-proc/</link>
		<comments>http://raywampler.wordpress.com/2009/06/23/dynamic-output-columns-from-a-sql-stored-proc/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 04:52:45 +0000</pubDate>
		<dc:creator>raywampler</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Stored Procedure]]></category>
		<category><![CDATA[Transact SQL]]></category>

		<guid isPermaLink="false">http://raywampler.wordpress.com/?p=38</guid>
		<description><![CDATA[ Here&#8217;s an interesting problem: create a sales report that accepts a start date and an end date. The output should be a table with one row per salesperson and a column for each month showing the total sales for that &#8230; <a href="http://raywampler.wordpress.com/2009/06/23/dynamic-output-columns-from-a-sql-stored-proc/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=38&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p> Here&#8217;s an interesting problem: create a sales report that accepts a start date and an end date. The output should be a table with one row per salesperson and a column for each month showing the total sales for that person for that month, starting with the month of the start date and ending with the month of the end date.</p>
<p>Let&#8217;s use Microsoft&#8217;s sample Adventureworks database. If you don&#8217;t have it installed already you can download from the CodePlex samples site here:</p>
<p><a href="http://www.codeplex.com/MSFTDBProdSamples/">http://www.codeplex.com/MSFTDBProdSamples/</a></p>
<p>We can get the information that we need for the report from the table AdventureWorks.Sales.SalesOrderHeader. The table has the following fields that we&#8217;ll use for the report:</p>
<p>OrderDate<br />
SalesPersonID<br />
SubTotal</p>
<p>We can get the sales person name from the included view Sales.vSalesPerson.</p>
<p>The first thought might be to use a PIVOT command. There&#8217;s even a view included with the database, vSalesPersonSalesByFiscalYears, that displays sales by fiscal year using PIVOT. However PIVOT requires fixed output columns and the requirement is that we show all the months between the start and end date, so we have a variable number of columns depending on the input dates.</p>
<p>The technique that I&#8217;m going to describe will output as many monthly columns as you need and will work even in Sql Server 2000, where there be no PIVOT command.</p>
<p>Let&#8217;s begin with the SQL that would calculate the total sales by sales person for just one month. I&#8217;ll make use of the <a href="http://raywampler.wordpress.com/2009/06/22/calculate-the-next-month/">NextMonth function </a>that I described in a previous posting.</p>
<p>DECLARE @startdate datetime<br />
DECLARE @enddate datetime</p>
<p>SET @startdate = &#8217;08/01/2003&#8242; &#8212; just a random date for which there is data<br />
SET @enddate = dbo.NextMonth(@startdate) -1 &#8212; Last day of start month</p>
<p>Select FirstName, LastName, soh.SalesPersonID,<br />
Sum(SubTotal) TotalSales<br />
FROM Sales.SalesOrderHeader soh<br />
JOIN Sales.vSalesPerson sp<br />
ON soh.SalesPersonID = sp.SalesPersonID<br />
WHERE OrderDate BETWEEN @startdate AND @enddate<br />
GROUP BY FirstName, LastName, soh.SalesPersonID</p>
<p>Now lets say that we have an arbitrary start date and end date, such as</p>
<p>SET @startdate = &#8217;08/01/2003&#8242;<br />
SET @enddate = &#8217;03/01/2004&#8242;</p>
<p>We want our query results to sales person information, total sales for the period and the sales for each month within the period:</p>
<p>FirstName<br />
LastName<br />
SalesPersonID<br />
TotalSales<br />
Aug 03<br />
Sep 03<br />
Oct 03<br />
Nov 03<br />
Dec 03<br />
Jan 04<br />
Feb 04<br />
Mar 04</p>
<p>In this example there are eight months, but there could just as easily be one or twenty. The way we can handle any number of months between start and end date is to:</p>
<p>1. First, combine all the sales orders into one row per sales person per month<br />
2. Create a temporary table with the sales person info and a total<br />
3. Loop through each month between start and end date<br />
4. Add a new column for each month<br />
5. Update the new column with the total for each month<br />
6. Display the temp table at the end with all the months</p>
<p>The full source code for the stored proc appears at the end this blog. Most of the steps are straightforward except #4. How do we dynamically add a column for each month? First we need to generate a unique column. We can use the short name of the month plus the year for a unique name for each monthly total:</p>
<p>SET @ColumnName = &#8216;['+Left(DATENAME(mm,@currDate),3)+' '+Right(Convert(varchar(4), Year(@currDate)),2)+']&#8216;</p>
<p>Now we can dynamically add a column to the table using the EXEC command which dynamically executes sql text. EXEC can be dangerous if the text you execute includes user input because it makes you vulnerable to sql injection attack, but in our case we have complete over the text we are executing. Here&#8217;s the command:</p>
<p>EXEC(&#8216;ALTER TABLE ##SalesByMonth ADD &#8216; + @ColumnName +&#8217; float&#8217;)</p>
<p>This will add a new float column named with the month and year. To populate it with data we&#8217;ll need to execute another dynamic statement, updating the new column with the total generated in step one for each sales person for that month.</p>
<p>We call the first temp table #MonthlySalesData and the dynamic output table is #SalesByMonth. Our dynamic update for the newly added column looks like this:</p>
<p>EXEC(<br />
&#8216;UPDATE #SalesByMonth&#8217; +<br />
&#8216; SET &#8216; + @ColumnName + &#8216; = MonthlySales&#8217; +<br />
&#8216; FROM #MonthlySalesData msd&#8217; +<br />
&#8216; JOIN #SalesByMonth sbm&#8217; +<br />
&#8216; ON sbm.[SalesPersonID] = msd.[SalesPersonID]&#8216; +<br />
&#8216; WHERE OrderMonth = &#8221;&#8217; + @currMonth + &#8221;&#8221;)</p>
<p>We put this together into a stored proc named SalesPersonSalesByMonth which you can call with a start and end date likes this:</p>
<p>EXEC dbo.SalesPersonSalesByMonth<br />
@startdate = &#8217;07/01/2003&#8242;,<br />
@enddate = &#8217;09/01/2003&#8242;</p>
<p>The output looks like this:</p>
<p><img class="size-full wp-image-53" title="SalesPersonByMonthOutput" src="http://raywampler.files.wordpress.com/2009/06/salespersonbymonthoutput.png?w=500&#038;h=285" alt="Sales Person By Month Output" width="500" height="285" /> </p>
<p>And finally here is the full source of the stored procedure which unfortunately WordPress will totally mangle, making my code unreadable by inserting hundreds of extra line feeds, many of them in the middle of a line and turning every blank line into four blank lines and messing up the font, color and every other aspect of my source.  Thank you Word Press.</p>
<pre><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">
<pre>IF</pre>
<p></span></span><span style="color:#ff00ff;font-size:x-small;"><span style="color:#ff00ff;font-size:x-small;">OBJECT_ID</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">'dbo.SalesPersonSalesByMonth'</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> </span><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">'P'</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">IS</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">NOT</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">NULL</span></span></p>
<pre><span style="font-size:x-small;">
<pre> </pre>
<p></span></pre>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">DROP</span></span><span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">PROCEDURE</span></span><span style="font-size:x-small;"> dbo</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">.</span></span><span style="font-size:x-small;">SalesPersonSalesByMonth</span></p>
<pre><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">
<pre>GO
CREATE</pre>
<p></span></span></pre>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">PROCEDURE</span></span><span style="font-size:x-small;"> dbo</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">.</span></span><span style="font-size:x-small;">SalesPersonSalesByMonth</span></p>
<pre><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">
<pre>(</pre>
<p></span></span></pre>
<p><span style="font-size:x-small;"></p>
<pre>@startdate</pre>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"> AS DECLARE <span style="font-size:x-small;">@currMonth </span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">datetime</span></span> <span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"> </span></span>DECLARE </span></span><span style="font-size:x-small;">@ColumnName </span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">varchar</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">10</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)</span></span> <span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;"> </span></span>  <span style="color:#008000;font-size:x-small;"><span style="color:#008000;font-size:x-small;"></p>
<pre>-- Create a temp table for the first staff of the calculations</pre>
<p></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"></span></span> </p>
<pre>CREATE</pre>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">TABLE</span></span><span style="font-size:x-small;"> #MonthlySalesData</span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"> </span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span> </p>
<p><span style="font-size:x-small;"></p>
<pre>FirstName</pre>
<p><span style="font-size:x-small;"></p>
<pre>LastName</pre>
<p><span style="font-size:x-small;"></p>
<pre>SalesPersonID</pre>
<p><span style="font-size:x-small;"></p>
<pre>OrderMonth</pre>
<p><span style="font-size:x-small;"></p>
<pre>MonthlySales</pre>
<p><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;"></p>
<pre>)</pre>
<p></span></span><span style="color:#008000;font-size:x-small;"><span style="color:#008000;font-size:x-small;"></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"></p>
<pre>INSERT</pre>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">INTO</span></span><span style="font-size:x-small;"> #MonthlySalesData </span> </p>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"></p>
<pre>Select</pre>
<p><span style="color:#ff00ff;font-size:x-small;"><span style="color:#ff00ff;font-size:x-small;">Sum</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">SubTotal</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)</span></span><span style="font-size:x-small;"> MonthlySales</span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">FROM</span></span><span style="font-size:x-small;"> Sales</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">.</span></span><span style="font-size:x-small;">SalesOrderHeader soh</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">JOIN</span></span><span style="font-size:x-small;"> Sales</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">.</span></span><span style="font-size:x-small;">vSalesPerson sp</span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">ON</span></span><span style="font-size:x-small;"> soh</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">.</span></span><span style="font-size:x-small;">SalesPersonID </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">=</span></span><span style="font-size:x-small;"> sp</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">.</span></span><span style="font-size:x-small;">SalesPersonID</span> <span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">WHERE</span></span><span style="font-size:x-small;"> OrderDate </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">BETWEEN</span></span><span style="font-size:x-small;"> @startdate </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">AND</span></span><span style="font-size:x-small;"> @enddate</span> <span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">GROUP</span></span><span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">BY</span></span><span style="font-size:x-small;"> FirstName</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> LastName</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> soh</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">.</span></span><span style="font-size:x-small;">SalesPersonID</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> </span><span style="color:#ff00ff;font-size:x-small;"><span style="color:#ff00ff;font-size:x-small;">DateAdd</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">d</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">-</span></span><span style="color:#ff00ff;font-size:x-small;"><span style="color:#ff00ff;font-size:x-small;">Day</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">OrderDate</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)+</span></span><span style="font-size:x-small;">1</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> OrderDate</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)</span></span> <span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;"> </span></span>  <span style="color:#008000;font-size:x-small;"><span style="color:#008000;font-size:x-small;"></p>
<pre>-- Create initial temp table with sales person info and overall total</pre>
<p></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"></span></span></p>
<pre>CREATE</pre>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">TABLE</span></span><span style="font-size:x-small;"> #SalesByMonth</span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"> </span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span> </p>
<p><span style="font-size:x-small;"></p>
<pre>FirstName</pre>
<p><span style="font-size:x-small;"></p>
<pre>LastName</pre>
<p><span style="font-size:x-small;"></p>
<pre>SalesPersonID</pre>
<p><span style="font-size:x-small;"></p>
<pre>TotalSales</pre>
<p><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;"></p>
<pre>)
 </pre>
<p></span></span><span style="color:#008000;font-size:x-small;"><span style="color:#008000;font-size:x-small;"></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"></p>
<pre>INSERT</pre>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">INTO</span></span><span style="font-size:x-small;"> #SalesByMonth </span> </p>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"></p>
<pre>Select</pre>
<p><span style="color:#008000;font-size:x-small;"><span style="color:#008000;font-size:x-small;"></p>
<pre>-- Set up the range for the first month</pre>
<p></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"></span></span></p>
<pre>SET</pre>
<p><span style="font-size:x-small;">@currMonth </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">=</span></span><span style="font-size:x-small;"> @startdate</span> </p>
<p><span style="color:#008000;font-size:x-small;"><span style="color:#008000;font-size:x-small;"></p>
<pre>-- Loop through each month from start to end</pre>
<p></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"></span></span></p>
<pre>WHILE</pre>
<p><span style="font-size:x-small;">@currMonth </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">&lt;=</span></span><span style="font-size:x-small;"> @enddate</span> </p>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"></p>
<pre>BEGIN</pre>
<p></span></span><span style="font-size:x-small;"></span> </p>
<pre> </pre>
<p><span style="color:#008000;font-size:x-small;"><span style="color:#008000;font-size:x-small;">-- Name the column with month and year</span></span> </p>
<pre><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">SET</span></span><span style="font-size:x-small;"> @ColumnName </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">=</span></span><span style="font-size:x-small;"> </span><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">'['</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+Left(</span></span><span style="color:#ff00ff;font-size:x-small;"><span style="color:#ff00ff;font-size:x-small;">DATENAME</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">mm</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;">@currMonth</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">),</span></span><span style="font-size:x-small;">3</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)+</span></span><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">' '</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+Right(</span></span><span style="color:#ff00ff;font-size:x-small;"><span style="color:#ff00ff;font-size:x-small;">Convert</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">varchar</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">4</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">),</span></span><span style="font-size:x-small;"> </span><span style="color:#ff00ff;font-size:x-small;"><span style="color:#ff00ff;font-size:x-small;">Year</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">@currMonth</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)),</span></span><span style="font-size:x-small;">2</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)+</span></span><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">']'</span></span></pre>
<p><span style="font-size:x-small;"></p>
<pre> </pre>
<p></span> <span style="color:#008000;font-size:x-small;"><span style="color:#008000;font-size:x-small;">-- Add the column to the output table</span></span></p>
<pre><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">EXEC</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">'ALTER TABLE #SalesByMonth ADD '</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"> @ColumnName </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">' float'</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)</span></span></pre>
<p><span style="font-size:x-small;"></p>
<pre> </pre>
<p></span> <span style="color:#008000;font-size:x-small;"><span style="color:#008000;font-size:x-small;">-- Replace all nulls with 0s so there will be no nulls in output</span></span></p>
<pre><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">EXEC</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">'UPDATE #SalesByMonth SET '</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"> @ColumnName </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"> </span><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">' = 0'</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)</span></span></pre>
<p><span style="font-size:x-small;"></p>
<pre> </pre>
<p></span> <span style="color:#008000;font-size:x-small;"><span style="color:#008000;font-size:x-small;">-- Update the column with the sum for each sales person</span></span></p>
<pre><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">EXEC</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;"></span>
<pre><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">'UPDATE #SalesByMonth'</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"></span>
<pre><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">' SET '</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"> @ColumnName </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"> </span><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">' = MonthlySales'</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"></span>
<pre><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">' FROM #MonthlySalesData msd'</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"></span>
<pre><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">' JOIN #SalesByMonth sbm'</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"></span>
<pre><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">' ON sbm.[SalesPersonID] = msd.[SalesPersonID]'</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"></span>
<pre><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">' WHERE OrderMonth = '''</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"> @currMonth </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">+</span></span><span style="font-size:x-small;"> </span><span style="color:#ff0000;font-size:x-small;"><span style="color:#ff0000;font-size:x-small;">''''</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)</span></span>
<span style="font-size:x-small;">
<pre>  </pre>
<p></span> <span style="color:#008000;font-size:x-small;"><span style="color:#008000;font-size:x-small;">-- Go to the next month</span></span><span style="font-size:x-small;"></span></p>
<pre><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">SET</span></span><span style="font-size:x-small;"> @currMonth </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">=</span></span><span style="font-size:x-small;"> dbo</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">.</span></span><span style="font-size:x-small;">NextMonth</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">@currMonth</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)</span></span>
<span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">
<pre>END
SELECT</pre>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"></p>
<pre>DROP</pre>
<p><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;"></p>
<pre>DROP</pre>
<p>GO  </span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">TABLE</span></span><span style="font-size:x-small;"> #SalesByMonth</span></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">TABLE</span></span><span style="font-size:x-small;"> #MonthlySalesData</span></span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">*</span></span><span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">FROM</span></span><span style="font-size:x-small;"> #SalesByMonth</span></pre>
<p> </pre>
<p> </pre>
<p> </pre>
<p> </pre>
<p> </pre>
<p> </pre>
<p> </pre>
<p></span></span><span style="font-size:x-small;">FirstName</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> LastName</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> SalesPersonID</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> </span><span style="color:#ff00ff;font-size:x-small;"><span style="color:#ff00ff;font-size:x-small;">Sum</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">MonthlySales</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)</span></span><span style="font-size:x-small;"> TotalSales</span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">FROM</span></span><span style="font-size:x-small;"> #MonthlySalesData</span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">GROUP</span></span><span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">BY</span></span><span style="font-size:x-small;"> FirstName</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> LastName</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> SalesPersonID</span> <span style="font-size:x-small;"> </span> </span></span></p>
<pre>-- Now group monthly sales data into one row per salesperson</pre>
<p></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">money</span></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">int</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">varchar</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">50</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">),</span></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">varchar</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">50</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">),</span></span></span></span><span style="font-size:x-small;">FirstName</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> LastName</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> soh</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">.</span></span><span style="font-size:x-small;">SalesPersonID</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> </span><span style="color:#ff00ff;font-size:x-small;"><span style="color:#ff00ff;font-size:x-small;">DateAdd</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">d</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> </span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">-</span></span><span style="color:#ff00ff;font-size:x-small;"><span style="color:#ff00ff;font-size:x-small;">Day</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">OrderDate</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)+</span></span><span style="font-size:x-small;">1</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span><span style="font-size:x-small;"> OrderDate</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">)</span></span><span style="font-size:x-small;"> OrderMonth</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span></span></span> </p>
<pre>-- Combine all the sales order rows
-- into data by sales person by month</pre>
<p></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">money</span></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">dateTime</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">int</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">varchar</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">50</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">),</span></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">varchar</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">(</span></span><span style="font-size:x-small;">50</span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">),</span></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">datetime</span></span><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;">,</span></span> <span style="font-size:x-small;"></p>
<pre>@enddate</pre>
<p><span style="color:#808080;font-size:x-small;"><span style="color:#808080;font-size:x-small;"></p>
<pre>)</pre>
<p></span></span></span><span style="color:#0000ff;font-size:x-small;"><span style="color:#0000ff;font-size:x-small;">datetime</span></span></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raywampler.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raywampler.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raywampler.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raywampler.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raywampler.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raywampler.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raywampler.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raywampler.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raywampler.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raywampler.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raywampler.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raywampler.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raywampler.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raywampler.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=38&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raywampler.wordpress.com/2009/06/23/dynamic-output-columns-from-a-sql-stored-proc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4f3b05a2cc79da12d35be08929d988cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raywampler</media:title>
		</media:content>

		<media:content url="http://raywampler.files.wordpress.com/2009/06/salespersonbymonthoutput.png" medium="image">
			<media:title type="html">SalesPersonByMonthOutput</media:title>
		</media:content>
	</item>
		<item>
		<title>Calculate the next month</title>
		<link>http://raywampler.wordpress.com/2009/06/22/calculate-the-next-month/</link>
		<comments>http://raywampler.wordpress.com/2009/06/22/calculate-the-next-month/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 05:29:10 +0000</pubDate>
		<dc:creator>raywampler</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Transact SQL]]></category>
		<category><![CDATA[User Defined Functions]]></category>

		<guid isPermaLink="false">http://raywampler.wordpress.com/?p=45</guid>
		<description><![CDATA[Here&#8217;s a Sql Server user-defined function that you may find useful. Let&#8217;s say that you have date and that you need to go to the first day of the next month. This can get complicated because months have different numbers &#8230; <a href="http://raywampler.wordpress.com/2009/06/22/calculate-the-next-month/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=45&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a Sql Server user-defined function that you may find useful.  Let&#8217;s say that you have date and that you need to go to the first day of the next month.  This can get complicated because months have different numbers of days (30 days hath September&#8230;) and the year changes when you calculate the next month from December.</p>
<p>The way to do this correctly every time with the least amount of logic is to go back to the first day of the current month and then add one month.  Both steps can be done using DateAdd.</p>
<p>To get to the first day of month (assuming no time component) subtract the day of month and add one:</p>
<p>  DateAdd(d, -Day(@dateIn)+1, @dateIn)</p>
<p>If the date is 4/15/07 then the day is 15.  If you subtract 15 and add 1 then the result is 4/1/07.  This works whether it&#8217;s the first, the 31st, or February 29th on a leap year.</p>
<p>Now to get to the first day of the next month use DateAdd again to add one month.  The DateAdd function figures out the correct result regardless of how many days the month has or if you&#8217;re rolling into the next year.</p>
<p>   DATEADD(mm, 1, @dateOut)</p>
<p>You can now put these commands together into a convenient Sql Server user defined function:</p>
<p>IF EXISTS (SELECT * FROM sysobjects WHERE type = &#8216;Fn&#8217; AND name = &#8216;NextMonth&#8217;)<br />
	BEGIN<br />
		DROP  FUNCTION  dbo.NextMonth<br />
	END<br />
GO</p>
<p>CREATE FUNCTION dbo.NextMonth<br />
	(<br />
		@dateIn datetime<br />
	)<br />
RETURNS datetime<br />
AS<br />
BEGIN<br />
	DECLARE @dateOut datetime</p>
<p>	SET @dateOut = DateAdd(d, -Day(@dateIn)+1, @dateIn)<br />
	SET @dateOut = DATEADD(mm, 1, @dateOut)</p>
<p>	RETURN @dateOut<br />
END<br />
GO</p>
<p>GRANT EXEC ON dbo.NextMonth TO PUBLIC<br />
GO</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raywampler.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raywampler.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raywampler.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raywampler.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raywampler.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raywampler.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raywampler.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raywampler.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raywampler.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raywampler.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raywampler.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raywampler.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raywampler.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raywampler.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=45&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raywampler.wordpress.com/2009/06/22/calculate-the-next-month/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4f3b05a2cc79da12d35be08929d988cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raywampler</media:title>
		</media:content>
	</item>
		<item>
		<title>Server Management Server Object and it&#8217;s Collections</title>
		<link>http://raywampler.wordpress.com/2009/04/20/server-management-server-object-and-its-collections/</link>
		<comments>http://raywampler.wordpress.com/2009/04/20/server-management-server-object-and-its-collections/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 04:55:50 +0000</pubDate>
		<dc:creator>raywampler</dc:creator>
				<category><![CDATA[SQL Management Objects]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[SMO]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://raywampler.wordpress.com/?p=30</guid>
		<description><![CDATA[In my previous blog I explained how to use SMO (SQL Management Objects) to create a connection to SQL Server and return a Server object. The Server object is your gateway into everything contained within the SQL Server and all &#8230; <a href="http://raywampler.wordpress.com/2009/04/20/server-management-server-object-and-its-collections/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=30&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my <a title="Making a connection" href="http://raywampler.wordpress.com/2009/04/09/making-a-connection/">previous blog </a>I explained how to use SMO (SQL Management Objects) to create a connection to SQL Server and return a Server object.  The Server object is your gateway into everything contained within the SQL Server and all of the operations that you can perform with SMO.  I will spend next few posts covering the Server object in detail.  This post will focus on:</p>
<h2>SMO Server Object Collections</h2>
<h3>Databases</h3>
<p>The most important collection that the server object exposes is <strong>Databases</strong>.  This collection gives you access to all the daatabases on the server.  You can access these in a number of ways.  If you know the database name then you use it as a selector:</p>
<pre><span style="color:#2b91af;"><span style="color:#2b91af;">Server</span></span><span> server = </span><span style="color:#0000ff;"><span style="color:#0000ff;">new</span></span><span> </span><span style="color:#2b91af;"><span style="color:#2b91af;">Server</span></span><span>(</span><span style="color:#a31515;"><span style="color:#a31515;">@"(local)\SQLEXPRESS"</span></span><span>);<span>
</span></span><span style="color:#2b91af;"><span style="color:#2b91af;">Database</span></span><span> dbAW = server.Databases[</span><span style="color:#a31515;"><span style="color:#a31515;">"AdventureWorks"</span></span><span>];</span></pre>
<p>You can also access a database using an integer index:</p>
<pre><span style="color:#2b91af;"><span style="color:#2b91af;"><span style="color:#2b91af;"><span style="color:#2b91af;">Database</span></span></span></span><span> dbAW = server.Databases[4];</span></pre>
<p>That technique would be more useful if you wanted to iterate through the databases:</p>
<pre style="padding-left:30px;"><span style="color:#2b91af;"><span style="color:#2b91af;">Database</span></span><span> db;<span>
</span></span><span style="color:#0000ff;"><span style="color:#0000ff;">for</span></span><span> (</span><span style="color:#0000ff;"><span style="color:#0000ff;">int</span></span><span> i = 0; i &lt; server.Databases.Count; i++)
{</span></pre>
<pre style="padding-left:60px;"><span>db = server.Databases[i];
lbDisplay.Items.Add(db.Name);</span></pre>
<pre style="padding-left:30px;"><span>}
</span></pre>
<p><span>You could also iterate through the databases using foreach:<span> </span></span></p>
<pre style="padding-left:30px;"><span><span>

</span></span>
<span style="color:#0000ff;"><span style="color:#0000ff;">foreach</span></span><span>(</span><span style="color:#2b91af;"><span style="color:#2b91af;">Database</span></span><span> db </span><span style="color:#0000ff;"><span style="color:#0000ff;">in</span></span><span> server.Databases)</span>

{

lbDisplay.Items.Add(db.Name);

}</pre>
<p> If you want to create a new database you can use the Create method of Databases collection:</p>
<pre><span><span>

</span></span>
<span style="color:#0000ff;"><span style="color:#0000ff;">private</span></span><span> </span><span style="color:#2b91af;"><span style="color:#2b91af;">Database</span></span><span> CreateDatabase(</span><span style="color:#0000ff;"><span style="color:#0000ff;">string</span></span><span> databaseName)</span>

<span style="color:#2b91af;"><span style="color:#2b91af;">Server</span></span><span> server = </span><span style="color:#0000ff;"><span style="color:#0000ff;">new</span></span><span> </span><span style="color:#2b91af;"><span style="color:#2b91af;">Server</span></span><span>(</span><span style="color:#a31515;"><span style="color:#a31515;">@"(local)\SQLEXPRESS"</span></span><span>);<span>

</span></span>

<span style="color:#2b91af;"><span style="color:#2b91af;">Database</span></span><span> newDb = </span><span style="color:#0000ff;"><span style="color:#0000ff;">new</span></span><span> </span><span style="color:#2b91af;"><span style="color:#2b91af;">Database</span></span><span>(server, databaseName);</span>

<span style="color:#0000ff;"><span style="color:#0000ff;">return</span></span><span> newDb;</span>

}

server.Databases.Add(newDb);
<span>

</span>

{
<span>

</span></pre>
<p>Next I will cover the other collections of the Server object:</p>
<ul>
<li>Audits</li>
<li>Backup Devices</li>
<li>Credentials</li>
<li>Endpoints</li>
<li>Languages</li>
<li>LinkedServers</li>
<li>Logins</li>
<li>Properties</li>
<li>Roles</li>
<li>SystemDataTypes</li>
<li>SystemMessages</li>
<li>Triggers</li>
<li>UserDefinedMessages</li>
</ul>
<pre>
 </pre>
<p><span></p>
<pre> </pre>
<p></span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raywampler.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raywampler.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raywampler.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raywampler.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raywampler.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raywampler.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raywampler.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raywampler.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raywampler.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raywampler.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raywampler.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raywampler.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raywampler.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raywampler.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=30&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raywampler.wordpress.com/2009/04/20/server-management-server-object-and-its-collections/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4f3b05a2cc79da12d35be08929d988cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raywampler</media:title>
		</media:content>
	</item>
		<item>
		<title>Making a connection</title>
		<link>http://raywampler.wordpress.com/2009/04/09/making-a-connection/</link>
		<comments>http://raywampler.wordpress.com/2009/04/09/making-a-connection/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 14:59:15 +0000</pubDate>
		<dc:creator>raywampler</dc:creator>
				<category><![CDATA[Introduction]]></category>
		<category><![CDATA[SQL Management Objects]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[SMO]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql connection string]]></category>
		<category><![CDATA[SQL programming]]></category>

		<guid isPermaLink="false">http://raywampler.wordpress.com/?p=25</guid>
		<description><![CDATA[The first step to using SQL Management Objects is making a connect to the database server. Once you have a connection you can create a Server object that will allow you to programmatically do just about anything that you could through the SQL Server Management Studio.

 
 <a href="http://raywampler.wordpress.com/2009/04/09/making-a-connection/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=25&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The first step to using SQL Management Objects is making a connect to the database server.  You do this using the <span style="font-size:x-small;"><span style="color:#2b91af;">ServerConnection</span></span> object.  For example, if you want to connect to your SQL Express 2008 database the c# code would look like this:</p>
<pre style="padding-left:30px;"><span style="font-size:x-small;color:#2b91af;"><span style="font-size:x-small;color:#2b91af;">ServerConnection</span></span><span style="font-size:x-small;">conn;<span style="font-size:x-small;">conn = </span></span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">new</span></span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#2b91af;"><span style="font-size:x-small;color:#2b91af;">ServerConnection</span></span><span style="font-size:x-small;">(); conn.ServerInstance = "<span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">(local)\SQLEXPRESS</span></span>";
<span style="font-size:x-small;">conn.LoginSecure = </span></span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">true</span></span><span style="font-size:x-small;">; conn.Connect(); </span></pre>
<p>ServerInstance takes the name of the SQL Server as a string.  If you&#8217;ve installed SQL Express locally then the default name is <span style="color:#0000ff;">(local)\SQLEXPRESS</span>.  Setting  LoginSecure = <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">true </span></span>makes the connection using Windows Authentication.  If you wanted to connect using SQL Server authentication then the code would be like this:</p>
<pre style="padding-left:30px;"><span style="font-size:x-small;"><span style="font-size:x-small;">conn.LoginSecure = </span></span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">false</span></span><span style="font-size:x-small;">;<span style="font-size:x-small;">conn.Login = </span></span><span style="font-size:x-small;color:#a31515;"><span style="font-size:x-small;color:#a31515;">"sa"</span></span><span style="font-size:x-small;">;<span style="font-size:x-small;">conn.Password = </span></span><span style="font-size:x-small;color:#a31515;"><span style="font-size:x-small;color:#a31515;">"PassW0rd!"</span></span><span style="font-size:x-small;">;</span></pre>
<p><span style="font-size:x-small;">What can do once you have a connection?  You can create a Server object:</span></p>
<p style="padding-left:30px;"><span style="font-size:x-small;"><span style="font-size:x-small;">sqlServer = </span></span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">new</span></span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#2b91af;"><span style="font-size:x-small;color:#2b91af;">Server</span></span><span style="font-size:x-small;">(conn);</span></p>
<p>The server object gives you a few collections like Databases, Logins, and Settings, that you can use to access, add, delete, and modify these objects.  You use the server object to run Transact-SQL statements directly, you can view and modify all the configuration settings, and you can programmatically do just about anything that you could through the SQL Server Management Studio.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raywampler.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raywampler.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raywampler.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raywampler.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raywampler.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raywampler.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raywampler.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raywampler.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raywampler.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raywampler.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raywampler.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raywampler.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raywampler.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raywampler.wordpress.com/25/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=25&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raywampler.wordpress.com/2009/04/09/making-a-connection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4f3b05a2cc79da12d35be08929d988cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raywampler</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting started with SQL Management Objects</title>
		<link>http://raywampler.wordpress.com/2008/12/05/getting-started-with-sql-management-objects/</link>
		<comments>http://raywampler.wordpress.com/2008/12/05/getting-started-with-sql-management-objects/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 05:50:47 +0000</pubDate>
		<dc:creator>raywampler</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://raywampler.wordpress.com/?p=18</guid>
		<description><![CDATA[The first thing you need to get started with SMO is SQL Server.  If you have Visual Studio then it installs a local instance of SQL Server Express on your computer by default.  For most development activity SQL Express is &#8230; <a href="http://raywampler.wordpress.com/2008/12/05/getting-started-with-sql-management-objects/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=18&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The first thing you need to get started with SMO is SQL Server.  If you have Visual Studio then it installs a local instance of SQL Server Express on your computer by default.  For most development activity SQL Express is just fine.</p>
<p>You can even download and install SQL Express by itself for free:</p>
<p><a href="http://www.microsoft.com/express/sql">http://www.microsoft.com/express/sql</a></p>
<p>The DLLs for SMO get installed in the program directory for SQL Server and they appear in the list of .Net References:</p>
<div id="attachment_21" class="wp-caption alignnone" style="width: 495px"><img class="size-full wp-image-21" title="addsmoreference" src="http://raywampler.files.wordpress.com/2008/12/addsmoreference.jpg?w=500" alt="Add SMO Reference"   /><p class="wp-caption-text">Add SMO Reference</p></div>
<p>If you want to access all the capabilities of SMO you will also need to add a references to:</p>
<ul>
<li>Microsoft.SqlServer.ConnectionInfo</li>
<li>Microsoft.SqlServer.Management.Sdk.Sfc</li>
<li>Microsfot.SqlServer.SqlEnum</li>
</ul>
<p>The top-level SMO object is Server.  To connect to a sql server is fairly easy.   First you reference the SMO in your code module (the folowing examples are in C#):</p>
<p style="padding-left:30px;"> <span style="color:#0000ff;">using </span>Microsoft.SqlServer.Management.Smo;</p>
<p> To connect to SQL Server you create a new instance of the Server object, passing the name of the SQL Server instance to the constructor.  For example, if you want to connect to your local SQL Express instance the code would be:</p>
<p style="padding-left:30px;"><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">string</span></span><span style="font-size:x-small;"> serverName = </span><span style="font-size:x-small;color:#a31515;"><span style="font-size:x-small;color:#a31515;">@&#8221;(local)\SQLEXPRESS&#8221;</span></span><span style="font-size:x-small;">;<br />
</span><span style="font-size:x-small;color:#2b91af;"><span style="font-size:x-small;color:#2b91af;">Server</span></span><span style="font-size:x-small;"> server = </span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">new</span></span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#2b91af;"><span style="font-size:x-small;color:#2b91af;">Server</span></span><span style="font-size:x-small;">(serverName);</span></p>
<p>You can then use the Server object to perform server-level tasks such as create, backup, restore, and drop databases, add logins, and enumerate processes.  It also has some useful collections:</p>
<ul>
<li>Databases</li>
<li>Logins</li>
<li>Roles</li>
<li>Settings</li>
</ul>
<p>In upcoming posts we will explore all the fabulous things you can do with these objects.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raywampler.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raywampler.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raywampler.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raywampler.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raywampler.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raywampler.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raywampler.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raywampler.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raywampler.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raywampler.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raywampler.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raywampler.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raywampler.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raywampler.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=18&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raywampler.wordpress.com/2008/12/05/getting-started-with-sql-management-objects/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4f3b05a2cc79da12d35be08929d988cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raywampler</media:title>
		</media:content>

		<media:content url="http://raywampler.files.wordpress.com/2008/12/addsmoreference.jpg" medium="image">
			<media:title type="html">addsmoreference</media:title>
		</media:content>
	</item>
		<item>
		<title>Welcome to my SMO (SQL Management Objects) Blog</title>
		<link>http://raywampler.wordpress.com/2008/12/01/welcome-to-my-smo-sql-management-objects-blog/</link>
		<comments>http://raywampler.wordpress.com/2008/12/01/welcome-to-my-smo-sql-management-objects-blog/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 06:24:12 +0000</pubDate>
		<dc:creator>raywampler</dc:creator>
				<category><![CDATA[Introduction]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[computers]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[SMO]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[SQL Management Objects]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://raywampler.wordpress.com/?p=5</guid>
		<description><![CDATA[I&#8217;m Ray Wampler and I have decided to blog about SMO: SQL Management Objects, which is yet another Microsoft Data Access library, geared toward providing a complete administrative API for Microsoft SQL Server that you can use with any .Net &#8230; <a href="http://raywampler.wordpress.com/2008/12/01/welcome-to-my-smo-sql-management-objects-blog/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=5&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m <a title="Ray Wampler" href="http://www.RayWampler.com">Ray Wampler </a>and I have decided to blog about SMO: SQL Management Objects, which is yet another Microsoft Data Access library, geared toward providing a complete administrative API for Microsoft SQL Server that you can use with any .Net language such as C# or VB.net.</p>
<p>I have been programming for almost 30 years and started database development back in 1984 with dBase II. (Trivia Note: there never was a dBase I.  Ashton-Tate thought it would be better marketing to start at version 2.)  When I worked at EDS (Electronic Data Systems, founded by Ross Perot) I started with Microsoft Access version 1.0 and SQL Server 4.21.  I then went to work for Pacer/CATS, a company that made ticketing and concessions software for theaters and amusement parks.  There I became the database administrator and architect, working with Access, and SQL Server.  Since then I worked with every version of SQL Server, plus Oracle and MySQL.  I still prefer Microsoft SQL Server because it offers the best performance and ease-of-use at a reasonable cost.</p>
<p>I began programming with Microsoft .net since version 1.0 in 2002, much of it involving databases accessed through ADO and ADO.net.  I&#8217;ve also been keeping up with Microsoft&#8217;s evolving data access technologies: LINQ and the ADO Entity Framework, as well as using various data access layer and libraries like the Microsoft Enterprise Practices Data Access Library, Nhibernate, and .Net Tiers.</p>
<p>Where does SMO fit into all this?  Unlike ADO and it&#8217;s predecessor ODBC, which offer generic access to any number of data sources, SMO is for SQL Server only.  Although this sounds limiting to anyone experienced with n-tier programming, by specializing in SQL Server SMO lets you easily do many things that would be difficult or impossible with a database-agnostic data access technology.</p>
<p>Some of the things you can do with SMO include:</p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Modify and view SQL Server settings, information, and user options.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">View and modify SQL Server settings and configuration options.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Back up and restore databases.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Create a new database or drop and existing database.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Register the instance of SQL Server in the Active Directory directory service.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Subscribe to and handle SQL Server events.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Reference databases, endpoints, credentials, logins, linked servers, system messages, DDL triggers, system data types, and user-defined messages.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Detach and attach databases.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Stop processes or databases.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Grant, deny, or revoke permissions on the database.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Enumerate information about the server.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Read the error log.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">View and modify SQL Server engine objects.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Perform DDL (data definition language) tasks on an instance of SQL Server.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Script databases dependencies.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Transfer database schemas and data.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Reference database objects in collections, such as tables, users, and triggers.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Set up database mirroring.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Check data, allocations, catalogs, and tables.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Issue a checkpoint.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Grant, revoke, and deny permissions to users on the database.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Run Transact-SQL statements.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Enumerate database information, such as locks or object permissions.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Monitor the number of transactions.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Set the database offline or online.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Change the owner of the database.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Update statistics.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Shrink the database.</span></p>
<p style="margin-left:.5in;vertical-align:top;text-indent:-.25in;line-height:140%;"><span style="font-size:10pt;color:black;line-height:140%;font-family:Symbol;"><span>·<span style="font:7pt &quot;">         </span></span></span><span style="font-size:8pt;color:black;line-height:140%;font-family:Verdana;">Truncate the log. </span></p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:small;font-family:Times New Roman;"> Hopefully the large list above answers the question: how can you have an entire blog dedicated to an obscure, specialized and little-known Microsoft technology?  As I&#8217;ve been learning SMO I&#8217;ve been Googling for help on all the tasks that I&#8217;ve needed to do, and have not found a good reference or set of tutorials.  So my each issue of my SMO blog will be dedicated to demonstrating how to do each of the above tasks and many more.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raywampler.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raywampler.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raywampler.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raywampler.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raywampler.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raywampler.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raywampler.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raywampler.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raywampler.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raywampler.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raywampler.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raywampler.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raywampler.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raywampler.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=5&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raywampler.wordpress.com/2008/12/01/welcome-to-my-smo-sql-management-objects-blog/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4f3b05a2cc79da12d35be08929d988cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raywampler</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello world!</title>
		<link>http://raywampler.wordpress.com/2008/12/01/hello-world/</link>
		<comments>http://raywampler.wordpress.com/2008/12/01/hello-world/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 04:56:52 +0000</pubDate>
		<dc:creator>raywampler</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=1&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Welcome to <a href="http://wordpress.com/">WordPress.com</a>. This is your first post. Edit or delete it and start blogging!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/raywampler.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/raywampler.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/raywampler.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/raywampler.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/raywampler.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/raywampler.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/raywampler.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/raywampler.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/raywampler.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/raywampler.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/raywampler.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/raywampler.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/raywampler.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/raywampler.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=raywampler.wordpress.com&amp;blog=5701309&amp;post=1&amp;subd=raywampler&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://raywampler.wordpress.com/2008/12/01/hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4f3b05a2cc79da12d35be08929d988cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">raywampler</media:title>
		</media:content>
	</item>
	</channel>
</rss>
