<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 't-sql' and 'productivity'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=t-sql,productivity&amp;orTags=0</link><description>Search results matching tags 't-sql' and 'productivity'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>T-SQL Tuesday 24: Ode to Composable Code</title><link>http://sqlblog.com/blogs/merrill_aldrich/archive/2011/11/08/t-sql-tuesday-24-ode-to-composable-code.aspx</link><pubDate>Tue, 08 Nov 2011 06:54:05 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:39690</guid><dc:creator>merrillaldrich</dc:creator><description>&lt;p&gt;&lt;a href="http://bradsruminations.blogspot.com/2011/10/invitation-for-t-sql-tuesday-024-prox-n.html"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;float:right;border-top:0px;border-right:0px;padding-top:0px;" title="TSQL2sDay150x150" border="0" alt="TSQL2sDay150x150" align="right" src="http://sqlblog.com/blogs/merrill_aldrich/TSQL2sDay150x150_626C4176.jpg" width="154" height="154" /&gt;&lt;/a&gt;I love the T-SQL Tuesday tradition, &lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2009/11/30/invitation-to-participate-in-t-sql-tuesday-001-date-time-tricks.aspx"&gt;started by Adam Machanic&lt;/a&gt; and hosted this month by &lt;a href="http://bradsruminations.blogspot.com/2011/10/invitation-for-t-sql-tuesday-024-prox-n.html"&gt;Brad Shulz&lt;/a&gt;. I am a little pressed for time this month, so today’s post is a short ode to how I love &lt;em&gt;saving&lt;/em&gt; time with Composable Code in SQL. Composability is one of the very best features of SQL, but sometimes gets picked on due to both real and imaginary performance worries. I like to pick composable solutions when I can, while keeping the perf issues in mind, because they are just so handy and eliminate so much hassle and complexity.&lt;/p&gt;  &lt;p&gt;What is composability? Here’s a quick example. Stored procedure calls do not compose. That fact creates pretty ugly workarounds like creating temp tables, guessing at their schema requirements, and using insert … exec to get back results from a stored proc -- just to work with their results in any meaningful way:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;col1&lt;span style="color:gray;"&gt;, &lt;/span&gt;col2&lt;span style="color:gray;"&gt;, &lt;/span&gt;col2
&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;mytable t
&lt;span style="color:gray;"&gt;INNER JOIN &lt;/span&gt;dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;myview v &lt;span style="color:blue;"&gt;ON &lt;/span&gt;t&lt;span style="color:gray;"&gt;.&lt;/span&gt;keycol &lt;span style="color:gray;"&gt;= &lt;/span&gt;v&lt;span style="color:gray;"&gt;.&lt;/span&gt;fkeycol
&lt;span style="color:gray;"&gt;INNER JOIN &lt;/span&gt;&lt;span style="color:blue;"&gt;EXEC &lt;/span&gt;dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;storedProcThatJustReturnsRows p 
    &lt;span style="color:blue;"&gt;ON &lt;/span&gt;oops&lt;span style="color:gray;"&gt;.&lt;/span&gt;thatWill &lt;span style="color:gray;"&gt;= &lt;/span&gt;&lt;span style="color:blue;"&gt;NEVER&lt;/span&gt;&lt;span style="color:gray;"&gt;.&lt;/span&gt;work –- #FAIL&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is because the results returned from a stored procedure are not really defined in terms of shape: the server cannot tell what to expect pack from a stored procedure, if anything, and therefore cannot allow that result to be composed in the context of an enclosing SQL Statement. That limitation is why I listed insert-exec among my &lt;a href="http://sqlblog.com/blogs/merrill_aldrich/archive/2009/08/18/top-10-t-sql-code-smells.aspx"&gt;Top 10 T-SQL Code Smells&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;On the other hand, structures that do have a well-defined “result shape,” including views and table-valued functions, are composable:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;col1&lt;span style="color:gray;"&gt;, &lt;/span&gt;col2&lt;span style="color:gray;"&gt;, &lt;/span&gt;col2
&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;mytable t
&lt;span style="color:gray;"&gt;INNER JOIN &lt;/span&gt;dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;myview v &lt;span style="color:blue;"&gt;ON &lt;/span&gt;t&lt;span style="color:gray;"&gt;.&lt;/span&gt;keycol &lt;span style="color:gray;"&gt;= &lt;/span&gt;v&lt;span style="color:gray;"&gt;.&lt;/span&gt;fkeycol
&lt;span style="color:gray;"&gt;INNER JOIN &lt;/span&gt;dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;myTVF&lt;span style="color:gray;"&gt;( &lt;/span&gt;@someParameter &lt;span style="color:gray;"&gt;) &lt;/span&gt;&lt;span style="color:blue;"&gt;ON &lt;/span&gt;&lt;span style="color:gray;"&gt;...&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;


&lt;p&gt;This provides a huge amount of flexibility when writing code.&lt;/p&gt;

&lt;p&gt;The very best composable behavior is when the code also allows the SQL Server query optimizer to unpack and optimize the content of the composed structures in the larger context of your query, which is true in most cases for views and inline table-valued functions. There are some specific limitations that can prevent this type of expansion/optimization, and, of course, it’s important to be aware of those issues from a performance point of view. But in general, if you can allow the optimizer greater flexibility in choosing a query plan, it at least has a chance at improving performance.&lt;/p&gt;

