<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>SQL Fool - SQL Fool</title>
	
	<link>http://sqlfool.com</link>
	<description>Self-Professed SQL Scripting Junkie!</description>
	<lastBuildDate>Wed, 15 May 2013 20:22:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/SqlFool" /><feedburner:info uri="sqlfool" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>SqlDev PowerShell 101: Getting Started with PowerShell</title>
		<link>http://feedproxy.google.com/~r/SqlFool/~3/thR08ZnSQ7w/</link>
		<comments>http://sqlfool.com/2013/05/sqldev-powershell-101/#comments</comments>
		<pubDate>Mon, 13 May 2013 15:56:27 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[SqlDev PowerShell Series]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1902</guid>
		<description><![CDATA[<p>This is the first in a new series focused on PowerShell for SQL developers. That means using PowerShell to create and/or modify table schema, stored procedures, and indexes. In other words, automating code development and database tuning processes. This is mostly what I&#8217;ve been using PowerShell for so far, and I have to say, I am LOVING it! It&#8217;s allowing me to save *days* of coding and validating. Disclaimer: I am NOT a PowerShell expert, just a newly converted enthusiast who wants to save you from the headaches I myself have suffered while trying to learn PoSh. So I&#8217;ll mostly focus on the &#8220;how to&#8217;s&#8221; instead of the &#8220;why&#8217;s&#8221; in this series. I&#8217;m going to start at the very beginning and assume you have never opened PowerShell before in your life. Basically, I&#8217;m going to assume you&#8217;re where I was just a few short months ago. So let us start by finding the application. If you&#8217;re using Windows 7 or later, the good news is that PowerShell comes pre-installed on the OS. We just need to configure it to allow the execution of scripts. Click on the Start icon &#8211;> &#8220;Search programs and files&#8221; &#8211;> type &#8220;PowerShell.&#8221; The first time … <a href="http://sqlfool.com/2013/05/sqldev-powershell-101/"> Continue reading <span class="meta-nav">&#8594; </span></a></p><p>The post <a href="http://sqlfool.com/2013/05/sqldev-powershell-101/">SqlDev PowerShell 101: Getting Started with PowerShell</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>This is the first in a new series focused on PowerShell for SQL developers. That means using PowerShell to create and/or modify table schema, stored procedures, and indexes. In other words, automating code development and database tuning processes. This is mostly what I&#8217;ve been using PowerShell for so far, and I have to say, I am LOVING it! It&#8217;s allowing me to save *days* of coding and validating. </p>
<p><strong>Disclaimer:</strong> I am NOT a PowerShell expert, just a newly converted enthusiast who wants to save you from the headaches I myself have suffered while trying to learn PoSh. So I&#8217;ll mostly focus on the &#8220;how to&#8217;s&#8221; instead of the &#8220;why&#8217;s&#8221; in this series. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I&#8217;m going to start at the very beginning and assume you have never opened PowerShell before in your life. Basically, I&#8217;m going to assume you&#8217;re where I was just a few short months ago. So let us start by finding the application. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>If you&#8217;re using Windows 7 or later, the good news is that PowerShell comes pre-installed on the OS. We just need to configure it to allow the execution of scripts. Click on the Start icon &#8211;> &#8220;Search programs and files&#8221; &#8211;> type &#8220;PowerShell.&#8221; </p>
<div id="attachment_1905" class="wp-caption left" style="width: 233px"><a href="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_1.jpg"><img src="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_1-223x300.jpg" alt="Searching for PowerShell" width="223" height="300" class="size-medium wp-image-1905" /></a><p class="wp-caption-text">Searching for PowerShell</p></div>
<p>The first time we open PowerShell, we will need to run it as Administrator for elevated permissions. To do this, right-click on the Windows PowerShell icon that should now be on your Taskbar, then click on &#8220;Run as Administrator.&#8221; While you&#8217;re there, also add it to your Taskbar and/or Start Menu for easy retrieval in the future. </p>
<div id="attachment_1906" class="wp-caption left" style="width: 310px"><a href="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_2.jpg"><img src="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_2-300x209.jpg" alt="Running PowerShell as Administrator" width="300" height="209" class="size-medium wp-image-1906" /></a><p class="wp-caption-text">Running PowerShell as Administrator</p></div>
<p>Now we need to change the Execution Policy from &#8220;Restricted&#8221; &#8212; which prevents the execution of PowerShell scripts &#8212; to something a little more accessible. For the purposes of this post, we&#8217;re going to set it to &#8220;RemoteSigned,&#8221; with the understanding that this may pose some security risks. You can read more about Execution Policy settings here: <a href="http://technet.microsoft.com/en-us/library/ee176847.aspx" title="Using the Get-ExecutionPolicy Cmdlet" target="_blank">Using the Get-ExecutionPolicy Cmdlet</a>.</p>
<p>Similarly, you can run this command inside of PowerShell to get more details:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Help</span> About_Signing</pre></td></tr></table></div>

<p>Now that we have PowerShell open in Administrator mode, execute this command:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Set-ExecutionPolicy</span> RemoteSigned</pre></td></tr></table></div>

<p>You will be prompted to confirm the change. Type &#8220;Y&#8221;. You can then confirm that the change has taken place by typing:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-ExecutionPolicy</span></pre></td></tr></table></div>

<div id="attachment_1907" class="wp-caption left" style="width: 310px"><a href="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_3.jpg"><img src="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_3-300x119.jpg" alt="Changing Execution Policy" width="300" height="119" class="size-medium wp-image-1907" /></a><p class="wp-caption-text">Changing Execution Policy</p></div>
<p>Alrighty, now we&#8217;re ready to write some PowerShell! </p>
<p>Go ahead and close your PowerShell window, then re-open as you normally would (not in Administrator mode; we don&#8217;t need elevated permissions any longer).</p>
<p>Let&#8217;s start with some simple commands. For this post, I have scripted all objects from AdventureWorks to a new directory called C:\MyCode\. We can take a look at the files in the directory using a cmdshell command that&#8217;s probably very familiar: Dir. Go ahead and run that command now.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Dir</span></pre></td></tr></table></div>

<div id="attachment_1908" class="wp-caption left" style="width: 310px"><a href="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_4.jpg"><img src="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_4-300x100.jpg" alt="Running Dir in PowerShell" width="300" height="100" class="size-medium wp-image-1908" /></a><p class="wp-caption-text">Running Dir in PowerShell</p></div>
<p>So does that mean PowerShell supports all the old cmdshell commands that we know and love (to hate)? Well, yes and no. The PowerShell devs, forward-thinking folks that they are, knew that having support for cmdshell syntax would probably ease the transition for many people, so they created aliases. You can see this by running the Get-Alias cmdlet:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Alias</span> <span style="color: #008080; font-weight: bold;">Dir</span></pre></td></tr></table></div>

<div id="attachment_1921" class="wp-caption left" style="width: 310px"><a href="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_5.jpg"><img src="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_5-300x43.jpg" alt="Get-Alias Dir" width="300" height="43" class="size-medium wp-image-1921" /></a><p class="wp-caption-text">Get-Alias Dir</p></div>
<p>What we see is that, under the covers, Dir is nothing more than an alias for Get-ChildItem. So let&#8217;s switch to using Get-ChildItem from here on out. </p>
<p>One thing that took me a while to discover, but which is very helpful, is the auto-complete functionality inherent in PowerShell. To use this, just start typing a command, such as &#8220;Get-Ch&#8221;, and hit the Tab key. As you press Tab, PowerShell will iterate through the list of available cmdlets / switches that are available in the current context. Keep hitting Tab until you get to Get-ChildItem, then execute that command.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-ChildItem</span></pre></td></tr></table></div>

<div id="attachment_1922" class="wp-caption left" style="width: 310px"><a href="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_6.jpg"><img src="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_6-300x59.jpg" alt="Get-ChildItem" width="300" height="59" class="size-medium wp-image-1922" /></a><p class="wp-caption-text">Get-ChildItem</p></div>
<p>Hmm, those results look familiar. That&#8217;s a good sign. But what else can we do with Get-ChildItem? Let&#8217;s find out by using the Get-Help cmdlet:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Help</span> <span style="color: #008080; font-weight: bold;">Get-ChildItem</span></pre></td></tr></table></div>

<div id="attachment_1923" class="wp-caption left" style="width: 310px"><a href="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_7.jpg"><img src="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_7-300x125.jpg" alt="Get-Help Get-ChildItem" width="300" height="125" class="size-medium wp-image-1923" /></a><p class="wp-caption-text">Get-Help Get-ChildItem</p></div>
<p>Type &#8220;Get-ChildItem -&#8221; (make sure to include the hyphen, or you&#8217;ll be tabbing through way more than you&#8217;d like) and press Tab to iterate through the list of available switches. Stop when you get to the -Recurse switch. If you go too far, don&#8217;t worry&#8230; you can hit the Shift+Tab to go in reverse direction. This allows Get-ChildItem to execute against child folders too, so we&#8217;ll add that to our command.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Recurse</span></pre></td></tr></table></div>

<p>So far, so good. Now what else can we do? </p>
<p>At this point, we need to discuss piping. Piping is a way to stack multiple cmdlets together to produce a single, final, perfect result. It&#8217;s one of the features that adds the &#8220;power&#8221; in PowerShell. You can read more about piping here: <a href="http://technet.microsoft.com/en-us/library/ee176927.aspx" title="Piping and the Pipeline in Windows PowerShell" target="_blank">Piping and the Pipeline in Windows PowerShell</a>.</p>
<p>So let&#8217;s build our first pipe. We know how to return all the files in a directory, but how do we *search* the directory for files that contain a specific keyword? It turns out it&#8217;s actually not that hard&#8230; we just need to use the Select-String cmdlet. To do this, press the Up arrow on your keyboard to retrieve the previously executed command, then type a pipe (|) before adding the Select-String cmdlet:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Recurse</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select-String</span> <span style="color: #008080; font-style: italic;">-Pattern</span> <span style="color: #800000;">&quot;SalesOrderDetail&quot;</span></pre></td></tr></table></div>

<div id="attachment_1920" class="wp-caption left" style="width: 310px"><a href="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_8.jpg"><img src="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_8-300x67.jpg" alt="Search Files" width="300" height="67" class="size-medium wp-image-1920" /></a><p class="wp-caption-text">Search Files</p></div>
<p>So what&#8217;s this doing? Well, it&#8217;s iterating through each of the files listed in the directory and subdirectories, then calling the Select-String cmdlet to find files that contain the word &#8220;SalesOrderDetail.&#8221; It&#8217;s even showing us what line the pattern is found on. Pretty awesome, huh? But I think we can do better. </p>
<p>One of the best things about PowerShell is the sheer amount of documentation available. Case in point: TechNet has a <a href="http://technet.microsoft.com/en-us/magazine/ff714569.aspx" target="_blank">quick reference of PowerShell 2.0 cmdlets</a>. Looking at the list, some interesting cmdlets catch my eye, including Select-Object and Get-Unique. If we pipe these cmdlets to our existing command, we get something like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Recurse</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select-String</span> <span style="color: #008080; font-style: italic;">-Pattern</span> <span style="color: #800000;">&quot;SalesOrderDetail&quot;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Sort-Object</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select-Object</span> Path <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Get-Unique</span> <span style="color: #008080; font-style: italic;">-AsString</span></pre></td></tr></table></div>

<div id="attachment_1924" class="wp-caption left" style="width: 310px"><a href="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_9.jpg"><img src="http://sqlfool.com/wp-content/uploads/2013/05/PowerShell_101_9-300x55.jpg" alt="Final PowerShell Command" width="300" height="55" class="size-medium wp-image-1924" /></a><p class="wp-caption-text">Final PowerShell Command</p></div>
<p>This returns a very easy-to-consume list of files that contain the pattern &#8220;SalesOrderDetail.&#8221; Imagine using this to search through hundreds of files and thousands of lines of code to find dependencies. Powerful PowerShell indeed.</p>
<p>So that&#8217;s it for this first post and introduction to PowerShell. If you&#8217;re looking for more resources, here are some PowerShell-related sites I&#8217;ve found especially helpful:</p>
<ul>
<li><a href="http://technet.microsoft.com/en-us/magazine/ff714569.aspx" target="_blank">Overview of Cmdlets Available in Windows PowerShell</a></li>
<li><a href="http://technet.microsoft.com/en-us/scriptcenter/dd772285.aspx" target="_blank">A Task-Based Guide to Windows PowerShell Cmdlets</a></li>
<li><a href="http://blogs.technet.com/b/heyscriptingguy/" target="_blank">Hey, Scripting Guy! Blog</a></li>
<li><a href="http://sqlblog.com/blogs/allen_white/archive/tags/PowerShell/default.aspx" target="_blank">Allen White&#8217;s PowerShell posts</a></li>
<li><a href="http://www.kendalvandyke.com/search/label/PowerShell" target="_blank">Kendal Van Dyke&#8217;s PowerShell posts</a></li>
<li><a href="http://www.mssqltips.com/sqlserverauthor/18/edwin-sarmiento/" target="_blank">Edwin Sarmiento&#8217;s blog</a></li>
</ul>
<p>Happy Scripting! <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The post <a href="http://sqlfool.com/2013/05/sqldev-powershell-101/">SqlDev PowerShell 101: Getting Started with PowerShell</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p><img src="http://feeds.feedburner.com/~r/SqlFool/~4/thR08ZnSQ7w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2013/05/sqldev-powershell-101/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://sqlfool.com/2013/05/sqldev-powershell-101/</feedburner:origLink></item>
		<item>
		<title>Conversion Issues Upgrading to PowerShell 3.0</title>
		<link>http://feedproxy.google.com/~r/SqlFool/~3/Ch_xSDzrv0I/</link>
		<comments>http://sqlfool.com/2013/03/conversion-issues-upgrading-powershell-3-0/#comments</comments>
		<pubDate>Wed, 20 Mar 2013 17:05:48 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[Teradata]]></category>
		<category><![CDATA[PowerShell 3.0]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[xp_cmdshell]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1883</guid>
		<description><![CDATA[<p>This post probably has a small audience, but there were a handful of conversion issues I ran into when I upgraded to PowerShell 3.0. It was difficult finding posts related to the errors I encountered, so I&#8217;m posting my experiences in the hopes that it helps someone else down the road. The first issue I encountered was calling PowerShell from xp_cmdshell on a remote SQL Server. In PowerShell 2.0, I used this syntax: EXEC xp_cmdshell 'powershell.exe -Command &#34;get-executionpolicy&#34;' # PoSh 2.0 Results AllSigned &#160; # PoSh 3.0 Results 'powershell.exe' is not recognized as an internal or external command, operable program or batch file. To fix this issue, I had to fully qualify the PowerShell directory: EXEC xp_cmdshell 'c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command &#34;get-executionpolicy&#34;' # PoSh 3.0 Results AllSigned Another issue I ran into was calling the Teradata .NET Data Provider. In PowerShell 2.0, I was calling the Invoke-SqlStmt function I previously posted to connect from a Windows server to a remote Teradata server. PS X:\&#62; .\Invoke-SqlStmt.ps1 -C TDATA -S MyTDServerName -Q &#34;SELECT * FROM DBC.Tables;&#34; -Verbose Everything worked great. However, once I upgraded to PowerShell 3.0, I was getting this error: Exception calling &#34;GetFactory&#34; with &#34;1&#34; argument(s): &#34;Unable to find the requested .Net … <a href="http://sqlfool.com/2013/03/conversion-issues-upgrading-powershell-3-0/"> Continue reading <span class="meta-nav">&#8594; </span></a></p><p>The post <a href="http://sqlfool.com/2013/03/conversion-issues-upgrading-powershell-3-0/">Conversion Issues Upgrading to PowerShell 3.0</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>This post probably has a small audience, but there were a handful of conversion issues I ran into when I upgraded to PowerShell 3.0. It was difficult finding posts related to the errors I encountered, so I&#8217;m posting my experiences in the hopes that it helps someone else down the road. </p>
<p>The first issue I encountered was calling PowerShell from <a href="http://msdn.microsoft.com/en-us/library/ms175046.aspx" target="_blank">xp_cmdshell</a> on a remote SQL Server.</p>
<p>In PowerShell 2.0, I used this syntax:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> xp_cmdshell <span style="color: #FF0000;">'powershell.exe -Command &quot;get-executionpolicy&quot;'</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># PoSh 2.0 Results</span>
AllSigned
&nbsp;
<span style="color: #008000;"># PoSh 3.0 Results</span>
<span style="color: #800000;">'powershell.exe'</span> is not recognized as an internal or external command<span style="color: pink;">,</span>
operable program or batch file.</pre></td></tr></table></div>

<p>To fix this issue, I had to fully qualify the PowerShell directory:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> xp_cmdshell <span style="color: #FF0000;">'c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command &quot;get-executionpolicy&quot;'</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># PoSh 3.0 Results</span>
AllSigned</pre></td></tr></table></div>

<p>Another issue I ran into was calling the Teradata .NET Data Provider. In PowerShell 2.0, I was calling the <a href="http://sqlfool.com/2013/02/invoke-sqlstmt/" target="_blank">Invoke-SqlStmt function I previously posted</a> to connect from a Windows server to a remote Teradata server.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> X:\<span style="color: pink;">&gt;</span> .\Invoke<span style="color: pink;">-</span>SqlStmt.ps1 <span style="color: pink;">-</span>C TDATA <span style="color: pink;">-</span>S MyTDServerName <span style="color: pink;">-</span>Q <span style="color: #800000;">&quot;SELECT * FROM DBC.Tables;&quot;</span> <span style="color: #008080; font-style: italic;">-Verbose</span></pre></td></tr></table></div>

<p>Everything worked great. However, once I upgraded to PowerShell 3.0, I was getting this error:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">Exception calling &quot;GetFactory&quot; with &quot;1&quot; argument(s): &quot;Unable to find the requested .Net Framework Data Provider.  It may not be installed.&quot;
At line:1 char:1
+ $Factory = [System.Data.Common.DbProviderFactories]::GetFactory(&quot;Teradata.Client ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException</pre></td></tr></table></div>

<p>I couldn&#8217;t find anything that would explain this behavior, and I confirmed that everything still worked great when ran from PowerShell 2.0. Ultimately, I decided to install the Teradata .NET Data Provider locally, which solved the problem. I didn&#8217;t find anything that explained why I could previously use a remote adapter and now can only use a local adapter. I don&#8217;t really understand the problem, but I was able to solve it. </p>
<p>Here&#8217;s the syntax to install the <a href="http://downloads.teradata.com/download/connectivity/dot-net-data-provider" target="_blank">Teradata .NET Data Provider</a> remotely using xp_cmdshell.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> xp_cmdshell <span style="color: #FF0000;">'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command &quot;X:\drop\tdnetdp__windows_indep.13.11.00.01.exe /s /v&quot;/qn&quot;'</span></pre></td></tr></table></div>

<p>Note that this code is installing the 13.11 adapter, so you may need to change the filename if you&#8217;re installing a different version.</p>
<p>And to leave things on a positive note, one awesome improvement of PowerShell 3.0 over 2.0 is the redirection operators. I&#8217;m a big fan of logging and verbose messaging, but in 2.0, I had to put extra steps into the code to log these messages. In 3.0, I&#8217;ve been able to log my verbose output simply by appending &#8220;*> directory&#8221; to my commands, i.e.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> xp_cmdshell <span style="color: #FF0000;">'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command &quot;X:\MyAwesomeScript.ps1 -Verbose *&gt; X:\MyAwesomeLog.txt&quot;'</span></pre></td></tr></table></div>

<p>I think that is just so cool. You can read more about the different redirection operators by running this in PowerShell:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">get-help</span> redirect</pre></td></tr></table></div>

<p>Happy Scripting! <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The post <a href="http://sqlfool.com/2013/03/conversion-issues-upgrading-powershell-3-0/">Conversion Issues Upgrading to PowerShell 3.0</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p><img src="http://feeds.feedburner.com/~r/SqlFool/~4/Ch_xSDzrv0I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2013/03/conversion-issues-upgrading-powershell-3-0/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://sqlfool.com/2013/03/conversion-issues-upgrading-powershell-3-0/</feedburner:origLink></item>
		<item>
		<title>Invoke-SqlStmt – PowerShell for SQL Server &amp; Teradata Queries</title>
		<link>http://feedproxy.google.com/~r/SqlFool/~3/rnAEGSbUUic/</link>
		<comments>http://sqlfool.com/2013/02/invoke-sqlstmt/#comments</comments>
		<pubDate>Fri, 15 Feb 2013 15:59:59 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[Teradata]]></category>
		<category><![CDATA[PoSh]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1866</guid>
		<description><![CDATA[<p>I&#8217;ve been using PowerShell quite a bit the last few months, and I finally feel I&#8217;m to the point where I can start sharing some of what I&#8217;ve learned. I started a blog post that walks through using PowerShell for database development and automation, and I quickly realized that a lot of what I want to show is dependent upon the first function I wrote, Invoke-SqlStmt. And by &#8220;wrote,&#8221; I mean &#8220;blatantly borrowed from Chad Miller&#8217;s Invoke-SqlCmd2 script&#8221; . Thank you, Chad! The main difference is that my function supports connecting to either Teradata or SQL Server, depending on the parameters passed. One thing worth mentioning is that I spent a lot of time researching all of the different ways to connect to SQL Server. In the end, I went with ADO.NET, as a lot of what I will be doing is working with data sets (i.e. running a query against ServerA and writing the results to ServerB). Invoke-SqlStmt ################################################################################################## # # .NAME # Invoke-SqlStmt.ps1 # # .SYNOPSIS # Executes a SQL statement against either a Teradata or SQL Server source using ADO.NET # # .INPUT # C - Connection Type; Required; MSSQL or TDATA # S - Server Name; … <a href="http://sqlfool.com/2013/02/invoke-sqlstmt/"> Continue reading <span class="meta-nav">&#8594; </span></a></p><p>The post <a href="http://sqlfool.com/2013/02/invoke-sqlstmt/">Invoke-SqlStmt &#8211; PowerShell for SQL Server &#038; Teradata Queries</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been using PowerShell quite a bit the last few months, and I finally feel I&#8217;m to the point where I can start sharing some of what I&#8217;ve learned. </p>
<p>I started a blog post that walks through using PowerShell for database development and automation, and I quickly realized that a lot of what I want to show is dependent upon the first function I wrote, Invoke-SqlStmt. And by &#8220;wrote,&#8221; I mean &#8220;blatantly borrowed from Chad Miller&#8217;s <a href="http://gallery.technet.microsoft.com/scriptcenter/7985b7ef-ed89-4dfd-b02a-433cc4e30894#content" target="_blank">Invoke-SqlCmd2 script</a>&#8221; <img src='http://sqlfool.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  . Thank you, Chad! The main difference is that my function supports connecting to either Teradata or SQL Server, depending on the parameters passed. </p>
<p>One thing worth mentioning is that I spent a lot of time researching all of the different ways to connect to SQL Server. In the end, I went with ADO.NET, as a lot of what I will be doing is working with data sets (i.e. running a query against ServerA and writing the results to ServerB). </p>
<p><strong>Invoke-SqlStmt</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">##################################################################################################</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#   .NAME</span>
<span style="color: #008000;">#       Invoke-SqlStmt.ps1</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#   .SYNOPSIS</span>
<span style="color: #008000;">#       Executes a SQL statement against either a Teradata or SQL Server source using ADO.NET</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#   .INPUT</span>
<span style="color: #008000;">#       C - Connection Type; Required; MSSQL or TDATA</span>
<span style="color: #008000;">#       S - Server Name; Required; server name for any valid SQL Server or data instance</span>
<span style="color: #008000;">#       Q - Query; Optional; either Q or F is required</span>
<span style="color: #008000;">#       F - Filename; Optional; either Q or F is required</span>
<span style="color: #008000;">#       U - Username; Optional; if not supplied, Integrated mode is assumed</span>
<span style="color: #008000;">#       P - Password; Optional; if not supplied, Integrated mode is assumed</span>
<span style="color: #008000;">#       T - Connection Timeout; Optional; defaults to 600s but can be overridden</span>
<span style="color: #008000;">#       AS - Optional; can specify whether DataSet, DataTable, or DataRow will be returned</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#   .OUTPUTS</span>
<span style="color: #008000;">#       System.Data.(DataSet|DataTable|DataRow)</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#   .CREDIT</span>
<span style="color: #008000;">#       Modified from Chad Miller's Invoke-SqlCmd2 script:</span>
<span style="color: #008000;">#       http://gallery.technet.microsoft.com/scriptcenter/7985b7ef-ed89-4dfd-b02a-433cc4e30894#content</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#  --------------------------------------------------------------------------------------------</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#   .EXAMPLES</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#   Invoke-SqlStmt -C MSSQL -S &quot;(local)&quot; -Q &quot;SELECT @@SERVERNAME;&quot;</span>
<span style="color: #008000;">#     This example connects to a named instance of the D Engine on a computer and runs a basic T-SQL Q.</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#   Invoke-SqlStmt -C MSSQL -S &quot;(local)&quot; -F &quot;C:\myScript.sql&quot; | Out-File -filePath &quot;C:\myOutput.txt&quot;</span>
<span style="color: #008000;">#     This example reads a file containing T-SQL statements, runs the file, and writes the output to another file.</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#   Invoke-SqlStmt  -C MSSQL -S &quot;(local)&quot; -Q &quot;PRINT 'hello world'&quot; -Verbose</span>
<span style="color: #008000;">#     This example uses the PowerShell -Verbose parameter to return the message output of the PRINT command.</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#   Invoke-SqlStmt  -C TDATA -S &quot;(local)&quot; -Q &quot;SELECT * FROM DBC.Tables;&quot;</span>
<span style="color: #008000;">#     This example connects to Teradata and executes a query. </span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#  --------------------------------------------------------------------------------------------</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#   AUTHOR:     Michelle Ufford</span>
<span style="color: #008000;">#   CREATED:    2012-12-12</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#  CHANGE HISTORY:</span>
<span style="color: #008000;">#  --------------------------------------------------------------------------------------------</span>
<span style="color: #008000;">#  ChangeDate/Version   Author                  Description</span>
<span style="color: #008000;">#  --------------------------------------------------------------------------------------------</span>
<span style="color: #008000;">#  20121212.0           Michelle Ufford         Initial Creation</span>
<span style="color: #008000;">#  20121218.1           Michelle Ufford         Tweaks</span>
<span style="color: #008000;">##################################################################################################</span>
&nbsp;
<span style="color: #0000FF;">function</span> Invoke<span style="color: pink;">-</span>SqlStmt
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>CmdletBinding<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">0</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span>ValidateSet<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;MSSQL&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;TDATA&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$C</span><span style="color: pink;">=</span><span style="color: #800000;">&quot;MSSQL&quot;</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">1</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>  <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$S</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">2</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$D</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">3</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Q</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">4</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span>ValidateScript<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">test-path</span> <span style="color: #000080;">$_</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$F</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">5</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$U</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">6</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$P</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">8</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span>Int32<span style="color: #000000;">&#93;</span><span style="color: #800080;">$T</span><span style="color: pink;">=</span><span style="color: #804000;">600</span><span style="color: pink;">,</span>
    <span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Position<span style="color: pink;">=</span><span style="color: #804000;">9</span><span style="color: pink;">,</span> Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span>ValidateSet<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;DataSet&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;DataTable&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;DataRow&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$AS</span><span style="color: pink;">=</span><span style="color: #800000;">&quot;DataRow&quot;</span>
    <span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #008000;"># Check for input files</span>
    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$F</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #800080;">$filePath</span> <span style="color: pink;">=</span> $<span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">resolve-path</span> <span style="color: #800080;">$F</span><span style="color: #000000;">&#41;</span>.path
        <span style="color: #800080;">$Q</span> <span style="color: pink;">=</span>  <span style="color: #000000;">&#91;</span>System.IO.File<span style="color: #000000;">&#93;</span>::ReadAllText<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;$filePath&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008000;"># ----------------------</span>
    <span style="color: #008000;"># Teradata Connections</span>
    <span style="color: #008000;"># ----------------------</span>
    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$C</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;TDATA&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #800080;">$ConnectionString</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Data Source={0}; User Id={1}; Password={2}; Connection Pooling Timeout={3};&quot;</span> <span style="color: #FF0000;">-f</span> <span style="color: #800080;">$S</span><span style="color: pink;">,</span> <span style="color: #800080;">$U</span><span style="color: pink;">,</span> <span style="color: #800080;">$P</span><span style="color: pink;">,</span> <span style="color: #800080;">$T</span>
&nbsp;
        Add<span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">Type</span> <span style="color: pink;">-</span>AssemblyName System.Data
        <span style="color: #800080;">$Factory</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Data.Common.DbProviderFactories<span style="color: #000000;">&#93;</span>::GetFactory<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Teradata.Client.Provider&quot;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #800080;">$Connection</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Factory</span>.CreateConnection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #800080;">$Connection</span>.ConnectionString <span style="color: pink;">=</span> <span style="color: #800080;">$ConnectionString</span>
        <span style="color: #800080;">$Connection</span>.Open<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #800080;">$Command</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Connection</span>.CreateCommand<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #800080;">$Command</span>.CommandText <span style="color: pink;">=</span> <span style="color: #800080;">$Q</span>
        <span style="color: #800080;">$DataSet</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.DataSet
        <span style="color: #800080;">$DataAdapter</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Factory</span>.CreateDataAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #800080;">$DataAdapter</span>.SelectCommand <span style="color: pink;">=</span> <span style="color: #800080;">$Command</span>
        <span style="color: #800080;">$DataAdapter</span>.SelectCommand.CommandTimeout <span style="color: pink;">=</span> <span style="color: #800080;">$T</span>
        <span style="color: #000000;">&#91;</span>void<span style="color: #000000;">&#93;</span> <span style="color: #800080;">$DataAdapter</span>.Fill<span style="color: #000000;">&#40;</span><span style="color: #800080;">$DataSet</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>
        <span style="color: #800080;">$Connection</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
        <span style="color: #008000;"># get a table contained in the DataSet, http://technet.microsoft.com/en-us/library/dd347701.aspx</span>
        <span style="color: #0000FF;">switch</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$As</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
              <span style="color: #800000;">'DataSet'</span>   <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Output</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DataSet</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>
              <span style="color: #800000;">'DataTable'</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Output</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DataSet</span>.Tables<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>
              <span style="color: #800000;">'DataRow'</span>   <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Output</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DataSet</span>.Tables<span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">else</span>
    <span style="color: #008000;"># ------------------------</span>
    <span style="color: #008000;"># SQL Server Connections</span>
    <span style="color: #008000;"># ------------------------</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #800080;">$Connection</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.SqlClient.SQLConnection
&nbsp;
        <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$U</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$ConnectionString</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Server={0};Database={1};User ID={2};Password={3};Trusted_Connection=False;Connect Timeout={4}&quot;</span> <span style="color: #FF0000;">-f</span> <span style="color: #800080;">$S</span><span style="color: pink;">,</span><span style="color: #800080;">$D</span><span style="color: pink;">,</span><span style="color: #800080;">$U</span><span style="color: pink;">,</span><span style="color: #800080;">$P</span><span style="color: pink;">,</span><span style="color: #800080;">$T</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0000FF;">else</span>
        <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$ConnectionString</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Server={0};Database={1};Integrated Security=True;Connect Timeout={2}&quot;</span> <span style="color: #FF0000;">-f</span> <span style="color: #800080;">$S</span><span style="color: pink;">,</span><span style="color: #800080;">$D</span><span style="color: pink;">,</span><span style="color: #800080;">$CT</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #800080;">$Connection</span>.ConnectionString<span style="color: pink;">=</span><span style="color: #800080;">$ConnectionString</span>
&nbsp;
        <span style="color: #008000;">#Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller</span>
        <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$PSBoundParameters</span>.Verbose<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #800080;">$Connection</span>.FireInfoMessageEventOnUserErrors<span style="color: pink;">=</span><span style="color: #800080;">$true</span>
            <span style="color: #800080;">$Handler</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Data.SqlClient.SqlInfoMessageEventHandler<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">Write-Verbose</span> <span style="color: #800000;">&quot;$($_)&quot;</span><span style="color: #000000;">&#125;</span>
            <span style="color: #800080;">$Connection</span>.add_InfoMessage<span style="color: #000000;">&#40;</span><span style="color: #800080;">$Handler</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #800080;">$Connection</span>.Open<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #800080;">$Command</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.SqlClient.SqlCommand
        <span style="color: #800080;">$Command</span>.Connection <span style="color: pink;">=</span> <span style="color: #800080;">$Connection</span>
        <span style="color: #800080;">$Command</span>.CommandText <span style="color: pink;">=</span> <span style="color: #800080;">$Q</span>
        <span style="color: #800080;">$Command</span>.CommandTimeout <span style="color: pink;">=</span> <span style="color: #800080;">$T</span>
        <span style="color: #800080;">$DataSet</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.DataSet
        <span style="color: #800080;">$DataAdapter</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.SqlClient.SqlDataAdapter
        <span style="color: #800080;">$DataAdapter</span>.SelectCommand <span style="color: pink;">=</span> <span style="color: #800080;">$Command</span>
        <span style="color: #000000;">&#91;</span>void<span style="color: #000000;">&#93;</span><span style="color: #800080;">$DataAdapter</span>.Fill<span style="color: #000000;">&#40;</span><span style="color: #800080;">$DataSet</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>
        <span style="color: #800080;">$Connection</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
        <span style="color: #0000FF;">switch</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$As</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #800000;">'DataSet'</span>   <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Output</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DataSet</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>
            <span style="color: #800000;">'DataTable'</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Output</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DataSet</span>.Tables<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>
            <span style="color: #800000;">'DataRow'</span>   <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Output</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DataSet</span>.Tables<span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>I plan to have a follow-up post up shortly that will walk you through how to use this script step-by-step. </p>
<p>The post <a href="http://sqlfool.com/2013/02/invoke-sqlstmt/">Invoke-SqlStmt &#8211; PowerShell for SQL Server &#038; Teradata Queries</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p><img src="http://feeds.feedburner.com/~r/SqlFool/~4/rnAEGSbUUic" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2013/02/invoke-sqlstmt/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://sqlfool.com/2013/02/invoke-sqlstmt/</feedburner:origLink></item>
		<item>
		<title>Index Definition Audit Script</title>
		<link>http://feedproxy.google.com/~r/SqlFool/~3/c0ogLQeegWY/</link>
		<comments>http://sqlfool.com/2012/10/index-definition-audit-script/#comments</comments>
		<pubDate>Mon, 15 Oct 2012 15:01:06 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[audit]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[maintenace]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1850</guid>
		<description><![CDATA[<p>Recently, I needed to audit indexes on two different servers. I wanted to compare things like index keys and included columns, partitioning keys, unique constraints, clustered indexes and primary keys, and filter index definitions. Basically, I wanted to make sure that the indexing of two databases on two different servers were completely in sync. To do this, I wrote the following scripts. The first script will audit a single database or even a single table. The second script once more makes use of Aaron Bertrand&#8217;s sp_foreachdb procedure to iterate through every database on a server. To do a quick and easy compare, I dumped the results to a single table on each server and used Red Gate&#8217;s SQL Data Compare to find the differences. This is another metadata script, so it should be fairly lightweight. That said, all of the usual disclaimers apply: This script will only run on SQL 2008 or newer because of the filtered index component. See Patrick&#8217;s solution in the comments below for a mod that works in 2005 too. It worked for me, but YMMV depending on editions, collations, creative settings, etc. I don&#8217;t currently have access to any instances older than SQL Server 2012, … <a href="http://sqlfool.com/2012/10/index-definition-audit-script/"> Continue reading <span class="meta-nav">&#8594; </span></a></p><p>The post <a href="http://sqlfool.com/2012/10/index-definition-audit-script/">Index Definition Audit Script</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>Recently, I needed to audit indexes on two different servers. I wanted to compare things like index keys and included columns, partitioning keys, unique constraints, clustered indexes and primary keys, and filter index definitions. Basically, I wanted to make sure that the indexing of two databases on two different servers were completely in sync. To do this, I wrote the following scripts. The first script will audit a single database or even a single table. The second script once more makes use of <a href="http://www.mssqltips.com/sqlservertip/2201/making-a-more-reliable-and-flexible-spmsforeachdb/" target="_blank">Aaron Bertrand&#8217;s sp_foreachdb procedure</a> to iterate through every database on a server. </p>
<p>To do a quick and easy compare, I dumped the results to a single table on each server and used <a href="http://www.red-gate.com/products/sql-development/sql-data-compare/" target="_blank">Red Gate&#8217;s SQL Data Compare</a> to find the differences. </p>
<p>This is another metadata script, so it should be fairly lightweight. That said, all of the usual disclaimers apply:</p>
<ul>
<li>This script will only run on SQL 2008 or newer because of the filtered index component. See Patrick&#8217;s solution in the comments below for a mod that works in 2005 too.</li>
<li>It worked for me, but YMMV depending on editions, collations, creative settings, etc.
<li>I don&#8217;t currently have access to any instances older than SQL Server 2012, so please respond with any backwards compatibility issues.</li>
<li>If you don&#8217;t have sp_foreachdb installed and don&#8217;t want to install it, you can replace it with sp_msforeachdb, but be aware databases may be skipped</li>
</ul>
<p>Enjoy. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Single-Database Version</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">WITH</span> indexCTE <span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
    <span style="color: #0000FF;">SELECT</span> st.<span style="color: #FF00FF;">object_id</span>                                                                         <span style="color: #0000FF;">AS</span> objectID
        , st.<span style="color: #202020;">name</span>                                                                               <span style="color: #0000FF;">AS</span> tableName
        , si.<span style="color: #202020;">index_id</span>                                                                           <span style="color: #0000FF;">AS</span> indexID
        , si.<span style="color: #202020;">name</span>                                                                               <span style="color: #0000FF;">AS</span> indexName
        , si.<span style="color: #202020;">type_desc</span>                                                                          <span style="color: #0000FF;">AS</span> indexType
        , sc.<span style="color: #202020;">column_id</span>                                                                          <span style="color: #0000FF;">AS</span> columnID
        , sc.<span style="color: #202020;">name</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> sic.<span style="color: #202020;">is_descending_key</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' DESC'</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">''</span> <span style="color: #0000FF;">END</span>                <span style="color: #0000FF;">AS</span> columnName
        , sic.<span style="color: #202020;">key_ordinal</span>                                                                       <span style="color: #0000FF;">AS</span> ordinalPosition
        , <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> sic.<span style="color: #202020;">is_included_column</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #808080;">AND</span> key_ordinal <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> sc.<span style="color: #202020;">name</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">END</span>   <span style="color: #0000FF;">AS</span> indexKeys
        , <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> sic.<span style="color: #202020;">is_included_column</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> sc.<span style="color: #202020;">name</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">END</span>                       <span style="color: #0000FF;">AS</span> includedColumns
        , sic.<span style="color: #202020;">partition_ordinal</span>                                                                 <span style="color: #0000FF;">AS</span> partitionOrdinal
        , <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> sic.<span style="color: #202020;">partition_ordinal</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> sc.<span style="color: #202020;">name</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">END</span>                        <span style="color: #0000FF;">AS</span> partitionColumns
        , si.<span style="color: #202020;">is_primary_key</span>                                                                     <span style="color: #0000FF;">AS</span> isPrimaryKey
        , si.<span style="color: #202020;">is_unique</span>                                                                          <span style="color: #0000FF;">AS</span> isUnique
        , si.<span style="color: #202020;">is_unique_constraint</span>                                                               <span style="color: #0000FF;">AS</span> isUniqueConstraint
        , si.<span style="color: #202020;">has_filter</span>                                                                         <span style="color: #0000FF;">AS</span> isFilteredIndex
        , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>si.<span style="color: #202020;">filter_definition</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>                                                    <span style="color: #0000FF;">AS</span> filterDefinition
    <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">tables</span>                         <span style="color: #0000FF;">AS</span> st
    <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">indexes</span>                  <span style="color: #0000FF;">AS</span> si 
        <span style="color: #0000FF;">ON</span> si.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span>   st.<span style="color: #FF00FF;">object_id</span>
    <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">index_columns</span>            <span style="color: #0000FF;">AS</span> sic 
	    <span style="color: #0000FF;">ON</span> sic.<span style="color: #FF00FF;">object_id</span><span style="color: #808080;">=</span>si.<span style="color: #FF00FF;">object_id</span>
        <span style="color: #808080;">AND</span> sic.<span style="color: #202020;">index_id</span><span style="color: #808080;">=</span>si.<span style="color: #202020;">index_id</span> 
    <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">columns</span>                  <span style="color: #0000FF;">AS</span> sc 
	    <span style="color: #0000FF;">ON</span> sc.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> sic.<span style="color: #FF00FF;">object_id</span> 
	    and sc.<span style="color: #202020;">column_id</span> <span style="color: #808080;">=</span> sic.<span style="color: #202020;">column_id</span>
<span style="color: #808080;">&#41;</span> 
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> 
      <span style="color: #FF00FF;">@@SERVERNAME</span>                                      <span style="color: #0000FF;">AS</span> ServerName
    , <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>                                         <span style="color: #0000FF;">AS</span> DatabaseName
    , tableName
    , indexName
    , indexType
    , <span style="color: #FF00FF;">STUFF</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>
            <span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">', '</span> <span style="color: #808080;">+</span> indexKeys
                <span style="color: #0000FF;">FROM</span> indexCTE
            <span style="color: #0000FF;">WHERE</span> objectID <span style="color: #808080;">=</span> cte.<span style="color: #202020;">objectID</span>
                <span style="color: #808080;">AND</span> indexID <span style="color: #808080;">=</span> cte.<span style="color: #202020;">indexID</span>
                <span style="color: #808080;">AND</span> indexKeys <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> 
            <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> ordinalPosition
                <span style="color: #0000FF;">FOR</span> XML <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>, 
      TYPE<span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>,<span style="color: #FF0000;">'varchar(max)'</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">1</span>,<span style="color: #000;">1</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>           <span style="color: #0000FF;">AS</span> indexKeys
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">STUFF</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>
            <span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">', '</span> <span style="color: #808080;">+</span> includedColumns
                <span style="color: #0000FF;">FROM</span> indexCTE
            <span style="color: #0000FF;">WHERE</span> objectID <span style="color: #808080;">=</span> cte.<span style="color: #202020;">objectID</span>
                <span style="color: #808080;">AND</span> indexID <span style="color: #808080;">=</span> cte.<span style="color: #202020;">indexID</span>
                <span style="color: #808080;">AND</span> includedColumns <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> 
            <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> columnID
                <span style="color: #0000FF;">FOR</span> XML <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>, 
      TYPE<span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>,<span style="color: #FF0000;">'varchar(max)'</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">1</span>,<span style="color: #000;">1</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>      <span style="color: #0000FF;">AS</span> includedColumns
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">STUFF</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>
            <span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">', '</span> <span style="color: #808080;">+</span> partitionColumns
                <span style="color: #0000FF;">FROM</span> indexCTE
            <span style="color: #0000FF;">WHERE</span> objectID <span style="color: #808080;">=</span> cte.<span style="color: #202020;">objectID</span>
                <span style="color: #808080;">AND</span> indexID <span style="color: #808080;">=</span> cte.<span style="color: #202020;">indexID</span>
                <span style="color: #808080;">AND</span> partitionColumns <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> 
            <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> partitionOrdinal
                <span style="color: #0000FF;">FOR</span> XML <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>, 
      TYPE<span style="color: #808080;">&#41;</span>.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>,<span style="color: #FF0000;">'varchar(max)'</span><span style="color: #808080;">&#41;</span>,<span style="color: #000;">1</span>,<span style="color: #000;">1</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>      <span style="color: #0000FF;">AS</span> partitionKeys
    , isPrimaryKey
    , isUnique
    , isUniqueConstraint
    , isFilteredIndex
    , FilterDefinition
<span style="color: #0000FF;">FROM</span> indexCTE <span style="color: #0000FF;">AS</span> cte
<span style="color: #008080;">--WHERE tableName = 'SalesOrderDetail'</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> tableName
    , indexName;</pre></td></tr></table></div>

<p><strong>Multi-Database Version</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#IndexAudit'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #IndexAudit;
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #IndexAudit
<span style="color: #808080;">&#40;</span>
      serverName                SYSNAME
    , databaseName              SYSNAME
    , tableName                 <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , indexName                 <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , indexType                 <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">60</span><span style="color: #808080;">&#41;</span>
    , indexKeys                 <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8000</span><span style="color: #808080;">&#41;</span>
    , includedColumns           <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8000</span><span style="color: #808080;">&#41;</span>
    , partitionColumns          <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8000</span><span style="color: #808080;">&#41;</span>
    , isPrimaryKey              <span style="color: #0000FF;">BIT</span>
    , isUnique                  <span style="color: #0000FF;">BIT</span>
    , isUniqueConstraint        <span style="color: #0000FF;">BIT</span>
    , isFilteredIndex           <span style="color: #0000FF;">BIT</span>
    , FilterDefinition          <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8000</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">EXECUTE</span> sp_foreachdb <span style="color: #FF0000;">'USE ?;
WITH indexCTE AS
(
    SELECT st.object_id                                                                         AS objectID
        , st.name                                                                               AS tableName
        , si.index_id                                                                           AS indexID
        , si.type_desc                                                                          AS indexType
        , si.name                                                                               AS indexName
        , sc.column_id                                                                          AS columnID
        , sc.name + CASE WHEN sic.is_descending_key = 1 THEN '</span><span style="color: #FF0000;">' DESC'</span><span style="color: #FF0000;">' ELSE '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">' END            AS columnName
        , sic.key_ordinal                                                                       AS ordinalPosition
        , CASE WHEN sic.is_included_column = 0 AND key_ordinal &gt; 0 THEN sc.name ELSE NULL END   AS indexKeys
        , CASE WHEN sic.is_included_column = 1 THEN sc.name ELSE NULL END                       AS includedColumns
        , sic.partition_ordinal                                                                 AS partitionOrdinal
        , CASE WHEN sic.partition_ordinal &gt; 0 THEN sc.name ELSE NULL END                        AS partitionColumns
        , si.is_primary_key                                                                     AS isPrimaryKey
        , si.is_unique                                                                          AS isUnique
        , si.is_unique_constraint                                                               AS isUniqueConstraint
        , si.has_filter                                                                         AS isFilteredIndex
        , COALESCE(si.filter_definition, '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">')                                                  AS filterDefinition
    FROM sys.tables                         AS st
    INNER JOIN sys.indexes                  AS si 
        ON si.object_id =   st.object_id
    INNER JOIN sys.index_columns            AS sic 
	    ON sic.object_id=si.object_id
        AND sic.index_id=si.index_id 
    INNER JOIN sys.columns                  AS sc 
	    ON sc.object_id = sic.object_id 
	    and sc.column_id = sic.column_id
) 
&nbsp;
INSERT INTO #IndexAudit
SELECT DISTINCT 
      @@SERVERNAME                                              AS ServerName
    , DB_NAME()                                                 AS DatabaseName
    , tableName
    , indexName
    , indexType
    , STUFF((
            SELECT '</span><span style="color: #FF0000;">', '</span><span style="color: #FF0000;">' + indexKeys
                FROM indexCTE
            WHERE objectID = cte.objectID
                AND indexID = cte.indexID
                AND indexKeys IS NOT NULL 
            ORDER BY ordinalPosition
                FOR XML PATH('</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">'), 
      TYPE).value('</span><span style="color: #FF0000;">'.'</span><span style="color: #FF0000;">','</span><span style="color: #FF0000;">'varchar(max)'</span><span style="color: #FF0000;">'),1,1,'</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">')             AS indexKeys
    , COALESCE(STUFF((
            SELECT '</span><span style="color: #FF0000;">', '</span><span style="color: #FF0000;">' + includedColumns
                FROM indexCTE
            WHERE objectID = cte.objectID
                AND indexID = cte.indexID
                AND includedColumns IS NOT NULL 
            ORDER BY columnID
                FOR XML PATH('</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">'), 
      TYPE).value('</span><span style="color: #FF0000;">'.'</span><span style="color: #FF0000;">','</span><span style="color: #FF0000;">'varchar(max)'</span><span style="color: #FF0000;">'),1,1,'</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">'), '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">')      AS includedColumns
    , COALESCE(STUFF((
            SELECT '</span><span style="color: #FF0000;">', '</span><span style="color: #FF0000;">' + partitionColumns
                FROM indexCTE
            WHERE objectID = cte.objectID
                AND indexID = cte.indexID
                AND partitionColumns IS NOT NULL 
            ORDER BY partitionOrdinal
                FOR XML PATH('</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">'), 
      TYPE).value('</span><span style="color: #FF0000;">'.'</span><span style="color: #FF0000;">','</span><span style="color: #FF0000;">'varchar(max)'</span><span style="color: #FF0000;">'),1,1,'</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">'), '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">')      AS partitionKeys
    , isPrimaryKey
    , isUnique
    , isUniqueConstraint
    , isFilteredIndex
    , FilterDefinition
FROM indexCTE AS cte
ORDER BY tableName
    , indexName;
'</span>;
&nbsp;
<span style="color: #008080;">-- For multi-server testing, dump results to a temp table and compare tables</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> #IndexAudit
<span style="color: #0000FF;">WHERE</span> databaseName <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb'</span>, <span style="color: #FF0000;">'master'</span>, <span style="color: #FF0000;">'msdb'</span>, <span style="color: #FF0000;">'model'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> serverName
    , databaseName
    , tableName
    , indexName;</pre></td></tr></table></div>

<p><strong>Example Results</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">ServerName   DatabaseName       tableName            indexName                      indexType    indexKeys                                                    includedColumns                                    partitionKeys isPrimaryKey isUnique isUniqueConstraint isFilteredIndex FilterDefinition
------------ ------------------ -------------------- ------------------------------ ------------ ------------------------------------------------------------ -------------------------------------------------- ------------- ------------ -------- ------------------ --------------- --------------------------------
SQLFOOL\2012 AdventureWorks2012 SalesOrderDetail     AK_SalesOrderDetail_rowguid    NONCLUSTERED  rowguid                                                                                                                      0            1        0                  0                                               
SQLFOOL\2012 AdventureWorks2012 SalesOrderDetail     FIX_SalesOrderDetail_1         NONCLUSTERED  CarrierTrackingNumber, SalesOrderID, ProductID               OrderQty, SpecialOfferID, LineTotal, rowguid                    0            0        0                  1               ([ModifiedDate]&gt;='2006-01-01')  
SQLFOOL\2012 AdventureWorks2012 SalesOrderDetail     IX_SalesOrderDetail_ProductID  NONCLUSTERED  ProductID                                                                                                                    0            0        0                  0                                               
SQLFOOL\2012 AdventureWorks2012 SalesOrderDetail     PK_SalesOrderDetail_SalesOrder CLUSTERED     SalesOrderID, SalesOrderDetailID                                                                                             1            1        0                  0</pre></td></tr></table></div>

<p>The post <a href="http://sqlfool.com/2012/10/index-definition-audit-script/">Index Definition Audit Script</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p><img src="http://feeds.feedburner.com/~r/SqlFool/~4/c0ogLQeegWY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2012/10/index-definition-audit-script/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://sqlfool.com/2012/10/index-definition-audit-script/</feedburner:origLink></item>
		<item>
		<title>Accelerating ETL Data Retrieval</title>
		<link>http://feedproxy.google.com/~r/SqlFool/~3/d-NE8Com0nk/</link>
		<comments>http://sqlfool.com/2012/10/accelerating-etl-data-retrieval/#comments</comments>
		<pubDate>Mon, 08 Oct 2012 17:08:22 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Business Intelligence]]></category>
		<category><![CDATA[PASS]]></category>
		<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[Presentations]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[large]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[Summit]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1814</guid>
		<description><![CDATA[<p>As I&#8217;ve mentioned previously, SQL Server 2012 Integration Services Design Patterns is now available on Amazon. PASS has invited the ENTIRE author team to give a pre-conference training session at Summit 2012. Precons are all-day training events and are held on the days before Summit. Our precon will be held on Monday, November 5th. I&#8217;ve attended several precons at Summit, and in my opinion, they provide unparalleled depth and access to the presenters. I expect our precon will be no different. So what will I be presenting on? Good question. I&#8217;m going to talk about Accelerating ETL Data Retrieval. Specifically, I&#8217;ll discuss strategies for retrieving data for full loads vs. incremental loads, and I&#8217;ll explore some strategies for retrieving data from large tables. To clarify &#8220;large,&#8221; since everyone has a different definition, I&#8217;ve successfully employed these strategies on tables with 10 billion rows. Now you might be reading this and thinking, &#8220;Hey, that doesn&#8217;t sound like SSIS!&#8221; Well&#8230; yes and no. While I will be discussing some strategies within SSIS, most of what I discuss will take place at the database layer. In fact, most of my content could be useful for any ETL tool. I still think it&#8217;s a … <a href="http://sqlfool.com/2012/10/accelerating-etl-data-retrieval/"> Continue reading <span class="meta-nav">&#8594; </span></a></p><p>The post <a href="http://sqlfool.com/2012/10/accelerating-etl-data-retrieval/">Accelerating ETL Data Retrieval</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>As I&#8217;ve <a href="http://sqlfool.com/2012/09/ssis-design-patterns/" target="_blank">mentioned previously</a>, <strong>SQL Server 2012 Integration Services Design Patterns</strong> is now available <a href="http://www.amazon.com/Server-Integration-Services-Design-Patterns/dp/1430237716" target="_blank">on Amazon</a>. PASS has invited the ENTIRE author team to give a <a href="http://www.sqlpass.org/summit/2012/Sessions/SessionDetails.aspx?sid=3159" target="_blank">pre-conference training session</a> at Summit 2012. Precons are all-day training events and are held on the days before Summit. Our precon will be held on Monday, November 5th. I&#8217;ve attended several precons at Summit, and in my opinion, they provide unparalleled depth and access to the presenters. I expect our precon will be no different.<br />
<a href="http://sqlfool.com/wp-content/uploads/2012/10/MVP_night.jpg"><img src="http://sqlfool.com/wp-content/uploads/2012/10/MVP_night-199x300.jpg" alt="" title="MVP Award" width="199" height="300" class="alignright size-medium wp-image-1836" target="_blank"/></a><br />
So what will I be presenting on? Good question. I&#8217;m going to talk about Accelerating ETL Data Retrieval. Specifically, I&#8217;ll discuss strategies for retrieving data for full loads vs. incremental loads, and I&#8217;ll explore some strategies for retrieving data from large tables. To clarify &#8220;large,&#8221; since everyone has a different definition, I&#8217;ve successfully employed these strategies on tables with 10 billion rows.  </p>
<p>Now you might be reading this and thinking, &#8220;Hey, that doesn&#8217;t sound like SSIS!&#8221; Well&#8230; yes and no. While I will be discussing some strategies within SSIS, most of what I discuss will take place at the database layer. In fact, most of my content could be useful for any ETL tool. I still think it&#8217;s a good topic for this precon for three reasons. First, most ETL developers I talk with &#8212; that is, people who specialize in and are primarily tasked with ETL &#8212; overlook the data retrieval step. I frequently hear, &#8220;I&#8217;m pulling all of the data, so it doesn&#8217;t matter, I can&#8217;t get the data any faster than SELECT * FROM.&#8221; That&#8217;s not always true, and I&#8217;ll explain why in the precon. Secondly, having a thorough understanding of data retrieval techniques is important. Data retrieval is one of the most &#8212; if not the single most &#8212; expensive components of ETL. Lastly, I think there&#8217;s a huge knowledge gap in this area. While there is a lot of discussion about query optimization, it rarely has to do with the type of query patterns that ETL requires. </p>
<p>So that&#8217;s what I&#8217;ll be talking about and why. What do you think? Worthwhile content? </p>
<p>Also, this is just one of the things that will be covered in our <a href="http://www.sqlpass.org/summit/2012/Sessions/SessionDetails.aspx?sid=3159" target="_blank">SSIS Design Patterns</a> precon. The rest of the author team &#8212; <a href="http://www.sqlblog.com/blogs/andy_leonard" target="_blank">Andy Leonard</a>, <a href="http://www.mattmasson.com/" target="_blank">Matt Masson</a>, <a href="http://www.timmitchell.net/" target="_blank">Tim Mitchell</a>, and <a href="http://jessicammoss.blogspot.com/" target="_blank">Jessica Moss</a> &#8212; is putting together great content that includes package execution, parent/child relationships, expressions and scripting, dynamic configurations, error handling, data flow internals, scalability and parallelism, automation, dynamic package generation, data warehousing patterns, and more!</p>
<p>As an aside, I am honored to announce that I have been re-awarded as Microsoft MVP for SQL Server. My husband took this picture of my trophy for me, which I think turned out really cool. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The post <a href="http://sqlfool.com/2012/10/accelerating-etl-data-retrieval/">Accelerating ETL Data Retrieval</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p><img src="http://feeds.feedburner.com/~r/SqlFool/~4/d-NE8Com0nk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2012/10/accelerating-etl-data-retrieval/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://sqlfool.com/2012/10/accelerating-etl-data-retrieval/</feedburner:origLink></item>
		<item>
		<title>Go Daddy Insiders</title>
		<link>http://feedproxy.google.com/~r/SqlFool/~3/8AJOutOH4Vo/</link>
		<comments>http://sqlfool.com/2012/10/go-daddy-insiders/#comments</comments>
		<pubDate>Wed, 03 Oct 2012 20:09:56 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[data types]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[GoDaddy.com]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[scalability]]></category>
		<category><![CDATA[Summit]]></category>
		<category><![CDATA[Super Bowl]]></category>
		<category><![CDATA[tuning]]></category>
		<category><![CDATA[VLDB]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1797</guid>
		<description><![CDATA[<p>I don&#8217;t hide the fact that I work at Go Daddy. All discussions of advertising methods aside, it&#8217;s a great company to work for. Not only am I treated well as an employee, I also get to work in a world-class technical environment. However, the marketing campaigns tend to steal the spotlight. As a result, few people are aware of technology that it takes to be the #1 hosting provider in the world. Some examples of little-known facts about Go Daddy: 10 billion DNS queries answered daily Over 35,000 servers &#038; 100,000 square feet of state-of-the-art global data centers 25 petabytes &#8212; yes, petabytes! &#8212; of networked data storage Pretty cool, huh? Go Daddy has launched a new blog called Inside Go Daddy as a way to share all the nitty gritty details of what it takes to support this kind of environment. Here&#8217;s a blurb from the site: This is your inside source for what’s going on with Go Daddy’s tech experts. You’ll get insight and opinions from Go Daddy’s tech leaders on industry topics, company projects &#038; open source initiatives … the leading edge, unconventional, “behind-the-scenes” information you won’t find anywhere else. It’s not PR, it’s not executive … <a href="http://sqlfool.com/2012/10/go-daddy-insiders/"> Continue reading <span class="meta-nav">&#8594; </span></a></p><p>The post <a href="http://sqlfool.com/2012/10/go-daddy-insiders/">Go Daddy Insiders</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>I don&#8217;t hide the fact that I work at <a href="http://www.godaddy.com" target="_blank">Go Daddy</a>. All discussions of advertising methods aside, <a href="http://www.godaddy.com/jobs/" target="_blank">it&#8217;s a great company to work for</a>. Not only am I treated well as an employee, I also get to work in a world-class technical environment. However, the marketing campaigns tend to steal the spotlight. As a result, few people are aware of technology that it takes to be the #1 hosting provider in the world. Some examples of little-known facts about Go Daddy:</p>
<ul>
<li>10 billion DNS queries answered daily</li>
<li>Over 35,000 servers &#038; 100,000 square feet of state-of-the-art global data centers</li>
<li>25 petabytes &#8212; yes, <em>petabytes</em>! &#8212; of networked data storage</li>
</ul>
<p>Pretty cool, huh? Go Daddy has launched a new blog called <a href="http://inside.godaddy.com/" target="_blank">Inside Go Daddy</a> as a way to share all the nitty gritty details of what it takes to support this kind of environment. Here&#8217;s a blurb from the site:</p>
<blockquote><p>This is your inside source for what’s going on with Go Daddy’s tech experts. You’ll get insight and opinions from Go Daddy’s tech leaders on industry topics, company projects &#038; open source initiatives … the leading edge, unconventional, “behind-the-scenes” information you won’t find anywhere else. It’s not PR, it’s not executive talk, it’s the story straight from Go Daddy’s developers, engineers &#038; IT personnel.</p></blockquote>
<p>Shockingly, I&#8217;ve signed up to blog about database scalability. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  I&#8217;ve just started a new series that explores the tuning and design changes required to support 27k transactions per second during the airing of Go Daddy&#8217;s Super Bowl commercials. Those who attended my <a href="http://summit2009.sqlpass.org/Agenda/ProgramSessions/SuperBowlSuperLoadALookatPerformance.aspx" target="_blank">Summit 2009</a> session might recognize some of the high-level content, but this series will explore the topics in depth and with never-before-revealed detail. My first article, <a href="http://inside.godaddy.com/scaling-database-data-types/" target="_blank">Scaling the Database: Data Types</a>, is now live. </p>
<p>If you find the content helpful or interesting, please share the article or leave a comment. My employer monitors blog traffic, and we have a bit of a contest going on to see what topics get the most hits. Quite frankly, it&#8217;d be cool if the SQL Server topics outperformed the NoSQL topics. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Also, I&#8217;ll entertain topic requests, so if there&#8217;s something you&#8217;re just dying to know about what we do or how we do it, let me know. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The post <a href="http://sqlfool.com/2012/10/go-daddy-insiders/">Go Daddy Insiders</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p><img src="http://feeds.feedburner.com/~r/SqlFool/~4/8AJOutOH4Vo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2012/10/go-daddy-insiders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://sqlfool.com/2012/10/go-daddy-insiders/</feedburner:origLink></item>
		<item>
		<title>SQL Agent Job Script</title>
		<link>http://feedproxy.google.com/~r/SqlFool/~3/VciuDPqS8Y0/</link>
		<comments>http://sqlfool.com/2012/09/sql-agent-job-script-2/#comments</comments>
		<pubDate>Wed, 19 Sep 2012 04:53:07 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Presentations]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[msdb]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[sql agent jobs]]></category>
		<category><![CDATA[sysjobs]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1777</guid>
		<description><![CDATA[<p>I&#8217;ve been working on a script for a demo in my upcoming 24 Hours of PASS presentation, which I thought I&#8217;d share on my blog. In the presentation, I use this script to demonstrate how to execute one or more queries dynamically against a list of SQL Server instances. The script itself explores SQL Agent Job metadata to get job statuses &#8212; when the job last ran, when it will run again, an aggregate count of the number of successful and failed executions in the queried time period, T-SQL code to disable the job, etc. I&#8217;ve only tested this in a handful of DEV and PROD environments, so please tell me if you experience any issues in your environment when running the script. As with most of my scripts, this will only work on SQL Server 2005 and newer. I was planning to send the SQL Server development team a case of #bacon had they finally fixed the sysjob% date and time columns in SQL Server 2012, but alas, it seems they shall sadly remain pork-free. Credit for the logic that handles the time conversion goes to some awesome community member who posted it on the MSDN forum. I wish … <a href="http://sqlfool.com/2012/09/sql-agent-job-script-2/"> Continue reading <span class="meta-nav">&#8594; </span></a></p><p>The post <a href="http://sqlfool.com/2012/09/sql-agent-job-script-2/">SQL Agent Job Script</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been working on a script for a demo in my <a href="http://www.sqlpass.org/24hours/fall2012/SessionsbySchedule/SessionDetails.aspx?sid=3718" target="_blank">upcoming 24 Hours of PASS presentation</a>, which I thought I&#8217;d share on my blog. In the presentation, I use this script to demonstrate how to execute one or more queries dynamically against a list of SQL Server instances.</p>
<p>The script itself explores SQL Agent Job metadata to get job statuses &#8212; when the job last ran, when it will run again, an aggregate count of the number of successful and failed executions in the queried time period, T-SQL code to disable the job, etc. I&#8217;ve only tested this in a handful of DEV and PROD environments, so please tell me if you experience any issues in your environment when running the script. As with most of my scripts, this will only work on SQL Server 2005 and newer. </p>
<p>I was planning to send the SQL Server development team a case of #bacon had they finally fixed the sysjob% date and time columns in SQL Server 2012, but alas, it seems they shall sadly remain pork-free. Credit for the logic that handles the time conversion goes to some awesome community member who posted it on the MSDN forum. I wish I had the URL, but it was a long time ago.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @jobHistory <span style="color: #0000FF;">TABLE</span>
<span style="color: #808080;">&#40;</span>
      job_id                <span style="color: #0000FF;">UNIQUEIDENTIFIER</span>
    , success               <span style="color: #0000FF;">INT</span>
    , cancel                <span style="color: #0000FF;">INT</span>
    , fail                  <span style="color: #0000FF;">INT</span>
    , retry                 <span style="color: #0000FF;">INT</span>
    , last_execution_id     <span style="color: #0000FF;">INT</span>
    , last_duration         <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8</span><span style="color: #808080;">&#41;</span>
    , last_execution_start  <span style="color: #0000FF;">DATETIME</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">WITH</span> lastExecution
<span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
    <span style="color: #0000FF;">SELECT</span> job_id
    , <span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span>instance_id<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> last_instance_id
<span style="color: #0000FF;">FROM</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobhistory</span>
<span style="color: #0000FF;">WHERE</span> step_id <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> job_id
<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @jobHistory
<span style="color: #0000FF;">SELECT</span> sjh.<span style="color: #202020;">job_id</span>
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> sjh.<span style="color: #202020;">run_status</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #808080;">AND</span> step_id <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> success
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> sjh.<span style="color: #202020;">run_status</span> <span style="color: #808080;">=</span> <span style="color: #000;">3</span> <span style="color: #808080;">AND</span> step_id <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> cancel
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> sjh.<span style="color: #202020;">run_status</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #808080;">AND</span> step_id <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> fail
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> sjh.<span style="color: #202020;">run_status</span> <span style="color: #808080;">=</span> <span style="color: #000;">2</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> retry
    , <span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> sjh.<span style="color: #202020;">step_id</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> instance_id <span style="color: #0000FF;">ELSE</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span> last_execution_id
    , <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> le.<span style="color: #202020;">job_id</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">THEN</span> sjh.<span style="color: #202020;">run_duration</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1000000</span> <span style="color: #0000FF;">AS</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><span style="color: #808080;">&#41;</span>,<span style="color: #000;">2</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">':'</span> 
            <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> le.<span style="color: #202020;">job_id</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">THEN</span> sjh.<span style="color: #202020;">run_duration</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1000000</span> <span style="color: #0000FF;">AS</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><span style="color: #808080;">&#41;</span>,<span style="color: #000;">4</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">':'</span> 
            <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> le.<span style="color: #202020;">job_id</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">THEN</span> sjh.<span style="color: #202020;">run_duration</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1000000</span> <span style="color: #0000FF;">AS</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><span style="color: #808080;">&#41;</span>,<span style="color: #000;">6</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>
            <span style="color: #0000FF;">AS</span> last_duration
    , <span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> le.<span style="color: #202020;">last_instance_id</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">THEN</span> 
        <span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">datetime</span>, <span style="color: #FF00FF;">RTRIM</span><span style="color: #808080;">&#40;</span>run_date<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> 
        <span style="color: #808080;">+</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>run_time <span style="color: #808080;">/</span> <span style="color: #000;">10000</span> <span style="color: #808080;">*</span>  <span style="color: #000;">3600</span><span style="color: #808080;">&#41;</span> 
        <span style="color: #808080;">+</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>run_time <span style="color: #808080;">%</span> <span style="color: #000;">10000</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">100</span> <span style="color: #808080;">*</span> <span style="color: #000;">60</span><span style="color: #808080;">&#41;</span> 
        <span style="color: #808080;">+</span> <span style="color: #808080;">&#40;</span>run_time  <span style="color: #808080;">%</span> <span style="color: #000;">10000</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">86399.9964</span><span style="color: #808080;">&#41;</span>
      <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'1900-01-01'</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> last_execution_start 
<span style="color: #0000FF;">FROM</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobhistory</span> <span style="color: #0000FF;">AS</span> sjh
<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span> lastExecution     <span style="color: #0000FF;">AS</span> le
    <span style="color: #0000FF;">ON</span> sjh.<span style="color: #202020;">job_id</span> <span style="color: #808080;">=</span> le.<span style="color: #202020;">job_id</span>
   <span style="color: #808080;">AND</span> sjh.<span style="color: #202020;">instance_id</span> <span style="color: #808080;">=</span> le.<span style="color: #202020;">last_instance_id</span>
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> sjh.<span style="color: #202020;">job_id</span>;
&nbsp;
<span style="color: #008080;">/* We need to parse the schedule into something we can understand */</span>
<span style="color: #0000FF;">DECLARE</span> @weekDay <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#40;</span>
      mask          <span style="color: #0000FF;">INT</span>
    , maskValue     <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">32</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @weekDay
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #FF0000;">'Sunday'</span>      <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #FF0000;">'Monday'</span>      <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">4</span>, <span style="color: #FF0000;">'Tuesday'</span>     <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">8</span>, <span style="color: #FF0000;">'Wednesday'</span>   <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">16</span>, <span style="color: #FF0000;">'Thursday'</span>   <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">32</span>, <span style="color: #FF0000;">'Friday'</span>     <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">64</span>, <span style="color: #FF0000;">'Saturday'</span>;
&nbsp;
&nbsp;
<span style="color: #008080;">/* Now let's get our schedule information */</span>
<span style="color: #0000FF;">WITH</span> myCTE
<span style="color: #0000FF;">AS</span><span style="color: #808080;">&#40;</span>
    <span style="color: #0000FF;">SELECT</span> sched.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'scheduleName'</span>
        , sched.<span style="color: #202020;">schedule_id</span>
        , jobsched.<span style="color: #202020;">job_id</span>
        , <span style="color: #0000FF;">CASE</span> 
            <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
                <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Once'</span> 
            <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> 
                <span style="color: #808080;">AND</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
                    <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Daily'</span>
            <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> 
                <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Every '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_interval</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">5</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' days'</span>
            <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">THEN</span> 
                <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span> 
                    <span style="color: #0000FF;">SELECT</span> maskValue 
                    <span style="color: #0000FF;">FROM</span> @weekDay <span style="color: #0000FF;">AS</span> x 
                    <span style="color: #0000FF;">WHERE</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">&amp;</span> x.<span style="color: #202020;">mask</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
                    <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> mask <span style="color: #0000FF;">FOR</span> XML RAW<span style="color: #808080;">&#41;</span>
                , <span style="color: #FF0000;">'&quot;/&gt;&lt;row maskValue=&quot;'</span>, <span style="color: #FF0000;">', '</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'&lt;row maskValue=&quot;'</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'&quot;/&gt;'</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> 
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
                        <span style="color: #808080;">AND</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
                            <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'; weekly'</span> 
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
                        <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'; every '</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' weeks'</span> <span style="color: #0000FF;">END</span>
            <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">16</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'On day '</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_interval</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' of every '</span>
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' months'</span> 
            <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">32</span> <span style="color: #0000FF;">THEN</span> 
                <span style="color: #0000FF;">CASE</span> 
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'First'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">2</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Second'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Third'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Fourth'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">16</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Last'</span>
                <span style="color: #0000FF;">END</span> <span style="color: #808080;">+</span> 
                <span style="color: #0000FF;">CASE</span> 
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' Sunday'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">2</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' Monday'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">3</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' Tuesday'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' Wednesday'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">5</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' Thursday'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">6</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' Friday'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">7</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' Saturday'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' Day'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">9</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' Weekday'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">10</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' Weekend'</span>
                <span style="color: #0000FF;">END</span>
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> 
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
                        <span style="color: #808080;">AND</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
                            <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'; monthly'</span>
                    <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
                        <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'; every '</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' months'</span> 
                  <span style="color: #0000FF;">END</span>
            <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">64</span>   <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'StartUp'</span>
            <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">128</span>  <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Idle'</span>
          <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'frequency'</span>
        , IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Every '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_subday_interval</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> 
            <span style="color: #0000FF;">CASE</span> 
                <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_subday_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">2</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' seconds'</span>
                <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_subday_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' minutes'</span>
                <span style="color: #0000FF;">WHEN</span> sched.<span style="color: #202020;">freq_subday_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">' hours'</span>
            <span style="color: #0000FF;">END</span>, <span style="color: #FF0000;">'Once'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'subFrequency'</span>
        , <span style="color: #FF00FF;">REPLICATE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'0'</span>, <span style="color: #000;">6</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_start_time</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> 
            <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_start_time</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'startTime'</span>
        , <span style="color: #FF00FF;">REPLICATE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'0'</span>, <span style="color: #000;">6</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_end_time</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> 
            <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_end_time</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'endTime'</span>
        , <span style="color: #FF00FF;">REPLICATE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'0'</span>, <span style="color: #000;">6</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>jobsched.<span style="color: #202020;">next_run_time</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> 
            <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>jobsched.<span style="color: #202020;">next_run_time</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'nextRunTime'</span>
        , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>jobsched.<span style="color: #202020;">next_run_date</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'nextRunDate'</span>
    <span style="color: #0000FF;">FROM</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysschedules</span>      <span style="color: #0000FF;">AS</span> sched
    <span style="color: #808080;">JOIN</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobschedules</span>   <span style="color: #0000FF;">AS</span> jobsched
        <span style="color: #0000FF;">ON</span> sched.<span style="color: #202020;">schedule_id</span> <span style="color: #808080;">=</span> jobsched.<span style="color: #202020;">schedule_id</span>
    <span style="color: #0000FF;">WHERE</span> sched.<span style="color: #202020;">enabled</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #008080;">/* Finally, let's look at our actual jobs and tie it all together */</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF00FF;">SERVERPROPERTY</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Servername'</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>             <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>serverName<span style="color: #808080;">&#93;</span>
    , job.<span style="color: #202020;">job_id</span>                                                        <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>jobID<span style="color: #808080;">&#93;</span>
    , job.<span style="color: #202020;">name</span>                                                          <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>jobName<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> job.<span style="color: #202020;">enabled</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Enabled'</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'Disabled'</span> <span style="color: #0000FF;">END</span>      <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>jobStatus<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">scheduleName</span>, <span style="color: #FF0000;">'(unscheduled)'</span><span style="color: #808080;">&#41;</span>                     <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>scheduleName<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">frequency</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>                                     <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>frequency<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">subFrequency</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>                                  <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>subFrequency<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">startTime</span>, <span style="color: #000;">1</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">':'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">startTime</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' - '</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">endTime</span>, <span style="color: #000;">1</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">':'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">endTime</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>                           <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>scheduleTime<span style="color: #808080;">&#93;</span> <span style="color: #008080;">-- HH:MM</span>
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunDate</span>, <span style="color: #000;">1</span>, <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'/'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunDate</span>, <span style="color: #000;">5</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'/'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunDate</span>, <span style="color: #000;">7</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' '</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunTime</span>, <span style="color: #000;">1</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">':'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunTime</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>                       <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>nextRunDate<span style="color: #808080;">&#93;</span>
      <span style="color: #008080;">/* Note: the sysjobschedules table refreshes every 20 min, so nextRunDate may be out of date */</span>
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>jh.<span style="color: #202020;">success</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>                                           <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>success<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>jh.<span style="color: #202020;">cancel</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>                                            <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>cancel<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>jh.<span style="color: #202020;">fail</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>                                              <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>fail<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>jh.<span style="color: #202020;">retry</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>                                             <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>retry<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>jh.<span style="color: #202020;">last_execution_id</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>                                 <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>lastExecutionID<span style="color: #808080;">&#93;</span>
    , jh.<span style="color: #202020;">last_execution_start</span>                                           <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>lastExecutionStart<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">COALESCE</span><span style="color: #808080;">&#40;</span>jh.<span style="color: #202020;">last_duration</span>, <span style="color: #FF0000;">'00:00:01'</span><span style="color: #808080;">&#41;</span>                            <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>lastDuration<span style="color: #808080;">&#93;</span>
    , <span style="color: #FF0000;">'EXECUTE msdb.dbo.sp_update_job @job_id = '</span><span style="color: #FF0000;">''</span> 
        <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>job.<span style="color: #202020;">job_id</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">36</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', @enabled = 0;'</span>            <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>disableSQLScript<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobs</span>               <span style="color: #0000FF;">AS</span> job
<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span> myCTE                     <span style="color: #0000FF;">AS</span> sched
    <span style="color: #0000FF;">ON</span> job.<span style="color: #202020;">job_id</span> <span style="color: #808080;">=</span> sched.<span style="color: #202020;">job_id</span>
<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span> @jobHistory               <span style="color: #0000FF;">AS</span> jh
    <span style="color: #0000FF;">ON</span> job.<span style="color: #202020;">job_id</span> <span style="color: #808080;">=</span> jh.<span style="color: #202020;">job_id</span>
<span style="color: #0000FF;">WHERE</span> job.<span style="color: #202020;">enabled</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #008080;">-- do not display disabled jobs</span>
    <span style="color: #008080;">--AND jh.last_execution_start &gt;= DATEADD(day, -1, GETDATE()) /* Pull just the last 24 hours */</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> nextRunDate;</pre></td></tr></table></div>

<p><strong>Example Output:</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">serverName       jobID                                 jobName                      jobStatus scheduleName                      frequency  subFrequency   scheduleTime   nextRunDate       success  cancel  fail  retry  lastExecutionID  lastExecutionStart       lastDuration  disableSQLScript
---------------- ------------------------------------- ---------------------------- --------- --------------------------------- ---------- -------------- -------------- ----------------- -------- ------- ----- ------ ---------------- ------------------------ ------------- ----------------------------------------------------------------------------------------------
SQLFOOL\SQL2012  180BFD8E-AE0C-44F8-992E-27522611992A  DW FactOrders Update         Enabled   Every 4 Hours                     Daily      Every 4 hours  00:00 - 23:59  2012/09/19 20:00  12       0       0     0      84               2012-09-17 14:00:01.000  00:00:04      EXECUTE msdb.dbo.sp_update_job @job_id = '180BFD8E-AE0C-44F8-992E-27522611992A', @enabled = 0;
SQLFOOL\SQL2012  3470C9E5-A2CD-454A-89A1-DEF55FF186D3  SSIS Server Maintenance Job  Enabled   SSISDB Scheduler                  Daily      Once           00:00 - 12:00  2012/09/20 00:00  3        0       0     0      68               2012-09-17 11:50:51.000  00:00:01      EXECUTE msdb.dbo.sp_update_job @job_id = '3470C9E5-A2CD-454A-89A1-DEF55FF186D3', @enabled = 0;
SQLFOOL\SQL2012  F965B24D-60EB-4B95-91B6-C7D66057A883  syspolicy_purge_history      Enabled   syspolicy_purge_history_schedule  Daily      Once           02:00 - 23:59  2012/09/20 02:00  3        0       1     1      70               2012-09-17 11:50:51.000  00:01:24      EXECUTE msdb.dbo.sp_update_job @job_id = 'F965B24D-60EB-4B95-91B6-C7D66057A883', @enabled = 0;</pre></td></tr></table></div>

<p>The post <a href="http://sqlfool.com/2012/09/sql-agent-job-script-2/">SQL Agent Job Script</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p><img src="http://feeds.feedburner.com/~r/SqlFool/~4/VciuDPqS8Y0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2012/09/sql-agent-job-script-2/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		<feedburner:origLink>http://sqlfool.com/2012/09/sql-agent-job-script-2/</feedburner:origLink></item>
		<item>
		<title>SSIS Design Patterns</title>
		<link>http://feedproxy.google.com/~r/SqlFool/~3/pKwZ3JngpDI/</link>
		<comments>http://sqlfool.com/2012/09/ssis-design-patterns/#comments</comments>
		<pubDate>Wed, 05 Sep 2012 12:30:21 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Presentations]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[Business Intelligence]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[Metadata]]></category>
		<category><![CDATA[parallel data warehouse]]></category>
		<category><![CDATA[PASS]]></category>
		<category><![CDATA[pdw]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[publication]]></category>
		<category><![CDATA[SSIS]]></category>
		<category><![CDATA[SSIS Design Patterns]]></category>
		<category><![CDATA[Summit]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1713</guid>
		<description><![CDATA[<p>Have you ever been tasked with a project that you know has been done before, probably thousands of times, but you didn&#8217;t know where to start because it was new to you? If you&#8217;re nodding your head yes, you&#8217;re not alone. The fact is, we all have. Almost two years ago, Andy Leonard invited me to be part of a book project. He had a vision of compiling a &#8220;recipe book&#8221; of common tasks that nearly every ETL professional encounters at some point in his or her career. Whereas most technical books explain how to use the product or specific features, Andy wanted to create a pattern-oriented book that would help jumpstart an SSIS project. With some trepidation, I humbly agreed to join the outstanding group of authors Andy had assembled: Andy Leonard (Blog &#124; Twitter) Matt Masson (Blog&#124; Twitter) Tim Mitchell (Blog &#124; Twitter) Jessica Moss (Blog &#124; Twitter) I was excited about this project for three reasons. First, the incredible list of authors. These are the folks that I have learned SSIS from! To be included amongst their ranks is a deep honor for me. Secondly, I couldn&#8217;t keep track of the number of times such a book … <a href="http://sqlfool.com/2012/09/ssis-design-patterns/"> Continue reading <span class="meta-nav">&#8594; </span></a></p><p>The post <a href="http://sqlfool.com/2012/09/ssis-design-patterns/">SSIS Design Patterns</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>Have you ever been tasked with a project that you know has been done before, probably thousands of times, but you didn&#8217;t know where to start because it was <strong>new to you</strong>? If you&#8217;re nodding your head <em>yes</em>, you&#8217;re not alone. The fact is, we all have.</p>
<p>Almost two years ago, Andy Leonard invited me to be part of a book project. He had a vision of compiling a &#8220;recipe book&#8221; of common tasks that nearly every ETL professional encounters at some point in his or her career. Whereas most technical books explain how to use the product or specific features, Andy wanted to create a pattern-oriented book that would help jumpstart an SSIS project.</p>
<p><img class="alignright size-full wp-image-1741" title="book" src="http://sqlfool.com/wp-content/uploads/2012/09/book.jpg" alt="" width="35%" height="35%" />With some trepidation, I humbly agreed to join the outstanding group of authors Andy had assembled:</p>
<ul>
<li><a href="http://www.amazon.com/author/andyleonard" target="_blank">Andy Leonard</a> (<a href="http://sqlblog.com/blogs/andy_leonard" target="_blank">Blog</a> | <a href="http://twitter.com/AndyLeonard" target="_blank">Twitter</a>)</li>
<li><a href="http://www.amazon.com/Matt-Masson/e/B005X3ATR8/" target="_blank">Matt Masson</a> (<a href="http://www.mattmasson.com/" target="_blank">Blog</a>| <a href="http://twitter.com/mattmasson" target="_blank">Twitter</a>)</li>
<li><a href="http://www.amazon.com/Tim-Mitchell/e/B008AQ2EX2/" target="_blank">Tim Mitchell</a> (<a href="http://www.timmitchell.net" target="_blank">Blog</a> | <a href="http://twitter.com/Tim_Mitchell" target="_blank">Twitter</a>)</li>
<li>Jessica Moss (<a href="http://www.jessicammoss.com/" target="_blank">Blog</a> | <a href="http://twitter.com/jessicammoss" target="_blank">Twitter</a>)</li>
</ul>
<p>I was excited about this project for three reasons. First, the incredible list of authors. These are the folks that I have learned SSIS from! To be included amongst their ranks is a deep honor for me. Secondly, I couldn&#8217;t keep track of the number of times such a book would have been useful to me throughout my own career. I sincerely hope that this book can save people some of the headaches that I myself have experienced. Lastly, having a book published has been on my bucket list since before I knew about bucket lists. Now I can cross that off and focus on <a href="http://www.virgingalactic.com/booking/" target="_blank">traveling to outer space</a>. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>The book is aptly titled &#8220;SQL Server 2012 Integration Services Design Patterns&#8221; and is out today. My contributions are the Metadata Collection and Parallel Data Warehouse (PDW) chapters. You can purchase the book at <a href="http://www.amazon.com/Server-Integration-Services-Design-Patterns/dp/1430237716/" target="_blank">Amazon</a> or <a href="http://www.apress.com/9781430237716" target="_blank">directly from Apress</a>.</p>
<p>The whole gang and I will be presenting a <a href="http://www.sqlpass.org/summit/2012/Sessions/SessionDetails.aspx?sid=3159" target="_blank">precon at PASS Summit 2012</a> that will explore SSIS Design Patterns in detail. I will write another blog post once I have decided on the particulars of what I&#8217;ll be presenting on. In the meantime, suffice it to say if you work with or around SSIS, this will be a precon you won&#8217;t want to miss. </p>
<p>One last thing. I didn&#8217;t have much space for acknowledgements in the book, so I want to take the opportunity here to express my gratitude to the following individuals. In no particular order, I&#8217;d like to thank:</p>
<ul>
<li>Andy, Matt, Tim, and Jessica &#8211; for allowing me the privilege of writing alongside you</li>
<li>John Hoang &#8211; for your invaluable contributions to the PDW chapter</li>
<li>Brian Davis &#8211; for being such a great technical editor</li>
<li>Cindy Bradley &#8211; for giving me the opportunity to work on the POC and for always providing me with interesting and challenging work <img src='http://sqlfool.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </li>
<li>Jeff Moberley &#8211; for being such a great mentor &#038; for challenging me to &#8220;think outside the box&#8221;</li>
<li>Marie Bayer &#8211; for being so willing to help whenever I have SSIS questions</li>
<li>Murshed Zaman, Martin Lee, Ted Tasker, Brian Walker, Jesse Fountain, &amp; Bruce Campbell &#8211; without your assistance, there would not have been a PDW chapter in the first place</li>
<li>Chris Leonard, Jimmy May, and Brent Ozar &#8211; for encouraging me to get involved in the SQL Server community; without you guys, I would not have had this opportunity</li>
<li>Aaron Bertrand &#8211; for creating sp_foreachdb, which I use ridiculously often</li>
<li>Mark Powers, Jonathan Gennick, and the rest of the wonderful Apress crew &#8211; for putting this book together</li>
</ul>
<p>My apologies to anyone I may have missed. </p>
<p>The post <a href="http://sqlfool.com/2012/09/ssis-design-patterns/">SSIS Design Patterns</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p><img src="http://feeds.feedburner.com/~r/SqlFool/~4/pKwZ3JngpDI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2012/09/ssis-design-patterns/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://sqlfool.com/2012/09/ssis-design-patterns/</feedburner:origLink></item>
		<item>
		<title>The SQL Server DBA’s Guide to Teradata</title>
		<link>http://feedproxy.google.com/~r/SqlFool/~3/EHjyOAo74GA/</link>
		<comments>http://sqlfool.com/2012/08/mssql-dba-guide-to-teradata/#comments</comments>
		<pubDate>Thu, 09 Aug 2012 16:19:40 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[Teradata]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1657</guid>
		<description><![CDATA[<p>6 months ago, I returned from maternity leave only to be handed the proverbial keys to a new database kingdom: Teradata. Being a long-time devotee of all things Microsoft SQL Server, I had quite a bit of learning to do to get up to speed on this new environment. In an effort to save others from the same headaches that plagued me the first few months, I have created a new guide to aid those new to the Teradata platform. I will primarily focus on what I have found most lacking: examples of how to convert SQL Server T-SQL to Teradata SQL. Rather than create a series of posts with random tidbits, I thought it would make more sense to create a single page for this guide and update it incrementally. You can find the very beginnings of this guide here: The SQL Server DBA&#8217;s Guide to Teradata. Good luck, and happy converting.</p><p>The post <a href="http://sqlfool.com/2012/08/mssql-dba-guide-to-teradata/">The SQL Server DBA&#8217;s Guide to Teradata</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>6 months ago, I returned from maternity leave only to be handed the proverbial keys to a new database kingdom: Teradata. Being a long-time devotee of all things Microsoft SQL Server, I had quite a bit of learning to do to get up to speed on this new environment. In an effort to save others from the same headaches that plagued me the first few months, I have created a new guide to aid those new to the Teradata platform. I will primarily focus on what I have found most lacking: examples of how to convert SQL Server T-SQL to Teradata SQL. Rather than create a series of posts with random tidbits, I thought it would make more sense to create a single page for this guide and update it incrementally. You can find the very beginnings of this guide here: <a href="http://sqlfool.com/mssql-dba-guide-to-teradata/">The SQL Server DBA&#8217;s Guide to Teradata</a>. Good luck, and happy converting. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The post <a href="http://sqlfool.com/2012/08/mssql-dba-guide-to-teradata/">The SQL Server DBA&#8217;s Guide to Teradata</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p><img src="http://feeds.feedburner.com/~r/SqlFool/~4/EHjyOAo74GA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2012/08/mssql-dba-guide-to-teradata/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://sqlfool.com/2012/08/mssql-dba-guide-to-teradata/</feedburner:origLink></item>
		<item>
		<title>TVP Permissions</title>
		<link>http://feedproxy.google.com/~r/SqlFool/~3/uvJZAp01FMs/</link>
		<comments>http://sqlfool.com/2012/06/tvp-permissions/#comments</comments>
		<pubDate>Fri, 08 Jun 2012 18:56:42 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[bulk operations]]></category>
		<category><![CDATA[Permissions]]></category>
		<category><![CDATA[syntax error]]></category>
		<category><![CDATA[table-valued parameters]]></category>
		<category><![CDATA[TVP]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1641</guid>
		<description><![CDATA[<p>Here&#8217;s just a short post to feed the search engine gerbils. So today, I created a stored procedure that accepted a list of ID&#8217;s and filtered them. The most efficient way to handle this was using table-valued parameters (TVPs). My code looks similiar to this: USE AdventureWorks2012; GO &#160; /* Create our new table type */ IF NOT EXISTS&#40;SELECT * FROM sys.types WHERE name = 'CustomerList'&#41; BEGIN CREATE TYPE dbo.CustomerList AS TABLE &#40; CustomerID INT , ID INT &#41;; END; &#160; IF OBJECTPROPERTY&#40;OBJECT_ID&#40;'dbo.CustomerTerritoryFilterGet_sp'&#41; , N'IsProcedure'&#41; IS NOT NULL DROP PROCEDURE dbo.CustomerTerritoryFilterGet_sp; GO &#160; /* Create a proc to test with */ CREATE PROCEDURE dbo.CustomerTerritoryFilterGet_sp @Customers CustomerList READONLY , @TerritoryFilter INT &#160; AS BEGIN &#160; SELECT DISTINCT sc.CustomerID, c.ID FROM Sales.Customer AS sc JOIN @Customers AS c ON sc.CustomerID = c.CustomerID WHERE sc.TerritoryID = @TerritoryFilter; &#160; RETURN 0; END GO &#160; /* Test our stored procedure */ DECLARE @myList CustomerList; INSERT INTO @myList VALUES &#40;1, 10&#41;, &#40;2, 20&#41;, &#40;3, 30&#41;, &#40;4, 40&#41;, &#40;5, 50&#41;, &#40;6, 60&#41;, &#40;7, 70&#41;; &#160; EXECUTE dbo.CustomerTerritoryFilterGet_sp @Customers = @myList , @TerritoryFilter = 1; GO &#160; GRANT EXECUTE ON dbo.CustomerTerritoryFilterGet_sp To myApp; GO This would typically be sufficient for most stored procedures. However, my app dev was … <a href="http://sqlfool.com/2012/06/tvp-permissions/"> Continue reading <span class="meta-nav">&#8594; </span></a></p><p>The post <a href="http://sqlfool.com/2012/06/tvp-permissions/">TVP Permissions</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>Here&#8217;s just a short post to feed the search engine gerbils. So today, I created a stored procedure that accepted a list of ID&#8217;s and filtered them. The most efficient way to handle this was using table-valued parameters (TVPs). My code looks similiar to this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">USE</span> AdventureWorks2012;
GO
&nbsp;
<span style="color: #008080;">/* Create our new table type */</span>
<span style="color: #0000FF;">IF</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">EXISTS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">types</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">=</span> <span style="color: #FF0000;">'CustomerList'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
    <span style="color: #0000FF;">CREATE</span> TYPE dbo.<span style="color: #202020;">CustomerList</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">TABLE</span> 
    <span style="color: #808080;">&#40;</span> 
          CustomerID <span style="color: #0000FF;">INT</span>
        , ID         <span style="color: #0000FF;">INT</span>
    <span style="color: #808080;">&#41;</span>;
<span style="color: #0000FF;">END</span>;
&nbsp;
<span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECTPROPERTY</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'dbo.CustomerTerritoryFilterGet_sp'</span><span style="color: #808080;">&#41;</span>
, N<span style="color: #FF0000;">'IsProcedure'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">PROCEDURE</span> dbo.<span style="color: #202020;">CustomerTerritoryFilterGet_sp</span>;
GO
&nbsp;
<span style="color: #008080;">/* Create a proc to test with */</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> dbo.<span style="color: #202020;">CustomerTerritoryFilterGet_sp</span>
      @Customers        CustomerList READONLY
    , @TerritoryFilter  <span style="color: #0000FF;">INT</span>
&nbsp;
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
    <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> sc.<span style="color: #202020;">CustomerID</span>, c.<span style="color: #202020;">ID</span>
    <span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">Customer</span> <span style="color: #0000FF;">AS</span> sc
    <span style="color: #808080;">JOIN</span> @Customers <span style="color: #0000FF;">AS</span> c
        <span style="color: #0000FF;">ON</span> sc.<span style="color: #202020;">CustomerID</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">CustomerID</span>
    <span style="color: #0000FF;">WHERE</span> sc.<span style="color: #202020;">TerritoryID</span> <span style="color: #808080;">=</span> @TerritoryFilter;
&nbsp;
    <span style="color: #0000FF;">RETURN</span> <span style="color: #000;">0</span>;
<span style="color: #0000FF;">END</span>
GO
&nbsp;
<span style="color: #008080;">/* Test our stored procedure */</span>
<span style="color: #0000FF;">DECLARE</span> @myList CustomerList;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @myList 
<span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">2</span>, <span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">3</span>, <span style="color: #000;">30</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>, <span style="color: #000;">40</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">5</span>, <span style="color: #000;">50</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">6</span>, <span style="color: #000;">60</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">7</span>, <span style="color: #000;">70</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">EXECUTE</span> dbo.<span style="color: #202020;">CustomerTerritoryFilterGet_sp</span>
      @Customers        <span style="color: #808080;">=</span> @myList
    , @TerritoryFilter  <span style="color: #808080;">=</span> <span style="color: #000;">1</span>;
GO
&nbsp;
<span style="color: #0000FF;">GRANT</span> <span style="color: #0000FF;">EXECUTE</span> <span style="color: #0000FF;">ON</span> dbo.<span style="color: #202020;">CustomerTerritoryFilterGet_sp</span> <span style="color: #0000FF;">To</span> myApp;
GO</pre></td></tr></table></div>

<p>This would typically be sufficient for most stored procedures. However, my app dev was getting the following error:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">The EXECUTE permission was denied on the object 'customerList', database 'AdventureWorks2012', schema 'dbo'.</pre></td></tr></table></div>

<p>Wait, we need to explicitly grant permissions to the new data type? Okay, that&#8217;s easy enough&#8230; let&#8217;s just run a quick GRANT statement:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">GRANT</span> <span style="color: #0000FF;">EXECUTE</span> <span style="color: #0000FF;">ON</span> dbo.<span style="color: #202020;">CustomerList</span> <span style="color: #0000FF;">To</span> myApp;</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">Msg 15151, Level 16, State 1, Line 1
Cannot find the object 'CustomerList', because it does not exist or you do not have permission.</pre></td></tr></table></div>

<p>Okay, now I&#8217;m confused&#8230; what am I missing? It turns out, you need to use a slightly different syntax to grant permissions to a new data type. This syntax, which explicitly tells SQL Server that we&#8217;re granting permissions on a Type class, works just fine:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">GRANT</span> <span style="color: #0000FF;">EXECUTE</span> <span style="color: #0000FF;">ON</span> TYPE::dbo.<span style="color: #202020;">CustomerList</span> <span style="color: #0000FF;">To</span> myApp;
&nbsp;
<span style="color: #008080;">/* Time to clean up! */</span>
<span style="color: #008080;">-- DROP PROC dbo.CustomerTerritoryFilterGet_sp;</span>
<span style="color: #008080;">-- DROP TYPE dbo.CustomerList;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">Command(s) completed successfully.</pre></td></tr></table></div>

<p>You can find the full syntax here on Books Online:<br />
<a href="http://msdn.microsoft.com/en-US/library/ms174346(v=SQL.90).aspx" target="_blank">http://msdn.microsoft.com/en-US/library/ms174346(v=SQL.90).aspx</a></p>
<p>The post <a href="http://sqlfool.com/2012/06/tvp-permissions/">TVP Permissions</a> appeared first on <a href="http://sqlfool.com">SQL Fool</a>.</p><img src="http://feeds.feedburner.com/~r/SqlFool/~4/uvJZAp01FMs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2012/06/tvp-permissions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://sqlfool.com/2012/06/tvp-permissions/</feedburner:origLink></item>
	</channel>
</rss>
