<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>SQL Server Blogs</title><link>http://sqlblogcasts.com/blogs/</link><description>Voices from the UK SQL Server Community</description><dc:language>en-US</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/UkSqlServerCommunityBlogs" /><feedburner:info uri="uksqlservercommunityblogs" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><title>SQL Saturday Edinburgh Agenda</title><link>http://feedproxy.google.com/~r/UkSqlServerCommunityBlogs/~3/vcShwNFx-ts/SQL-Saturday-Edinburgh-Agenda.aspx</link><pubDate>Sat, 11 May 2013 20:13:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:16466</guid><dc:creator>MartinBell</dc:creator><slash:comments>0</slash:comments><description>SQL Saturday Edinburgh Agenda...(&lt;a href="http://sqlblogcasts.com/blogs/martinbell/archive/2013/05/11/SQL-Saturday-Edinburgh-Agenda.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16466" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/martinbell/archive/tags/SQL+Saturday+Edinburgh/default.aspx">SQL Saturday Edinburgh</category><feedburner:origLink>http://sqlblogcasts.com/blogs/martinbell/archive/2013/05/11/SQL-Saturday-Edinburgh-Agenda.aspx</feedburner:origLink></item><item><title>Leeds Usergroup Slides and Scripts</title><link>http://feedproxy.google.com/~r/UkSqlServerCommunityBlogs/~3/c5DMGxQEI54/Leeds-Usergroup-Slides-and-Scripts.aspx</link><pubDate>Fri, 10 May 2013 07:53:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:16465</guid><dc:creator>MartinBell</dc:creator><slash:comments>0</slash:comments><description>Presentation and Slides from the Leeds User Group 7th May...(&lt;a href="http://sqlblogcasts.com/blogs/martinbell/archive/2013/05/10/Leeds-Usergroup-Slides-and-Scripts.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16465" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/martinbell/archive/tags/User+Group/default.aspx">User Group</category><category domain="http://sqlblogcasts.com/blogs/martinbell/archive/tags/Partitioned+Tables/default.aspx">Partitioned Tables</category><category domain="http://sqlblogcasts.com/blogs/martinbell/archive/tags/Column+Store/default.aspx">Column Store</category><feedburner:origLink>http://sqlblogcasts.com/blogs/martinbell/archive/2013/05/10/Leeds-Usergroup-Slides-and-Scripts.aspx</feedburner:origLink></item><item><title>Resolving data redundancy up front</title><link>http://feedproxy.google.com/~r/UkSqlServerCommunityBlogs/~3/4A3F5gXWIZ8/resolving-data-redundancy-up-front.aspx</link><pubDate>Mon, 22 Apr 2013 17:58:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:16463</guid><dc:creator>okeofs</dc:creator><slash:comments>0</slash:comments><description>&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;Introduction&lt;/font&gt;&lt;/font&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;As all of us
do when confronted with a problem, the resource of choice is to ‘Google it’.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;This is
where the plot thickens. Recently I was asked to stage data from numerous
databases which were to be loaded into a data warehouse. To make a long story
short, I was looking for a manner in which to obtain the table names from each
database, to ascertain potential overlap.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;font size="3" face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;font face="Calibri"&gt;&lt;font size="3"&gt;As the
source data comes from a SQL database created from dumps of a third party
product, &lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;one could say that there were
+/- 95 tables for each database. &lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;font size="3" face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;font size="3" face="Calibri"&gt;Yes I know
that first instinct is to use the system stored procedure “&lt;/font&gt;&lt;span style="color:blue;line-height:115%;font-size:10pt;mso-no-proof:yes;"&gt;exec&lt;/span&gt;&lt;span style="line-height:115%;font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:maroon;"&gt;sp_msforeachdb&lt;/span&gt;&lt;span style="color:blue;"&gt; &lt;/span&gt;&lt;span style="color:red;"&gt;&amp;#39;select &amp;quot;?&amp;quot; AS db,
* from [?].sys.tables&amp;#39;”.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font face="Calibri"&gt;&lt;font size="3"&gt;However, if one
stops to think about this, it would be nice to have all the results in a temporary
or disc based &lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;table; which in itself , implies
additional labour.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font face="Calibri"&gt;&lt;font size="3"&gt;This said, &lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;I decided to ‘re-invent’ the wheel. The full
code sample may be found at the bottom of this article.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3" face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;Define a few temporary tables and variables&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;span style="text-decoration:none;"&gt;&lt;font size="3" face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;declare&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; @SQL &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:fuchsia;"&gt;max&lt;/span&gt;&lt;span style="color:gray;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;declare&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;
@databasename &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;75&lt;span style="color:gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:green;font-size:10pt;mso-no-proof:yes;"&gt;/*&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:green;font-size:10pt;mso-no-proof:yes;"&gt;drop table
##rawdata3&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:green;font-size:10pt;mso-no-proof:yes;"&gt;drop table
#rawdata1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:green;font-size:10pt;mso-no-proof:yes;"&gt;drop table
#rawdata11&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:green;font-size:10pt;mso-no-proof:yes;"&gt;*/&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:green;font-size:10pt;mso-no-proof:yes;"&gt;-- A temp table
to hold the names of my databases&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;CREATE&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;TABLE&lt;/span&gt; #rawdata1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;(&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_name &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_size &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;remarks &lt;span style="color:blue;"&gt;Varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;--A temp table
with the same database names as above, HOWEVER using an &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;--Identity
number (recNO) as a loop variable.&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;--You will
note below that I loop through until I reach 25 (see below) as at&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;--that point the
system databases, the reporting server database etc begin. &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;--1- 24 are
user databases. These are really what I was looking for.&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;--Whilst NOT
the best solution,it works and the code was meant as a quick &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;--and dirty.&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;CREATE&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;TABLE&lt;/span&gt; #rawdata11&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;(&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;recNo &lt;span style="color:blue;"&gt;int&lt;/span&gt; &lt;span style="color:blue;"&gt;identity&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;1&lt;span style="color:gray;"&gt;,&lt;/span&gt;1&lt;span style="color:gray;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_name &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_size &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;remarks &lt;span style="color:blue;"&gt;Varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;--My output
table showing the database name and table name&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;CREATE&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;TABLE&lt;/span&gt; ##rawdata3&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;(&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_name &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;75&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;table_name &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;75&lt;span style="color:gray;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="color:gray;line-height:115%;font-size:10pt;mso-no-proof:yes;"&gt;)&lt;/span&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;span style="text-decoration:none;"&gt;&lt;font size="3" face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;Insert the database names into a
temporary table&lt;/font&gt;&lt;/font&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;I pull the
database names using the system stored procedure sp_databases&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;font size="3" face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;INSERT&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;INTO&lt;/span&gt; #rawdata1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;EXEC&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:maroon;"&gt;sp_databases&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;Go&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;u&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font face="Calibri"&gt;&lt;font size="3"&gt;Insert the results
from #rawdata1 into a table containing a record number &lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;#rawdata11 so that I can LOOP through the
extract&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/u&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3" face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;INSERT&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;into&lt;/span&gt; #rawdata11&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;select&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:gray;"&gt;*&lt;/span&gt; &lt;span style="color:blue;"&gt;from&lt;/span&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;#rawdata1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;We now declare 3 more variables:&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font face="Calibri"&gt;&lt;font size="3"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;@kounter
is used to keep track of our position within the loop.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font face="Calibri"&gt;&lt;font size="3"&gt;@databasename is used to keep track of the’ current
‘ database name being used in the current pass of the loop; &lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;as inorder to obtain the tables for that
database we&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;need to issue a ‘USE’
statement, an insert command and other related code parts. This is the challenging
part.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;@sql is a varchar(max) variable used to contain
the ‘USE’ statement PLUS the’ insert ‘ code statements.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;We now initalize @kounter to 1 .&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;declare&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; @kounter &lt;span style="color:blue;"&gt;int&lt;/span&gt;&lt;span style="color:gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;declare&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;
@databasename &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;75&lt;span style="color:gray;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;declare&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; @sql &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:fuchsia;"&gt;max&lt;/span&gt;&lt;span style="color:gray;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="color:blue;line-height:115%;font-size:10pt;mso-no-proof:yes;"&gt;set&lt;/span&gt;&lt;span style="line-height:115%;font-size:10pt;mso-no-proof:yes;"&gt; @kounter &lt;span style="color:gray;"&gt;=&lt;/span&gt;
1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="line-height:115%;font-size:10pt;mso-no-proof:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;The Loop&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font face="Calibri"&gt;&lt;font size="3"&gt;The astute reader
will remember that the temporary table #rawdata11 contains our&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;database names&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;and each ‘database row’ has a record number
(recNo). I am only interested in record numbers under 25. I now set the value
of the temporary variable @DatabaseName (see below) .Note that I used the row
number as a part of the predicate.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;Now, knowing the
database name, I can create dynamic T-SQL to be executed using the sp_sqlexec
stored procedure (see the code in red below).&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;Finally, after all
the tables for that given database have been placed in temporary table ##rawdata3,
I increment the counter and continue on.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;Note that I used a
global temporary table to ensure that the result set persists after the termination
of the run.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;At some stage, I
plan to redo this part of the code, as global temporary tables are not really
an ideal solution.&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;span style="line-height:115%;font-size:11pt;mso-no-proof:yes;mso-ansi-language:EN-US;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-fareast-language:EN-US;mso-bidi-language:AR-SA;mso-ascii-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;"&gt;&lt;br style="page-break-before:always;mso-special-character:line-break;" /&gt;
&lt;/span&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3" face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;WHILE &lt;/span&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;(&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;@kounter &lt;span style="color:gray;"&gt;&amp;lt;&lt;/span&gt; 25&lt;span style="color:gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;select&lt;/span&gt;
@DatabaseName &lt;span style="color:gray;"&gt;=&lt;/span&gt; database_name &lt;span style="color:blue;"&gt;from&lt;/span&gt; #rawdata11 &lt;span style="color:blue;"&gt;where&lt;/span&gt;
recNo &lt;span style="color:gray;"&gt;=&lt;/span&gt; @kounter&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;set&lt;/span&gt; @SQL &lt;span style="color:gray;"&gt;=&lt;/span&gt; &lt;span style="color:red;"&gt;&amp;#39;Use &amp;#39;&lt;/span&gt; &lt;span style="color:gray;"&gt;+&lt;/span&gt; @DatabaseName &lt;span style="color:gray;"&gt;+&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:red;font-size:10pt;mso-no-proof:yes;"&gt;&amp;#39; Insert into
##rawdata3 &amp;#39;&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:gray;"&gt;+&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;+&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:red;"&gt;&amp;#39; SELECT table_catalog,Table_name FROM
information_schema.tables&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;exec&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:maroon;"&gt;sp_sqlexec&lt;/span&gt;&lt;span style="color:blue;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;@Sql &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;SET&lt;/span&gt;
@kounter&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:gray;"&gt;=&lt;/span&gt;
@kounter &lt;span style="color:gray;"&gt;+&lt;/span&gt; 1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="line-height:115%;font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;span style="color:blue;line-height:115%;font-size:10pt;mso-no-proof:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;The full code extract&lt;/font&gt;&lt;/font&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;font size="3" face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;Here is the
full code sample.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;font size="3" face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;declare&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; @SQL &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:fuchsia;"&gt;max&lt;/span&gt;&lt;span style="color:gray;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;declare&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;
@databasename &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;75&lt;span style="color:gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:green;font-size:10pt;mso-no-proof:yes;"&gt;/*&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:green;font-size:10pt;mso-no-proof:yes;"&gt;drop table
##rawdata3&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:green;font-size:10pt;mso-no-proof:yes;"&gt;drop table
#rawdata1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:green;font-size:10pt;mso-no-proof:yes;"&gt;drop table
#rawdata11&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:green;font-size:10pt;mso-no-proof:yes;"&gt;*/&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;CREATE&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;TABLE&lt;/span&gt; #rawdata1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;(&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_name &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_size &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;remarks &lt;span style="color:blue;"&gt;Varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;CREATE&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;TABLE&lt;/span&gt; #rawdata11&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;(&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;recNo &lt;span style="color:blue;"&gt;int&lt;/span&gt; &lt;span style="color:blue;"&gt;identity&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;1&lt;span style="color:gray;"&gt;,&lt;/span&gt;1&lt;span style="color:gray;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_name &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_size &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;remarks &lt;span style="color:blue;"&gt;Varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;50&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;CREATE&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;TABLE&lt;/span&gt; ##rawdata3&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;(&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;database_name &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;75&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;table_name &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;75&lt;span style="color:gray;"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;INSERT&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;INTO&lt;/span&gt; #rawdata1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;EXEC&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:maroon;"&gt;sp_databases&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;go&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;INSERT&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;into&lt;/span&gt; #rawdata11&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;select&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:gray;"&gt;*&lt;/span&gt; &lt;span style="color:blue;"&gt;from&lt;/span&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;#rawdata1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;declare&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; @kounter &lt;span style="color:blue;"&gt;int&lt;/span&gt;&lt;span style="color:gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;declare&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;
@databasename &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;75&lt;span style="color:gray;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;declare&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; @sql &lt;span style="color:blue;"&gt;varchar&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:fuchsia;"&gt;max&lt;/span&gt;&lt;span style="color:gray;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;set&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; @kounter &lt;span style="color:gray;"&gt;=&lt;/span&gt; 1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;WHILE &lt;/span&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;(&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;@kounter &lt;span style="color:gray;"&gt;&amp;lt;&lt;/span&gt; 25&lt;span style="color:gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;select&lt;/span&gt;
@databasename &lt;span style="color:gray;"&gt;=&lt;/span&gt; database_name &lt;span style="color:blue;"&gt;from&lt;/span&gt; #rawdata11 &lt;span style="color:blue;"&gt;where&lt;/span&gt;
recNo &lt;span style="color:gray;"&gt;=&lt;/span&gt; @kounter&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;set&lt;/span&gt; @SQL &lt;span style="color:gray;"&gt;=&lt;/span&gt; &lt;span style="color:red;"&gt;&amp;#39;Use &amp;#39;&lt;/span&gt; &lt;span style="color:gray;"&gt;+&lt;/span&gt; @DatabaseName &lt;span style="color:gray;"&gt;+&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:red;font-size:10pt;mso-no-proof:yes;"&gt;&amp;#39; Insert into
##rawdata3 &amp;#39;&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:gray;"&gt;+&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:gray;font-size:10pt;mso-no-proof:yes;"&gt;+&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:red;"&gt;&amp;#39; SELECT table_catalog,Table_name FROM
information_schema.tables&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:blue;font-size:10pt;mso-no-proof:yes;"&gt;exec&lt;/span&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt; &lt;span style="color:maroon;"&gt;sp_sqlexec&lt;/span&gt;&lt;span style="color:blue;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;@Sql &lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;SET&lt;/span&gt;
@kounter&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:gray;"&gt;=&lt;/span&gt;
@kounter &lt;span style="color:gray;"&gt;+&lt;/span&gt; 1&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;select&lt;/span&gt; &lt;span style="color:gray;"&gt;*&lt;/span&gt; &lt;span style="color:blue;"&gt;from&lt;/span&gt; ##rawdata3&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;span style="line-height:115%;font-size:10pt;mso-no-proof:yes;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;where&lt;/span&gt;
table_name &lt;span style="color:gray;"&gt;like&lt;/span&gt; &lt;span style="color:red;"&gt;&amp;#39;%SalesOrderHeader%&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16463" width="1" height="1"&gt;</description><feedburner:origLink>http://sqlblogcasts.com/blogs/stevesimonsays/archive/2013/04/22/resolving-data-redundancy-up-front.aspx</feedburner:origLink></item><item><title>Walking the walk at SQL Saturday Edinburgh</title><link>http://feedproxy.google.com/~r/UkSqlServerCommunityBlogs/~3/o3HLXJMmWAA/Walking-the-Walk-at-SQL-Saturday-Edinburgh.aspx</link><pubDate>Sun, 21 Apr 2013 19:44:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:16461</guid><dc:creator>MartinBell</dc:creator><slash:comments>0</slash:comments><description>Walking the Walk at SQL Saturday Edinburgh in aid of *** cancer....(&lt;a href="http://sqlblogcasts.com/blogs/martinbell/archive/2013/04/21/Walking-the-Walk-at-SQL-Saturday-Edinburgh.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16461" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/martinbell/archive/tags/SQL+Saturday/default.aspx">SQL Saturday</category><feedburner:origLink>http://sqlblogcasts.com/blogs/martinbell/archive/2013/04/21/Walking-the-Walk-at-SQL-Saturday-Edinburgh.aspx</feedburner:origLink></item><item><title>My blog has now moved to http://dataidol.com/tonyrogerson</title><link>http://feedproxy.google.com/~r/UkSqlServerCommunityBlogs/~3/1oAKi4hoyQo/my-blog-has-now-moved-to-http-dataidol-com-tonyrogerson.aspx</link><pubDate>Fri, 19 Apr 2013 08:19:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:16460</guid><dc:creator>tonyrogerson</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I&amp;#39;ve decided to implement a new blogging infrastructure based on WordPress; the community server stuff I&amp;#39;m using has got a bit tired and WordPress offers a wealth of plug-ins to do pretty much everything I want.&lt;/p&gt;&lt;p&gt;So, see &lt;a href="http://dataidol.com/tonyrogerson"&gt;http://dataidol.com/tonyrogerson&lt;/a&gt; for my new blog; its not just a move, I have broadened my coverage to encompass all things data, the first couple of posts talk about Short-Stroking hard disks (something I&amp;#39;ve presented on a number of times now), I&amp;#39;ve also got some Erlang content.&lt;/p&gt;&lt;p&gt;Anyway, enjoy!&lt;/p&gt;&lt;p&gt;T&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16460" width="1" height="1"&gt;</description><feedburner:origLink>http://sqlblogcasts.com/blogs/tonyrogerson/archive/2013/04/19/my-blog-has-now-moved-to-http-dataidol-com-tonyrogerson.aspx</feedburner:origLink></item><item><title>Monitoring SQL Server Agent job run times</title><link>http://feedproxy.google.com/~r/UkSqlServerCommunityBlogs/~3/HJ3_EiZq2FI/monitoring-sql-server-agent-job-run-times.aspx</link><pubDate>Mon, 15 Apr 2013 22:21:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:16457</guid><dc:creator>okeofs</dc:creator><slash:comments>1</slash:comments><description>&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;span style="mso-no-proof:yes;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;span style="color:black;font-size:12pt;"&gt;Introduction&lt;/span&gt;&lt;/font&gt;&lt;font size="3"&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3"&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;span style="color:black;font-size:10pt;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style="color:black;font-size:12pt;"&gt;A few months back, I was
asked how long a particular nightly process took to run. It was a super
question and the one thing that struck me was that there were a plethora of
factors affecting the processing time. This said, I developed a query to
ascertain process run times, the average nightly run times and applied some
KPI’s to the end query. The end goal being to enable me to quickly detect
anomalies and processes that are running beyond their normal times. &lt;br /&gt;
As many of you are aware, most of the necessary data for this type of query,
lies within the MSDB database.&lt;br /&gt;
The core portion of the query is shown below.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;line-height:normal;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;" class="MsoNormal"&gt;&lt;span&gt;&lt;font color="#999999"&gt;&lt;strong&gt;&lt;font color="#cc99ff"&gt;select
sj.name,sh.run_date, sh.run_duration, &lt;br /&gt;
case &lt;br /&gt;
when len(sh.run_duration) = 6 then convert(varchar(8),sh.run_duration)&lt;br /&gt;
when len(sh.run_duration) = 5 then &amp;#39;0&amp;#39; + convert(varchar(8),sh.run_duration)&lt;br /&gt;
when len(sh.run_duration) = 4 then &amp;#39;00&amp;#39; + convert(varchar(8),sh.run_duration)&lt;br /&gt;
when len(sh.run_duration) = 3 then &amp;#39;000&amp;#39; + convert(varchar(8),sh.run_duration)&lt;br /&gt;
when len(sh.run_duration) = 2 then &amp;#39;0000&amp;#39; + convert(varchar(8),sh.run_duration)&lt;br /&gt;
when len(sh.run_duration) = 1 then &amp;#39;00000&amp;#39; +
convert(varchar(8),sh.run_duration)&lt;br /&gt;
end as tt&lt;br /&gt;
from dbo.sysjobs sj with (nolock)&lt;br /&gt;
inner join dbo.sysjobHistory sh with (nolock)&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;font color="#cc99ff"&gt; &lt;br /&gt;&lt;/font&gt;&lt;strong&gt;&lt;font color="#cc99ff"&gt;
on sj.job_id = sh.job_id &lt;br /&gt;
where sj.name = &amp;#39;My Agent Job&amp;#39;&lt;br /&gt;
and [sh.Message] like &amp;#39;%The job%&amp;#39;)&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:12pt;mso-ansi-language:EN;"&gt;Run_date
and run_duration are obvious fields. The field ‘Name’ is the name of the job
that we wish to follow. The only major challenge was that the format of the run
duration which was not as ‘user friendly’ as I would have liked. As an example,
the run duration 1 hour 10 minutes and 3 seconds would be displayed as 11003;
whereas I wanted it to display this in a more user friendly manner as &lt;font color="#ff0000"&gt;&lt;strong&gt;01:10:03&lt;/strong&gt;.&lt;/font&gt; &lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:12pt;mso-ansi-language:EN;"&gt;In
order to achieve this effect, we need to add leading zeros to the run_duration
based upon the case logic shown above.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:12pt;mso-ansi-language:EN;"&gt;At
this point what we need to do add colons between the hours and minutes and one
between the minutes and seconds. To achieve this I nested the query shown above
(in purple) within a ‘super’ query. Thus the run time ([Run Time]) is
constructed concatenating a series of substrings (See below in Blue).&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:12pt;mso-ansi-language:EN;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span&gt;&lt;strong&gt;&lt;font color="#3366ff"&gt;select
run_date,substring(convert(varchar(20),tt),1,2) + &amp;#39;:&amp;#39; +&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span&gt;&lt;strong&gt;&lt;font color="#3366ff"&gt;substring(convert(varchar(20),tt),3,2) +
&amp;#39;:&amp;#39; +&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span&gt;&lt;strong&gt;&lt;font color="#3366ff"&gt;substring(convert(varchar(20),tt),5,2) as [run_time]
from&lt;/font&gt; (&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span&gt;&lt;strong&gt;&lt;font color="#cc99ff"&gt;select
sj.name,sh.run_date, sh.run_duration,&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;case
&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;when
len(sh.run_duration) = 6 then convert(varchar(8),sh.run_duration)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;when
len(sh.run_duration) = 5 then &amp;#39;0&amp;#39; + convert(varchar(8),sh.run_duration)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;when
len(sh.run_duration) = 4 then &amp;#39;00&amp;#39; + convert(varchar(8),sh.run_duration)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;when
len(sh.run_duration) = 3 then &amp;#39;000&amp;#39; + convert(varchar(8),sh.run_duration)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;when
len(sh.run_duration) = 2 then &amp;#39;0000&amp;#39; + convert(varchar(8),sh.run_duration)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;when
len(sh.run_duration) = 1 then &amp;#39;00000&amp;#39; + convert(varchar(8),sh.run_duration)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;end
as tt&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;from
dbo.sysjobs sj with (nolock)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;inner
join dbo.sysjobHistory sh with (nolock) &lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;on
sj.job_id = sh.job_id &lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;where
sj.name = &amp;#39;My Agent Job&amp;#39;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span&gt;&lt;strong&gt;&lt;font color="#999999"&gt;&lt;font color="#cc99ff"&gt;and
[sh.Message] like &amp;#39;%The job%&amp;#39;&lt;/font&gt;&lt;/font&gt;&lt;font color="#3366ff"&gt;) a&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;
&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:12pt;mso-ansi-language:EN;"&gt;Now
that I had each nightly run time in hours, minutes and seconds (01:10:03), I
decided that it would very productive to calculate a rolling run time average.
To do this, I decided to do the calculations in base units of seconds. &lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;span style="color:black;font-size:12pt;mso-ansi-language:EN;"&gt;This
said, I encapsulated the query shown above into a further ‘super’ query (see
the code in RED below). This encapsulation is shown below. The astute reader
will note that I used implied casting from integer to string, which is not the
best method to use however it works. This said and if I were constructing the
query again I would definitely do an &lt;strong&gt;explicit&lt;/strong&gt;
convert. To Recap: I now have a key field of ‘1’, each and every applicable run
date and the total number of SECONDS that the process ran for each run date,
all of this data within the #rawdata1 temporary table.&lt;/span&gt;&lt;span style="color:black;font-size:12pt;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;span style="color:red;font-size:14pt;mso-ansi-language:EN;"&gt;&lt;font size="3" face="courier new,courier"&gt;Select
1 as keyy,run_date,(substring(b.run_time,1,2)*3600) +
(substring(b.run_time,4,2)*60) + (substring(b.run_time,7,2)) as
run_time_in_Seconds,run_time into #rawdata1 from (&lt;/font&gt; &lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;span style="color:red;font-size:14pt;mso-ansi-language:EN;"&gt;&lt;/span&gt;&lt;font color="#3366ff"&gt;&lt;span&gt;&lt;strong&gt;select run_date,substring(convert(varchar(20),tt),1,2)
+ &amp;#39;:&amp;#39; + &lt;/strong&gt;&lt;/span&gt;&lt;span&gt;&lt;strong&gt;substring(convert(varchar(20),tt),3,2) +
&amp;#39;:&amp;#39; +&lt;/strong&gt;&lt;/span&gt;&lt;/font&gt;&lt;span&gt;&lt;strong&gt;&lt;font color="#3366ff"&gt;substring(convert(varchar(20),tt),5,2) as
[run_time] from (&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span&gt;&lt;strong&gt;&lt;font color="#cc99ff"&gt;select
sj.name,sh.run_date, sh.run_duration,&lt;/font&gt; &lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;case
&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;when
len(sh.run_duration) = 6 then convert(varchar(8),sh.run_duration)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;when
len(sh.run_duration) = 5 then &amp;#39;0&amp;#39; + convert(varchar(8),sh.run_duration)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;&lt;font color="#cc99ff"&gt;when
len(sh.run_duration) = 4 then &amp;#39;00&amp;#39; + convert(varchar(8),sh.run_duration)&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span&gt;&lt;strong&gt;&lt;font color="#cc99ff"&gt;when
len(sh.run_duration)&amp;nbsp;&amp;nbsp;&amp;nbsp; = 3 then &amp;#39;000&amp;#39; + convert(varchar(8),sh.run_duration)&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span&gt;&lt;strong&gt;&lt;font color="#cc99ff"&gt;when
len(sh.run_duration)&amp;nbsp;&amp;nbsp;&amp;nbsp; = 2 then &amp;#39;0000&amp;#39; + convert(varchar(8),sh.run_duration)&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;&lt;font color="#cc99ff"&gt;when
len(sh.run_duration) = 1 then &amp;#39;00000&amp;#39; + convert(varchar(8),sh.run_duration)&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;end
as tt&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;from
dbo.sysjobs sj with (nolock)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;inner
join dbo.sysjobHistory sh with (nolock)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;on
sj.job_id = sh.job_id &lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="font-size:14pt;mso-ansi-language:EN;"&gt;&lt;strong&gt;where
sj.name = &amp;#39;My Agent Job&amp;#39;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span&gt;&lt;font color="#cc99ff"&gt;&lt;strong&gt;and
[sh.Message] like &amp;#39;%The job%&lt;/strong&gt;&amp;#39;&lt;/font&gt;&lt;strong&gt;&lt;font color="#999999"&gt;)&lt;/font&gt; &lt;font color="#3366ff"&gt;a )&lt;/font&gt;&lt;font color="#ff0000"&gt;b&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:red;font-size:12pt;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:14pt;mso-ansi-language:EN;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;line-height:normal;" class="MsoNormal"&gt;&lt;strong&gt;&lt;u&gt;&lt;span style="color:black;font-size:12pt;mso-ansi-language:EN;"&gt;Calculating
the average run time&lt;/span&gt;&lt;/u&gt;&lt;span style="color:black;font-size:14pt;mso-ansi-language:EN;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:12pt;mso-ansi-language:EN;"&gt;We now select each run time in seconds from
#rawdata1 and place the values into another temporary table called #rawdata2.
Once again we create a ‘key’, a hardwired ‘1’.&lt;/span&gt;&lt;span style="color:black;font-size:10pt;mso-ansi-language:EN;"&gt; &lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:10pt;mso-ansi-language:EN;"&gt;&lt;font size="3" face="courier new,courier"&gt;select 1 as Keyy, run_time_in_Seconds into
#rawdata2 from #rawdata1&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:12pt;mso-ansi-language:EN;"&gt;The purpose of doing so is to make the
average time AVG() available to the query immediately without having to do
adverse grouping.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;" class="MsoNormal"&gt;&lt;span&gt;&lt;strong&gt;Applying KPI Logic&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:12pt;mso-ansi-language:EN;"&gt;At this point, we shall apply some logic to
determine whether processing times are within the norms. We do this by applying
colour names. Obviously, this example is a super one for SSRS and traffic light
icons.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:12pt;mso-ansi-language:EN;"&gt;&lt;/span&gt;&lt;span style="color:black;font-size:14pt;mso-ansi-language:EN;mso-no-proof:yes;"&gt;&lt;font size="3" face="courier new,courier"&gt;select rd1.run_date,
rd1.run_time, rd1.run_time_in_Seconds ,Avg(rd2.run_time_in_Seconds) as
Average_run_time_in_seconds,&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:14pt;mso-ansi-language:EN;mso-no-proof:yes;"&gt;&lt;font size="3" face="courier new,courier"&gt;case&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:14pt;mso-ansi-language:EN;mso-no-proof:yes;"&gt;&lt;font size="3" face="courier new,courier"&gt;whenConvert(decimal(10,1),rd1.run_time_in_Seconds)/Avg(rd2.run_time_in_Seconds)&amp;lt;=
1.2 then &amp;#39;Green&amp;#39; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 0pt;line-height:normal;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:14pt;mso-ansi-language:EN;mso-no-proof:yes;"&gt;&lt;font size="3" face="courier new,courier"&gt;when
Convert(decimal(10,1),rd1.run_time_in_Seconds)/Avg(rd2.run_time_in_Seconds)&amp;lt;
1.4 then &amp;#39;Yellow&amp;#39; else &amp;#39;Red&amp;#39;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;line-height:normal;" class="MsoNormal"&gt;&lt;span style="color:black;font-size:14pt;mso-ansi-language:EN;mso-no-proof:yes;"&gt;&lt;strong&gt;&lt;font size="3" face="courier new,courier"&gt;end as [color],&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:black;font-size:12pt;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;span&gt;&lt;strong&gt;Calculating
the Average Run Time in Hours Minutes and Seconds and the end of the query.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

case&lt;br /&gt;when len(convert(varchar(2),Avg(rd2.run_time_in_Seconds)/(3600))) = 1 then &amp;#39;0&amp;#39; + convert(varchar(2),Avg(rd2.run_time_in_Seconds)/(3600))&lt;br /&gt;else convert(varchar(2),Avg(rd2.run_time_in_Seconds)/(3600))&lt;br /&gt;end + &amp;#39;:&amp;#39; + &lt;br /&gt;case &lt;br /&gt;when len(convert(varchar(2),Avg(rd2.run_time_in_Seconds)%(3600)/60)) = 1 then &amp;#39;0&amp;#39; + convert(varchar(2),Avg(rd2.run_time_in_Seconds)%(3600)/60)&lt;br /&gt;else convert(varchar(2),Avg(rd2.run_time_in_Seconds)%(3600)/60)&lt;br /&gt;end + &amp;#39;:&amp;#39; + case &lt;br /&gt;when len(convert(varchar(2),Avg(rd2.run_time_in_Seconds)%60)) = 1 then &amp;#39;0&amp;#39; + convert(varchar(2),Avg(rd2.run_time_in_Seconds)%60)&lt;br /&gt;else convert(varchar(2),Avg(rd2.run_time_in_Seconds)%60)&lt;br /&gt;end as [Average Run Time HH:MM:SS] from #rawdata2 rd2 innerjoin #rawdata1 rd1&lt;br /&gt;on rd1.keyy = rd2.keyy&lt;br /&gt;group by run_date,rd1.run_time ,rd1.run_time_in_Seconds &lt;br /&gt;order by run_date desc&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;span&gt;&lt;strong&gt;The complete code example&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

use msdb&lt;br /&gt;go&lt;br /&gt;/*&lt;br /&gt;drop table #rawdata1&lt;br /&gt;drop table #rawdata2&lt;br /&gt;go&lt;br /&gt;*/&lt;br /&gt;select 1 as keyy,run_date,(substring(b.run_time,1,2)*3600) + (substring(b.run_time,4,2)*60) + (substring(b.run_time,7,2)) as run_time_in_Seconds,run_time into #rawdata1 from (&lt;br /&gt;select run_date,substring(convert(varchar(20),tt),1,2) + &amp;#39;:&amp;#39; +&lt;br /&gt;substring(convert(varchar(20),tt),3,2) + &amp;#39;:&amp;#39; +&lt;br /&gt;substring(convert(varchar(20),tt),5,2) as [run_time] from (&lt;br /&gt;select name,run_date, run_duration, &lt;br /&gt;case&lt;br /&gt;whenlen(run_duration) = 6 then convert(varchar(8),run_duration)&lt;br /&gt;whenlen(run_duration) = 5 then &amp;#39;0&amp;#39; + convert(varchar(8),run_duration)&lt;br /&gt;whenlen(run_duration) = 4 then &amp;#39;00&amp;#39; + convert(varchar(8),run_duration)&lt;br /&gt;whenlen(run_duration) = 3 then &amp;#39;000&amp;#39; + convert(varchar(8),run_duration)&lt;br /&gt;whenlen(run_duration) = 2 then &amp;#39;0000&amp;#39; + convert(varchar(8),run_duration)&lt;br /&gt;whenlen(run_duration) = 1 then &amp;#39;00000&amp;#39; + convert(varchar(8),run_duration)&lt;br /&gt;end as tt&lt;br /&gt;from dbo.sysjobs sj with (nolock)&lt;br /&gt;innerjoin dbo.sysjobHistory sh with (nolock) &lt;br /&gt;on sj.job_id = sh.job_id &lt;br /&gt;where name = &amp;#39;My Agent Job&amp;#39;&lt;br /&gt;and [Message] like &amp;#39;%The job%&amp;#39;) a ) b&lt;br /&gt;select 1 as Keyy, run_time_in_Seconds into #rawdata2 from #rawdata1&lt;br /&gt;select rd1.run_date, rd1.run_time, rd1.run_time_in_Seconds ,Avg(rd2.run_time_in_Seconds) as Average_run_time_in_seconds,&lt;br /&gt;case&lt;br /&gt;whenConvert(decimal(10,1),rd1.run_time_in_Seconds)/Avg(rd2.run_time_in_Seconds)&amp;lt;= 1.2 then &amp;#39;Green&amp;#39; &lt;br /&gt;when Convert(decimal(10,1),rd1.run_time_in_Seconds)/Avg(rd2.run_time_in_Seconds)&amp;lt; 1.4 then &amp;#39;Yellow&amp;#39; else &amp;#39;Red&amp;#39;&lt;br /&gt;end as [color],&lt;br /&gt;Case &lt;br /&gt;when len(convert(varchar(2),Avg(rd2.run_time_in_Seconds)/(3600))) = 1 then &amp;#39;0&amp;#39; + convert(varchar(2),Avg(rd2.run_time_in_Seconds)/(3600))&lt;br /&gt;else convert(varchar(2),Avg(rd2.run_time_in_Seconds)/(3600))&lt;br /&gt;end + &amp;#39;:&amp;#39; + &lt;br /&gt;case &lt;br /&gt;when len(convert(varchar(2),Avg(rd2.run_time_in_Seconds)%(3600)/60)) = 1 then &amp;#39;0&amp;#39; + convert(varchar(2),Avg(rd2.run_time_in_Seconds)%(3600)/60)&lt;br /&gt;else convert(varchar(2),Avg(rd2.run_time_in_Seconds)%(3600)/60)&lt;br /&gt;end + &amp;#39;:&amp;#39; + &lt;br /&gt;case &lt;br /&gt;when len(convert(varchar(2),Avg(rd2.run_time_in_Seconds)%60)) = 1 then &amp;#39;0&amp;#39; + convert(varchar(2),Avg(rd2.run_time_in_Seconds)%60)&lt;br /&gt;else convert(varchar(2),Avg(rd2.run_time_in_Seconds)%60)&lt;br /&gt;end as [Average Run Time HH:MM:SS] &lt;br /&gt;from #rawdata2 rd2 innerjoin #rawdata1 rd1&lt;br /&gt;on rd1.keyy = rd2.keyy&lt;br /&gt;group by run_date,rd1.run_time ,rd1.run_time_in_Seconds &lt;br /&gt;order by run_date desc&lt;br /&gt;&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Calibri"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;&lt;p style="margin:0in 0in 10pt;" class="MsoNormal"&gt;&lt;font face="Times New Roman"&gt;

&lt;/font&gt;&lt;/p&gt;&lt;/font&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16457" width="1" height="1"&gt;</description><feedburner:origLink>http://sqlblogcasts.com/blogs/stevesimonsays/archive/2013/04/15/monitoring-sql-server-agent-job-run-times.aspx</feedburner:origLink></item><item><title>Restoring databases to a set drive and directory</title><link>http://feedproxy.google.com/~r/UkSqlServerCommunityBlogs/~3/ZS-bo2hbh1o/restoring-databases-to-a-set-drive-and-directory.aspx</link><pubDate>Mon, 15 Apr 2013 22:18:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:16456</guid><dc:creator>okeofs</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;span style="line-height:115%;font-size:12pt;"&gt;Restoring databases to a set drive 
and directory&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;font size="3"&gt;Introduction&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&lt;font size="3"&gt;Often people say 
that necessity is the mother of invention. In this case I was faced with the 
dilemma of having to restore several databases, with multiple ‘ndf’ files, and 
having to restore them with different physical file names, drives and 
directories on servers other than the servers from which they 
originated.&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&lt;font size="3"&gt;As most of us would 
do, I went to Google to see if I could find some code to achieve this task and 
found some interesting snippets on Pinal Dave’s website.&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&lt;font size="3"&gt;Naturally, I had to 
take it further than the code snippet, HOWEVER it was a great place to 
start.&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;font size="3"&gt;Creating a temp table to 
hold database file details&lt;/font&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;First off, I created 
a temp table which would hold the details of the individual data files within 
the database. Although there are a plethora of fields (within the temp table 
below), I utilize LogicalName only within this example. The temporary table 
structure may be seen below:&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;create 
table #tmp&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;(&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;LogicalName 
nvarchar(128) &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;,PhysicalName 
nvarchar(260) &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;,Type 
char(1) &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;,FileGroupName 
nvarchar(128) &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;,Size 
numeric(20,0) &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;,MaxSize 
numeric(20,0),&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;Fileid 
tinyint,&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;CreateLSN 
numeric(25,0),&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;DropLSN 
numeric(25, 0),&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;UniqueID 
uniqueidentifier,&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;ReadOnlyLSN 
numeric(25,0),&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;ReadWriteLSN 
numeric(25,0),&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;BackupSizeInBytes 
bigint,&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;SourceBlocSize 
int,&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;FileGroupId 
int,&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;LogGroupGUID 
uniqueidentifier,&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;DifferentialBaseLSN 
numeric(25,0),&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;DifferentialBaseGUID 
uniqueidentifier,&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;IsReadOnly 
bit,&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;IsPresent 
bit, &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;TDEThumbPrint 
varchar(50)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span style="line-height:115%;font-size:10pt;mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA;"&gt;&lt;br style="page-break-before:always;mso-special-character:line-break;" /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 10pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;We now declare and 
populate a variable(@path), setting the variable to the path to our SOURCE 
database backup.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;declare 
@path varchar(50)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;set @path 
= &amp;#39;P:\DATA\MYDATABASE.bak&amp;#39;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;From this point, we 
insert the file details of our database into the temp table. Note that we do so 
by utilizing a restore statement HOWEVER doing so in ‘filelistonly’ 
mode.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;insert 
#tmp&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;EXEC 
(&amp;#39;restore &lt;b style="mso-bidi-font-weight:normal;"&gt;filelistonly&lt;/b&gt; from disk = 
&amp;#39;&amp;#39;&amp;#39; + @path + &amp;#39;&amp;#39;&amp;#39;&amp;#39;)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;At this point, I depart 
from what I gleaned from Pinal Dave.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;I now instantiate a few 
more local variables. The use of each variable will be evident within the cursor 
(which follows):&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;Declare 
@RestoreString as Varchar(max)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;Declare 
@NRestoreString as NVarchar(max)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;Declare 
@LogicalName&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;as 
varchar(75)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;Declare 
@counter as int&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;Declare 
@rows as int&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;set 
@counter = 1&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;select 
@rows = COUNT(*) from #tmp&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span&gt;-- Count the number of records in the 
temp&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;-- table&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;span&gt;&lt;font size="3"&gt;Declaring and 
populating the cursor&lt;/font&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;At this point I do 
realize that many people are cringing about the use of a cursor. Being an Oracle 
professional as well, I have learnt that there is a time and place for 
cursors.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;I would remind the 
reader that the data that will be read into the cursor is from a local temp 
table and as such, any locking of the records (within the temp table) is not 
really an issue.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;DECLARE 
MY_CURSOR Cursor &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;FOR 
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;Select 
LogicalName &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;From 
#tmp&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;span&gt;&lt;font size="3"&gt;Parsing the logical 
names from within the cursor.&lt;/font&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;A small caveat that 
works in our favour, &lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;is that the first 
logical name (of our database) is the logical name of the primary data file 
(.mdf). Other files, except for the very last logical name, belong to secondary 
data files. The last logical name is that of our database log 
file.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;span&gt;&lt;span style="text-decoration:none;"&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;I now open my cursor 
and populate the variable @RestoreString&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;Open 
My_Cursor &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;set 
@RestoreString = &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&amp;#39;RESTORE 
DATABASE [MYDATABASE] FROM DISK = N&amp;#39;&amp;#39;P:\DATA\ MYDATABASE.bak&amp;#39;&amp;#39;&amp;#39; + &amp;#39; with&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&amp;#39;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;We now fetch the first 
record from the temp table.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;Fetch 
NEXT FROM MY_Cursor INTO @LogicalName&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;While there are STILL 
records left within the cursor, we dynamically build our restore string. Note 
that we are using concatenation to create ‘one big restore executable 
string’.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;Note also that the 
target physical file name is hardwired, as is the target 
directory.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;While 
(@@FETCH_STATUS &amp;lt;&amp;gt; -1)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;BEGIN&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;IF 
(@@FETCH_STATUS &amp;lt;&amp;gt; -2) &lt;span&gt;-- As long as there 
are no rows missing&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;select 
@RestoreString =&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;case 
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;when 
@counter = 1 then &lt;span&gt;-- This is the mdf 
file&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;@RestoreString + &amp;#39;move&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;N&amp;#39;&amp;#39;&amp;#39; + @LogicalName + &amp;#39;&amp;#39;&amp;#39;&amp;#39; + &amp;#39; TO 
N’’X:\DATA1\&amp;#39;+ @LogicalName + &amp;#39;.mdf&amp;#39; + &amp;#39;&amp;#39;&amp;#39;&amp;#39; + &amp;#39;, &amp;#39;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;-- 
OK, if it passes through here we are dealing with an .ndf 
file&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;-- 
Note that Counter must be greater than 1 and less than the number of 
rows.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;when 
@counter &amp;gt; 1 and @counter &amp;lt; @rows then &lt;span&gt;-- 
These are the ndf file(s)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;@RestoreString + &amp;#39;move&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;N&amp;#39;&amp;#39;&amp;#39; + @LogicalName + &amp;#39;&amp;#39;&amp;#39;&amp;#39; + &amp;#39; TO 
N’’X:\DATA1\&amp;#39;+ @LogicalName + &amp;#39;.ndf&amp;#39; + &amp;#39;&amp;#39;&amp;#39;&amp;#39; + &amp;#39;, &amp;#39;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;-- 
OK, if it passes through here we are dealing with the log 
file&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;When 
@LogicalName like &amp;#39;%log%&amp;#39; then&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;@RestoreString + &amp;#39;move&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;N&amp;#39;&amp;#39;&amp;#39; + @LogicalName + &amp;#39;&amp;#39;&amp;#39;&amp;#39; + &amp;#39; TO 
N’’X:\DATA1\&amp;#39;+ @LogicalName + &amp;#39;.ldf&amp;#39; +&amp;#39;&amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;end&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;--Increment 
the counter&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;set 
@counter = @counter + 1&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;FETCH 
NEXT FROM MY_CURSOR INTO @LogicalName&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;END&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;At this point we have 
populated the varchar(max) variable @RestoreString with a concatenation of all 
the necessary file names. What we now need to do is to run the sp_executesql 
stored procedure, to effect the restore.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;First, we must place 
our ‘concatenated string’ into an nvarchar based variable. Obviously this will 
only work as long as the length of @RestoreString is less than varchar(max) / 
2.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;set @NRestoreString = 
@RestoreString&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;EXEC sp_executesql 
@NRestoreString&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;Upon completion of this 
step, the database should be restored to the 
server.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;I now close and 
deallocate the cursor, and to be clean, I would also drop my temp 
table.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;CLOSE 
MY_CURSOR&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;DEALLOCATE 
MY_CURSOR&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="line-height:115%;font-size:10pt;"&gt;GO&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;u&gt;&lt;span&gt;&lt;font size="3"&gt;Conclusion&lt;/font&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;Restoration of 
databases on different servers with different physical names and on different 
drives are a fact of life. Through the use of a few variables and a simple 
cursor, we may achieve an efficient and effective way to achieve this 
task.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16456" width="1" height="1"&gt;</description><feedburner:origLink>http://sqlblogcasts.com/blogs/stevesimonsays/archive/2013/04/15/restoring-databases-to-a-set-drive-and-directory.aspx</feedburner:origLink></item><item><title>On the move</title><link>http://feedproxy.google.com/~r/UkSqlServerCommunityBlogs/~3/XpCt2zfc7DI/on-the-move.aspx</link><pubDate>Sun, 14 Apr 2013 19:01:41 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:16455</guid><dc:creator>Dave Ballantyne</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Its been a fair few years since I started blogging on SQLBlogCasts and the time has now come to find a new home.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/sqlandthelike/16600796_s_0C73341C.jpg"&gt;&lt;img title="16600796_s" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;display:inline;padding-right:0px;" border="0" alt="16600796_s" src="http://sqlblogcasts.com/blogs/sqlandthelike/16600796_s_thumb_07908060.jpg" width="244" height="220" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;From now on (along with a few others of the UK SQL Server community) I shall be blogging on dataidol.com.&amp;#160; My page will be at &lt;a title="http://dataidol.com/davebally" href="http://dataidol.com/davebally"&gt;http://dataidol.com/davebally&lt;/a&gt; , please update any feeds etc and I hope to see you there.&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16455" width="1" height="1"&gt;</description><feedburner:origLink>http://sqlblogcasts.com/blogs/sqlandthelike/archive/2013/04/14/on-the-move.aspx</feedburner:origLink></item><item><title>Sub query pass through</title><link>http://feedproxy.google.com/~r/UkSqlServerCommunityBlogs/~3/uZAieXh9-1o/sub-query-pass-through.aspx</link><pubDate>Fri, 22 Mar 2013 09:10:14 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:16454</guid><dc:creator>SQL and the like</dc:creator><slash:comments>0</slash:comments><description>Occasionally in forums and on client sites I see conditional subqueries in statements. This is where the developer has decided that it is only necessary to process some data under a certain condition.&amp;#160; By way of example, something like this : Create Procedure GetOrder @SalesOrderId integer, @CountDetails tinyint as Select SOH.salesorderid , case when @CountDetails = 1 then (Select count(*) from Sales.SalesOrderDetail SOD where SOH.SalesOrderID = SOD.SalesOrderID) end from sales.SalesOrderHeader...(&lt;a href="http://sqlblogcasts.com/blogs/sqlandthelike/archive/2013/03/22/sub-query-pass-through.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16454" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/sqlandthelike/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://sqlblogcasts.com/blogs/sqlandthelike/archive/tags/SQLServer/default.aspx">SQLServer</category><category domain="http://sqlblogcasts.com/blogs/sqlandthelike/archive/tags/Query+Plan/default.aspx">Query Plan</category><category domain="http://sqlblogcasts.com/blogs/sqlandthelike/archive/tags/Optimizer/default.aspx">Optimizer</category><feedburner:origLink>http://sqlblogcasts.com/blogs/sqlandthelike/archive/2013/03/22/sub-query-pass-through.aspx</feedburner:origLink></item><item><title>Architects and Architectures</title><link>http://feedproxy.google.com/~r/UkSqlServerCommunityBlogs/~3/XFaY8Y-MgjM/architects_2D00_and_2D00_architectures.aspx</link><pubDate>Tue, 12 Mar 2013 18:55:45 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:16450</guid><dc:creator>GavinPayneUK</dc:creator><slash:comments>1</slash:comments><description>In the last few months I’ve been presenting a new session at community events that’s had a positive reception that I’m pleasantly surprised at. It’s had several titles, but currently its “How to be a more successful architect” and I originally created it as an overview of my Microsoft Certified Architect journey, although I’ve taken it on a different course since then. Hopefully of interest to everyone The session is hopefully interesting to everyone in the IT industry, not just a specific type of...(&lt;a href="http://sqlblogcasts.com/blogs/gavinpayneuk/archive/2013/03/12/architects_2D00_and_2D00_architectures.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16450" width="1" height="1"&gt;</description><feedburner:origLink>http://sqlblogcasts.com/blogs/gavinpayneuk/archive/2013/03/12/architects_2D00_and_2D00_architectures.aspx</feedburner:origLink></item></channel></rss>
