<?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>Barry King</title>
	<atom:link href="http://barry-king.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://barry-king.com</link>
	<description>Data, Development and Technology</description>
	<lastBuildDate>Wed, 02 Jan 2013 11:12:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='barry-king.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Barry King</title>
		<link>http://barry-king.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://barry-king.com/osd.xml" title="Barry King" />
	<atom:link rel='hub' href='http://barry-king.com/?pushpress=hub'/>
		<item>
		<title>CUBE and GROUPING</title>
		<link>http://barry-king.com/2012/12/28/cube-and-grouping/</link>
		<comments>http://barry-king.com/2012/12/28/cube-and-grouping/#comments</comments>
		<pubDate>Fri, 28 Dec 2012 11:24:27 +0000</pubDate>
		<dc:creator>Barry</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[sql 2008]]></category>
		<category><![CDATA[SQL 2012]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[cube]]></category>
		<category><![CDATA[grouping]]></category>
		<category><![CDATA[grouping_id]]></category>
		<category><![CDATA[rollup]]></category>

		<guid isPermaLink="false">http://barry-king.com/?p=352</guid>
		<description><![CDATA[CUBE was introduced in SQL Server 2005. When you use the CUBE operator it generates a result set of every possible combination from your set of columns; much like a CUBE would be generated if you were using analysis services. the CUBE operator isn&#8217;t something I&#8217;ve had to use much so I&#8217;ll show you an example. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=352&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>CUBE was introduced in SQL Server 2005. When you use the CUBE operator it generates a result set of every possible combination from your set of columns; much like a CUBE would be generated if you were using analysis services. the CUBE operator isn&#8217;t something I&#8217;ve had to use much so I&#8217;ll show you an example.</p>
<p>Lets imagine a Company gets rated by it&#8217;s employees in various categories out of 100. This is a table that holds a summarised rating for a Company; we&#8217;ll insert a single row into this as well so we can query on it.</p>
<p><pre class="brush: sql;">
CREATE TABLE CompanyRating
(
    CompanyID INT,
    CareerOpportunities INT,
    CompensationAndBenefits INT,
    WorkLifeBalance INT,
    SeniorLeadership INT,
    CultureAndValues INT
)

INSERT INTO CompanyRating
 ( CompanyId, CareerOpportunities, CompensationAndBenefits,
 WorkLifeBalance, SeniorLeadership, CultureAndValues )
VALUES
 ( 1, 60, 90, 75, 90, 10 )

</pre></p>
<p>To see the CUBE in action we&#8217;ll write a very simple query.</p>
<p><pre class="brush: sql;">

SELECT
 MIN(CompanyId) AS CompanyId , CareerOpportunities, CompensationAndBenefits,
 WorkLifeBalance, SeniorLeadership, CultureAndValues
FROM
 CompanyRating
GROUP BY
 CareerOpportunities, CompensationAndBenefits, WorkLifeBalance,
 SeniorLeadership, CultureAndValues
 WITH CUBE

</pre></p>
<p>This produces results for all combinations specified in the GROUP BY.</p>
<p><img class="alignnone size-full wp-image-360" alt="results" src="http://sqldev.files.wordpress.com/2012/12/results1.png?w=594&#038;h=327" width="594" height="327" /></p>
<p>Useful huh? What for?. Well today I had a use for it. I wanted to generate a bitwise and sum each combination so I could use it as part of a search process I&#8217;m working on; the idea is that you pass in &#8220;I&#8217;m interested in Companies with a high rating in Work Life Balance and Culture And Values&#8221; and it would return the top 25% of companies rated high for that combination. For this to be fast, I wanted to cache the combinations.</p>
<p>To get a bitwise easily we can use another feature introduced at the same time called GROUPING. This is used to tell us whether a particular column within the GROUP BY expression is aggregated (1) or not (0) for each row in our results. This is really useful for generating a bitwise, because whenever there is a value for a column I&#8217;ll need to sum the bit value I assign to each column to create the bitwise.</p>
<p>I think this is easier to explain with an example:</p>
<p><pre class="brush: sql;">

SELECT
 CASE WHEN GROUPING(CareerOpportunities) = 0 THEN 1 ELSE 0 END C1,
 CASE WHEN GROUPING(CompensationAndBenefits) = 0 THEN 2 ELSE 0 END C2,
 CASE WHEN GROUPING(WorkLifeBalance) = 0 THEN 4 ELSE 0 END C3,
 CASE WHEN GROUPING(SeniorLeadership) = 0 THEN 8 ELSE 0 END C4,
 CASE WHEN GROUPING(CultureAndValues) = 0 THEN 16 ELSE 0 END C5,
 CompanyID, CareerOpportunities, CompensationAndBenefits,
 WorkLifeBalance, SeniorLeadership, CultureAndValues
FROM
 CompanyRating
GROUP BY
 CompanyID, CareerOpportunities, CompensationAndBenefits, WorkLifeBalance,
 SeniorLeadership, CultureAndValues
 WITH CUBE
HAVING CompanyID IS NOT NULL

</pre></p>
<p>Here I&#8217;ve allocated each column a unique bit value; 1,2,4,8,16,32.  The GROUPING function is used to say , &#8220;is WorkLifeBalance not aggregated (so displayed) for this row?&#8221;. I want to have the bit value if this is 0 (zero &#8211; not aggregated). To finish up this example I&#8217;ll add the bit values to get the bitwise and add the rating values to get a total per bitwise.</p>
<p><pre class="brush: sql;">

;WITH Ratings
AS
(
SELECT
 CASE WHEN GROUPING(CareerOpportunities) = 0 THEN 1 ELSE 0 END C1,
 CASE WHEN GROUPING(CompensationAndBenefits) = 0 THEN 2 ELSE 0 END C2,
 CASE WHEN GROUPING(WorkLifeBalance) = 0 THEN 4 ELSE 0 END C3,
 CASE WHEN GROUPING(SeniorLeadership) = 0 THEN 8 ELSE 0 END C4,
 CASE WHEN GROUPING(CultureAndValues) = 0 THEN 16 ELSE 0 END C5,
 CompanyID, CareerOpportunities, CompensationAndBenefits,
 WorkLifeBalance, SeniorLeadership, CultureAndValues
FROM
 CompanyRating
GROUP BY
 CompanyID, CareerOpportunities, CompensationAndBenefits, WorkLifeBalance,
 SeniorLeadership, CultureAndValues
 WITH CUBE
HAVING CompanyID IS NOT NULL
)
SELECT
CompanyID,
(C1+C2+C3+C4+C5) AS Bitwise,
ISNULL(CareerOpportunities,0)+ ISNULL( CompensationAndBenefits,0)+ ISNULL( WorkLifeBalance,0)
+ISNULL(SeniorLeadership,0) + ISNULL( CultureAndValues,0) AS RatingTotal
FROM Ratings

</pre></p>
<p>We can now store this in a table and use it in queries to quickly get us the top 25 percent companies with a particular combination of rating.</p>
<p><pre class="brush: sql;">

SELECT TOP 25 PERCENT CompanyID, RatingTotal
FROM CompanyRatingCache
WHERE Bitwise = 20 -- WorkLifeBalance + CultureAndValues
ORDER BY RatingTotal DESC

</pre></p>
<p>I&#8217;d like to explore other uses for CUBE and possibly ROLLUP too and hear about you have used these features. Please comment or get in touch and I hope you enjoyed this post.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqldev.wordpress.com/352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqldev.wordpress.com/352/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=352&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://barry-king.com/2012/12/28/cube-and-grouping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>51.517408 -0.120299</georss:point>
		<geo:lat>51.517408</geo:lat>
		<geo:long>-0.120299</geo:long>
		<media:content url="http://0.gravatar.com/avatar/634cab94086b59e6231a6a8e6e302238?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Coolcoder</media:title>
		</media:content>

		<media:content url="http://sqldev.files.wordpress.com/2012/12/results1.png" medium="image">
			<media:title type="html">results</media:title>
		</media:content>
	</item>
		<item>
		<title>Inserting the results of a stored procedure into a table</title>
		<link>http://barry-king.com/2012/10/18/inserting-the-results-of-a-stored-procedure-into-a-table/</link>
		<comments>http://barry-king.com/2012/10/18/inserting-the-results-of-a-stored-procedure-into-a-table/#comments</comments>
		<pubDate>Thu, 18 Oct 2012 19:19:09 +0000</pubDate>
		<dc:creator>Barry</dc:creator>
				<category><![CDATA[Article]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL 2000]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[sql 2008]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[data access]]></category>
		<category><![CDATA[openquery]]></category>
		<category><![CDATA[sp_serveroption]]></category>
		<category><![CDATA[tsqlt]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://barry-king.com/?p=341</guid>
		<description><![CDATA[Recently, I needed to find out the schema of a resultset returned by a Stored Procedure so I could pass some failing SQL Unit Tests. I needed to know the columns, the column order and exact datatypes. Without access to a client to run the procedure I needed a way to do this with just [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=341&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Recently, I needed to find out the schema of a resultset returned by a Stored Procedure so I could pass some failing SQL Unit Tests. I needed to know the columns, the column order and exact datatypes.</p>
<p>Without access to a client to run the procedure I needed a way to do this with just Management Studio.  I wanted to insert the results into a table that didn&#8217;t exist so I could look at the schema that got generated.</p>
<p>As it, eventually, turned out it is fairly easy using OPENQUERY. However, as this wasn&#8217;t immediately obvious I thought I’d blog about it in the hope this might be helpful to others.</p>
<p>OPENQUERY requires that you have DATA ACCESS enabled, this is because you’d normally use this to open a query from a linked server and Microsoft in all their wisdom and security conscious minds decided to have this feature disabled by default. For me, I&#8217;m going to call my local instance which OPENQUERY works just as well with.</p>
<p><pre class="brush: sql;">

exec sp_serveroption @server = '{Server\Instance}'
,@optname = 'DATA ACCESS'
 ,@optvalue = 'TRUE'

</pre></p>
<p>Now that is enabled you can perform an insert into a new table from a procedure of your choice.</p>
<p><pre class="brush: sql;">

SELECT * INTO new_temp FROM OPENQUERY([Server\Instance], 'EXEC db.schema.procname' )
</pre></p>
<p>Don’t worry, your procedure can have parameters – I&#8217;ve just left them out in this example. New_temp can be any table that doesn&#8217;t exist. When you run this it will execute the procedure and insert the results into the table.</p>
<p>The only real limitation is that it will take the first resultset only.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqldev.wordpress.com/341/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqldev.wordpress.com/341/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=341&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://barry-king.com/2012/10/18/inserting-the-results-of-a-stored-procedure-into-a-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>51.517408 -0.120299</georss:point>
		<geo:lat>51.517408</geo:lat>
		<geo:long>-0.120299</geo:long>
		<media:content url="http://0.gravatar.com/avatar/634cab94086b59e6231a6a8e6e302238?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Coolcoder</media:title>
		</media:content>
	</item>
		<item>
		<title>The Systemico Model</title>
		<link>http://barry-king.com/2012/08/23/337/</link>
		<comments>http://barry-king.com/2012/08/23/337/#comments</comments>
		<pubDate>Thu, 23 Aug 2012 15:01:05 +0000</pubDate>
		<dc:creator>Barry</dc:creator>
				<category><![CDATA[Article]]></category>

		<guid isPermaLink="false">http://barry-king.com/2012/08/23/337/</guid>
		<description><![CDATA[Reblogged from Barry O&#039;Reilly: Manage value not cost One of the foremost challenges we constantly encounter on development projects is the focus of teams and product managers on managing cost rather than value. In its most basic form this manifests itself daily in user stories where teams tend to concentrate on prioritisation, scope management and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=337&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<div class="reblog-post"><p class="reblog-from"><img alt='' src='http://0.gravatar.com/avatar/347645f2d490f7ba3f419cb614b6f276?s=25&amp;d=identicon&amp;r=G' class='avatar avatar-25' height='25' width='25' /> <a href="http://barryoreilly.com/2012/08/21/the-systemico-model/">Reblogged from Barry O&#039;Reilly:</a></p><div class="wpcom-enhanced-excerpt"><div class="wpcom-enhanced-excerpt-content"><a href="http://barryoreilly.com/2012/08/21/the-systemico-model/" target="_self"><img src="http://barryoreilly.files.wordpress.com/2012/07/realisingvalue.jpg?w=594" alt="Click to visit the original post" class="size-full" /></a><ul class="thumb-list"><li><a href="http://barryoreilly.com/2012/08/21/the-systemico-model/" target="_self"><img src="http://barryoreilly.files.wordpress.com/2012/07/systemico-grid.jpg?w=72&crop=1&h=72" alt="Click to visit the original post" class="size-thumb" width="72" height="72" /></a></li><li><a href="http://barryoreilly.com/2012/08/21/the-systemico-model/" target="_self"><img src="http://barryoreilly.files.wordpress.com/2012/07/systemico-userstory-card.jpg?w=72&crop=1&h=72" alt="Click to visit the original post" class="size-thumb" width="72" height="72" /></a></li><li><a href="http://barryoreilly.com/2012/08/21/the-systemico-model/" target="_self"><img src="http://barryoreilly.files.wordpress.com/2012/07/systemico-grid-only.jpg?w=72&crop=1&h=72" alt="Click to visit the original post" class="size-thumb" width="72" height="72" /></a></li><li><a href="http://barryoreilly.com/2012/08/21/the-systemico-model/" target="_self"><img src="http://barryoreilly.files.wordpress.com/2012/07/systemico.jpg?w=72&crop=1&h=72" alt="Click to visit the original post" class="size-thumb" width="72" height="72" /></a></li><li><a href="http://barryoreilly.com/2012/08/21/the-systemico-model/" target="_self"><img src="http://barryoreilly.files.wordpress.com/2012/07/systemico-release.jpg?w=72&crop=1&h=72" alt="Click to visit the original post" class="size-thumb" width="72" height="72" /></a></li></ul>
<h1>Manage value <em>not</em> cost</h1>
<p>One of the foremost challenges we constantly encounter on development projects is the focus of teams and product managers on managing cost rather than value. In its most basic form this manifests itself daily in user stories where teams tend to concentrate on prioritisation, scope management and throughput in terms of the size of the work. The usual question I hear once a story is described is ‘What size it is?’, in short – how much is that going to cost?</p>
</div> <p class="read-more"><a href="http://barryoreilly.com/2012/08/21/the-systemico-model/" target="_self"><span>Read more&hellip;</span> 1,574 more words</a></p></div></div> ]]></content:encoded>
			<wfw:commentRss>http://barry-king.com/2012/08/23/337/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/634cab94086b59e6231a6a8e6e302238?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Coolcoder</media:title>
		</media:content>
	</item>
		<item>
		<title>Testing tools for SQL development</title>
		<link>http://barry-king.com/2012/08/22/testing-tools-for-sql-development/</link>
		<comments>http://barry-king.com/2012/08/22/testing-tools-for-sql-development/#comments</comments>
		<pubDate>Wed, 22 Aug 2012 16:58:09 +0000</pubDate>
		<dc:creator>Barry</dc:creator>
				<category><![CDATA[SQL 2000]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[sql 2008]]></category>
		<category><![CDATA[SQL 2012]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[RedGate SQL Test]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[Test Driven Developement]]></category>
		<category><![CDATA[tsqlt]]></category>

		<guid isPermaLink="false">http://barry-king.com/?p=299</guid>
		<description><![CDATA[I&#8217;ve recently put together an article on using SQL Test/tSQLt, which you can find here. However, I wanted to briefly talk about the alternatives as I see them. Before any commercial testing tools were around we could just use a combination of manual scripts that called procedures and used temporary tables to hold data. It [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=299&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve recently put together an article on using SQL Test/tSQLt, which you can find<a title="Test Driven Development in the world of SQL" href="http://barry-king.com/2012/08/22/test-driven-development-in-the-world-of-sql/"> here.</a></p>
<p>However, I wanted to briefly talk about the alternatives as I see them.</p>
<p>Before any commercial testing tools were around we could just use a combination of manual scripts that called procedures and used temporary tables to hold data. It wasn&#8217;t that intuitive and there was little reason to put the effort in as automating those tests wasn&#8217;t easy either.</p>
<p><a title="tsqlunit" href="http://sourceforge.net/apps/trac/tsqlunit/" target="_blank">TSQLUnit</a> is a framework that appears to be no longer supported but went some way to realising the dream. It worked as simple stored procedures that you could run, it is kind of forerunner to tSQLt in my opinion as tSQLt offers a much cleaner approach.</p>
<p>Microsoft has had tools for SQL testing that they introduced as the Database Edition of Visual Studio 2008. Anyone that&#8217;s used this will know it didn&#8217;t go far enough, it made it easy to create tests for testing the contract but everything else was complex and hugely lacking information. Microsoft&#8217;s choice in making it only available to users of this edition when most SQL developers used Management Studio also didn&#8217;t help it&#8217;s introduction, usage or the product&#8217;s evolution.</p>
<p>It&#8217;s also available in Visual Studio 2010 but only in Premium or Ultimate Editions (although you can run tests in the Professional Edition).<strong> I believe this is a big mistake, everyone needs to be able to write tests &#8211; it shouldn&#8217;t be a premium feature.</strong></p>
<p>I don&#8217;t think database tests are included in Visual Studio 2012 at all, although you can open 2010 projects using the new SQL Server Data Tools.</p>
<p>So, there was no common way to implement TDD, no structured approach and importantly for everyone &#8211; very little information or articles demonstrating real examples.</p>
<p>Perhaps there&#8217;s just not enough people doing TDD in SQL? I hope that by doing a series of articles that I can help spread the word of TDD for database professionals or anyone that writes SQL as part of their role.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqldev.wordpress.com/299/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqldev.wordpress.com/299/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=299&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://barry-king.com/2012/08/22/testing-tools-for-sql-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<georss:point>51.517408 -0.120299</georss:point>
		<geo:lat>51.517408</geo:lat>
		<geo:long>-0.120299</geo:long>
		<media:content url="http://0.gravatar.com/avatar/634cab94086b59e6231a6a8e6e302238?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Coolcoder</media:title>
		</media:content>
	</item>
		<item>
		<title>Test Driven Development in the world of SQL</title>
		<link>http://barry-king.com/2012/08/22/test-driven-development-in-the-world-of-sql/</link>
		<comments>http://barry-king.com/2012/08/22/test-driven-development-in-the-world-of-sql/#comments</comments>
		<pubDate>Wed, 22 Aug 2012 16:57:16 +0000</pubDate>
		<dc:creator>Barry</dc:creator>
				<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[sql 2008]]></category>
		<category><![CDATA[SQL 2012]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://barry-king.com/?p=206</guid>
		<description><![CDATA[Any software/web developer worth hiring would tell you that Test Driven Development is important. Although it takes time to do properly,  It can increase productivity as features tend to be developed with a clearer understanding of the problem and make changing business requirements much easier to implement. Importantly, your code quality will improve and there [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=206&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Any software/web developer worth hiring would tell you that Test Driven Development is important. Although it takes time to do properly,  It can increase productivity as features tend to be developed with a clearer understanding of the problem and make changing business requirements much easier to implement. Importantly, your code quality will improve and there will be less bugs.</p>
<p>In the world of SQL development we are a little behind the curve on this but as SQL Developers, DBAs, or anyone writing SQL code (yes, it is code) we need to embrace Test Driven Development (TDD) and learn to love the power this gives.</p>
<p>What&#8217;s important to test? Everything! The more test coverage you have the more confidence you will have in making change. Its not a <a title="silver bullet" href="http://goo.gl/1MhpT" target="_blank">silver bullet</a> but it will change the way you think about your work and make it cleaner and better architected. From my experiences there&#8217;s three types of tests you&#8217;ll need to learn about and I&#8217;ll cover them before moving onto the some examples; Contract, Data Quality and Process.</p>
<p><strong>The Contract</strong></p>
<p>What do I mean by the contract? Well, the contract between the application code and the data; every Stored Procedure or SQL statement has an implicit contract with the code. The code expects certain metadata when it calls SQL Server , either directly or via a framework. So the contract is simple, the columns and data types expected by the calling code should not change unexpectedly once created.</p>
<p>This contract is not one written in stone, in fact its very fragile and this is why creating a test based on expectations is a good start before testing data or process.</p>
<p><strong>Data Quality</strong></p>
<p>This is related to the expected values when calling a statement or procedure; if you call X with parameters Y and Z you expect data to be 1, 2 and 3. You really need to think about proper coverage and writing tests to test a single value or parameter where possible. Its as simple as this but up until now not that easy to implement.</p>
<p><strong>Process</strong></p>
<p>Sometimes you will want to test that a number of processes have worked as you expect them to,  in Test Driven Development this is often but not exclusively an integration test. An example is you might want to run a test that calls a procedure that inserts some data and then calls another procedure where you test the output. You can assert that individual steps work and that the final output or result is as expected.</p>
<p>So as a guiding rule, Statements and Procedures are not always participating in a complex process but they should have at least one Contract test and one or more Data Quality tests.</p>
<p><strong>Test Driven Development</strong></p>
<p>So you know of the types of tests we need to think about but what is Test Driven Development? I think the best way to think about it is that instead of just going ahead and writing SQL, you need to think of how you can test what you are writing before you write the code. This is kind of a crazy concept to get your head around until you start doing it.  How can I write a test for something that doesn&#8217;t exist?</p>
<p>In the land of application and web development, there are many tools to help you with this &#8211; <a title="resharper" href="http://www.jetbrains.com/resharper/" target="_blank">Resharper</a> from <a title="jetbrains" href="http://www.jetbrains.com/" target="_blank">JetBrains</a> being a personal favourite of mine. However, the basic principles are the same regardless of the product or technology. When testing SQL, specifically a stored procedure, it would look something like this:</p>
<ul>
<li>Create a test to check for the existence of the stored procedure.</li>
<li>Run it and it will fail as it doesn&#8217;t exist.</li>
<li>Do the minimal amount to pass the test; you can create an empty (stub) stored procedure.</li>
<li>Run the test and it will pass.</li>
</ul>
<p>This pattern is known as red, green and refactor.  To repeat; you write your test and it will fail &#8211; this is important as the failure will help give you confidence when the test passes. Creating the test first means you are really thinking about your expectations.  Then you make it pass by writing the code and then you refactor (make changes) to improve your code.</p>
<p>Often this cycle runs many times, <a title="extract till you drop" href="https://sites.google.com/site/unclebobconsultingllc/one-thing-extract-till-you-drop" target="_blank">extracting</a> and refactoring until you are happy with the quality.</p>
<p>You&#8217;d then create more tests to test the Contract and Data Quality etc. Each iteration builds up your suite of tests until you have the good coverage and fully functioning code.</p>
<p><strong>How we can achieve this in the world of SQL</strong></p>
<p>Whether you have been developing with SQL for years, new to SQL or have come from a development background it has until now been a frustrating experience. In this article I&#8217;m concentrating on a specific product and framework but there are some alternatives. For reference, I&#8217;ve summarised my thoughts on those <a title="Testing tools for SQL development" href="http://barry-king.com/2012/08/22/testing-tools-for-sql-development/">here</a>.</p>
<p>This year <a title="redgate" href="http://www.red-gate.com/" target="_blank">RedGate</a> introduced a product called <a title="sql test" href="http://www.red-gate.com/products/sql-development/sql-test/" target="_blank">SQL Test </a>that allows you to apply TDD to SQL.  SQL Test is a user interface that runs in SQL Server Management Studio, making it an easy choice for most SQL developers. Internally, it uses an open source library called <a title="tsqlt" href="http://tsqlt.org/" target="_blank">tSQLt</a>.  What SQL Test really does over and above the free library is offer an intuitive interface and set of command line tools that can run as part of your continuous build and deployment process.</p>
<p>I&#8217;ll cover creating the three types of tests I mentioned earlier to give you an overview and understanding of how it works using SQL Test and the tSQLt library.</p>
<p>Let&#8217;s get started.</p>
<p>You can download a 28 day trial of RedGate&#8217;s SQL Test at <a title="redgate sql test" href="http://www.red-gate.com/products/sql-development/sql-test/" target="_blank">http://www.red-gate.com/products/sql-development/sql-test/</a>. Once installed, you can add an existing database to SQL Test by clicking the link in the SQL  Test window.</p>
<p><a href="http://sqldev.files.wordpress.com/2012/08/sqltest.png"><img class="alignnone size-medium wp-image-240" title="RedGate SQL Test Add Database to SQL Test" src="http://sqldev.files.wordpress.com/2012/08/sqltest.png?w=300&#038;h=61" alt="Add Database to SQL Test" width="300" height="61" /></a></p>
<p>When you add a database to SQL Test, it will install the tSQLt framework into the database you select, set TRUSTWORTHY ON for and enable SQL CLR. This is a requirement of the framework.</p>
<p>Let&#8217;s create a Contract test. For this we&#8217;ll create a simple table called <strong>Property</strong>, it&#8217;s going to hold details of properties for a fictitious estate agent. For the purposes of this test we will assume this to be an existing table, if it was a new table you could create a test to check it&#8217;s existence first.</p>
<p><pre class="brush: sql;">

CREATE TABLE Property

(

PropertyID INT NOT NULL,
EstateAgentId INT NOT NULL,
Longitude FLOAT NOT NULL,
Latitude FLOAT NOT NULL

)
</pre></p>
<p>Now, lets assume we want to write a stored procedure to return all the properties for a specific Estate Agent. As our first test is going to be Contract type test we just want to verify that the output contains the correct columns. Later, we&#8217;ll write other tests to confirm the Data Quality. You might create a test to check the existence of the stored procedure first but I&#8217;ll cover that later. For now, this is our initial stub procedure:</p>
<p><pre class="brush: sql;">

CREATE PROCEDURE up_Property_select_by_EstateAgentId
    @EstateAgentId INT
AS
    SELECT 1
GO
</pre></p>
<p>As you can see, this doesn&#8217;t actually do much. Its not meant to. We are going to create the test now that will check the contract (metadata) returned by this procedure.</p>
<p>Using SQL Test, click the New Test button. You will get a dialog that allows you to create the test.</p>
<p><a href="http://sqldev.files.wordpress.com/2012/08/sqltest_newtest1.png"><img class="alignnone size-medium wp-image-249" title="new test" src="http://sqldev.files.wordpress.com/2012/08/sqltest_newtest1.png?w=300&#038;h=246" alt="new test" width="300" height="246" /></a></p>
<p>The test name should be as descriptive as possible. It will display when you run the tests. Names can contain spaces but avoid special characters. SQL Test will prefix your test name with the word <strong>test</strong>.</p>
<p>The Test Class is a way to group related tests, SQL Test will put these tests into a schema named the same as the class you specify. Tests are just procedures, once you click Create Test it will open in a new query window ready for you to type the details of the test. SQL Test provides a handy template.</p>
<p><pre class="brush: sql;">

ALTER PROCEDURE [TDD].[test the contract for the property stored proc is correct]
AS
BEGIN
 --Assemble
 -- This section is for code that sets up the environment. It often
 -- contains calls to methods such as tSQLt.FakeTable and tSQLt.SpyProcedure
 -- along with INSERTs of relevant data.
 -- For more information, see http://tsqlt.org/user-guide/isolating-dependencies/

 --Act
 -- Execute the code under test like a stored procedure, function or view
 -- and capture the results in variables or tables.

 --Assert
 -- Compare the expected and actual values, or call tSQLt.Fail in an IF statement.
 -- Available Asserts: tSQLt.AssertEquals, tSQLt.AssertEqualsString, tSQLt.AssertEqualsTable
 -- For a complete list, see: http://tsqlt.org/user-guide/assertions/
 EXEC tSQLt.Fail 'TODO:Implement this test.'

END;

</pre></p>
<p>As you can see, it provides a lot of comments that contain guidance. I&#8217;ve included the links in the resources at the end of this article. We are going to make some changes so we can run our first test.</p>
<p><pre class="brush: sql;">&lt;/pre&gt;
ALTER PROCEDURE [TDD].[test the contract for the property stored proc is correct]
AS
BEGIN

--Assemble
IF OBJECT_ID(N'_expected', 'U') &gt; 0 DROP TABLE _expected;
CREATE TABLE _expected (PropertyId INT, Point GEOGRAPHY);
INSERT _expected VALUES(999, GEOGRAPHY::Point(5.6 , 8.4, 4326))

EXEC tSQLt.FakeTable @TableName = N'Property'
INSERT dbo.Property VALUES(1, 1, 51.3, 1.37)

DECLARE @expected NVARCHAR(MAX) = 'SELECT * FROM _expected'
DECLARE @actual NVARCHAR(MAX) = 'EXEC [up_Property_select_by_EstateAgentId] 1'

--Assert
EXEC tSQLt.AssertResultSetsHaveSameMetaData @expected, @actual

END
&lt;pre&gt;

</pre></p>
<p>So, what&#8217;s going on here? Well, we define the expected schema as a command statement. The expectation is that the execution of the procedure will return a resultset with two columns; PropertyId (INT) and Point (GEOGRAPHY). Next we define the actual command. This statement executes the stored procedure with the required parameter.</p>
<p>Whoa! what&#8217;s this FakeTable thing? I know we created the table earlier but when writing tests we should make sure the test is self contained and not contaminated by existing data. As the stored procedure uses the table Property, we need a way to protect this table and make our test contained. SQL Test has the concept of a FakeTable. When you use the FakeTable method it renames your real table and creates an empty table with the same schema minus any constraints. All this is done within a transaction, as is everything when you run a test.</p>
<p>So we Fake the Property table and then run the assertion. The assertion method <strong>AssertResultSetsHaveSameMetaData </strong>will run and compare the two commands. Thanks to <a title="Greg Lucas" href="https://twitter.com/datacentricity" target="_blank">Greg Lucus</a> for pointing out that when using AssertResultSetsHaveSameMetaData, there needs to be at least one row returned. Also if using a GEOGRAPHY data type then your comparison tables need to be in the same schema.</p>
<p>If the two have the same contract (metadata), then the test passes otherwise it will fail.</p>
<p>Let&#8217;s run the test.</p>
<p><a href="http://sqldev.files.wordpress.com/2012/08/sqltest_runtest.png"><img class="alignnone size-medium wp-image-273" title="run test" src="http://sqldev.files.wordpress.com/2012/08/sqltest_runtest.png?w=300&#038;h=111" alt="run test" width="300" height="111" /></a></p>
<p>Yes! This is what we wanted. We wrote the test and it included our expectation. When the stored procedure ran it didn&#8217;t match what we expected. It Failed. The detailed output shows all the column information we need to resolve this.  Now we need to update our code (the stored procedure)  so that we can pass the test.</p>
<p><pre class="brush: sql;">

ALTER PROCEDURE up_Property_select_by_EstateAgentId
 @EstateAgentId INT
AS
 SELECT PropertyId,
 GEOGRAPHY::Point(Longitude , Latitude, 4326) AS Point
 FROM PROPERTY
GO

</pre></p>
<p>When we run the test again, we see that it passes.</p>
<p><a href="http://sqldev.files.wordpress.com/2012/08/sqltest_runtest_success.png"><img class="alignnone size-medium wp-image-275" title="run test" src="http://sqldev.files.wordpress.com/2012/08/sqltest_runtest_success.png?w=300&#038;h=110" alt="run test " width="300" height="110" /></a></p>
<p>Now if the contract changes which is any of the column names or data types, the test will fail again. We now have some very valuable test coverage.</p>
<p>Next we are going to look at a Data Quality test. Given an Estate Agent, return the property associated with that Estate Agent. In our test we are going to prove that the procedure returns the property for the Estate Agent we specify only by Faking the Property table and inserting two rows, one for the Estate Agent we are testing and one for another. We expect that the procedure will just return one. This is our test.</p>
<p><pre class="brush: sql;">
ALTER PROCEDURE [TDD].[test returns a valid property for an agent]
AS
BEGIN
 --Assemble
 EXEC tSQLt.FakeTable @TableName = N'Property'

CREATE TABLE [TDD].[expected]
 (PropertyId INT, Point GEOGRAPHY)
 CREATE TABLE [TDD].[actual]
 (PropertyId INT, Point GEOGRAPHY)

 INSERT INTO Property (PropertyId, EstateAgentId, Longitude, Latitude)
 VALUES( 1, 1, 1.0,1.0), ( 2, 2, 1.0,1.0)

--Act
 INSERT INTO [TDD].[expected] (PropertyId, Point)
 VALUES (1, GEOGRAPHY::Point(1.0,1.0,4326))

INSERT INTO [TDD].[actual]
 EXEC [up_Property_select_by_EstateAgentId] @EstateAgentId =1

 --Assert
 EXEC tSQLt.AssertEqualsTable @Expected = N'[TDD].[expected]',
 @Actual = N'[TDD].[actual]'

END;
</pre></p>
<p>As you can see, we fake the Property table again and we create two tables to hold the expected and actual values within the TDD class (schema) so that they are dropped when the test completes. We insert two rows into our faked table, with the expectation that our procedure will return one row. Finally, we insert the results of the procedure into the actual table and then assert that the actual and expected tables match.</p>
<p>When we run this, it fails. Again, as expected as we haven&#8217;t added the functionality yet to the procedure to filter the data.</p>
<p>When you start Test Driven Development it is so tempting to write too much code. However, its very important to iterate and only add functionality when you have a covering test.</p>
<p>Now, we will refactor the procedure.</p>
<p><pre class="brush: sql;">
ALTER PROCEDURE up_Property_select_by_EstateAgentId
 @EstateAgentId INT
AS
 SELECT PropertyId,
 GEOGRAPHY::Point(Longitude , Latitude, 4326) AS Point
 FROM PROPERTY
 WHERE EstateAgentId = @EstateAgentId
GO
</pre></p>
<p>We run the tests and they all pass.</p>
<p><a href="http://sqldev.files.wordpress.com/2012/08/sqltest_runall.png"><img class="alignnone size-medium wp-image-292" title="run all tests" src="http://sqldev.files.wordpress.com/2012/08/sqltest_runall.png?w=300&#038;h=118" alt="run all tests" width="300" height="118" /></a></p>
<p>An integration test or process test can be used to make sure a series of events occur for a given scenario. You may have a procedure that calls another which then returns data but it only does this under a specific condition or parameter.  Testing these kind of interactions can be complex but SQL Test and tSQLt offer some handy methods to help you handle many of these complexities.</p>
<p>I&#8217;m going to use a method called <strong>SpyProcedure</strong> which is neat way to determine if a procedure has been called by another procedure or you want to replace functionality of a stored procedure or if you just want to verify parameters that have been passed. What this does is replace your procedure with a doppelganger, it has the same name and signature but basically does nothing apart from log the parameters passed to it. SpyProcedure also allows you to optionally call a command at the same time, useful for insert or updating data as part of your test.</p>
<p>We are going to create a test that checks that an audit has been logged when someone calls the up_Property_select_by_EstateAgentId procedure we have already created. The auditing will be done by a new stored procedure that we will apply the spy to.</p>
<p>Before we can do that we&#8217;ll create a test to verify the auditing procedure exists and a test to verify the auditing procedure inserts data when called. This is  a good example of needing to think about what needs to be tested before you write the code.</p>
<p>Checking for the existence of an object is done using the SQL Test method <strong>AssertObjectExists. </strong>We can write a very simple test using this method:</p>
<p><pre class="brush: sql;">
ALTER PROCEDURE [TDD].[test that the Audit procedure exists]

AS
BEGIN

EXEC tSQLt.AssertObjectExists 'up_Audit_insert'

END;
</pre></p>
<p>As expected this test will fail. Let&#8217;s create the stub for the procedure.</p>
<p><pre class="brush: sql;">
CREATE PROCEDURE up_Audit_insert
    @Event VARCHAR (50),
    @Date DATETIME
AS
    SELECT 1
GO

</pre></p>
<p>Now it passes, lets move onto the data quality test. We need to test that when this is called it inserts data into an Audit table. I&#8217;ve include the schema for the Audit table below, see if you can write a test to check it&#8217;s existence before you create the table.</p>
<p><pre class="brush: sql;">
CREATE TABLE Audit
(
    AuditId INT NOT NULL IDENTITY(1,1),
    Event VARCHAR (50) NOT NULL,
    EventDate DATETIME NOT NULL
)
</pre></p>
<p>Now we can create the test that checks that calling the procedure inserts the expected data by faking the Audit table.</p>
<p><pre class="brush: sql;">
ALTER PROCEDURE [TDD].[test that audit insert proc inserts data]
AS
BEGIN

--Assemble
 DECLARE @expectedEventDate DATETIME = GETDATE()
 DECLARE @expectedEvent VARCHAR(50) = 'Test'

CREATE TABLE [TDD].[expected]
 (
 AuditId INT NOT NULL ,
 Event VARCHAR (50) NOT NULL,
 EventDate DATETIME NOT NULL
 )

 INSERT INTO [TDD].[expected]
 VALUES (1,@expectedEvent, @expectedEventDate)

EXEC tSQLt.FakeTable @TableName = N'Audit',@Identity = 1

 --Act
 EXEC dbo.up_Audit_insert @Event = @expectedEvent,
 @Date = @expectedEventDate

--Assert

 EXEC tSQLt.AssertEqualsTable @Expected = N'[TDD].[expected]',
 @Actual = N'Audit'

END;

</pre></p>
<p>This time we have used FakeTable with a new parameter called @Identity, this fakes the table and retains the identity information. Without this, the faked table will not have an identity and the column would be NULL if data was inserted.</p>
<p>You can probably guess the outcome of this test. It fails. This is because we stubbed out the Audit insert procedure and it actually isn&#8217;t doing an insert.  Refactoring the stored procedure to include the functionality will make the test pass, try this yourself.</p>
<p>We are are ready to write our final test. This will call our original up_Property_select_by_EstateAgentId procedure and test that the audit procedure was called. When you apply SpyProcedure to a procedure, the log will be created that will store the parameters that got passed. The log is named [Procedure]_SpyProcedureLog. It will contain an _id_ column and a column for every parameter. Our final test looks like this:</p>
<p><pre class="brush: sql;">

ALTER PROCEDURE [TDD].[test that audit is called when property proc is run]
AS
BEGIN
 --Assemble

 EXEC tSQLt.FakeTable @TableName = N'Property'
 EXEC tSQLt.SpyProcedure @ProcedureName = N'up_Audit_insert'

 --Act
 EXEC dbo.up_Property_select_by_EstateAgentId @EstateAgentId = 1

 --Assert
 IF NOT EXISTS (SELECT 1 FROM up_Audit_insert_spyprocedurelog
 BEGIN
 EXEC tSQLt.Fail @Message0 = N'Audit was not called'
 END

END;

</pre></p>
<p>We Fake the Property table because we don&#8217;t want real data making our test brittle. We apply the spy to the up_Audit_insert procedure so that we can just track it got called. We execute the up_Property_select_by_EstateAgentId procedure and then assert that the log is not empty. If it&#8217;s empty, we fail the test with a custom message.</p>
<p>Not surprisingly, the test fails.</p>
<p>Simply refactor your up_Property_select_by_EstateAgentId to include the following code that calls the up_Audit_insert procedure.</p>
<p><pre class="brush: sql;">

DECLARE @eventDate DATETIME = GETDATE()

EXEC dbo.up_Audit_insert @Event = 'Property selected',
 @Date = @eventDate

</pre></p>
<p>Now, run all yours tests.</p>
<p><a href="http://sqldev.files.wordpress.com/2012/08/sqltest_final.png"><img class="alignnone size-medium wp-image-315" title="run all tests" src="http://sqldev.files.wordpress.com/2012/08/sqltest_final.png?w=300&#038;h=155" alt="run all tests" width="300" height="155" /></a></p>
<p>We have covered a lot of ground. We have a created Contract (metadata) test, Data Quality tests and a Process test. Hopefully by now you have a good understanding of the basics behind TDD in SQL with SQL Test and tSQLt.</p>
<p>So, how much of this can be done with just tSQLt? Well, as SQL Test is powered by tSQLt maybe not surprisingly apart from not having the user interface to create and run tests it is identical. This makes it very transferable to people within your company or team that don&#8217;t have a SQL Test license or if you don&#8217;t mind not having the graphical interface. To run tests manually without or without SQL Test installed (you must install tSQLt manually if you have not installed SQL Test) you simply use the <strong>Run</strong> method of the tSQt framework:</p>
<p><pre class="brush: sql;">
-- runs all tests
tSQLt.Run

-- runs all tests for specific class
tSQLt.Run 'TDD'

-- run specific test
tSQLt.Run @testname = 'TDD.[test the contract for the property stored proc is correct]'
</pre></p>
<p>There are currently some limitations that might cause a problem for some environments, most notably as FakeTable renames your existing table it cannot be used when the table you want to fake is replicated or has any indexed views against it. I believe the tSQLt team are working on enhancements to resolve this.</p>
<p>There seems to be active development on tSQLt and now RedGate have incorporated this framework in their commercial product it has by the far the best chance of support in the future.</p>
<p><strong>Conclusion and future reading</strong></p>
<p>And finally, I hope this article has helped you understand how Test Driven Development can and should be applied to SQL development and how easy this can be.</p>
<p>As SQL developers and professionals clean and testable code is often overlooked. Although not directly related to SQL I highly recommend<a title="the clean coder" href="http://www.amazon.co.uk/The-Clean-Coder-Professional-Programmers/dp/0137081073/ref=sr_1_1?ie=UTF8&amp;qid=1345537946&amp;sr=8-1" target="_blank"> The Clean Coder</a> and <a title="clean code" href="http://www.amazon.co.uk/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sr_1_1?s=books&amp;ie=UTF8&amp;qid=1345537981&amp;sr=1-1" target="_blank">Clean Code</a> as great reads to make you a better and cleaner coder of SQL.</p>
<p>These are some useful links covering the subject of this post:</p>
<ul>
<li><a title="tSQLt framework" href="http://tsqlt.org/" target="_blank">tSQLt Framework</a></li>
<li><a title="isolating dependencies" href="http://tsqlt.org/user-guide/isolating-dependencies/" target="_blank">Isolating Dependencies</a></li>
<li><a title="assertions" href="http://tsqlt.org/user-guide/assertions/" target="_blank">Assertions</a></li>
<li><a title="RedGate SQL Test Quick Guide" href="http://www.red-gate.com/products/sql-development/sql-test/assets/Test-quick-reference-guide.pdf" target="_blank">RedGate SQL Test Quick Guide</a></li>
<li><a title="greg lucus blog" href="http://datacentricity.net/tag/tsqlt/" target="_blank">Greg Lucus&#8217;s blog with some good tSQLt examples</a></li>
</ul>
<p>TDD is now accessible to SQL development and we should all learn to love the power that this gives.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqldev.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqldev.wordpress.com/206/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=206&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://barry-king.com/2012/08/22/test-driven-development-in-the-world-of-sql/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<georss:point>51.517408 -0.120299</georss:point>
		<geo:lat>51.517408</geo:lat>
		<geo:long>-0.120299</geo:long>
		<media:content url="http://0.gravatar.com/avatar/634cab94086b59e6231a6a8e6e302238?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Coolcoder</media:title>
		</media:content>

		<media:content url="http://sqldev.files.wordpress.com/2012/08/sqltest.png?w=300" medium="image">
			<media:title type="html">RedGate SQL Test Add Database to SQL Test</media:title>
		</media:content>

		<media:content url="http://sqldev.files.wordpress.com/2012/08/sqltest_newtest1.png?w=300" medium="image">
			<media:title type="html">new test</media:title>
		</media:content>

		<media:content url="http://sqldev.files.wordpress.com/2012/08/sqltest_runtest.png?w=300" medium="image">
			<media:title type="html">run test</media:title>
		</media:content>

		<media:content url="http://sqldev.files.wordpress.com/2012/08/sqltest_runtest_success.png?w=300" medium="image">
			<media:title type="html">run test</media:title>
		</media:content>

		<media:content url="http://sqldev.files.wordpress.com/2012/08/sqltest_runall.png?w=300" medium="image">
			<media:title type="html">run all tests</media:title>
		</media:content>

		<media:content url="http://sqldev.files.wordpress.com/2012/08/sqltest_final.png?w=300" medium="image">
			<media:title type="html">run all tests</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER - Soft Delete - IsDelete Column - Your Opinion</title>
		<link>http://barry-king.com/2012/08/15/204/</link>
		<comments>http://barry-king.com/2012/08/15/204/#comments</comments>
		<pubDate>Wed, 15 Aug 2012 11:50:34 +0000</pubDate>
		<dc:creator>Barry</dc:creator>
				<category><![CDATA[Article]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL 2000]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[sql 2008]]></category>

		<guid isPermaLink="false">http://barry-king.com/2012/08/15/204/</guid>
		<description><![CDATA[Reblogged from SQL Server Journey with SQL Authority: Just a day ago, I was reading the blog post of Michale J Swart. If you are a regular reader of this blog, I am sure you will be familiar with him. He is a very interesting blogger for sure. He recently wrote an article about Ten [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=204&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<div class="reblog-post"><p class="reblog-from"><img alt='' src='http://1.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=25&amp;d=identicon&amp;r=G' class='avatar avatar-25' height='25' width='25' /> <a href="http://blog.sqlauthority.com/2010/09/03/sql-server-soft-delete-isdelete-column-your-opinion/">Reblogged from SQL Server Journey with SQL Authority:</a></p><div class="wpcom-enhanced-excerpt"><div class="wpcom-enhanced-excerpt-content"><a href="http://blog.sqlauthority.com/2010/09/03/sql-server-soft-delete-isdelete-column-your-opinion/" target="_self"><img src="http://s0.wp.com/imgpress?url=http%3A%2F%2Fwww.pinaldave.com%2Fbimg%2Frecyclebin.jpg" alt="Click to visit the original post" class="size-full" /></a>
<p>Just a day ago, I was reading the blog post of <a href="http://michaeljswart.com/">Michale J Swart</a>. If you are a regular reader of this blog, I am sure you will be familiar with him. He is a very interesting blogger for sure. He recently wrote an article about <a href="http://michaeljswart.com/?p=823">Ten Things I hate to See in T-SQL</a>; it was really fun, but the thing which caught my eyes was the subject of&hellip;</p>
</div> <p class="read-more"><a href="http://blog.sqlauthority.com/2010/09/03/sql-server-soft-delete-isdelete-column-your-opinion/" target="_self"><span>Read more&hellip;</span> 523 more words</a></p></div></div><div class="reblogger-note"><div class='reblogger-note-content'>
I think it depends on your requirements, I've used a similar flag to indicate "active" records but this also had an automated process to move inactive records into a separate table so those operations could batched.
</div></div>]]></content:encoded>
			<wfw:commentRss>http://barry-king.com/2012/08/15/204/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>51.517408 -0.120299</georss:point>
		<geo:lat>51.517408</geo:lat>
		<geo:long>-0.120299</geo:long>
		<media:content url="http://0.gravatar.com/avatar/634cab94086b59e6231a6a8e6e302238?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Coolcoder</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL Server Test Driven Development with Redgate&#8217;s SQL Test</title>
		<link>http://barry-king.com/2012/08/13/sql-server-test-driven-development-with-redgates-sql-test/</link>
		<comments>http://barry-king.com/2012/08/13/sql-server-test-driven-development-with-redgates-sql-test/#comments</comments>
		<pubDate>Mon, 13 Aug 2012 11:26:53 +0000</pubDate>
		<dc:creator>Barry</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[redgate]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[sql test]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[tsqlt]]></category>

		<guid isPermaLink="false">http://barry-king.com/?p=187</guid>
		<description><![CDATA[I&#8217;ll be providing a more detailed post on this shortly, in the meantime please check out http://www.red-gate.com/products/sql-development/sql-test/. SQL Test internally uses tSQLt, a popular open source framework, combined with Redgate&#8217;s expert knowledge of providing add-ons to management studio means that we now have an integrated toolset for test driven development in the SQL world. Why [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=187&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ll be providing a more detailed post on this shortly, in the meantime please check out <a href="http://www.red-gate.com/products/sql-development/sql-test/" rel="nofollow">http://www.red-gate.com/products/sql-development/sql-test/</a>.</p>
<p>SQL Test internally uses <a title="tSQLt" href="http://goo.gl/rYpsG" target="_blank">tSQLt</a>, a popular open source framework, combined with Redgate&#8217;s expert knowledge of providing add-ons to management studio means that we now have an integrated toolset for test driven development in the SQL world.</p>
<p>Why let the developers have all the fun? TDD is now accessible to SQL development and we should all learn to love the power that this gives.</p>
<p><strong>update: </strong>I&#8217;ve published my article on using <a title="Test Driven Development in the world of SQL" href="http://barry-king.com/2012/08/22/test-driven-development-in-the-world-of-sql/">Test Driven Development in the world of SQL</a>. Have a read and let me know your thoughts.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqldev.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqldev.wordpress.com/187/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=187&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://barry-king.com/2012/08/13/sql-server-test-driven-development-with-redgates-sql-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>51.517408 -0.120299</georss:point>
		<geo:lat>51.517408</geo:lat>
		<geo:long>-0.120299</geo:long>
		<media:content url="http://0.gravatar.com/avatar/634cab94086b59e6231a6a8e6e302238?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Coolcoder</media:title>
		</media:content>
	</item>
		<item>
		<title>RDBMS vs MongoDB concepts</title>
		<link>http://barry-king.com/2011/08/12/rdbms-vs-mongodb-concepts/</link>
		<comments>http://barry-king.com/2011/08/12/rdbms-vs-mongodb-concepts/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 07:42:01 +0000</pubDate>
		<dc:creator>Barry</dc:creator>
				<category><![CDATA[Article]]></category>
		<category><![CDATA[NOSQL]]></category>
		<category><![CDATA[RavenDB]]></category>
		<category><![CDATA[guardian]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[nosql]]></category>
		<category><![CDATA[ravendb]]></category>

		<guid isPermaLink="false">http://barry-king.com/?p=182</guid>
		<description><![CDATA[I highly recommend this video about how the Guardian has implemented MongoDB; it talks about the history of their site&#8217;s development and the common evolution most large scale sites go through. http://www.infoq.com/presentations/Why-I-Chose-MongoDB-for-Guardian They compare concepts between traditional relational databases and MongoDB. Although specific to MongoDB, most NOSQL solutions have equivalent concepts, including RavenDb. Have you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=182&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I highly recommend this video about how the Guardian has implemented MongoDB; it talks about the history of their site&#8217;s development and the common evolution most large scale sites go through.</p>
<p><a href="http://www.infoq.com/presentations/Why-I-Chose-MongoDB-for-Guardian">http://www.infoq.com/presentations/Why-I-Chose-MongoDB-for-Guardian</a></p>
<p>They compare concepts between traditional relational databases and MongoDB.</p>
<p><a href="http://sqldev.files.wordpress.com/2011/08/mongo.png"><img class="alignnone size-medium wp-image-183" title="mongodb concepts" src="http://sqldev.files.wordpress.com/2011/08/mongo.png?w=300&#038;h=186" alt="mongodb concepts" width="300" height="186" /></a></p>
<p>Although specific to MongoDB, most NOSQL solutions have equivalent concepts, including RavenDb.</p>
<p>Have you switched all or part of your data needs to a NOSQL solution? I&#8217;d like to hear how those implementations went, what issues you faced and how much of your data is still relational and why?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqldev.wordpress.com/182/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqldev.wordpress.com/182/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=182&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://barry-king.com/2011/08/12/rdbms-vs-mongodb-concepts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>51.517408 -0.120299</georss:point>
		<geo:lat>51.517408</geo:lat>
		<geo:long>-0.120299</geo:long>
		<media:content url="http://0.gravatar.com/avatar/634cab94086b59e6231a6a8e6e302238?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Coolcoder</media:title>
		</media:content>

		<media:content url="http://sqldev.files.wordpress.com/2011/08/mongo.png?w=300" medium="image">
			<media:title type="html">mongodb concepts</media:title>
		</media:content>
	</item>
		<item>
		<title>ASP.Net Page life cycle or Silver to his friends</title>
		<link>http://barry-king.com/2011/05/25/asp-net-page-life-cycle-or-silver-to-his-friends/</link>
		<comments>http://barry-king.com/2011/05/25/asp-net-page-life-cycle-or-silver-to-his-friends/#comments</comments>
		<pubDate>Wed, 25 May 2011 18:06:02 +0000</pubDate>
		<dc:creator>Barry</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.net page life cycle]]></category>
		<category><![CDATA[oninit]]></category>
		<category><![CDATA[onload]]></category>
		<category><![CDATA[onpreinit]]></category>
		<category><![CDATA[page life cycle]]></category>
		<category><![CDATA[render]]></category>
		<category><![CDATA[silver]]></category>

		<guid isPermaLink="false">http://sqldev.wordpress.com/?p=172</guid>
		<description><![CDATA[A colleague mentioned this the other day and I noticed it wasn&#8217;t advertised much on the web so here it is, an easy way to remember the ASP.Net page life cycle. S &#8211; Start. (OnPreInit) I &#8211; Initialize. (OnInit, InitComplete, OnPreLoad) L &#8211; Load. (OnLoad) V &#8211; Validation. (Validate) E &#8211; Event Handling. (OnLoadComplete, OnPreRender, OnSaveStateComplete) R &#8211; Render. (Render) Oh [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=172&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>A colleague mentioned this the other day and I noticed it wasn&#8217;t advertised much on the web so here it is, an easy way to remember the ASP.Net page life cycle.</p>
<ul>
<li><strong>S &#8211; Start.</strong> (<tt>OnPreInit</tt>)</li>
<li><strong>I &#8211; Initialize.</strong> (<tt>OnInit, InitComplete, OnPreLoad</tt>)</li>
<li><strong>L &#8211; Load.</strong> (<tt>OnLoad</tt>)</li>
<li><strong>V &#8211; Validation.</strong> (<tt>Validate</tt>)</li>
<li><strong>E &#8211; Event Handling.</strong> (<tt>OnLoadComplete, OnPreRender, OnSaveStateComplete</tt>)</li>
<li><strong>R &#8211; Render.</strong> (<tt>Render</tt>)</li>
</ul>
<p>Oh and &#8230;<strong> U &#8211; Unload.</strong> (<tt>OnUnload</tt>) but of course SILVER sounds nicer.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqldev.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqldev.wordpress.com/172/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=172&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://barry-king.com/2011/05/25/asp-net-page-life-cycle-or-silver-to-his-friends/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>51.517408 -0.120299</georss:point>
		<geo:lat>51.517408</geo:lat>
		<geo:long>-0.120299</geo:long>
		<media:content url="http://0.gravatar.com/avatar/634cab94086b59e6231a6a8e6e302238?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Coolcoder</media:title>
		</media:content>
	</item>
		<item>
		<title>Re-sequencing a sequence</title>
		<link>http://barry-king.com/2011/04/20/resequencing-a-sequence/</link>
		<comments>http://barry-king.com/2011/04/20/resequencing-a-sequence/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 07:29:27 +0000</pubDate>
		<dc:creator>Barry</dc:creator>
				<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[sql 2008]]></category>
		<category><![CDATA[cte]]></category>
		<category><![CDATA[ordered items]]></category>
		<category><![CDATA[resequence]]></category>
		<category><![CDATA[sequence]]></category>
		<category><![CDATA[sequences]]></category>

		<guid isPermaLink="false">http://sqldev.wordpress.com/?p=86</guid>
		<description><![CDATA[Let&#8217;s say you have a table that is ordered by a column but also contains a sequence number used for overiding the order sequence. This table may look something like this Now, this initially is populated in sequence like so: You can see I deliberately left a gap for item &#8216;D&#8217;. If I was to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=86&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Let&#8217;s say you have a table that is ordered by a column but also contains a sequence number used for overiding the order sequence.</p>
<p>This table may look something like this</p>
<p><pre class="brush: sql;">

CREATE TABLE OrderedItems

(

Item VARCHAR (50),
Sequence INT

)
</pre></p>
<p>Now, this initially is populated in sequence like so:</p>
<p><pre class="brush: sql;">
INSERT  INTO OrderedItems
        ( Item, Sequence )
VALUES  ( 'A', '1' ),
        ( 'B', '2' ),
        ( 'C', '3' ),
        ( 'E', '4' ),
        ( 'F', '5' )
</pre></p>
<p>You can see I deliberately left a gap for item &#8216;D&#8217;.<br />
If I was to insert &#8216;D&#8217; as a new item and made the sequence 6, what I really want now is to re-sequence the table, ordering by the Item.  If the column used for holding the sequence is part of a constraint or key of the table this isn&#8217;t easy. So how can we achieve this?<br />
Well, as it turns out, we can use common table expressions to very easily do this.</p>
<p><pre class="brush: sql;">

WITH LFUpdate AS (
select *, ROW_NUMBER() OVER (ORDER BY Item) AS NewSeq
from OrderedItems

)
UPDATE LFUpdate SET Sequence = NewSeq ;

</pre></p>
<p>What do you think, useful?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqldev.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqldev.wordpress.com/86/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=barry-king.com&#038;blog=2565351&#038;post=86&#038;subd=sqldev&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://barry-king.com/2011/04/20/resequencing-a-sequence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>51.517408 -0.120299</georss:point>
		<geo:lat>51.517408</geo:lat>
		<geo:long>-0.120299</geo:long>
		<media:content url="http://0.gravatar.com/avatar/634cab94086b59e6231a6a8e6e302238?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Coolcoder</media:title>
		</media:content>
	</item>
	</channel>
</rss>