&lt;p&gt;Example: suppose I have a stored procedure that returns 10 columns and 100 rows, and I only really need the values from 2 columns in 25 rows. If I have to execute the proc, output all the results to a temp table, then work on them, there’s a decent chance the optimizer could factor out quite a lot of the work involved if it were given more options. Encapsulating the procedure code so fully as to force the optimizer into a specific execution sequence can actually limit the ways the query could be executed, and thereby “defeat” one of the server’s primary and most valuable features.&lt;/p&gt;

&lt;p&gt;I don’t mean to argue that stored procs don’t have a place, because they do. I also would not argue that this type of inline optimization always works, because it doesn’t. But in the main I prefer to take advantage of composability when I can, only optimize back from that where there’s a legitimate reason to do so, and try in those cases to understand why optimization isn’t working.&lt;/p&gt;

&lt;p&gt;(And three cheers to the SQL Server team for following this philosophy in moving to dynamic management &lt;em&gt;views and functions&lt;/em&gt; and away from diagnostic stored procs!)&lt;/p&gt;</description></item><item><title>How to Run a Series of T-SQL Scripts in a Specific Order</title><link>http://sqlblog.com/blogs/merrill_aldrich/archive/2011/09/12/how-to-run-a-series-of-t-sql-scripts-in-a-specific-order.aspx</link><pubDate>Mon, 12 Sep 2011 16:36:37 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:38409</guid><dc:creator>merrillaldrich</dc:creator><description>&lt;p&gt;Another post in the handy-but-not-bleeding-edge category.&lt;/p&gt;  &lt;p&gt;In the past few months I’ve seen a number of folks struggle with how to reliably/repeatedly execute a heap of T-SQL Script files, in order. One could certainly argue about why there’s the &lt;em&gt;need&lt;/em&gt; for piles of scripts in text files, but that’s outside the scope of this post – today I want to focus on how to “get 'er done,” and save that philosophical discussion for another time.&lt;/p&gt;  &lt;h2&gt;Problem: Heap o’ Script Files&lt;/h2&gt;  &lt;p&gt;You receive a folder full of scripts from (who else) your ISV, together with a list of the files in the order they should be executed. This is obviously fraught with opportunities for error if one were to manually execute them each, one at a time.&lt;/p&gt;  &lt;h2&gt;Workaround: SQLCMD and :r &lt;/h2&gt;  &lt;p&gt;Thankfully, there’s a very easy way to encapsulate this with a SQLCMD “master” script to call all the individual files, predictably, in the correct order. The &lt;strong&gt;:r&lt;/strong&gt; construct in SQLCMD is a file include, which means you can easily make a script that calls another text file, or many other text files, from disk.&lt;/p&gt;  &lt;p&gt;First, copy your ordered file list and paste it into a query window. Be careful to avoid whitespace before and after each line of text:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;somefile&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:blue;"&gt;sql
&lt;/span&gt;another file&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:blue;"&gt;sql
&lt;/span&gt;thirdfile&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:blue;"&gt;sql&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Set the query to SQLCMD mode.&lt;/p&gt;

&lt;p&gt;Next, use a regex find/replace to &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Prepend each line with the text &lt;strong&gt;:r &amp;quot;yourFilePath\&lt;/strong&gt; &lt;/li&gt;

  &lt;li&gt;End each line with a double-quotation mark, &lt;strong&gt;&amp;quot;&lt;/strong&gt; &lt;/li&gt;

  &lt;li&gt;Add a &lt;strong&gt;GO&lt;/strong&gt; batch terminator between each line &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is, find any line containing some text, using this regex:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;^{.+}&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then &lt;em&gt;Replace All&lt;/em&gt; using something like:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;:r &amp;quot;c:\\some Folder\\\1&amp;quot;\nGO&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;em&gt;\1&lt;/em&gt; within the replace expression will be populated with the original text from each line, which will, in effect, surround the original text with the quotation marks, file path, and GO. The extra backslashes are required to escape special characters including newline and backslash itself.&lt;/p&gt;

&lt;p&gt;The resulting script should look like this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="background:silver;"&gt;:r &amp;quot;c:\some Folder\somefile.sql&amp;quot;
&lt;/span&gt;&lt;span style="color:blue;"&gt;GO
&lt;/span&gt;&lt;span style="background:silver;"&gt;:r &amp;quot;c:\some Folder\another file.sql&amp;quot;
&lt;/span&gt;&lt;span style="color:blue;"&gt;GO
&lt;/span&gt;&lt;span style="background:silver;"&gt;:r &amp;quot;c:\some Folder\thirdfile.sql&amp;quot;
&lt;/span&gt;&lt;span style="color:blue;"&gt;GO&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Be careful with subtleties like whitespace around the file names – regex is tough to read and get correct, so it’s easy to make an error. It might take a couple of tries.&lt;/p&gt;

&lt;p&gt;At the top of the script, again, you may also want this line:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="background:silver;"&gt;:ON Error EXIT
&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;This will cause the client to abort on any error from the scripts, instead of continuing.&lt;/p&gt;

&lt;p&gt;Check, triple check, quadruple check your work, and you should have a meta-script that will call all the SQL files in order.&lt;/p&gt;

&lt;p&gt;Cheers, and happy scripting!&lt;/p&gt;</description></item></channel></rss>