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

<channel>
	<title>Rob Boek &#187; T-SQL</title>
	<atom:link href="http://robboek.com/tag/t-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://robboek.com</link>
	<description></description>
	<lastBuildDate>Sat, 15 May 2010 19:16:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Who Should Write Database Code?</title>
		<link>http://robboek.com/2009/03/20/who-should-write-database-code/</link>
		<comments>http://robboek.com/2009/03/20/who-should-write-database-code/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 07:14:38 +0000</pubDate>
		<dc:creator>Rob Boek</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://robboek.com/2009/03/20/who-should-write-database-code/</guid>
		<description><![CDATA[This week, I ran into a query that did 770 GB worth of logical reads…on a 3 GB database!!!
Kimberly Tripp asked “Who’s job is it anyway?” Should database design and database coding be done by the Application Developer, the DBA, or should there be a separate “Database Developer” role? There are many variables that make [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>This week, I ran into a query that did 770 GB worth of logical reads…<strong>on a 3 GB database!!!</strong></p>
<p>Kimberly Tripp asked “<a href="http://www.sqlskills.com/BLOGS/KIMBERLY/post/Whose-job-is-it-anyway.aspx">Who’s job is it anyway?</a>” Should database design and database coding be done by the Application Developer, the DBA, or should there be a separate “Database Developer” role? There are many variables that make this a complicated question. Here are a few of my thoughts: </p>
<h3>The Application Developer</h3>
<p><strong>PRO:</strong> Database development is part of the overall application development process. Basic T-SQL syntax is pretty easy for application developers to pick up, and most should already know how to write select statements. Also, application developers already have to be familiar with the requirements of the application.</p>
<p><strong>CON:</strong> The best practices for good application code don’t translate well into good database code. Application developers are trained to break everything up into small manageable steps. When they need to do something in the database, the natural tendency is to try to use the tools that work well on the application side. This often results in things like cursors, while loops, temp tables, UDFs, and lots of small steps. Database development requires a very different mind set. Also, SQL Server is a huge product. It’s hard enough for someone dedicated to SQL Server to keep up with and master it, let alone someone who is trying to do application development as well.</p>
<h3>The DBA</h3>
<p><strong>PRO:</strong> DBAs work with databases. They understand indexes and are familiar with database tools like SSMS, SSIS, and Profiler. They know that cursors are not the answer to everything.</p>
<p><strong>CON:</strong> Most (not all) DBAs are, as the name implies, focused on administration and don’t write code. DBAs are responsible for database uptime and stability which may get in the way of meeting development deliverables.</p>
<h3>The Database Developer</h3>
<p><strong>PRO:</strong> Dedicated database developers are focused on writing good database code. They should be good at thinking in terms of set-based logic, and be familiar with the various tools that SQL Server provides. In theory, this should result in higher quality database code in less time.</p>
<p><strong>CON:</strong> Many companies don’t have enough work to justify a full-time person dedicated to database development. Even if they do, it is likely spread across multiple projects which requires the database developer to keep track of multiple sets of requirements and deal with competing priorities.</p>
<h3>Other Thoughts</h3>
<p>While dedicated database developers should on average write better code, it doesn’t always matter if a query runs in 1-2 seconds or if it runs in 100-200 ms. The less efficient code may be “good enough.”</p>
<p>On the other hand, most development is done on a very small subset of data. A good database developer will know how the query will react as the amount of data increases. Will it perform the same, get linearly worse, or will performance decrease exponentially as rows are added.</p>
<p>If you plan on scaling your application, it’s worth it to make sure your database development is done right. To me, it doesn’t matter if the work is done by the application developer, the DBA, or a dedicated database developer. Just use the person who does the best job.</p>
<p>I’d love to hear your thoughts.</p>
]]></content:encoded>
			<wfw:commentRss>http://robboek.com/2009/03/20/who-should-write-database-code/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Adam Machanic’s T-SQL Challenge</title>
		<link>http://robboek.com/2009/02/27/adam-machanics-t-sql-challenge/</link>
		<comments>http://robboek.com/2009/02/27/adam-machanics-t-sql-challenge/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 19:46:54 +0000</pubDate>
		<dc:creator>Rob Boek</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[contest]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://robboek.com/2009/02/27/adam-machanics-t-sql-challenge/</guid>
		<description><![CDATA[For you T-SQL experts out there, Adam Machanic just posted a challenge with a great prize. The best submission wins a full MSDN subscription valued at around $10,000.
Check it out!
]]></description>
			<content:encoded><![CDATA[<p></p><p>For you T-SQL experts out there, <a href="http://sqlblog.com/blogs/adam_machanic/">Adam Machanic</a> just <a href="http://sqlblog.com/blogs/adam_machanic/archive/2009/02/27/t-sql-challenge-grouped-string-concatenation.aspx">posted a challenge</a> with a great prize. The best submission wins a full MSDN subscription valued at around $10,000.</p>
<p><a href="http://sqlblog.com/blogs/adam_machanic/archive/2009/02/27/t-sql-challenge-grouped-string-concatenation.aspx">Check it out!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://robboek.com/2009/02/27/adam-machanics-t-sql-challenge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FizzBuzz in T-SQL</title>
		<link>http://robboek.com/2009/02/03/fizzbuzz-in-t-sql/</link>
		<comments>http://robboek.com/2009/02/03/fizzbuzz-in-t-sql/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 19:16:04 +0000</pubDate>
		<dc:creator>Rob Boek</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://robboek.com/?p=95</guid>
		<description><![CDATA[Pinal Dave posted yesterday about how to solve the Fizz Buzz problem using T-SQL.
Definition of FizzBuzz Puzzle : Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://blog.sqlauthority.com/" rel="nofollow">Pinal Dave</a> <a href="http://blog.sqlauthority.com/2009/02/02/sql-server-t-sql-script-for-fizzbuzz-logic/" rel="nofollow">posted yesterday</a> about how to solve the <a href="http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/">Fizz Buzz problem</a> using T-SQL.</p>
<blockquote><p><strong>Definition of FizzBuzz Puzzle</strong> : Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.</p>
</blockquote>
<p>His solution works, but he is using procedural logic. Some of the biggest causes of performance problems in SQL Server are caused by application developers who try to use procedural logic instead of using the set-based logic that databases are meant for.</p>
<p>Here is how to solve the FizzBuzz problem using set-based logic in T-SQL:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">WITH</span> Numbers<span style="color: #808080;">&#40;</span>Number<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span>
  <span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>
  <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
  <span style="color: #0000FF;">SELECT</span> Number <span style="color: #808080;">+</span> <span style="color: #000;">1</span>
  <span style="color: #0000FF;">FROM</span> Numbers
  <span style="color: #0000FF;">WHERE</span> Number <span style="color: #808080;">&lt;</span> <span style="color: #000;">100</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span>
  <span style="color: #0000FF;">CASE</span> 
    <span style="color: #0000FF;">WHEN</span> Number <span style="color: #808080;">%</span> <span style="color: #000;">3</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #808080;">AND</span> Number <span style="color: #808080;">%</span> <span style="color: #000;">5</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'FizzBuzz'</span>
    <span style="color: #0000FF;">WHEN</span> Number <span style="color: #808080;">%</span> <span style="color: #000;">3</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Fizz'</span>
    <span style="color: #0000FF;">WHEN</span> Number <span style="color: #808080;">%</span> <span style="color: #000;">5</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Buzz'</span>
    <span style="color: #0000FF;">ELSE</span> <span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span>, Number<span style="color: #808080;">&#41;</span>
  <span style="color: #0000FF;">END</span>
<span style="color: #0000FF;">FROM</span> Numbers
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Number</pre></div></div>

<p>Update 3/15/2010:<br /> Here is another, much faster solution, taken from ideas I found <a href="http://ask.sqlservercentral.com/questions/4241/whats-the-best-way-to-solve-the-fizzbuzz-question/">here</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @num <span style="color: #0000FF;">INT</span> <span style="color: #808080;">=</span> <span style="color: #000;">1000000</span>
&nbsp;
<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">STATISTICS</span> <span style="color: #0000FF;">TIME</span> <span style="color: #0000FF;">ON</span>;
<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">STATISTICS</span> IO <span style="color: #0000FF;">ON</span>;
&nbsp;
;WITH
  L0   <span style="color: #0000FF;">AS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">AS</span> c <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span> <span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,
  L1   <span style="color: #0000FF;">AS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">AS</span> c <span style="color: #0000FF;">FROM</span> L0 <span style="color: #0000FF;">AS</span> A, L0 <span style="color: #0000FF;">AS</span> B<span style="color: #808080;">&#41;</span>,
  L2   <span style="color: #0000FF;">AS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">AS</span> c <span style="color: #0000FF;">FROM</span> L1 <span style="color: #0000FF;">AS</span> A, L1 <span style="color: #0000FF;">AS</span> B<span style="color: #808080;">&#41;</span>,
  L3   <span style="color: #0000FF;">AS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">AS</span> c <span style="color: #0000FF;">FROM</span> L2 <span style="color: #0000FF;">AS</span> A, L2 <span style="color: #0000FF;">AS</span> B<span style="color: #808080;">&#41;</span>,
  L4   <span style="color: #0000FF;">AS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">AS</span> c <span style="color: #0000FF;">FROM</span> L3 <span style="color: #0000FF;">AS</span> A, L3 <span style="color: #0000FF;">AS</span> B<span style="color: #808080;">&#41;</span>,
  L5   <span style="color: #0000FF;">AS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">AS</span> c <span style="color: #0000FF;">FROM</span> L4 <span style="color: #0000FF;">AS</span> A, L4 <span style="color: #0000FF;">AS</span> B<span style="color: #808080;">&#41;</span>,
  Numbers <span style="color: #0000FF;">AS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> ROW_NUMBER<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">NULL</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> Number <span style="color: #0000FF;">FROM</span> L5<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #808080;">&#40;</span>@num<span style="color: #808080;">&#41;</span>
  Number,
  <span style="color: #0000FF;">CASE</span> 
    <span style="color: #0000FF;">WHEN</span> Number <span style="color: #808080;">%</span> <span style="color: #000;">15</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'FizzBuzz'</span>
    <span style="color: #0000FF;">WHEN</span> Number <span style="color: #808080;">%</span> <span style="color: #000;">3</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Fizz'</span>
    <span style="color: #0000FF;">WHEN</span> Number <span style="color: #808080;">%</span> <span style="color: #000;">5</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Buzz'</span>
    <span style="color: #0000FF;">ELSE</span> <span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span><span style="color: #808080;">&#41;</span>, Number<span style="color: #808080;">&#41;</span>
  <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> FizzBuzz
<span style="color: #0000FF;">INTO</span> #FizzBuzz
<span style="color: #0000FF;">FROM</span> Numbers
&nbsp;
<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">STATISTICS</span> <span style="color: #0000FF;">TIME</span> <span style="color: #0000FF;">OFF</span>;
<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">STATISTICS</span> IO <span style="color: #0000FF;">OFF</span>;
&nbsp;
<span style="color: #008080;">--SELECT FizzBuzz FROM #FizzBuzz ORDER BY Number</span>
&nbsp;
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #FizzBuzz</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://robboek.com/2009/02/03/fizzbuzz-in-t-sql/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
