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

<channel>
	<title>tim laqua dot com &#187; sql</title>
	<atom:link href="http://timlaqua.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://timlaqua.com</link>
	<description>Thoughts and Code from Tim Laqua</description>
	<lastBuildDate>Fri, 16 Mar 2012 16:48:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Uncivil UNIONs</title>
		<link>http://timlaqua.com/2012/02/uncivil-unions/</link>
		<comments>http://timlaqua.com/2012/02/uncivil-unions/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 16:12:43 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=878</guid>
		<description><![CDATA[If you asked any SQL Server Developer or DBA what UNION does, they would tell you it combines two result sets. Then if you were to ask them if they knew it removed duplicates without the ALL argument, they would say yes. That's academic, but I dare you to go look at their and your [...]]]></description>
			<content:encoded><![CDATA[<p>If you asked any SQL Server Developer or DBA what UNION does, they would tell you it combines two result sets.  Then if you were to ask them if they knew it removed duplicates without the ALL argument, they would say yes.  That's academic, but I dare you to go look at their and your code and see how often UNION without the ALL argument is used.  UNION is evil.  Why do I say that?  Because DISTINCT is evil and blocking operations are evil so it follows that UNION is evil.</p>
<p>Would you be OK with the following query as the source for a fact table?</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span>
<span style="color: #808080;">&#40;</span>
  <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> <span style="color: #808080;">&#91;</span>c1<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>c2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>t1<span style="color: #808080;">&#93;</span>
  <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
  <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> <span style="color: #808080;">&#91;</span>c1<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>c2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>t2<span style="color: #808080;">&#93;</span>
<span style="color: #808080;">&#41;</span> a</pre></div></div>

<p>No?  Then why are you OK with this?</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span>c1<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>c2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>t1<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">UNION</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span>c1<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>c2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>t2<span style="color: #808080;">&#93;</span></pre></div></div>

<p>Personally, I tend to use UNION to combine data from two different sources.  For example, if I wanted to combine the sales transactions from two JDE instances - the query might look like:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span>c1<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>c2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>JDE1<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>F42119<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">UNION</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span>c1<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>c2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>JDE2<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>F42119<span style="color: #808080;">&#93;</span></pre></div></div>

<p>Each line here in a sales detail represents a line on an order, so assuming you selected all the PK columns in your query, a UNION won't remove any rows within either set, but when combined - it's possible the same PK could exist in both systems.  Now you could split hairs and say it's highly unlikely the rest of the row would be identical, but that's not the point - it's wrong to ever DISTINCT a set of transactions intended for creating a fact table.  Sure, in this particular case you could add a "SourceSystem" column that would stop that, but then you still have to accept that the UNION is blocking and expensive.  Just because a DISTINCT doesn't modify results doesn't mean it didn't do any work - it actually did a great deal of work.  Now consider that we're usually not selecting 2 columns, we're selecting 10-20. Do you want to wait for it to figure out all the rows are distinct when you already knew that?  Further, if they're not distinct you probably screwed up a JOIN somewhere.</p>
<p>As far as coding standards regarding UNION go, I would suggest never using UNION without the ALL clause and doing your DISTINCT elsewhere if that's actually what you intended.  Does that seem silly?  Yes - but I would wager you won't ever use the 2nd one outside of building Dimensions.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span>c1<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>c2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>JDE1<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>F42119<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span>c1<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>c2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>JDE2<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>F42119<span style="color: #808080;">&#93;</span>
&nbsp;
<span style="color: #008080;">--OR</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span>
<span style="color: #808080;">&#40;</span>
  <span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span>c1<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>c2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>JDE1<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>F42119<span style="color: #808080;">&#93;</span>
  <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
  <span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span>c1<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>c2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>JDE2<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>F42119<span style="color: #808080;">&#93;</span>
<span style="color: #808080;">&#41;</span></pre></div></div>

<p>And don't get me started on unioning subqueries with GROUP BY's - Why don't you just throw half your processers in the garbage, light tempdb on fire and put all your data in heaps.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2012/02/uncivil-unions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>SQL Schema Source Control 2.0</title>
		<link>http://timlaqua.com/2011/04/sql-schema-source-control-2-0/</link>
		<comments>http://timlaqua.com/2011/04/sql-schema-source-control-2-0/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 11:30:26 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=500</guid>
		<description><![CDATA[SQL Schema Source Control is, at it's core, just another database scripting app. Where this app differs from others is that the sole purpose of scripting out the database is to check it in to source control (sorry, it still only supports SVN). I think this may have once gone under the name "SQL2SVN," but [...]]]></description>
			<content:encoded><![CDATA[<p>SQL Schema Source Control is, at it's core, just another database scripting app.  Where this app differs from others is that the sole purpose of scripting out the database is to check it in to source control (sorry, it still only supports SVN).</p>
<p>I think this may have once gone under the name "SQL2SVN," but was rebranded before being checked in to Codeplex for the first time.  This is a re-write of most of the application to be more modular and extensible in the future.  Additional features found in this release are:<span id="more-500"></span></p>
<ul>
<li>Scripting of Configuration settings via <strong>sp_configure</strong></li>
<li>Scripting of Logins</li>
<li>Scripting of Indexes</li>
<li>Scripting of Users</li>
<li>Scripting of Views</li>
<li>Scripting of Keys (Primary and Foreign)</li>
<li>Scripting of Constraints</li>
<li>Scripting of Defaults</li>
<li>Scripting of Stored Procedures</li>
<li>Scripting of Functions</li>
<li>Scripting of Partition Schemes</li>
<li>Scripting of Partition Functions</li>
<li>Regular Expression support for database names, allows you to script all databases excluding system databases... or including them... whatever</li>
</ul>
<p>The application and source code are available on Codeplex: <a href="http://sqlschemasourcectrl.codeplex.com/">SQL Schema Source Control</a></p>
<p><a href="http://timlaqua.com/wp-content/uploads/2011/04/sssc2.jpg"><img src="http://timlaqua.com/wp-content/uploads/2011/04/sssc2.jpg" alt="" title="sssc2" width="727" height="399" class="alignleft size-full wp-image-524" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2011/04/sql-schema-source-control-2-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monitoring Failed Report Server Subscriptions</title>
		<link>http://timlaqua.com/2011/04/monitoring-failed-report-server-subscriptions/</link>
		<comments>http://timlaqua.com/2011/04/monitoring-failed-report-server-subscriptions/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 11:30:51 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[ssrs]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=465</guid>
		<description><![CDATA[While we love to empower our users as much as possible, we still need to pay attention to them and what they're up to. The first place I usually see users running amok as Microsoft BI adoption grows in organization is the Report Subscriptions in SSRS. They go nuts with them. I was at one [...]]]></description>
			<content:encoded><![CDATA[<p>While we love to empower our users as much as possible, we still need to pay attention to them and what they're up to.  The first place I usually see users running amok as Microsoft BI adoption grows in organization is the Report Subscriptions in SSRS.  They go nuts with them.  I was at one company where 50% of the report subscriptions went to users outside of the company - that means our customers were depending on these subscriptions!  Which brings up an interesting licensing debacle...  but that's another story.  Needless to say, when people set up a subscription, they expect it to work.  If it doesn't, we REALLY need to let someone know.</p>
<p>Here, we have a procedure to monitor for Reporting Services subscription failures.  Specifically, email subscription failures.  When a failed subscription is detected, an email is sent to both the subscription owner and a digest of failures is sent to the specified admin group.<span id="more-465"></span></p>
<p>This procedure has saved me so many times it's not even funny.  The last thing you want is your end users knowing about a failure before you.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/************************************************************************
***
*** Procedure:       Alert_FailedReportingServicesSubscriptions
*** Purpose:         Notifies administrators and subscription owners that
***                  subscriptions were not delivered as expected
***      
*** Author:          tl
*** Date Created:    2009-10-28
*** 
*** Revision History
*** Date        Author   Description
*** 2009-10-28  tl       Created
*** 
************************************************************************/</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Alert_FailedReportingServicesSubscriptions<span style="color: #808080;">&#93;</span>
   @ReportServerBaseURL <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span>
  ,@AdminEmail <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
  ,@FromAddress <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
  ,@MailDomain <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
  ,@NTDomain <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
  ,@ReportServerDatabase <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'ReportServer'</span>
  ,@ReportServerSchema <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'dbo'</span>
  ,@HoursToCheck <span style="color: #0000FF;">INT</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
  ,@HourSchedule <span style="color: #0000FF;">INT</span> <span style="color: #808080;">=</span> <span style="color: #000;">16777215</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
  <span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;
&nbsp;
  <span style="color: #0000FF;">IF</span> @HourSchedule <span style="color: #808080;">&amp;</span> <span style="color: #FF00FF;">POWER</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span>, <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">HOUR</span>, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span>
  <span style="color: #0000FF;">BEGIN</span>
    <span style="color: #0000FF;">DECLARE</span> @ReportTableOpen <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span>
    <span style="color: #FF0000;">'&lt;style&gt;
    table { font-family: Calibri, Verdana, Ariel, sans-serif;  font-size: 90%; }
    th { padding-left: 3pt; font-style: italic; text-align: left; border-bottom: 1px solid black;}
    td { padding-left: 3pt; white-space: nowrap; }
    p { font-family: Calibri, Verdana, Ariel, sans-serif;  font-size: 95%; text-indent: 0pt; margin-bottom: 12pt; }
    &lt;/style&gt;
    &lt;p&gt;The following subscription(s) that you created was/were not sent to specified recipients at the date/time you scheduled.  Database Administrators has been notified.  Please reply to this email if you have any questions.&lt;/p&gt;
    &lt;table&gt;
    &lt;tr&gt;&lt;th&gt;Owner&lt;/th&gt;&lt;th&gt;Report&lt;/th&gt;&lt;th&gt;Last Run&lt;/th&gt;&lt;th&gt;Intended Recipients&lt;/th&gt;&lt;th&gt;Status&lt;/th&gt;&lt;/tr&gt;'</span>
&nbsp;
    <span style="color: #0000FF;">DECLARE</span> @ReportTableClose <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'&lt;/table&gt;'</span>
&nbsp;
    <span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #FailedReport
      <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
      ,<span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
      ,<span style="color: #808080;">&#91;</span>Link<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2048</span><span style="color: #808080;">&#41;</span>
      ,<span style="color: #808080;">&#91;</span>LastRunTime<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">DATETIME</span>
      ,<span style="color: #808080;">&#91;</span>OwnerUsername<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
      ,<span style="color: #808080;">&#91;</span>IntendedRecipients<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span>
      ,<span style="color: #808080;">&#91;</span>Message<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #0000FF;">DECLARE</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'
    INSERT INTO #FailedReport
      ([Name]
      ,[Link]
      ,[LastRunTime]
      ,[OwnerUsername]
      ,[IntendedRecipients]
      ,[Message])
    SELECT
       b.Name
      ,'</span><span style="color: #FF0000;">'&lt;a href=&quot;'</span> <span style="color: #808080;">+</span> @ReportServerBaseURL <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">' + b.Path + '</span><span style="color: #FF0000;">'&quot;&gt;'</span><span style="color: #FF0000;">' + b.Name + '</span><span style="color: #FF0000;">'&lt;/a&gt;'</span><span style="color: #FF0000;">' AS [Link]
      ,LastRunTime 
      ,REPLACE(c.UserName, '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> @NTDomain <span style="color: #808080;">+</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">92</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">') AS [OwnerUserName]
      ,REPLACE(CAST(ExtensionSettings AS XML).value
        ('</span><span style="color: #FF0000;">'(//ParameterValue[Name=&quot;TO&quot;]/Value)[1]'</span><span style="color: #FF0000;">'
        ,'</span><span style="color: #FF0000;">'VARCHAR(1024)'</span><span style="color: #FF0000;">')
      + '</span><span style="color: #FF0000;">';'</span><span style="color: #FF0000;">' 
      + ISNULL(CAST(ExtensionSettings AS XML).value
        ('</span><span style="color: #FF0000;">'(//ParameterValue[Name=&quot;CC&quot;]/Value)[1]'</span><span style="color: #FF0000;">'
        ,'</span><span style="color: #FF0000;">'VARCHAR(1024)'</span><span style="color: #FF0000;">'), '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">'),'</span><span style="color: #FF0000;">';;'</span><span style="color: #FF0000;">','</span><span style="color: #FF0000;">';'</span><span style="color: #FF0000;">') AS [IntendedRecipients]
      ,LEFT(ISNULL(LastStatus, '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">'), 1024) AS [Message]
    FROM '</span> <span style="color: #808080;">+</span> @ReportServerDatabase <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.'</span> <span style="color: #808080;">+</span> @ReportServerSchema <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.Subscriptions a
      INNER JOIN '</span> <span style="color: #808080;">+</span> @ReportServerDatabase <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.'</span> <span style="color: #808080;">+</span> @ReportServerSchema <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.Catalog b WITH(NOLOCK) 
        ON a.Report_OID=b.ItemId
      INNER JOIN '</span> <span style="color: #808080;">+</span> @ReportServerDatabase <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.'</span> <span style="color: #808080;">+</span> @ReportServerSchema <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.Users c WITH(NOLOCK)
        ON a.OwnerID = c.UserID
    WHERE 
      a.DeliveryExtension = '</span><span style="color: #FF0000;">'Report Server Email'</span><span style="color: #FF0000;">'
      AND LastStatus NOT LIKE '</span><span style="color: #FF0000;">'Mail Sent%'</span><span style="color: #FF0000;">' 
      AND LastStatus NOT LIKE '</span><span style="color: #FF0000;">'% 0 errors.'</span><span style="color: #FF0000;">'
      AND LastStatus NOT LIKE '</span><span style="color: #FF0000;">'New Sub%'</span><span style="color: #FF0000;">'
      AND LastStatus NOT LIKE '</span><span style="color: #FF0000;">'Pending%'</span><span style="color: #FF0000;">'
      AND LastRunTime &gt;= DATEADD(HOUR, -1 * '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@HoursToCheck <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;">', GETDATE())
    ORDER BY REPLACE(c.UserName, '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> @NTDomain <span style="color: #808080;">+</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">92</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">'), b.Name'</span>
&nbsp;
    <span style="color: #0000FF;">EXECUTE</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">SQL</span><span style="color: #808080;">&#41;</span>
&nbsp;
    <span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> #FailedReport<span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span>
    <span style="color: #0000FF;">BEGIN</span>
      <span style="color: #0000FF;">DECLARE</span> 
         @FullReportTable <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">''</span>
        ,@LastOwnerId <span style="color: #0000FF;">INT</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
        ,@Username <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
&nbsp;
      <span style="color: #0000FF;">DECLARE</span> @Owner <span style="color: #0000FF;">TABLE</span>
          <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>OwnerId<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
          ,<span style="color: #808080;">&#91;</span>Username<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
&nbsp;
      <span style="color: #0000FF;">DECLARE</span> @Notification <span style="color: #0000FF;">TABLE</span>
        <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>NotificationId<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
        ,<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TO</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
        ,<span style="color: #808080;">&#91;</span>Body<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</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> @Owner
        <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Username<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
      <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> OwnerUsername
      <span style="color: #0000FF;">FROM</span> #FailedReport
      <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> OwnerUsername
&nbsp;
      <span style="color: #0000FF;">DECLARE</span> @UserFailedReport <span style="color: #0000FF;">TABLE</span>
        <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
        ,<span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
        ,<span style="color: #808080;">&#91;</span>Link<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2048</span><span style="color: #808080;">&#41;</span>
        ,<span style="color: #808080;">&#91;</span>LastRunTime<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">DATETIME</span>
        ,<span style="color: #808080;">&#91;</span>OwnerUsername<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
        ,<span style="color: #808080;">&#91;</span>IntendedRecipients<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span>
        ,<span style="color: #808080;">&#91;</span>Message<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
&nbsp;
      <span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">EXISTS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">FROM</span> @Owner <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>OwnerId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&gt;</span> @LastOwnerId<span style="color: #808080;">&#41;</span>
      <span style="color: #0000FF;">BEGIN</span>
        <span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> @UserFailedReport
&nbsp;
        <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> 
           @Username <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Username<span style="color: #808080;">&#93;</span>
          ,@LastOwnerId <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>OwnerId<span style="color: #808080;">&#93;</span>
        <span style="color: #0000FF;">FROM</span> @Owner 
        <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>OwnerId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&gt;</span> @LastOwnerId
        <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #808080;">&#91;</span>OwnerId<span style="color: #808080;">&#93;</span>
&nbsp;
        <span style="color: #0000FF;">DECLARE</span> 
           @UserReportTable <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">''</span>
          ,@LastId <span style="color: #0000FF;">INT</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
&nbsp;
        <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @UserFailedReport
          <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span>
          ,<span style="color: #808080;">&#91;</span>Link<span style="color: #808080;">&#93;</span>
          ,<span style="color: #808080;">&#91;</span>LastRunTime<span style="color: #808080;">&#93;</span>
          ,<span style="color: #808080;">&#91;</span>OwnerUsername<span style="color: #808080;">&#93;</span>
          ,<span style="color: #808080;">&#91;</span>IntendedRecipients<span style="color: #808080;">&#93;</span>
          ,<span style="color: #808080;">&#91;</span>Message<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">SELECT</span>
           <span style="color: #808080;">&#91;</span>Name<span style="color: #808080;">&#93;</span>
          ,<span style="color: #808080;">&#91;</span>Link<span style="color: #808080;">&#93;</span>
          ,<span style="color: #808080;">&#91;</span>LastRunTime<span style="color: #808080;">&#93;</span>
          ,<span style="color: #808080;">&#91;</span>OwnerUserName<span style="color: #808080;">&#93;</span>
          ,<span style="color: #808080;">&#91;</span>IntendedRecipients<span style="color: #808080;">&#93;</span>
          ,<span style="color: #808080;">&#91;</span>Message<span style="color: #808080;">&#93;</span>
        <span style="color: #0000FF;">FROM</span> #FailedReport
        <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>OwnerUsername<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> @Username
&nbsp;
        <span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">EXISTS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">FROM</span> @UserFailedReport <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&gt;</span> @LastId<span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">BEGIN</span>
&nbsp;
          <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span>
             @UserReportTable <span style="color: #808080;">+=</span> <span style="color: #FF0000;">'&lt;tr&gt;&lt;td&gt;'</span> <span style="color: #808080;">+</span> IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'&lt;a href=&quot;mailto:'</span> <span style="color: #808080;">+</span> <span style="color: #808080;">&#91;</span>OwnerUsername<span style="color: #808080;">&#93;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'@'</span> <span style="color: #808080;">+</span> @MailDomain <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&quot;&gt;'</span> <span style="color: #808080;">+</span> <span style="color: #808080;">&#91;</span>OwnerUsername<span style="color: #808080;">&#93;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&lt;/a&gt;'</span>, <span style="color: #FF0000;">'Unknown'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&lt;/td&gt;&lt;td&gt;'</span> <span style="color: #808080;">+</span> IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Link<span style="color: #808080;">&#93;</span>, <span style="color: #FF0000;">'Unavailable'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&lt;/td&gt;&lt;td&gt;'</span> <span style="color: #808080;">+</span> IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">VARCHAR</span>, <span style="color: #808080;">&#91;</span>LastRunTime<span style="color: #808080;">&#93;</span>, <span style="color: #000;">120</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'Never'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&lt;/td&gt;&lt;td&gt;'</span> <span style="color: #808080;">+</span> IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>IntendedRecipients<span style="color: #808080;">&#93;</span>, <span style="color: #FF0000;">'Unknown'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&lt;/td&gt;&lt;td&gt;'</span> <span style="color: #808080;">+</span> IS<span style="color: #808080;">NULL</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Message<span style="color: #808080;">&#93;</span>,<span style="color: #FF0000;">'Unknown'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&lt;/td&gt;&lt;/tr&gt;'</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">13</span><span style="color: #808080;">&#41;</span>
            ,@LastId <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span>
          <span style="color: #0000FF;">FROM</span> @UserFailedReport
          <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&gt;</span> @LastId
          <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span>
        <span style="color: #0000FF;">END</span>
&nbsp;
        <span style="color: #0000FF;">SET</span> @FullReportTable <span style="color: #808080;">+=</span> @UserReportTable <span style="color: #808080;">+</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">13</span><span style="color: #808080;">&#41;</span>
&nbsp;
        <span style="color: #0000FF;">SET</span> @UserReportTable <span style="color: #808080;">=</span> @ReportTableOpen <span style="color: #808080;">+</span> @UserReportTable <span style="color: #808080;">+</span> @ReportTableClose
&nbsp;
        <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @Notification <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TO</span><span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Body<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>@Username <span style="color: #808080;">+</span> <span style="color: #FF0000;">'@'</span> <span style="color: #808080;">+</span> @MailDomain, @UserReportTable<span style="color: #808080;">&#41;</span>
      <span style="color: #0000FF;">END</span>
&nbsp;
&nbsp;
      <span style="color: #0000FF;">SET</span> @FullReportTable <span style="color: #808080;">=</span> @ReportTableOpen <span style="color: #808080;">+</span> @FullReportTable <span style="color: #808080;">+</span> @ReportTableClose
&nbsp;
      <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @Notification <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TO</span><span style="color: #808080;">&#93;</span>, <span style="color: #808080;">&#91;</span>Body<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>@AdminEmail, @FullReportTable<span style="color: #808080;">&#41;</span>
&nbsp;
      <span style="color: #0000FF;">DECLARE</span> 
         @LastNotificationId <span style="color: #0000FF;">INT</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
        ,@<span style="color: #0000FF;">TO</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
        ,@Body <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span>
&nbsp;
      <span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">EXISTS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">FROM</span> @Notification <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>NotificationId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&gt;</span> @LastNotificationId<span style="color: #808080;">&#41;</span>
      <span style="color: #0000FF;">BEGIN</span>
&nbsp;
        <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> 
           @<span style="color: #0000FF;">TO</span> <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TO</span><span style="color: #808080;">&#93;</span>
          ,@Body <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Body<span style="color: #808080;">&#93;</span>
          ,@LastNotificationId <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>NotificationId<span style="color: #808080;">&#93;</span>
        <span style="color: #0000FF;">FROM</span> @Notification
        <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>NotificationId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&gt;</span> @LastNotificationId
        <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #808080;">&#91;</span>NotificationId<span style="color: #808080;">&#93;</span>
&nbsp;
        <span style="color: #0000FF;">EXEC</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sp_send_dbmail</span>
           @recipients <span style="color: #808080;">=</span> @<span style="color: #0000FF;">TO</span>
          ,@from_address <span style="color: #808080;">=</span> @FromAddress
          ,@subject <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Failed Report Manager Subscription(s)'</span>
          ,@body <span style="color: #808080;">=</span> @Body
          ,@importance <span style="color: #808080;">=</span> <span style="color: #FF0000;">'High'</span>
          ,@body_format <span style="color: #808080;">=</span> <span style="color: #FF0000;">'HTML'</span>  
&nbsp;
        <span style="color: #008080;">--DECLARE @ProcName VARCHAR(255) = OBJECT_NAME(@@PROCID)</span>
&nbsp;
        <span style="color: #008080;">--EXEC [dbo].[Utility_Alert_SendMail] </span>
        <span style="color: #008080;">--   @AlertSubject = 'Failed Report Manager Subscription(s)'</span>
        <span style="color: #008080;">--  ,@AlertRecipients = @To</span>
        <span style="color: #008080;">--  ,@AlertBody = @Body</span>
        <span style="color: #008080;">--  ,@AlertFormat = 'HTML'</span>
        <span style="color: #008080;">--  ,@AlertSource = @ProcName</span>
        <span style="color: #008080;">--  ,@FromAddress = @FromAddress</span>
&nbsp;
      <span style="color: #0000FF;">END</span>
    <span style="color: #0000FF;">END</span>
&nbsp;
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #FailedReport
  <span style="color: #0000FF;">END</span>
<span style="color: #0000FF;">END</span></pre></div></div>

<p>Now to run it:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">EXEC <span style="color: #66cc66;">&#91;</span>Alert_FailedReportingServicesSubscriptions<span style="color: #66cc66;">&#93;</span> 
   @ReportServerBaseURL <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'http://reports.yourcompany.com/Reports/Pages/Report.aspx?ItemPath='</span>
  <span style="color: #66cc66;">,</span>@AdminEmail <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Business Intelligence &lt;BusinessIntelligence@yourcompany.com&gt;'</span>
  <span style="color: #66cc66;">,</span>@FromAddress <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Business Intelligence &lt;BusinessIntelligence@yourcompany.com&gt;'</span>
  <span style="color: #66cc66;">,</span>@MailDomain <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'yourcompany.com'</span>
  <span style="color: #66cc66;">,</span>@NTDomain <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'AWESOMECO'</span></pre></div></div>

<p><em>Set paramaters as follows:</em></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">@ReportServerBaseURL VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- The url string needed view a report (including the ItemPath querystring variable</span>
<span style="color: #808080; font-style: italic;">-- An example is 'http://reports.yourcompany.com/Reports/Pages/Report.aspx?ItemPath='</span>
<span style="color: #808080; font-style: italic;">-- You can obtain this by simply viewing a report and looking at the address bar	</span>
&nbsp;
@AdminEmail VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- Email that will receive digest emails</span>
&nbsp;
@FromAddress VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- From email, they should be able to reply to this with questions</span>
&nbsp;
@MailDomain VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- Domain that you can send email to by appending their user name to</span>
<span style="color: #808080; font-style: italic;">-- So if you set this to 'example.com' and your username is EXAMPLE\tlaqua</span>
<span style="color: #808080; font-style: italic;">-- it would attempt to send an email to tlaqua@example.com</span>
&nbsp;
@NTDomain VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- NTDomain for usernames.  If your username is EXAMPLE\tlaqua, 'EXAMPLE'</span>
<span style="color: #808080; font-style: italic;">-- is the @NTDomain</span>
&nbsp;
@ReportServerDatabase VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'ReportServer'</span>
<span style="color: #808080; font-style: italic;">-- Name of ReportServer database - MUST be on the server procedure runs on </span>
&nbsp;
@ReportServerSchema VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'dbo'</span>
<span style="color: #808080; font-style: italic;">-- Schema for ReportServer tables</span>
&nbsp;
@HoursToCheck INT <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
<span style="color: #808080; font-style: italic;">-- This should conicide with how often you run the procedure</span>
<span style="color: #808080; font-style: italic;">-- If you run the procedure hourly, set this to 1 so it checks 1 hour back</span>
<span style="color: #808080; font-style: italic;">-- If you run the procedure daily, set it to 24</span>
&nbsp;
@HourSchedule INT <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">16777215</span>
<span style="color: #808080; font-style: italic;">-- Bitmask for hours of the day to run.  If you want it to only run if</span>
<span style="color: #808080; font-style: italic;">-- it is 9AM, 6PM, and 11PM, set this value to 8651264:</span>
<span style="color: #808080; font-style: italic;">-- POWER(2, 9) + POWER(2, 18) + POWER(2, 23) = 8651264</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2011/04/monitoring-failed-report-server-subscriptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Locating Rampant Database Growth</title>
		<link>http://timlaqua.com/2010/03/locating-rampant-database-growth/</link>
		<comments>http://timlaqua.com/2010/03/locating-rampant-database-growth/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 17:40:02 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=358</guid>
		<description><![CDATA[Every now and then you get a helpful alert from one of your database servers about disk space being low (you're monitoring that, right?), or a bunch of autogrowth alerts (you're monitoring that too, right?) - but what happens when you get these for a database that you don't expect growth in? Further, what happens [...]]]></description>
			<content:encoded><![CDATA[<p>Every now and then you get a helpful alert from one of your database servers about disk space being low (you're monitoring that, right?), or a bunch of autogrowth alerts (you're monitoring that too, right?) - but what happens when you get these for a database that you don't expect growth in?  Further, what happens when that database is growing rampantly (say like 1GB/hr in my case) and it's a canned database from a 3rd party product?  This time it was the database that SolarWinds uses for collection - and apparently it was collecting a lot of something that it wasn't collecting before.</p>
<ol>
<li>you send out an email asking the end users of the system (IT in this case) if anything changed</li>
<li>adjust so said system stops trying to fill up your drives</li>
</ol>
<p>Unfortunately, there's often quite a few possibilities for "why" a database is growing - and when it's a canned product, you don't always have the best understanding of why it does what it does when it does it.  As a Database Admin, you can help diagnose the problem by letting everyone know what exactly is growing:<br />
<span id="more-358"></span><br />
First, figure out what table is growing:<br />
<em>spSpaceUsed.sql</em></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #808080; font-style: italic;">#Temp_SpSpaceUsed</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>rows<span style="color: #66cc66;">&#93;</span> INT
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>reserved<span style="color: #66cc66;">&#93;</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#93;</span>	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>index_size<span style="color: #66cc66;">&#93;</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>unused<span style="color: #66cc66;">&#93;</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>CreatedTime<span style="color: #66cc66;">&#93;</span> DATETIME <span style="color: #993333; font-weight: bold;">DEFAULT</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
EXEC sp_msforeachtable <span style="color: #ff0000;">'INSERT INTO #Temp_SpSpaceUsed ([name],[rows],[reserved],[data],[index_size],[unused]) EXEC sp_spaceused [?]'</span>
&nbsp;
WAITFOR DELAY <span style="color: #ff0000;">'00:01:00'</span>
&nbsp;
EXEC sp_msforeachtable <span style="color: #ff0000;">'INSERT INTO #Temp_SpSpaceUsed ([name],[rows],[reserved],[data],[index_size],[unused]) EXEC sp_spaceused [?]'</span>
&nbsp;
;WITH SpSpaceUsed <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> 
		<span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span>
		<span style="color: #66cc66;">,</span>CAST<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> LEN<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> INT<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>CreatedTime<span style="color: #66cc66;">&#93;</span>
		<span style="color: #66cc66;">,</span>ROW_NUMBER<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> OVER <span style="color: #66cc66;">&#40;</span>PARTITION <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#91;</span>CreatedTime<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ASC</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>RowNumber<span style="color: #66cc66;">&#93;</span>
	<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #808080; font-style: italic;">#Temp_SpSpaceUsed	</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> 
	 a<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span>b<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">DATA</span> <span style="color: #66cc66;">-</span> a<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">DATA</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>GrowthKB<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span>CAST<span style="color: #66cc66;">&#40;</span>b<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">DATA</span> <span style="color: #66cc66;">-</span> a<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">DATA</span> <span style="color: #993333; font-weight: bold;">AS</span> FLOAT<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> DateDiff<span style="color: #66cc66;">&#40;</span>MILLISECOND<span style="color: #66cc66;">,</span> a<span style="color: #66cc66;">.</span>CreatedTime<span style="color: #66cc66;">,</span> b<span style="color: #66cc66;">.</span>CreatedTime<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">1000.0</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">60.0</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">60.0</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>GrowthKBPerHour<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">FROM</span> SpSpaceUsed a
	<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> SpSpaceUsed b
		<span style="color: #993333; font-weight: bold;">ON</span> a<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> b<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span>
			<span style="color: #993333; font-weight: bold;">AND</span> a<span style="color: #66cc66;">.</span>RowNumber <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
			<span style="color: #993333; font-weight: bold;">AND</span> b<span style="color: #66cc66;">.</span>RowNumber <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span>
	GrowthKBPerHour <span style="color: #993333; font-weight: bold;">DESC</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #808080; font-style: italic;">#Temp_SpSpaceUsed</span></pre></div></div>

<p>Next, look at the culprit (assuming there's one table doing all the damage - I've found that's usually the case) and try to decipher the column names.  Finally, run a few queries against the table - WITH(NOLOCK) of course - hey, this table is pretty busy.  Now try and see where all the action is coming from.  In this case, one of the columns was [IP] so I just did a COUNT(1) by IP for the last hour's records.  Sure enough, 99% of the records were coming from a single IP, sent that IP off to the guys who know what IPs mean, and they knew exactly which change caused the growth.</p>
<p>Bottom line - nobody knows everything about everything, but we can learn pretty quick with the right information.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/03/locating-rampant-database-growth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script sp_configure Output To Migrate Settings Between SQL Servers</title>
		<link>http://timlaqua.com/2010/02/script-sp_configure-output-to-migrate-settings-between-sql-servers/</link>
		<comments>http://timlaqua.com/2010/02/script-sp_configure-output-to-migrate-settings-between-sql-servers/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 23:22:30 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=355</guid>
		<description><![CDATA[Sometimes when setting up a new server, all you want is for it to work and be configured exactly like the old server. In some cases, the new server is almost identical in every way already. If you've worked much with SQL Server, you know very well that there are a billion switches and knobs [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when setting up a new server, all you want is for it to work and be configured exactly like the old server.  In some cases, the new server is almost identical in every way already.  If you've worked much with SQL Server, you know very well that there are a billion switches and knobs and every time you do this, you forget at least one.  I had a similar situation recently and figured I'd be lazy about it and just turned the output of sp_configure in to a script I could execute on the new server:<br />
<span id="more-355"></span><br />
<em>scriptSpConfigure.sql - run this on the old/source server</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'show advanced options'</span> , <span style="color: #000;">1</span>;
GO
<span style="color: #0000FF;">RECONFIGURE</span>;
GO
<span style="color: #0000FF;">DECLARE</span> @spConfigureOutput <span style="color: #0000FF;">TABLE</span>
	<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
	,<span style="color: #808080;">&#91;</span>minimum<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span>
	,<span style="color: #808080;">&#91;</span>maximum<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span>
	,<span style="color: #808080;">&#91;</span>config_value<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span>
	,<span style="color: #808080;">&#91;</span>run_value<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @spConfigureOutput
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span>	<span style="color: #FF0000;">'EXEC sp_configure '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>config_value <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">FROM</span>	@spConfigureOutput
GO
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'show advanced options'</span> , <span style="color: #000;">0</span>;
GO
<span style="color: #0000FF;">RECONFIGURE</span>;
GO</pre></div></div>

<p>Copy the selected rows and paste in to another SSMS window, add the show advanced options switch above and below - should end up with something like this after you <strong>remove all the settings you don't care about or don't want on the new server</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'show advanced options'</span> , <span style="color: #000;">1</span>;
GO
<span style="color: #0000FF;">RECONFIGURE</span>;
GO
&nbsp;
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'Ad Hoc Distributed Queries'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'Agent XPs'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'clr enabled'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'cost threshold for parallelism'</span>, <span style="color: #000;">5</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'cursor threshold'</span>, <span style="color: #808080;">-</span><span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'Database Mail XPs'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'default full-text language'</span>, <span style="color: #000;">1033</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'default trace enabled'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'fill factor (%)'</span>, <span style="color: #000;">80</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'ft crawl bandwidth (max)'</span>, <span style="color: #000;">100</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'ft notify bandwidth (max)'</span>, <span style="color: #000;">100</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'max full-text crawl range'</span>, <span style="color: #000;">4</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'max server memory (MB)'</span>, <span style="color: #000;">10000</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'max text repl size (B)'</span>, <span style="color: #000;">65536</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'min memory per query (KB)'</span>, <span style="color: #000;">1024</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'min server memory (MB)'</span>, <span style="color: #000;">10000</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'nested triggers'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'network packet size (B)'</span>, <span style="color: #000;">4096</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'PH timeout (s)'</span>, <span style="color: #000;">60</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'query wait (s)'</span>, <span style="color: #808080;">-</span><span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'remote access'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'remote login timeout (s)'</span>, <span style="color: #000;">20</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'remote query timeout (s)'</span>, <span style="color: #000;">900</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'scan for startup procs'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'server trigger recursion'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'SMO and DMO XPs'</span>, <span style="color: #000;">1</span>
&nbsp;
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'show advanced options'</span> , <span style="color: #000;">0</span>;
<span style="color: #0000FF;">RECONFIGURE</span>;
GO;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/02/script-sp_configure-output-to-migrate-settings-between-sql-servers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script RESTORE DATABASE &#8230; WITH MOVE Stub</title>
		<link>http://timlaqua.com/2010/02/script-restore-database-with-move-stub/</link>
		<comments>http://timlaqua.com/2010/02/script-restore-database-with-move-stub/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 18:29:26 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=349</guid>
		<description><![CDATA[I think most people setup the drives on their Development servers to match their Production servers - this is so that restores go smoothly and files go where they're supposed to, things match up nicely, etc. Unfortunately, when you create a full backup all the backup file contains is the logical name of all the [...]]]></description>
			<content:encoded><![CDATA[<p>I think most people setup the drives on their Development servers to match their Production servers - this is so that restores go smoothly and files go where they're supposed to, things match up nicely, etc.  Unfortunately, when you create a full backup all the backup file contains is the logical name of all the files - no physical paths.  This means that if the database doesn't exist on the destination server yet, the engine has absolutely no idea where to put the files or what to name them.</p>
<p>The following script can be executed in the context of the source (original) database that was backed up to script out the MOVE statements if your plan is to put the files in the same place on the destination server.<br />
<span id="more-349"></span><br />
<em>scriptRestoreWithMove.sql - run this in the context of the source database</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @MoveOption <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">TABLE</span>
	<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
	,<span style="color: #808080;">&#91;</span>MoveOption<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'RESTORE DATABASE '</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DB_ID</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #008080;">-- Edit this to match where you're restoring from</span>
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'FROM DISK = '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DB_ID</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.bak'</span><span style="color: #FF0000;">''</span>  
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'WITH'</span>
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @MoveOption <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>MoveOption<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span>
    <span style="color: #FF0000;">'MOVE '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> a.<span style="color: #202020;">name</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">' TO '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> a.<span style="color: #202020;">FILENAME</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>MOVE <span style="color: #0000FF;">OPTION</span><span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span>
    dbo.<span style="color: #202020;">sysfiles</span> a
&nbsp;
<span style="color: #0000FF;">DECLARE</span> 
	 @LastId <span style="color: #0000FF;">INT</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
	,@MoveOptionText <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">EXISTS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">FROM</span> @MoveOption <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&gt;</span> @LastId<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #0000FF;">SELECT</span> 		<span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span>
			 @MoveOptionText <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>MoveOption<span style="color: #808080;">&#93;</span> 
			,@LastId <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span>
	<span style="color: #0000FF;">FROM</span>		@MoveOption
	<span style="color: #0000FF;">WHERE</span>		<span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&gt;</span> @LastId
	<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span>	<span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ASC</span>
&nbsp;
	<span style="color: #0000FF;">PRINT</span> <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> @LastId <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">''</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">','</span> <span style="color: #0000FF;">END</span> <span style="color: #808080;">+</span> @MoveOptionText
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'GO'</span>
&nbsp;
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'ALTER DATABASE '</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DB_ID</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' SET MULTI_USER'</span>
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'GO'</span></pre></div></div>

<p><em>And the output looks like this:</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">RESTORE</span> <span style="color: #0000FF;">DATABASE</span> AdventureWorksDW
<span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">DISK</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'AdventureWorksDW.bak'</span>
<span style="color: #0000FF;">WITH</span>
MOVE <span style="color: #FF0000;">'AdventureWorksDW_Data'</span> <span style="color: #0000FF;">TO</span> <span style="color: #FF0000;">'D:<span style="color: #000099; font-weight: bold;">\A</span>dventureWorksDW<span style="color: #000099; font-weight: bold;">\A</span>dventureWorksDW_Data.mdf'</span>
,MOVE <span style="color: #FF0000;">'AdventureWorksDW_Log'</span> <span style="color: #0000FF;">TO</span> <span style="color: #FF0000;">'T:<span style="color: #000099; font-weight: bold;">\A</span>dventureWorksDW<span style="color: #000099; font-weight: bold;">\A</span>dventureWorksDW_Log.LDF'</span>
GO
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">DATABASE</span> AdventureWorksDW <span style="color: #0000FF;">SET</span> MULTI_USER
GO</pre></div></div>

<p>If you end up wanting to do something similar with existing databases, make sure to add a command before the RESTORE to set SINGLE_USER mode.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/02/script-restore-database-with-move-stub/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clone Analysis Services Partitions with PowerShell</title>
		<link>http://timlaqua.com/2010/01/clone-analysis-services-partitions-with-powershell/</link>
		<comments>http://timlaqua.com/2010/01/clone-analysis-services-partitions-with-powershell/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 20:45:20 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[amo]]></category>
		<category><![CDATA[analysis services]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=322</guid>
		<description><![CDATA[Most of us with large Analysis Services cubes partition our cubes by month or year or some other time-based slice and we have all, at one point or another, developed some way to create partitions for new months on-demand. Often, the solution to this seems to be a C# console application or SSIS package using [...]]]></description>
			<content:encoded><![CDATA[<p>Most of us with large Analysis Services cubes partition our cubes by month or year or some other time-based slice and we have all, at one point or another, developed some way to create partitions for new months on-demand.  Often, the solution to this seems to be a C# console application or SSIS package using AMO to create a new partition based off an existing partition.  The problem I see with this is that maintaining it requires opening up the project or package, making changes, re-compiling, deploying, testing, deploying to production, verifying, etc.  It also requires that whoever is going to maintain it is comfortable with C#.</p>
<p>To simplify the maintenance and get rid of the "black box" factor that utility apps like this tend to have, I put together a PowerShell script to do the same thing and a stored procedure to call the script.  Really, it doesn't matter what you use as you're most likely using an almost identical chunk of code to get your new partition created - my argument is that using PowerShell instead of C# or SSIS reduces the cost of maintenance, improves readability, and facilitates better understanding throughout your team.<br />
<span id="more-322"></span><br />
<em>The PowerShell Script: cloneSSASPartition.ps1</em></p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$ServerName</span><span style="color: pink;">,</span> 		<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Analysis Services Instance #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$DatabaseName</span><span style="color: pink;">,</span> 		<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Database our Cube is in #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$CubeName</span><span style="color: pink;">,</span>     		<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Cube that our Measure Group is in #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$MeasureGroupName</span><span style="color: pink;">,</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Measure Group that our Partition is in #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$SourcePartitionName</span><span style="color: pink;">,</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Partition we want to merge in to another partition #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$NewPartitionName</span><span style="color: pink;">,</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The desired name of the new partition #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$NewPartitionQuery</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The Query source for the new partition (will use the same datasource as the SourcePartition #&gt;</span>
<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.AnalysisServices&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">&gt;</span><span style="color: #800080;">$NULL</span>
&nbsp;
<span style="color: #800080;">$server</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> Microsoft.AnalysisServices.Server
<span style="color: #800080;">$server</span>.Connect<span style="color: #000000;">&#40;</span><span style="color: #800080;">$ServerName</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800080;">$server</span>.Databases.GetByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$DatabaseName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$cube</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.Cubes.GetByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$CubeName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$measureGroup</span> <span style="color: pink;">=</span> <span style="color: #800080;">$cube</span>.MeasureGroups.FindByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$MeasureGroupName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$sourcePartition</span> <span style="color: pink;">=</span> <span style="color: #800080;">$measureGroup</span>.Partitions.GetByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$SourcePartitionName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$sourceQueryBinding</span> <span style="color: pink;">=</span> <span style="color: #800080;">$sourcePartition</span>.Source
&nbsp;
<span style="color: #800080;">$newPartition</span> <span style="color: pink;">=</span> <span style="color: #800080;">$sourcePartition</span>.Clone<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$newPartition</span>.ID <span style="color: pink;">=</span> <span style="color: #800080;">$NewPartitionName</span>
<span style="color: #800080;">$newPartition</span>.Name <span style="color: pink;">=</span> <span style="color: #800080;">$NewPartitionName</span>
<span style="color: #800080;">$measureGroup</span>.Partitions.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$newPartition</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">&gt;</span> <span style="color: #800080;">$NULL</span>
<span style="color: #800080;">$newPartition</span>.Source <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> Microsoft.AnalysisServices.QueryBinding<span style="color: #000000;">&#40;</span><span style="color: #800080;">$sourceQueryBinding</span>.DataSourceID<span style="color: pink;">,</span> <span style="color: #800080;">$NewPartitionQuery</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$newPartition</span>.Update<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #800080;">$server</span>.Disconnect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p><em>The stored procedure to call the script: [Utility_CloneSSASPartition_S01] - assumes you placed cloneSSASPartition.ps1 at the root of the C: drive.  If you put it elsewhere, update the proc to reflect the actual location.</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
&nbsp;
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
&nbsp;
<span style="color: #008080;">/*************************************************************************************
***
*** Procedure:  		[Utility_CloneSSASPartition]
*** Purpose:		Executes PowerShell script to clone a SSAS partition
***			
***			
*** Author:		tl
*** Date Created:		2010-01-15
*** 
*** Revision History
*** Date		Author			Description
*** 2010-01-15	tl			Created
*************************************************************************************/</span>
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_CloneSSASPartition_S01<span style="color: #808080;">&#93;</span>
<span style="color: #808080;">&#40;</span>
	@ServerName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@DatabaseName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@CubeName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@MeasureGroupName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@SourcePartitionName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@NewPartitionName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@NewPartitionQuery <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span> 
&nbsp;
<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>
<span style="color: #0000FF;">DECLARE</span> @cmd <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: #0000FF;">SET</span> @cmd <span style="color: #808080;">=</span> 
	<span style="color: #FF0000;">'powershell.exe -Command &quot;&amp; {'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' C:<span style="color: #000099; font-weight: bold;">\c</span>loneSSASPartition.ps1'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -ServerName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @ServerName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -DatabaseName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @DatabaseName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -CubeName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @CubeName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -MeasureGroupName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @MeasureGroupName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -SourcePartitionName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @SourcePartitionName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -NewPartitionName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @NewPartitionName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -NewPartitionQuery:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @NewPartitionQuery <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>}&quot;'</span>
&nbsp;
<span style="color: #0000FF;">PRINT</span> @cmd
<span style="color: #0000FF;">EXEC</span> xp_cmdshell @cmd</pre></div></div>

<p>And, finally, we execute it</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_CloneSSASPartition_S01<span style="color: #808080;">&#93;</span>
	 @ServerName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TESTSERVER'</span>
	,@DatabaseName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TestDatabase'</span>
	,@CubeName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TestCube'</span>
	,@MeasureGroupName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Test Measure Group'</span>
	,@SourcePartitionName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Test Partition - 200912'</span>
	,@NewPartitionName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Test Partition - 201001'</span>
	,@NewPartitionQuery <span style="color: #808080;">=</span> <span style="color: #FF0000;">'SELECT * FROM dbo.FactTableMonth('</span><span style="color: #FF0000;">'1/1/2010'</span><span style="color: #FF0000;">', '</span><span style="color: #FF0000;">'2/1/2010'</span><span style="color: #FF0000;">')'</span></pre></div></div>

<p>Again, I'm not saying there's anything "wrong" with using C# or SSIS Packages to do this sort of thing, I'm just pointing out that C# isn't always the answer - especially when someone other than you needs to maintain this thing in the future.  Keep it simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/01/clone-analysis-services-partitions-with-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Trending SQL Server Agent Job Duration by Hour</title>
		<link>http://timlaqua.com/2009/12/trending-sql-server-agent-job-duration-by-hour/</link>
		<comments>http://timlaqua.com/2009/12/trending-sql-server-agent-job-duration-by-hour/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 03:07:38 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[agent job]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server 2008]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=302</guid>
		<description><![CDATA[Earlier today I noticed a SQL Server Agent job taking a little longer than usual (or what I thought was longer than usual). Let's face it, we're not staring at the Job Activity monitor all day, so unless you've written a report to monitor job run times - on occasion you ask yourself "is that [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier today I noticed a SQL Server Agent job taking a little longer than usual (or what I thought was longer than usual).  Let's face it, we're not staring at the Job Activity monitor all day, so unless you've written a report to monitor job run times - on occasion you ask yourself "is that a normal run time for this thing?"  The job I was curious about happened to be a job that runs throughout the day and should only have real work to do once or twice an hour - and it should run roughly the same amount of time on any given business day for a given hour (i.e. at 1:00 PM on any given business day, this thing should do the same amount of work).</p>
<p>So I came up with the following query to PIVOT the run duration on the hour the job executed:<span id="more-302"></span></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @JobName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Agent Job Name'</span>
&nbsp;
;WITH JobHistory <span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
  <span style="color: #0000FF;">SELECT</span>
     a.<span style="color: #202020;">run_date</span>
    ,a.<span style="color: #202020;">run_time</span> <span style="color: #808080;">/</span> <span style="color: #000;">10000</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">HOUR</span><span style="color: #808080;">&#93;</span>
    ,<span style="color: #808080;">&#40;</span>a.<span style="color: #202020;">run_duration</span> <span style="color: #808080;">/</span> <span style="color: #000;">10000</span> <span style="color: #808080;">*</span> <span style="color: #000;">60</span> <span style="color: #808080;">*</span> <span style="color: #000;">60</span>	<span style="color: #008080;">-- Hours</span>
     <span style="color: #808080;">+</span> a.<span style="color: #202020;">run_duration</span> <span style="color: #808080;">%</span> <span style="color: #000;">10000</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: #008080;">-- Minutes</span>
     <span style="color: #808080;">+</span> a.<span style="color: #202020;">run_duration</span> <span style="color: #808080;">%</span> <span style="color: #000;">100</span>			<span style="color: #008080;">-- Seconds</span>
     <span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>DurationMinutes<span style="color: #808080;">&#93;</span>
  <span style="color: #0000FF;">FROM</span>
    msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobhistory</span> a <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobs</span> b <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">ON</span> 
      a.<span style="color: #808080;">&#91;</span>job_id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>job_id<span style="color: #808080;">&#93;</span>
      <span style="color: #808080;">AND</span> b.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> @JobName
      <span style="color: #808080;">AND</span> step_id <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
      <span style="color: #808080;">AND</span> run_status <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span>
  JobHistory	
  PIVOT
  <span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>DurationMinutes<span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">FOR</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">HOUR</span><span style="color: #808080;">&#93;</span> 
    <span style="color: #808080;">IN</span>  <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>00<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>01<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>02<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>03<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>04<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>05<span style="color: #808080;">&#93;</span>
        ,<span style="color: #808080;">&#91;</span>06<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>07<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>08<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>09<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">10</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">11</span><span style="color: #808080;">&#93;</span>
        ,<span style="color: #808080;">&#91;</span><span style="color: #000;">12</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">13</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">14</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">15</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">16</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">17</span><span style="color: #808080;">&#93;</span>
        ,<span style="color: #808080;">&#91;</span><span style="color: #000;">18</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">19</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">20</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">21</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">22</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">23</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
  <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> p</pre></div></div>

<p><em>UPDATE: Added <strong>AND run_status = 1</strong></em><br/></p>
<p>Then made a table (not sure why, but I can't resist doing the heatmap thing):<br />
<a href="http://timlaqua.com/wp-content/uploads/2009/12/agentJobDurationTable.png"><img src="http://timlaqua.com/wp-content/uploads/2009/12/agentJobDurationTable-300x52.png" alt="" title="agentJobDurationTable" width="300" height="52" class="alignnone size-medium wp-image-304" /></a><br/><em>click the image to enlarge</em></p>
<p>And, of course, the chart so you can point at a picture should you have to explain something to somebody in the future:<br />
<a href="http://timlaqua.com/wp-content/uploads/2009/12/agentJobDurationChart.png"><img src="http://timlaqua.com/wp-content/uploads/2009/12/agentJobDurationChart-300x146.png" alt="" title="agentJobDurationChart" width="300" height="146" class="alignnone size-medium wp-image-305" /></a><br/><em>click the image to enlarge</em></p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/12/trending-sql-server-agent-job-duration-by-hour/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Determining when RESTORE DATABASE command will complete (SQL Server 2008)</title>
		<link>http://timlaqua.com/2009/09/determining-when-restore-database-command-will-complete-sql-server-2008/</link>
		<comments>http://timlaqua.com/2009/09/determining-when-restore-database-command-will-complete-sql-server-2008/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 17:48:22 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[RESTORE DATABASE]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server 2008]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=238</guid>
		<description><![CDATA[Ah, I see you just started restoring that 1TB monster and now everyone wants to know when it's going to be finished, where you're at in the process, etc. Fear not, Microsoft is very good at making up fictional numbers for us to use as rough estimates! I usually add 10-20% on top of these [...]]]></description>
			<content:encoded><![CDATA[<p>Ah, I see you just started restoring that 1TB monster and now everyone wants to know when it's going to be finished, where you're at in the process, etc.  Fear not, Microsoft is very good at making up fictional numbers for us to use as rough estimates!  I usually add 10-20% on top of these estimates just incase the database gremlins wander by to ruin your day again.  Or incase you encounter "storage issues."</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> 
	 percent_complete <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>PercentComplete<span style="color: #808080;">&#93;</span>
	,estimated_completion_time<span style="color: #808080;">/</span><span style="color: #000;">1000.0</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>RemainingMinutes<span style="color: #808080;">&#93;</span>
	,total_elapsed_time<span style="color: #808080;">/</span><span style="color: #000;">1000.0</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>ElapsedMinutes<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#40;</span>estimated_completion_time<span style="color: #808080;">+</span>total_elapsed_time<span style="color: #808080;">&#41;</span><span style="color: #808080;">/</span><span style="color: #000;">1000.0</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>TotalMinutes<span style="color: #808080;">&#93;</span>
	,<span style="color: #FF00FF;">DATEADD</span><span style="color: #808080;">&#40;</span>MILLISECOND, estimated_completion_time, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</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>EstimatedTimeOfCompletion<span style="color: #808080;">&#93;</span>
	,st.<span style="color: #0000FF;">TEXT</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>CommandSQL<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_exec_requests</span> r
	cross apply sys.<span style="color: #202020;">dm_exec_sql_text</span><span style="color: #808080;">&#40;</span>r.<span style="color: #202020;">sql_handle</span><span style="color: #808080;">&#41;</span> st
<span style="color: #0000FF;">WHERE</span> command <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'%RESTORE DATABASE%'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/09/determining-when-restore-database-command-will-complete-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Determining how long a database will be IN RECOVERY (SQL Server 2008)</title>
		<link>http://timlaqua.com/2009/09/determining-how-long-a-database-will-be-in-recovery-sql-server-2008/</link>
		<comments>http://timlaqua.com/2009/09/determining-how-long-a-database-will-be-in-recovery-sql-server-2008/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 14:16:16 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[errorlog]]></category>
		<category><![CDATA[in recovery]]></category>
		<category><![CDATA[kill]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[rollback]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server 2008]]></category>
		<category><![CDATA[xp_readerrorlog]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=233</guid>
		<description><![CDATA[So, your MSSQL service crashed in the middle of a big transaction? Or you bumped the service while it was rolling back some gigantic schema change (like say a column add on a 800 million row table)? Well, as you prepare your resume in preparation for the fallout from this debockle, you can use the [...]]]></description>
			<content:encoded><![CDATA[<p>So, your MSSQL service crashed in the middle of a big transaction?  Or you bumped the service while it was rolling back some gigantic schema change (like say a column add on a 800 million row table)?  Well, as you prepare your resume in preparation for the fallout from this debockle, you can use the following query to see how much time you have left.  Or, I should say, how much time it thinks you have left... which seems to swing wildly up and down...  microsoft math ftw.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;">&nbsp;
<span style="color: #0000FF;">DECLARE</span> @DBName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">64</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'databasename'</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @ErrorLog <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">TABLE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>LogDate<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">24</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#91;</span>ProcessInfo<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">64</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</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> @ErrorLog
<span style="color: #0000FF;">EXEC</span> sys.<span style="color: #202020;">xp_readerrorlog</span> <span style="color: #000;">0</span>, <span style="color: #000;">1</span>, <span style="color: #FF0000;">'Recovery of database'</span>, @DBName
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">5</span>
	 <span style="color: #808080;">&#91;</span>LogDate<span style="color: #808080;">&#93;</span>
	,<span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span>, <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">') is '</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">4</span>,<span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">' complete ('</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">') is '</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> PercentComplete
	,<span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span>, <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'approximately'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">13</span>,<span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">' seconds remain'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'approximately'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">13</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> MinutesRemaining
	,<span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span>, <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'approximately'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">13</span>,<span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">' seconds remain'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'approximately'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">13</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> HoursRemaining
	,<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span>
&nbsp;
<span style="color: #0000FF;">FROM</span> @ErrorLog <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #808080;">&#91;</span>LogDate<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">DESC</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/09/determining-how-long-a-database-will-be-in-recovery-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

