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

<channel>
	<title>youdidwhatwithtsql.com &#187; SQL Server</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/sql-server/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Check Mirroring Status with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/check-mirroring-status-powershell/1436</link>
		<comments>http://www.youdidwhatwithtsql.com/check-mirroring-status-powershell/1436#comments</comments>
		<pubDate>Tue, 31 Jan 2012 12:21:23 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[mirroring]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=1436</guid>
		<description><![CDATA[Here&#8217;s a simple Powershell snippet to check the mirroring status on your SQL Server instances. ?View Code POWERSHELL# Load SMO extension &#91;System.Reflection.Assembly&#93;::LoadWithPartialName&#40;&#34;Microsoft.SqlServer.Smo&#34;&#41; &#124; Out-Null; &#160; # Servers to check $sqlservers = @&#40;&#34;server1&#34;, &#34;server2&#34;, &#34;server3&#34;&#41;; foreach&#40;$server in $sqlservers&#41; &#123; $srv = New-Object &#34;Microsoft.SqlServer.Management.Smo.Server&#34; $server; # Get mirrored databases $databases = $srv.Databases &#124; Where-Object &#123;$_.IsMirroringEnabled -eq $true&#125;; Write-Host [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/check-mirroring-status-powershell/1436">Check Mirroring Status with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a simple <a title="Windows Powershell" href="http://technet.microsoft.com/en-us/scriptcenter/dd742419" target="_blank">Powershell</a> snippet to check the <a title="SQL Server Mirroring Status" href="http://msdn.microsoft.com/en-us/library/ms365781.aspx" target="_blank">mirroring status</a> on your SQL Server instances.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1436code2'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14362"><td class="code" id="p1436code2"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Load SMO extension</span>
<span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.SqlServer.Smo&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>;
&nbsp;
<span style="color: #008000;"># Servers to check</span>
<span style="color: #800080;">$sqlservers</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;server1&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;server2&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;server3&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$server</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$sqlservers</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$srv</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #800000;">&quot;Microsoft.SqlServer.Management.Smo.Server&quot;</span> <span style="color: #800080;">$server</span>;
	<span style="color: #008000;"># Get mirrored databases</span>
	<span style="color: #800080;">$databases</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Databases <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><span style="color: #000080;">$_</span>.IsMirroringEnabled <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#125;</span>; 
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$server</span>;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;==================================&quot;</span>;
	<span style="color: #800080;">$databases</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select-Object</span> <span style="color: #008080; font-style: italic;">-Property</span> Name<span style="color: pink;">,</span> MirroringStatus <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Format-Table</span> <span style="color: #008080; font-style: italic;">-AutoSize</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>This will output something looking like below&#8230;</p>
<pre>server1
====================================

Name                 MirroringStatus
----                 ---------------
db1         		Synchronized
db2                    	Synchronized
db3              	Synchronized
db4			Synchronized
db5    			Synchronized
server2
=====================================

Name                 MirroringStatus
----                 ---------------
db1         		Synchronized
db2                    	Synchronized
db3              	Synchronized
db4			Synchronized
db5    			Synchronized
server3
=====================================

Name                 MirroringStatus
----                 ---------------
db1         		Synchronized
db2                    	Synchronized
db3              	Synchronized
db4			Synchronized
db5    			Synchronized</pre>
<p><map name='google_ad_map_1436_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1436?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1436_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1436&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcheck-mirroring-status-powershell%2F1436' title="Check Mirroring Status with Powershell" alt=" Check Mirroring Status with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/check-mirroring-status-powershell/1436">Check Mirroring Status with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/check-mirroring-status-powershell/1436" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/check-mirroring-status-powershell/1436/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>View backup file details with TSQL</title>
		<link>http://www.youdidwhatwithtsql.com/view-backup-file-details-with-tsql/1073</link>
		<comments>http://www.youdidwhatwithtsql.com/view-backup-file-details-with-tsql/1073#comments</comments>
		<pubDate>Sun, 20 Mar 2011 16:46:43 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Backups]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/view-backup-file-details-with-tsql/1073</guid>
		<description><![CDATA[In order to automate testing of backups it&#8217;s useful to be able to query backup files to access various bits of meta-data. We can do this with the RESTORE FILELISTONLY TSQL command. In the simplest format the command is as follows; ?View Code TSQLRESTORE FILELISTONLY FROM DISK = 'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Backup\AdventureWorks_20110320.bak'; This doesn&#8217;t help [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/view-backup-file-details-with-tsql/1073">View backup file details with TSQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In order to automate testing of backups it&#8217;s useful to be able to query backup files to access various bits of meta-data. We can do this with the <a title="RESTORE FILELISTONLY" href="http://msdn.microsoft.com/en-us/library/ms173778.aspx" target="_blank">RESTORE FILELISTONLY</a> TSQL command. In the simplest format the command is as follows;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1073code5'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10735"><td class="code" id="p1073code5"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">RESTORE</span> FILELISTONLY 
<span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">DISK</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'c:<span style="color: #000099; font-weight: bold;">\P</span>rogram Files<span style="color: #000099; font-weight: bold;">\M</span>icrosoft SQL Server<span style="color: #000099; font-weight: bold;">\M</span>SSQL10_50.SQLEXPRESS<span style="color: #000099; font-weight: bold;">\M</span>SSQL<span style="color: #000099; font-weight: bold;">\B</span>ackup<span style="color: #000099; font-weight: bold;">\A</span>dventureWorks_20110320.bak'</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/View-backup-file-details-with-TSQL_E57D/restore_filelistonly_tsql.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="restore filelistonly tsql" border="0" alt="restore filelistonly tsql thumb View backup file details with TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/View-backup-file-details-with-TSQL_E57D/restore_filelistonly_tsql_thumb.png" width="644" height="61" /></a></p>
<p>This doesn&#8217;t help us much with automation. We need to get this resultset into a table so we can make use of the information it provides in our stored procedures. We can do this with a bit of trickery with the <a title="TSQL EXECUTE command" href="http://msdn.microsoft.com/en-us/library/ms188332.aspx" target="_blank">EXECUTE</a> command.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1073code6'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10736"><td class="code" id="p1073code6"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Define a table variable to hold our resultset</span>
<span style="color: #008080;">-- Will only be accessible in the current batch. </span>
<span style="color: #008080;">-- Consider using a temp table for other needs</span>
<span style="color: #0000FF;">DECLARE</span> @filelist <span style="color: #0000FF;">TABLE</span>
<span style="color: #808080;">&#40;</span>
	LogicalName <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	PhysicalName <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">260</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>Type<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	FileGroupName <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #0000FF;">Size</span> <span style="color: #0000FF;">NUMERIC</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	MaxSize <span style="color: #0000FF;">NUMERIC</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	FileId <span style="color: #0000FF;">BIGINT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	CreateLSN <span style="color: #0000FF;">NUMERIC</span><span style="color: #808080;">&#40;</span><span style="color: #000;">25</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	DropLSN <span style="color: #0000FF;">NUMERIC</span><span style="color: #808080;">&#40;</span><span style="color: #000;">25</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	UniqueId <span style="color: #0000FF;">UNIQUEIDENTIFIER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	ReadOnlyLSN <span style="color: #0000FF;">NUMERIC</span><span style="color: #808080;">&#40;</span><span style="color: #000;">25</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	ReadWriteLSN <span style="color: #0000FF;">NUMERIC</span><span style="color: #808080;">&#40;</span><span style="color: #000;">25</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	BackupSizeInBytes <span style="color: #0000FF;">BIGINT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	SourceBlockSize <span style="color: #0000FF;">INT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	FileGroupId <span style="color: #0000FF;">INT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	LogGroupGUID <span style="color: #0000FF;">UNIQUEIDENTIFIER</span> <span style="color: #808080;">NULL</span>,
	DifferentialBaseLSN <span style="color: #0000FF;">NUMERIC</span><span style="color: #808080;">&#40;</span><span style="color: #000;">25</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	DifferentialBaseGUID <span style="color: #0000FF;">UNIQUEIDENTIFIER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	IsReadOnly <span style="color: #0000FF;">BIT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	IsPresent <span style="color: #0000FF;">BIT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	TDEThumbprint <span style="color: #0000FF;">VARBINARY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">32</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">-- Insert the data from the RESTORE FILELISTONLY command</span>
<span style="color: #008080;">-- into our table variable</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @filelist
<span style="color: #0000FF;">EXECUTE</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'RESTORE FILELISTONLY 
	FROM DISK = '</span><span style="color: #FF0000;">'c:<span style="color: #000099; font-weight: bold;">\P</span>rogram Files<span style="color: #000099; font-weight: bold;">\M</span>icrosoft SQL Server<span style="color: #000099; font-weight: bold;">\M</span>SSQL10_50.SQLEXPRESS<span style="color: #000099; font-weight: bold;">\M</span>SSQL<span style="color: #000099; font-weight: bold;">\B</span>ackup<span style="color: #000099; font-weight: bold;">\A</span>dventureWorks_20110320.bak'</span><span style="color: #FF0000;">''</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">-- Show that the table variable contains the data</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> @filelist;</pre></td></tr></table></div>

<p><map name='google_ad_map_1073_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1073?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1073_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1073&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fview-backup-file-details-with-tsql%2F1073' title="View backup file details with TSQL" alt=" View backup file details with TSQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/view-backup-file-details-with-tsql/1073">View backup file details with TSQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/view-backup-file-details-with-tsql/1073" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/view-backup-file-details-with-tsql/1073/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gathering SQL Server Data Cache Information: Part 2</title>
		<link>http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information-part-2/879</link>
		<comments>http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information-part-2/879#comments</comments>
		<pubDate>Wed, 29 Sep 2010 14:20:18 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[data cache]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information-part-2/879</guid>
		<description><![CDATA[In a previous post I showed how you can collect information on what is held in the data cache. The data collected here was just a simple summary of how much space each database was consuming. While useful we will need more detailed information on what is inside the cache to get a proper handle [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information-part-2/879">Gathering SQL Server Data Cache Information: Part 2</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In a previous post I showed how you can collect information on <a href="http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information/874" target="_blank">what is held in the data cache</a>. The data collected here was just a simple summary of how much space each database was consuming. While useful we will need more detailed information on what is inside the cache to get a proper handle on things.</p>
<p>The below query has been modified from the post: <a href="http://blogs.msdn.com/b/sqlperf/archive/2007/05/18/bufferpooldatapagesbreakdown.aspx" target="_blank">What’s swimming in your buffer pool?</a> This procedure will tell us exactly what objects are contained within the data cache.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p879code10'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p87910"><td class="code" id="p879code10"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
&nbsp;
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
&nbsp;
&nbsp;
<span style="color: #008080;">-- =============================================</span>
<span style="color: #008080;">-- Author:		Rhys Campbell</span>
<span style="color: #008080;">-- Create date: 2010-09-28</span>
<span style="color: #008080;">-- Description:	Gets info on objects in the data</span>
<span style="color: #008080;">-- cache broken by database.</span>
<span style="color: #008080;">-- =============================================</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>GetDataCacheObjects<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #008080;">-- SET NOCOUNT ON added to prevent extra result sets from</span>
	<span style="color: #008080;">-- interfering with SELECT statements.</span>
	<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;
&nbsp;
    <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">@@SERVERNAME</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'Server'</span>,
		   <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> collected_at,
		   <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span>database_id<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'database'</span>,
		   <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span><span style="color: #0000FF;">AS</span> cached_pages_count,
		   obj.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> objectname,
		   ind.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> indexname,
		   obj.<span style="color: #202020;">index_id</span> <span style="color: #0000FF;">AS</span> indexid
	<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_os_buffer_descriptors</span> <span style="color: #0000FF;">AS</span> bd
    <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
    <span style="color: #808080;">&#40;</span>
        <span style="color: #0000FF;">SELECT</span>       <span style="color: #FF00FF;">object_id</span> <span style="color: #0000FF;">AS</span> objectid,
                           <span style="color: #FF00FF;">object_name</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">object_id</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> name,
                           index_id,allocation_unit_id
        <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">allocation_units</span> <span style="color: #0000FF;">AS</span> au
            <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">partitions</span> <span style="color: #0000FF;">AS</span> p
                <span style="color: #0000FF;">ON</span> au.<span style="color: #202020;">container_id</span> <span style="color: #808080;">=</span> p.<span style="color: #202020;">hobt_id</span>
                    <span style="color: #808080;">AND</span> <span style="color: #808080;">&#40;</span>au.<span style="color: #202020;">type</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #808080;">OR</span> au.<span style="color: #202020;">type</span> <span style="color: #808080;">=</span> <span style="color: #000;">3</span><span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
        <span style="color: #0000FF;">SELECT</span>       <span style="color: #FF00FF;">object_id</span> <span style="color: #0000FF;">AS</span> objectid,
                           <span style="color: #FF00FF;">object_name</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">object_id</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> name,
                           index_id,allocation_unit_id
        <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">allocation_units</span> <span style="color: #0000FF;">AS</span> au
            <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">partitions</span> <span style="color: #0000FF;">AS</span> p
                <span style="color: #0000FF;">ON</span> au.<span style="color: #202020;">container_id</span> <span style="color: #808080;">=</span> p.<span style="color: #202020;">partition_id</span>
                    <span style="color: #808080;">AND</span> au.<span style="color: #202020;">type</span> <span style="color: #808080;">=</span> <span style="color: #000;">2</span>
    <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> obj
        <span style="color: #0000FF;">ON</span> bd.<span style="color: #202020;">allocation_unit_id</span> <span style="color: #808080;">=</span> obj.<span style="color: #202020;">allocation_unit_id</span>
	<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">OUTER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">indexes</span> ind 
		<span style="color: #0000FF;">ON</span>  obj.<span style="color: #202020;">objectid</span> <span style="color: #808080;">=</span> ind.<span style="color: #FF00FF;">object_id</span>
	<span style="color: #808080;">AND</span>  obj.<span style="color: #202020;">index_id</span> <span style="color: #808080;">=</span> ind.<span style="color: #202020;">index_id</span>
	<span style="color: #0000FF;">WHERE</span> bd.<span style="color: #202020;">page_type</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'data_page'</span>, <span style="color: #FF0000;">'index_page'</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span>database_id<span style="color: #808080;">&#41;</span>, obj.<span style="color: #202020;">name</span>, ind.<span style="color: #202020;">name</span>, obj.<span style="color: #202020;">index_id</span>
	<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> cached_pages_count <span style="color: #0000FF;">DESC</span>
&nbsp;
<span style="color: #0000FF;">END</span>
&nbsp;
&nbsp;
GO</pre></td></tr></table></div>

<p>This will produce a dataset similar to below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/objects_in_the_sql_server_data_cache.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="objects in the sql server data cache" border="0" alt="objects in the sql server data cache thumb Gathering SQL Server Data Cache Information: Part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/objects_in_the_sql_server_data_cache_thumb.png" width="644" height="294" /></a> </p>
<p>This proc can be called by the <a href="http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information/874" target="_blank">Powershell script in the previous post</a> (nice and easy to run against multiple servers) and will produce a csv file looking like below. Just change the line </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p879code11'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p87911"><td class="code" id="p879code11"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$proc</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;EXEC dbo.GetDataCache&quot;</span>;</pre></td></tr></table></div>

<p>to</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p879code12'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p87912"><td class="code" id="p879code12"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$proc</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;EXEC dbo.GetDataCacheObjects&quot;</span>;</pre></td></tr></table></div>

<p>This is ready to be imported into a database for future analysis.</p>
<pre>&quot;Server&quot;,&quot;collected_at&quot;,&quot;database&quot;,&quot;cached_pages_count&quot;,&quot;objectname&quot;,&quot;indexname&quot;,&quot;indexid&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;&quot;,&quot;1167&quot;,&quot;sysobjvalues&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;&quot;,&quot;304&quot;,&quot;syscolpars&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;&quot;,&quot;190&quot;,&quot;tweet_followersIds&quot;,&quot;PK__tweet_fo__3214EC0735DCF99B&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;&quot;,&quot;56&quot;,&quot;sysschobjs&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;test&quot;,&quot;32&quot;,&quot;sysobjvalues&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;&quot;,&quot;27&quot;,&quot;sysobjkeycrypts&quot;,&quot;cl&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;msdb&quot;,&quot;25&quot;,&quot;sysschobjs&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;master&quot;,&quot;22&quot;,&quot;sysobjvalues&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;msdb&quot;,&quot;14&quot;,&quot;sysobjvalues&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;test&quot;,&quot;13&quot;,&quot;syscolpars&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;msdb&quot;,&quot;13&quot;,&quot;syscolpars&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;&quot;,&quot;12&quot;,&quot;sysrscols&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;msdb&quot;,&quot;12&quot;,&quot;sysrscols&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;master&quot;,&quot;11&quot;,&quot;sysrscols&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;msdb&quot;,&quot;10&quot;,&quot;sysobjkeycrypts&quot;,&quot;cl&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;&quot;,&quot;9&quot;,&quot;sysschobjs&quot;,&quot;nc2&quot;,&quot;3&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;&quot;,&quot;8&quot;,&quot;sysidxstats&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;model&quot;,&quot;8&quot;,&quot;sysobjvalues&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;model&quot;,&quot;8&quot;,&quot;sysrscols&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;tempdb&quot;,&quot;8&quot;,&quot;sysobjvalues&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;tempdb&quot;,&quot;8&quot;,&quot;sysrscols&quot;,&quot;clst&quot;,&quot;1&quot;
&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;29/09/2010 14:58:32&quot;,&quot;test&quot;,&quot;8&quot;,&quot;sysrscols&quot;,&quot;clst&quot;,&quot;1&quot;</pre>
<p>Once several snapshots of the data cache have been made this should help us with performance problem diagnosis and estimate the “warm-up time” after a reboot.</p>
<p><map name='google_ad_map_879_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/879?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_879_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=879&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fgathering-sql-server-data-cache-information-part-2%2F879' title="Gathering SQL Server Data Cache Information: Part 2" alt=" Gathering SQL Server Data Cache Information: Part 2" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information-part-2/879">Gathering SQL Server Data Cache Information: Part 2</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information-part-2/879" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information-part-2/879/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Gathering SQL Server Data Cache Information</title>
		<link>http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information/874</link>
		<comments>http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information/874#comments</comments>
		<pubDate>Tue, 28 Sep 2010 12:17:59 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[buffer pool]]></category>
		<category><![CDATA[data cache]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information/874</guid>
		<description><![CDATA[I’m currently building a CMDB at work and I wanted to include a bit of performance data in the information collected. The data cache is probably the biggest consumer of memory within SQL Server so It makes sense to collect this for future analysis. As a start I’m gathering the amount of space each database [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information/874">Gathering SQL Server Data Cache Information</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I’m currently building a <a href="http://en.wikipedia.org/wiki/Configuration_management_database" target="_blank">CMDB</a> at work and I wanted to include a bit of performance data in the information collected. The data cache is probably the biggest consumer of memory within SQL Server so It makes sense to collect this for future analysis. As a start I’m gathering the amount of space each database consumes in the data cache. I’ve created the below proc from a query in the excellent <a href="http://www.brentozar.com/archive/2009/12/pro-sql-server-2008-internals-and-troubleshooting/" target="_blank">Pro SQL Server 2008 Internals &amp; Troubleshooting</a>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p874code15'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p87415"><td class="code" id="p874code15"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
<span style="color: #008080;">-- =============================================</span>
<span style="color: #008080;">-- Author:		Rhys Campbell</span>
<span style="color: #008080;">-- Create date: 2010-09-28</span>
<span style="color: #008080;">-- Description:	Provides a summary of how much</span>
<span style="color: #008080;">-- space each database is using in the data cache</span>
<span style="color: #008080;">-- =============================================</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> GetDataCache
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
	<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;
&nbsp;
    <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">*</span> <span style="color: #000;">8</span> <span style="color: #808080;">/</span> <span style="color: #000;">1024</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'Cache Size (MB)'</span>,
		   <span style="color: #0000FF;">CASE</span> database_id
				<span style="color: #0000FF;">WHEN</span> <span style="color: #000;">32767</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'ResourceDb'</span>
				<span style="color: #0000FF;">ELSE</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span>database_id<span style="color: #808080;">&#41;</span>
		   <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'Database'</span>,
		   <span style="color: #FF00FF;">@@SERVERNAME</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'Server'</span>,
		   <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'collected_at'</span>
	<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_os_buffer_descriptors</span>
	<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span>database_id<span style="color: #808080;">&#41;</span>, database_id
	<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF0000;">'Cache Size (MB)'</span> <span style="color: #0000FF;">DESC</span>;
&nbsp;
<span style="color: #0000FF;">END</span>
GO</pre></td></tr></table></div>

<p>This will produce a dataset similar to below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/sql_data_cache_use_by_database.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sql data cache use by database" border="0" alt="sql data cache use by database thumb Gathering SQL Server Data Cache Information" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/sql_data_cache_use_by_database_thumb.png" width="644" height="134" /></a> </p>
</p>
<p>I’ve written a <a href="http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx" target="_blank">Powershell</a> script to make it easy to query multiple servers. The script assumes that each sql instance to be queried contains the same database with the above proc. This script should be able to call any procedure as it simply exports the resultset to a csv file.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p874code16'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p87416"><td class="code" id="p874code16"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># array of sql server instances to query</span>
<span style="color: #800080;">$instances</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;localhost\sqlexpress&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;RHYS-VAIO\sqlexpress&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #008000;"># assumes proc is located in the same db on all servers</span>
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;test&quot;</span>;
<span style="color: #008000;"># proc to call</span>
<span style="color: #800080;">$proc</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;EXEC dbo.GetDataCache&quot;</span>;
<span style="color: #008000;"># Datetime stamp for filename</span>
<span style="color: #800080;">$dt</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span> <span style="color: #008080; font-style: italic;">-Format</span> <span style="color: #800000;">&quot;yyyyMMddHHmmss&quot;</span>;
&nbsp;
<span style="color: #008000;"># Query each instance</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$sql_instance</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$instances</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$con</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.SqlClient.SqlConnection<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Data Source=$sql_instance;Integrated Security=true;Initial Catalog=$database&quot;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$resultset</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #800000;">&quot;System.Data.DataSet&quot;</span> <span style="color: #800000;">&quot;myDs&quot;</span>;
	<span style="color: #800080;">$data_adap</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> <span style="color: #800000;">&quot;System.Data.SqlClient.SqlDataAdapter&quot;</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$proc</span><span style="color: pink;">,</span> <span style="color: #800080;">$con</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$data_adap</span>.Fill<span style="color: #000000;">&#40;</span><span style="color: #800080;">$resultset</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #008000;"># Replace any '\' chars from instance names to use in the filename</span>
	<span style="color: #800080;">$tmp_srv</span> <span style="color: pink;">=</span> <span style="color: #800080;">$sql_instance</span> <span style="color: #FF0000;">-replace</span> <span style="color: #800000;">&quot;\\&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;_&quot;</span>;
	<span style="color: #008000;"># Add data to csv file</span>
	<span style="color: #800080;">$resultset</span>.Tables<span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Csv</span> <span style="color: #008080; font-style: italic;">-NoTypeInformation</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\$tmp_srv<span style="color: #008080; font-weight: bold;">`_</span>sql_data_cache_$dt.csv&quot;</span>;
	<span style="color: #008000;"># Clean up</span>
	<span style="color: #800080;">$resultset</span>.Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$con</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The script will spit out a csv file for each sql instance ready to be imported into the CMDB.</p>
<pre>&quot;Cache Size (MB)&quot;,&quot;Database&quot;,&quot;Server&quot;,&quot;collected_at&quot;
&quot;0&quot;,&quot;master&quot;,&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;28/09/2010 13:12:52&quot;
&quot;0&quot;,&quot;msdb&quot;,&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;28/09/2010 13:12:52&quot;
&quot;1&quot;,&quot;test&quot;,&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;28/09/2010 13:12:52&quot;
&quot;20&quot;,&quot;TweetSQLV3&quot;,&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;28/09/2010 13:12:52&quot;
&quot;1&quot;,&quot;tweetsql_31&quot;,&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;28/09/2010 13:12:52&quot;
&quot;0&quot;,&quot;ResourceDb&quot;,&quot;RHYS-VAIO\SQLEXPRESS&quot;,&quot;28/09/2010 13:12:52&quot;</pre>
<p><map name='google_ad_map_874_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/874?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_874_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=874&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fgathering-sql-server-data-cache-information%2F874' title="Gathering SQL Server Data Cache Information" alt=" Gathering SQL Server Data Cache Information" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information/874">Gathering SQL Server Data Cache Information</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information/874" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/gathering-sql-server-data-cache-information/874/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deleting sequential duplicates with TSQL</title>
		<link>http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869</link>
		<comments>http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869#comments</comments>
		<pubDate>Wed, 01 Sep 2010 17:02:42 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[duplicates]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869</guid>
		<description><![CDATA[I was recently given a du-duping task which was much more difficult than I anticipated and taxed my SQL brain to its limits. I thought of using a CTE to do this but all of the examples I could find for deleting records with a CTE wouldn’t have worked in my situation. Essentially the table [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869">Deleting sequential duplicates with TSQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I was recently given a du-duping task which was much more difficult than I anticipated and taxed my SQL brain to its limits. I thought of using a <a href="http://msdn.microsoft.com/en-us/library/ms190766.aspx" target="_blank">CTE</a> to do this but all of the examples I could find for <a href="http://blog.sqlauthority.com/2009/06/23/sql-server-2005-2008-delete-duplicate-rows/" target="_blank">deleting records with a CTE</a> wouldn’t have worked in my situation.</p>
<p>Essentially the table had a business key consisting of 3 parts. Each record would also come with two values attached. The current system pumped data into this fairly regularly and we only wanted to keep values that were not sequentially duplicated. By “sequentially duplicated” I mean an insert into a feed table has the same values attached as the previously inserted record for the same business key. For example, assuming a single key, the below table illustrates the input sequence and the desired de-dupe result.</p>
<table border="0" cellspacing="2" cellpadding="2" width="400">
<tbody>
<tr>
<td valign="top" width="200"><strong>sequence</strong></td>
<td valign="top" width="200"><strong>de-duped</strong></td>
</tr>
<tr>
<td valign="top" width="200">1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</td>
<td valign="top" width="200">1</td>
</tr>
<tr>
<td valign="top" width="200">1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1</td>
<td valign="top" width="200">1,2,1</td>
</tr>
<tr>
<td valign="top" width="200">1,2,3,4,5,4,3,2,1</td>
<td valign="top" width="200">1,2,3,4,5,4,3,2,1</td>
</tr>
</tbody>
</table>
<p>Now for a SQL example;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p869code20'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86920"><td class="code" id="p869code20"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Create a test table</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">TestDupes</span>
<span style="color: #808080;">&#40;</span>
	Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,
	KeyPart1 <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	KeyPart2 <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	KeyPart3 <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	value1 <span style="color: #0000FF;">FLOAT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	value2 <span style="color: #0000FF;">FLOAT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
<span style="color: #008080;">-- Insert some test data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">TestDupes</span>
<span style="color: #808080;">&#40;</span>
	KeyPart1,
	KeyPart2,
	KeyPart3,
	value1,
	value2
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>;
GO</pre></td></tr></table></div>

<p>The records considered to be duplicates are outlined in red in the image below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/sequential_dupes.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sequential dupes" border="0" alt="sequential dupes thumb Deleting sequential duplicates with TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/sequential_dupes_thumb.png" width="644" height="451" /></a> </p>
<p>A sequentially de-duped dataset would look like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/no_sequential_dupes.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="no sequential dupes" border="0" alt="no sequential dupes thumb Deleting sequential duplicates with TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/no_sequential_dupes_thumb.png" width="644" height="282" /></a></p>
<p>The solution I came up with, after much head-scratching, involved the use of the <a href="http://msdn.microsoft.com/en-us/library/ms173825.aspx" target="_blank">DENSE_RANK</a> &amp; <a href="http://msdn.microsoft.com/en-us/library/ms186734.aspx" target="_blank">ROW_NUMBER</a> functions combined with a <a href="http://msdn.microsoft.com/en-us/library/ms190766.aspx" target="_blank">Common Table Expression</a>. The DENSE_RANK function organises each business key (KeyPart1, KeyPart2, KeyPart3) into groups. The ROW_NUMBER function gives us a sequence number for each record within the group. This is best illustrated with a select statement with another group thrown in for good measure.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p869code21'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86921"><td class="code" id="p869code21"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">TRUNCATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">TestDupes</span>;
<span style="color: #008080;">-- Insert some test data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">TestDupes</span>
<span style="color: #808080;">&#40;</span>
	KeyPart1,
	KeyPart2,
	KeyPart3,
	value1,
	value2
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #008080;">-- Group 2 </span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">5.5</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2.0</span>, <span style="color: #000;">2.5</span>;
GO
&nbsp;
<span style="color: #0000FF;">WITH</span> Dupe_CTE <span style="color: #808080;">&#40;</span>
			Id, 
			KeyPart1, 
			KeyPart2, 
			KeyPart3, 
			value1, 
			value2, 
			group_num, 
			group_row_num
		<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #0000FF;">SELECT</span> Id,
	       KeyPart1,
	       KeyPart2,
	       KeyPart3,
	       value1,
	       value2,
               DENSE_RANK<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> KeyPart1, KeyPart2, KeyPart3<span style="color: #808080;">&#41;</span>,
               ROW_NUMBER<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span><span style="color: #808080;">&#40;</span>PARTITION <span style="color: #0000FF;">BY</span> KeyPart1,
                                          KeyPart2,
                                          KeyPart3
                                 <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Id<span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">TestDupes</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> Dupe_CTE;</pre></td></tr></table></div>

<p>Here you can see the data is organised into groups and each row, within the group, is numbered according to it&#8217;s position.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/feed_dupes_two_sets.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="duplicates organise into groups with DENSE_RANK" border="0" alt="feed dupes two sets thumb Deleting sequential duplicates with TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/feed_dupes_two_sets_thumb.png" width="582" height="484" /></a></p>
<p>Using this information about each group we can execute a delete. The delete self-joins to the CTE comparing the values for value1 and value2.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p869code22'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86922"><td class="code" id="p869code22"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">WITH</span> Dupe_CTE <span style="color: #808080;">&#40;</span>
			Id, 
			KeyPart1, 
			KeyPart2, 
			KeyPart3, 
			value1, 
			value2, 
			group_num, 
			group_row_num
		<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #0000FF;">SELECT</span> Id,
		   KeyPart1,
		   KeyPart2,
		   KeyPart3,
		   value1,
		   value2,
           DENSE_RANK<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> KeyPart1, KeyPart2, KeyPart3<span style="color: #808080;">&#41;</span>,
           ROW_NUMBER<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span><span style="color: #808080;">&#40;</span>PARTITION <span style="color: #0000FF;">BY</span> KeyPart1,
                                          KeyPart2,
                                          KeyPart3
                             <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Id<span style="color: #808080;">&#41;</span>
           <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">TestDupes</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">DELETE</span> fg2
<span style="color: #0000FF;">FROM</span> Dupe_CTE fg1
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> Dupe_CTE fg2
                <span style="color: #0000FF;">ON</span> fg1.<span style="color: #202020;">Group_Num</span> <span style="color: #808080;">=</span> fg2.<span style="color: #202020;">Group_Num</span>
                <span style="color: #808080;">AND</span> fg1.<span style="color: #202020;">value1</span> <span style="color: #808080;">=</span> fg2.<span style="color: #202020;">value1</span>
                <span style="color: #808080;">AND</span> fg1.<span style="color: #202020;">value2</span> <span style="color: #808080;">=</span> fg2.<span style="color: #202020;">value2</span>
<span style="color: #0000FF;">WHERE</span> fg1.<span style="color: #202020;">group_row_num</span> <span style="color: #808080;">=</span> fg2.<span style="color: #202020;">group_row_num</span> <span style="color: #808080;">-</span> <span style="color: #000;">1</span>;</pre></td></tr></table></div>

<p>This will remove any duplicates, keeping the oldest record in each case, giving us a clean dataset.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/cleaned_up_group_duplicates.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="cleaned up group duplicates" border="0" alt="cleaned up group duplicates thumb Deleting sequential duplicates with TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/09/cleaned_up_group_duplicates_thumb.png" width="644" height="439" /></a></p>
<p>Can anyone think of a <strike>CURSOR</strike> better way of doing this?</p>
<p><map name='google_ad_map_869_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/869?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_869_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=869&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fdeleting-sequential-duplicates-with-tsql%2F869' title="Deleting sequential duplicates with TSQL" alt=" Deleting sequential duplicates with TSQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869">Deleting sequential duplicates with TSQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/deleting-sequential-duplicates-with-tsql/869/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Postgres Linked Server How To</title>
		<link>http://www.youdidwhatwithtsql.com/postgres-linked-server-how-to/590</link>
		<comments>http://www.youdidwhatwithtsql.com/postgres-linked-server-how-to/590#comments</comments>
		<pubDate>Sat, 30 Jan 2010 16:44:07 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Linked Server]]></category>
		<category><![CDATA[ODBC]]></category>
		<category><![CDATA[Postgres]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/postgres-linked-server-how-to/590</guid>
		<description><![CDATA[Just a quick post showing how to add a Postgres database server as a Linked Server in Microsoft&#160;SQL Server. Install the psqlODBC driver for Windows. Control Panel &#62; Administrative Tools &#62; Data Sources (ODBC). Click the &#34;System DSN&#34; tab and click the Add button. Choose &#34;PostreSQL ANSI&#34; and click Finish. Configure the data source by [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/postgres-linked-server-how-to/590">Postgres Linked Server How To</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Just a quick post showing how to add a <a href="http://www.postgresql.org/" target="_blank">Postgres</a> database server as a <a href="http://msdn.microsoft.com/en-us/library/ms188279.aspx" target="_blank">Linked Server</a> in <a href="http://www.microsoft.com/en/us/default.aspx" target="_blank">Microsoft</a>&#160;<a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a>.</p>
<ul>
<li>Install the <a href="http://www.postgresql.org/ftp/odbc/versions/" target="_blank">psqlODBC</a> driver for Windows. </li>
<li>Control Panel &gt; Administrative Tools &gt; Data Sources (ODBC). </li>
<li>Click the &quot;System DSN&quot; tab and click the Add button. </li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/new_data_source.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Create New Data Source Dialog" border="0" alt="new data source thumb Postgres Linked Server How To" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/new_data_source_thumb.png" width="644" height="483" /></a> </p>
<ul>
<li>Choose &quot;PostreSQL ANSI&quot; and click Finish. </li>
<li>Configure the data source by entering the server, username, password and database details. </li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Postgres_ODBC_Setup.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Postgres ODBC Setup" border="0" alt="Postgres ODBC Setup thumb Postgres Linked Server How To" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Postgres_ODBC_Setup_thumb.png" width="644" height="367" /></a> </p>
</p>
<ul>
<li>Click &quot;Test&quot; to confirm the data source functions. If it fails ensure that <a href="http://www.postgresql.org/" target="_blank">Postgres</a> is running and check your configuration details. </li>
<li>Click &quot;Save&quot; and then &quot;OK&quot; once successful. </li>
<li>Fire up <a href="http://msdn.microsoft.com/en-us/library/ms174173.aspx" target="_blank">SSMS</a> and run the following <a href="http://en.wikipedia.org/wiki/Transact-SQL" target="_blank">T-SQL</a> to create the Linked Server to Postgres. </li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p590code25'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p59025"><td class="code" id="p590code25"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">USE</span> <span style="color: #808080;">&#91;</span>master<span style="color: #808080;">&#93;</span>
GO
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_addlinkedserver</span> @server <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'POSTGRES'</span>, @srvproduct<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'Postgres'</span>, @provider<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'MSDASQL'</span>, @datasrc<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'PostgreSQL30'</span>
&nbsp;
GO
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_serveroption</span> @server<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'POSTGRES'</span>, @optname<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'collation compatible'</span>, @optvalue<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'false'</span>
GO
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_serveroption</span> @server<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'POSTGRES'</span>, @optname<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'data access'</span>, @optvalue<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'true'</span>
GO
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_serveroption</span> @server<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'POSTGRES'</span>, @optname<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'rpc'</span>, @optvalue<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'false'</span>
GO
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_serveroption</span> @server<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'POSTGRES'</span>, @optname<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'rpc out'</span>, @optvalue<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'false'</span>
GO
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_serveroption</span> @server<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'POSTGRES'</span>, @optname<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'connect timeout'</span>, @optvalue<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'0'</span>
GO
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_serveroption</span> @server<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'POSTGRES'</span>, @optname<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'collation name'</span>, @optvalue<span style="color: #808080;">=</span>null
GO
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_serveroption</span> @server<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'POSTGRES'</span>, @optname<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'query timeout'</span>, @optvalue<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'0'</span>
GO
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_serveroption</span> @server<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'POSTGRES'</span>, @optname<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'use remote collation'</span>, @optvalue<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'true'</span>
GO
<span style="color: #0000FF;">USE</span> <span style="color: #808080;">&#91;</span>master<span style="color: #808080;">&#93;</span>
GO
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_addlinkedsrvlogin</span> @rmtsrvname <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'POSTGRES'</span>, @locallogin <span style="color: #808080;">=</span> <span style="color: #808080;">NULL</span> , @useself <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'False'</span>
GO</pre></td></tr></table></div>

<p>Alternatively copy the below configuration in SSMS. The most critical thing here is to get the setting for &quot;Data source&quot; correct. This should be the name of the System DSN you added earlier.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Postgres_Linked_Server_Configuration.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Postgres Linked Server Configuration" border="0" alt="Postgres Linked Server Configuration thumb Postgres Linked Server How To" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Postgres_Linked_Server_Configuration_thumb.png" width="539" height="484" /></a> </p>
<ul>
<li>Finally execute the query below. This uses <a href="http://technet.microsoft.com/en-us/library/ms188427.aspx" target="_blank">OPENQUERY</a> and will list all the tables in your <a href="http://www.postgresql.org/" target="_blank">Postgres</a> system database. </li>
</ul>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p590code26'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p59026"><td class="code" id="p590code26"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">OPENQUERY</span><span style="color: #808080;">&#40;</span>POSTGRES, <span style="color: #FF0000;">'SELECT * FROM INFORMATION_SCHEMA.TABLES'</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>If everything is running correctly you should see something like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/postgres_openquery_result.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Postgres openquery result" border="0" alt="postgres openquery result thumb Postgres Linked Server How To" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/postgres_openquery_result_thumb.png" width="644" height="211" /></a></p>
<p><map name='google_ad_map_590_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/590?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_590_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=590&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fpostgres-linked-server-how-to%2F590' title="Postgres Linked Server How To" alt=" Postgres Linked Server How To" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/postgres-linked-server-how-to/590">Postgres Linked Server How To</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/postgres-linked-server-how-to/590" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/postgres-linked-server-how-to/590/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>View or function &#8216;dbo.Viewname&#8217; has more column names specified than columns defined</title>
		<link>http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546</link>
		<comments>http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546#comments</comments>
		<pubDate>Mon, 18 Jan 2010 21:08:22 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546</guid>
		<description><![CDATA[If you ever encounter this SQL Server error when selecting from a view then somebody has probably dropped columns from the base table. Here&#8217;s a quick run through of the problem. ?View Code TSQLCREATE TABLE dbo.Contact &#40; Id INTEGER IDENTITY&#40;1,1&#41; NOT NULL PRIMARY KEY CLUSTERED, FirstName VARCHAR&#40;50&#41; NOT NULL, LastName VARCHAR&#40;50&#41; NOT NULL, DOB DATETIME [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546">View or function &#8216;dbo.Viewname&#8217; has more column names specified than columns defined</a></p>
]]></description>
			<content:encoded><![CDATA[<p>If you ever encounter this <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> error when selecting from a view then somebody has probably dropped columns from the base table. Here&#8217;s a quick run through of the problem.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code35'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54635"><td class="code" id="p546code35"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Contact</span>
<span style="color: #808080;">&#40;</span>
	Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	FirstName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	LastName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	DOB <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	Phone <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	Email <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">200</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	Mobile <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	Website <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Insert a test record.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code36'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54636"><td class="code" id="p546code36"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Contacts</span>
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	DOB,
	Email,
	Website
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Rhys'</span>,
	<span style="color: #FF0000;">'Campbell'</span>,
	<span style="color: #FF0000;">'01-Jun-80'</span>,
	<span style="color: #FF0000;">'noone@tempinbox.com'</span>,
	<span style="color: #FF0000;">'http://www.youdidwhatwithtsql.com'</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Create a view on this table.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code37'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54637"><td class="code" id="p546code37"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> vw_Contacts
<span style="color: #0000FF;">AS</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
	<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Contacts</span>;</pre></td></tr></table></div>

<p>Verify the view is functional</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code38'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54638"><td class="code" id="p546code38"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> vw_Contacts;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/view_results1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sql server view results" border="0" alt="view results thumb1 View or function dbo.Viewname has more column names specified than columns defined" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/view_results_thumb1.png" width="644" height="62" /></a> </p>
<p>Now drop a column from the <strong>Contacts </strong>table.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code39'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54639"><td class="code" id="p546code39"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Contacts</span> <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">COLUMN</span> Phone;</pre></td></tr></table></div>

<p>Now try selecting from the view again.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code40'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54640"><td class="code" id="p546code40"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> vw_Contacts;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/sql_server_view_error.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="View or function &#39;vw_Contacts&#39; has more column names specified than columns defined" border="0" alt="sql server view error thumb View or function dbo.Viewname has more column names specified than columns defined" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/sql_server_view_error_thumb.png" width="644" height="72" /></a> </p>
<p>So what&#8217;s going on here? The view is expecting the <strong>Phone</strong> column to still be in the <strong>Contact</strong> table. We created our view with an asterisk so this shouldn&#8217;t matter right? <a href="http://en.wikipedia.org/wiki/Shooting_Stars#Catchphrases" target="_blank">Uvavu</a>! SQL Server stores metadata about the view when you create it. Changes to underlying tables can cause issues. Luckily the fix is easy.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code41'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54641"><td class="code" id="p546code41"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">sp_refreshview</span> <span style="color: #FF0000;">'vw_Contacts'</span>;</pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code42'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54642"><td class="code" id="p546code42"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> vw_Contacts;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/fixed_sql_server_view.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="fixed sql server view" border="0" alt="fixed sql server view thumb View or function dbo.Viewname has more column names specified than columns defined" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/fixed_sql_server_view_thumb.png" width="644" height="64" /></a> </p>
<p>This procedure will update the metadata stored by the view making it functional again. Presumably this is akin to dropping and recreating the view. This is a fairly trivial example but in a large system, with lots of views, and lots of developers, this could cause big headaches. I recommend you read the <a href="http://msdn.microsoft.com" target="_blank">MSDN</a> page for <a href="http://msdn.microsoft.com/en-us/library/ms187821.aspx" target="_blank">sp_refreshview</a>. There&#8217;s a couple of useful scripts in the comments section for making light work of this.</p>
<p><map name='google_ad_map_546_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/546?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_546_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=546&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fview-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined%2F546' title="View or function dbo.Viewname has more column names specified than columns defined" alt=" View or function dbo.Viewname has more column names specified than columns defined" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546">View or function &#8216;dbo.Viewname&#8217; has more column names specified than columns defined</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Views or functions cannot reference themselves directly or indirectly</title>
		<link>http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519</link>
		<comments>http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519#comments</comments>
		<pubDate>Thu, 14 Jan 2010 21:18:38 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[self-reference]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519</guid>
		<description><![CDATA[Today I received the following SQL Server error which I had never encountered before. Msg 4429, Level 16, State 1, Line 1 View or function &#8216;Table_1&#8242; contains a self-reference. Views or functions cannot reference themselves directly or indirectly. Msg 4413, Level 16, State 1, Line 1 Could not use view or function &#8216;Test.Table_1&#8242; because of [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519">Views or functions cannot reference themselves directly or indirectly</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Today I received the following <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> error which I had never encountered before.</p>
<blockquote><p>Msg 4429, Level 16, State 1, Line 1      <br />View or function &#8216;Table_1&#8242; contains a self-reference. Views or functions cannot reference themselves directly or indirectly.       <br />Msg 4413, Level 16, State 1, Line 1       <br />Could not use view or function &#8216;Test.Table_1&#8242; because of binding errors.</p>
</blockquote>
<p>Here&#8217;s a quick walk-through of the issue.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code48'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51948"><td class="code" id="p519code48"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Table_1<span style="color: #808080;">&#93;</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">int</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>test<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">nchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Insert some test data</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code49'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51949"><td class="code" id="p519code49"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Table_1</span>
<span style="color: #808080;">&#40;</span>
	test
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'One'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Two'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Three'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Four'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Five'</span>;</pre></td></tr></table></div>

<p>Add a schema called &#8216;Test&#8217; to the database by running the TSQL below. We will then create a view in this schema.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code50'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51950"><td class="code" id="p519code50"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">SCHEMA</span> Test;</pre></td></tr></table></div>

<p>Now create this view.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code51'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51951"><td class="code" id="p519code51"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> Test.<span style="color: #202020;">Table_1</span>
<span style="color: #0000FF;">AS</span>
	<span style="color: #0000FF;">SELECT</span> Id, Test
	<span style="color: #0000FF;">FROM</span> Table_1;</pre></td></tr></table></div>

<p>Now try to select from this view.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/sql_view_self_reference.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sql server view self reference error" border="0" alt="sql view self reference thumb Views or functions cannot reference themselves directly or indirectly" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/sql_view_self_reference_thumb.png" width="644" height="72" /></a>&#160;</p>
<p>I&#8217;d been creating lots of views for a third party application we&#8217;ve started to deploy at work. I&#8217;d simply created a new schema and named the views after the corresponding table in the database. This one was my fault for being a copy and paste monkey! I&#8217;d forgotten to specify the schema in one view. The fix?</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code52'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51952"><td class="code" id="p519code52"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">VIEW</span> Test.<span style="color: #202020;">Table_1</span>
<span style="color: #0000FF;">AS</span>
	<span style="color: #0000FF;">SELECT</span> Id, Test
	<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Table_1</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/view_results.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="view results" border="0" alt="view results thumb Views or functions cannot reference themselves directly or indirectly" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/view_results_thumb.png" width="156" height="144" /></a> </p>
<p>Even though my user <a href="http://msdn.microsoft.com/en-us/library/ms190387.aspx" target="_blank">default schema</a> was set to dbo <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> got its pants in a twist over this. So there you have it; it&#8217;s definitely good practice to specify schemas in your TSQL! </p>
<p><map name='google_ad_map_519_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/519?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_519_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=519&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fviews-or-functions-cannot-reference-themselves-directly-or-indirectly%2F519' title="Views or functions cannot reference themselves directly or indirectly" alt=" Views or functions cannot reference themselves directly or indirectly" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519">Views or functions cannot reference themselves directly or indirectly</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Discover SQL Servers with Powershell via the registry</title>
		<link>http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494</link>
		<comments>http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494#comments</comments>
		<pubDate>Mon, 28 Dec 2009 15:26:11 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494</guid>
		<description><![CDATA[Chuck Boyce Jr (blog &#124; twitter) recently commented on a limitation of the script from my post Discover SQL Servers with Powershell. The script does require that the SQLBrowser service is running for discovery to occur which may be a major issue for some. Here&#8217;s an alternative method that does not have this limitation. All [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494">Discover SQL Servers with Powershell via the registry</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Chuck Boyce Jr (<a href="http://chuckboyce.blogspot.com/" target="_blank">blog</a> | <a href="http://twitter.com/chuckboycejr" target="_blank">twitter</a>) recently <a href="http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell/357/comment-page-1#comment-120" target="_blank">commented</a> on a limitation of the script from my post <a href="http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell/357" target="_blank">Discover SQL Servers with Powershell</a>. The script does require that the <a href="http://msdn.microsoft.com/en-us/library/ms181087.aspx" target="_blank">SQLBrowser</a> service is running for discovery to occur which may be a major issue for some. Here&#8217;s an alternative method that does not have this limitation. All <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> instances should have their name registered in the following registry key <strong>HKLM\Software\Microsoft\Microsoft SQL Server\InstalledInstances</strong> which we can access remotely<strong> </strong>with <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/registry_installedinstances.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="The INSTALLEDINSTANCES registry key showing SQL Servers" border="0" alt="registry installedinstances thumb Discover SQL Servers with Powershell via the registry" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/registry_installedinstances_thumb.png" width="244" height="150" /></a> </p>
<p>All this script requires is that you place a text file containing computer names in your user profile folder (C:\Users\Rhys on my laptop).</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p494code54'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p49454"><td class="code" id="p494code54"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Text file containing computers to search for sql instances</span>
<span style="color: #800080;">$computers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;$env:USERPROFILE\computers.txt&quot;</span>;
&nbsp;
<span style="color: #008000;"># Check each computer for installed SQL Server</span>
<span style="color: #008000;"># instances by searching the registry</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$computer</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$computers</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	try
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$sql</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Microsoft.Win32.RegistryKey<span style="color: #000000;">&#93;</span>::OpenRemoteBaseKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'LocalMachine'</span><span style="color: pink;">,</span><span style="color: #800080;">$computer</span><span style="color: #000000;">&#41;</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'SOFTWARE\Microsoft\Microsoft SQL Server'</span><span style="color: #000000;">&#41;</span>.GetValue<span style="color: #000000;">&#40;</span><span style="color: #800000;">'InstalledInstances'</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$sqlserver</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$sql</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$sqlserver</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;MSSQLSERVER&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;"># Default instance</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;$computer (Default instance)&quot;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">else</span> <span style="color: #008000;"># Named instance</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;$computer\$sqlserver (Named instance)&quot;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	catch <span style="color: #000000;">&#91;</span>System.Exception<span style="color: #000000;">&#93;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Red <span style="color: #800000;">&quot;Error accessing $computer. &quot;</span> <span style="color: #000080;">$_</span>.Exception.Message;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>When you execute the script it will search each computer and list the SQL Server instances it discovered in the registry.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/discovered_sql_servers.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="discovered sql servers" border="0" alt="discovered sql servers thumb Discover SQL Servers with Powershell via the registry" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/discovered_sql_servers_thumb.png" width="244" height="134" /></a> </p>
<p>If you receive the error &quot;You cannot call a method on a null-valued expression&quot; then that computer does not have the registry key we are searching for. In other words; no SQL Server instances are on that computer. Hopefully this method should allow you to get a more complete view of the SQL Servers in your organisation. </p>
<p><map name='google_ad_map_494_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/494?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_494_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=494&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fdiscover-sql-servers-with-powershell-via-the-registry%2F494' title="Discover SQL Servers with Powershell via the registry" alt=" Discover SQL Servers with Powershell via the registry" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494">Discover SQL Servers with Powershell via the registry</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell-via-the-registry/494/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automated Date Range Testing of SSIS Packages</title>
		<link>http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466</link>
		<comments>http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466#comments</comments>
		<pubDate>Sun, 06 Dec 2009 19:40:52 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[SSIS]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466</guid>
		<description><![CDATA[I&#8217;m currently building a lot of SSIS packages that are primarily date driven. Many of these involve periods of 100 days, to several years, so I wanted to automate the testing of these packages. I&#8217;d previously automated the testing of stored procedures over date ranges but wanted a solution for testing the system as a [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466">Automated Date Range Testing of SSIS Packages</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently building a lot of <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> packages that are primarily date driven. Many of these involve periods of 100 days, to several years, so I wanted to automate the testing of these packages. I&#8217;d previously automated the <a href="http://www.youdidwhatwithtsql.com/testing-datetime-dependent-stored-procedures/433" target="_blank">testing of stored procedures over date ranges</a> but wanted a solution for testing the system as a whole. The solution I came up with involves the use of <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>. Essentially this script increments the date, by one day, before executing a package with <a href="http://msdn.microsoft.com/en-us/library/ms162810.aspx" target="_blank">dtexec</a>.</p>
<p>First lets create a simple <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> package that the Powershell script will call. Open <a href="http://msdn.microsoft.com/en-us/library/ms173767.aspx" target="_blank">BIDS</a> and create a new project. Add a string variable to the project called <strong>sql_date </strong>like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/sql_datevariable.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sql_date variable" border="0" alt="sql datevariable thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/sql_datevariable_thumb.png" width="644" height="183" /></a> </p>
<p>We&#8217;re going to use this variable to get the date as <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> recognises it so we know it&#8217;s the same as the system date. During my testing I discovered that SQL Server would take a few seconds pickup a system date change. Obviously this could distort your results so we need to ensure these are the same.</p>
<p>Next add an <a href="http://technet.microsoft.com/en-us/library/ms141003.aspx" target="_blank">Execute SQL Task</a>, from the toolbox, onto the designer. Call it &quot;Get SQL Server Date&quot;. Add a connection to the SQL Server your package will be executing against and change the <strong>ResultSet </strong>property to &quot;Single Row&quot;. Add the following TSQL to <strong>SQLStatement</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p466code57'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p46657"><td class="code" id="p466code57"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">VARCHAR</span>, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">103</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/execute_sql_task_date.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="execute_sql_task_date" border="0" alt="execute sql task date thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/execute_sql_task_date_thumb.png" width="244" height="208" /></a> </p>
<p>Next add a <a href="http://msdn.microsoft.com/en-us/library/ms141752.aspx" target="_blank">Script Task</a> onto the designer and call it <strong>MsgBoxDate</strong>. Right click the task, click edit, add <strong>sql_date</strong> to <strong>ReadOnlyVariables</strong> on the script tab. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/script_task.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="script_task" border="0" alt="script task thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/script_task_thumb.png" width="244" height="208" /></a> </p>
<p>Click &quot;Design Script&quot; and add the below code into the ide.</p>
<pre lang="VB.NET">Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

    Public Sub Main()
        Dim sql_date As String = Dts.Variables("sql_date").Value.ToString
        Dim msg As String = "SQL Server Date = " &#038; sql_date &#038; Environment.NewLine &#038; "System Date = " &#038; Format(Date.Now, "dd/MM/yyyy")
        MsgBox(msg)
        Dts.TaskResult = Dts.Results.Success
    End Sub

End Class</pre>
<p>Connect the tasks together with a precedence constraint. The package should look something like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/date_package.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="date_package" border="0" alt="date package thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/date_package_thumb.png" width="244" height="197" /></a> </p>
<p>Return to the SSIS Designer, click File &gt; Save Copy of Package.dtsx As. Save this package in the file system and remember the path as you&#8217;ll need it later. Below is the <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script that will change the current day, by one day, and then execute the package we just made. The while loop will verify that SQL Server has picked up the system date change before executing the package. Change the settings for <strong>$sqlserver </strong>and the package path for <a href="http://msdn.microsoft.com/en-us/library/ms162810.aspx" target="_blank">dtexec</a> as appropriate. The <strong>$days </strong>variable should be changed to the number of days you want to run the package for. In this example it&#8217;s just ten. The script will set your systems date back once it has successfully completed.. </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p466code58'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p46658"><td class="code" id="p466code58"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># SQL server and database settings</span>
<span style="color: #800080;">$sqlserver</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost\sql2005&quot;</span>;
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;master&quot;</span>;
<span style="color: #008000;"># Record the current date so we can set it back at the end of the script</span>
<span style="color: #008000;"># You may need to change this depending on your regional settings</span>
<span style="color: #800080;">$date</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span> <span style="color: #008080; font-style: italic;">-Format</span> <span style="color: #800000;">&quot;dd/MM/yyyy&quot;</span>;
&nbsp;
<span style="color: #008000;"># Connection string used for verifying sql server has registered the date change</span>
<span style="color: #800080;">$connection_string</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Data Source=$sqlserver;Initial Catalog=$database;Integrated Security=SSPI&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$days</span> <span style="color: pink;">=</span> <span style="color: #804000;">10</span>;
&nbsp;
<span style="color: #008000;"># Setup the database connection</span>
<span style="color: #800080;">$conn</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.SqlClient.SqlConnection<span style="color: #000000;">&#40;</span><span style="color: #800080;">$connection_string</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$conn</span>.Open<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$cmd</span> <span style="color: pink;">=</span> <span style="color: #800080;">$conn</span>.CreateCommand<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$cmd</span>.CommandText <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SELECT DATEPART(dd, GETDATE())&quot;</span>;
&nbsp;
&nbsp;
<span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$days</span>; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># Change the date by one day</span>
	<span style="color: #008080; font-weight: bold;">Set-Date</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span>.AddDays<span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #008000;"># SQL Server can take a few seconds to register the change</span>
	<span style="color: #008000;"># so we need to loop until the day matches before </span>
	<span style="color: #008000;"># executing our ssis package</span>
	<span style="color: #800080;">$reader</span> <span style="color: pink;">=</span> <span style="color: #800080;">$cmd</span>.ExecuteReader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$reader</span>.Read<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$sql_day</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reader</span>.GetValue<span style="color: #000000;">&#40;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$reader</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #0000FF;">while</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$sql_day</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-ne</span> <span style="color: #000000;">&#91;</span>DateTime<span style="color: #000000;">&#93;</span>::Now.Day<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Seconds</span> <span style="color: #804000;">2</span>;
		<span style="color: #008000;"># Check the sql day again</span>
		<span style="color: #800080;">$reader</span> <span style="color: pink;">=</span> <span style="color: #800080;">$cmd</span>.ExecuteReader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$reader</span>.Read<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$sql_day</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reader</span>.GetValue<span style="color: #000000;">&#40;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$reader</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #008000;"># Execute the package for this date</span>
	dtexec <span style="color: pink;">/</span>f <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\Package.dtsx&quot;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Clean up</span>
<span style="color: #800080;">$reader</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$conn</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008000;"># Put the date back with the current time</span>
<span style="color: #800080;">$time</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>DateTime<span style="color: #000000;">&#93;</span>::Now.TimeOfDay;
<span style="color: #008080; font-weight: bold;">Set-Date</span> <span style="color: #800000;">&quot;$date $time&quot;</span>;</pre></td></tr></table></div>

<p>The script will work through each day displaying a message box for each iteration.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/start_date.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="start_date" border="0" alt="start date thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/start_date_thumb.png" width="215" height="163" /></a> </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/end_date.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="end_date" border="0" alt="end date thumb Automated Date Range Testing of SSIS Packages" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/12/end_date_thumb.png" width="215" height="163" /></a> </p>
</p>
<p>Obviously this particular package needs human intervention to get it to complete. Your real-life packages wouldn&#8217;t have this limitation so this script could be used to test over a large date range. Hopefully this script will allow me to get out of the office a bit earlier when I need to test my packages!</p>
<p><map name='google_ad_map_466_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/466?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_466_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=466&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fautomated-date-range-testing-of-ssis-packages%2F466' title="Automated Date Range Testing of SSIS Packages" alt=" Automated Date Range Testing of SSIS Packages" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466">Automated Date Range Testing of SSIS Packages</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/automated-date-range-testing-of-ssis-packages/466/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

