<?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>Sat, 24 Jul 2010 16:26:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<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="Create New Data Source Dialog" 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" 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('p590code3'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5903"><td class="code" id="p590code3"><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" 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('p590code4'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5904"><td class="code" id="p590code4"><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" 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>
]]></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('p546code13'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54613"><td class="code" id="p546code13"><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('p546code14'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54614"><td class="code" id="p546code14"><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('p546code15'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54615"><td class="code" id="p546code15"><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('p546code16'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54616"><td class="code" id="p546code16"><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="sql server view results" 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('p546code17'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54617"><td class="code" id="p546code17"><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('p546code18'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54618"><td class="code" id="p546code18"><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="View or function &#39;vw_Contacts&#39; 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('p546code19'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54619"><td class="code" id="p546code19"><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('p546code20'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54620"><td class="code" id="p546code20"><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" 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>
]]></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>0</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('p519code26'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51926"><td class="code" id="p519code26"><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('p519code27'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51927"><td class="code" id="p519code27"><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('p519code28'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51928"><td class="code" id="p519code28"><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('p519code29'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51929"><td class="code" id="p519code29"><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 server view self reference error" 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('p519code30'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51930"><td class="code" id="p519code30"><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" 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>
]]></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="The INSTALLEDINSTANCES registry key showing SQL Servers" 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('p494code32'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p49432"><td class="code" id="p494code32"><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" 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>
]]></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_date variable" 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('p466code35'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p46635"><td class="code" id="p466code35"><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('p466code36'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p46636"><td class="code" id="p466code36"><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>
]]></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>
		<item>
		<title>Discover SQL Servers with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell/357</link>
		<comments>http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell/357#comments</comments>
		<pubDate>Sat, 05 Sep 2009 16:21:42 +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/357</guid>
		<description><![CDATA[With Powershell and SMO you can easily discover SQL Server instances running on your network in just a few lines of code. ?View Code POWERSHELL&#91;System.Reflection.Assembly&#93;::LoadWithPartialName&#40;&#34;Microsoft.SqlServer.Smo&#34;&#41; &#124; Out-Null; $smoObj = &#91;Microsoft.SqlServer.Management.Smo.SmoApplication&#93;; &#160; # This gets the sql servers available $sql = $smoObj::EnumAvailableSqlServers&#40;$false&#41; &#160; foreach&#40;$sqlserver in $sql&#41; &#123; Write-Host -ForegroundColor Green &#34;Discovered sql server: &#34; $sqlserver.Name; &#125; [...]<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/357">Discover SQL Servers with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>With <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> and <a href="http://msdn.microsoft.com/en-us/library/ms162169.aspx" target="_blank">SMO</a> you can easily discover <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> instances running on your network in just a few lines of code.</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('p357code38'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p35738"><td class="code" id="p357code38"><pre class="powershell" style="font-family:monospace;"><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>;
<span style="color: #800080;">$smoObj</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Microsoft.SqlServer.Management.Smo.SmoApplication<span style="color: #000000;">&#93;</span>;
&nbsp;
<span style="color: #008000;"># This gets the sql servers available</span>
<span style="color: #800080;">$sql</span> <span style="color: pink;">=</span> <span style="color: #800080;">$smoObj</span>::EnumAvailableSqlServers<span style="color: #000000;">&#40;</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span>
&nbsp;
<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: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Green <span style="color: #800000;">&quot;Discovered sql server: &quot;</span> <span style="color: #800080;">$sqlserver</span>.Name;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/discover_sql_servers_with_powershell.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="discover sql servers with powershell" border="0" alt="discover sql servers with powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/discover_sql_servers_with_powershell_thumb.png" width="244" height="147" /></a> </p>
<p>If this doesn’t seem to find all your sql servers check firewalls and the <a href="http://msdn.microsoft.com/en-us/library/ms181087.aspx" target="_blank">SQL Server Browser Service</a> is running.</p>
<p><map name='google_ad_map_357_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/357?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_357_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=357&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fdiscover-sql-servers-with-powershell%2F357' title="Discover SQL Servers with Powershell" alt=" Discover SQL Servers with Powershell" /></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/357">Discover SQL Servers with Powershell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/discover-sql-servers-with-powershell/357/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL Server Fulltext Search Primer</title>
		<link>http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248</link>
		<comments>http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248#comments</comments>
		<pubDate>Thu, 09 Jul 2009 16:10:50 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Fulltext]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248</guid>
		<description><![CDATA[Previously I wrote an article about fulltext searching with MySQL and thought I’d redo the same article but for SQL Server users. Depending of which side of the database wars you’re on SQL Server has either a more advanced or complicated way of doing things. Here’s a very brief introduction to the fulltext search features [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248">SQL Server Fulltext Search Primer</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Previously I wrote an article about <a href="http://www.youdidwhatwithtsql.com/mysql-fulltext-search-primer/224" target="_blank">fulltext searching with MySQL</a> and thought I’d redo the same article but for <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> users. Depending of which side of the <a href="http://www.sqlmag.com/Article/ArticleID/21431/sql_server_21431.html" target="_blank">database wars</a> you’re on <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> has either a more advanced or complicated way of doing things. Here’s a very brief introduction to the fulltext search features in <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx">SQL Server</a>.</p>
<p>First the fulltext search service needs to be enabled for the database.</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('p248code45'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24845"><td class="code" id="p248code45"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Enable fulltext searching on the database</span>
<span style="color: #0000FF;">USE</span> database1;
GO
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">sp_fulltext_database</span> <span style="color: #FF0000;">'enable'</span>;
GO
<span style="color: #008080;">-- Create a fulltext catalog</span>
<span style="color: #0000FF;">CREATE</span> FULLTEXT <span style="color: #0000FF;">CATALOG</span> ft_catalog_database1
GO</pre></td></tr></table></div>

<p>If your <a href="http://msdn.microsoft.com/en-us/library/ms187317.aspx">fulltext index</a> is going to be large, or very frequently searched, you may want to <a href="http://msdn.microsoft.com/en-us/library/ms187317.aspx" target="_blank">research options</a> to put the catalog on a different disk.</p>
<p>Create a table to hold 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('p248code46'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24846"><td class="code" id="p248code46"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Create a table to contain the poems</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> poems
<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;">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>,
	author <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>,
	title <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;">NOT</span> <span style="color: #808080;">NULL</span>,
	poem <span style="color: #0000FF;">TEXT</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 dummy 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('p248code47'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24847"><td class="code" id="p248code47"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Insert some data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> poems
<span style="color: #808080;">&#40;</span>
	author,
	title,
	poem
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span>
&nbsp;
	<span style="color: #FF0000;">'Johann Wolfgang von Goethe'</span>,
	<span style="color: #FF0000;">'Night Thoughts'</span>,
	<span style="color: #FF0000;">'Stars, you are unfortunate, I pity you,
Beautiful as you are, shining in your glory,
Who guide seafaring men through stress and peril
And have no recompense from gods or mortals,
Love you do not, nor do you know what love is.
Hours that are aeons urgently conducting
Your figures in a dance through the vast heaven,
What journey have you ended in this moment,
Since lingering in the arms of my beloved
I lost all memory of you and midnight.'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span>
	<span style="color: #FF0000;">'Nikki Giovanni'</span>,
	<span style="color: #FF0000;">'I Love You'</span>,
	<span style="color: #FF0000;">'I love you
because the Earth turns round the sun
because the North wind blows north
sometimes
because the Pope is Catholic
and most Rabbis Jewish
because winters flow into springs
and the air clears after a storm'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span>
	<span style="color: #FF0000;">'Lord Byron'</span>,
	<span style="color: #FF0000;">'She Walks In Beauty'</span>,
	<span style="color: #FF0000;">'She walks in beauty, like the night
Of cloudless climes and starry skies;
And all that'</span><span style="color: #FF0000;">'s best of dark and bright
Meet in her aspect and her eyes:
Thus mellow'</span><span style="color: #FF0000;">'d to that tender light
Which heaven to gaudy day denies.
&nbsp;
One shade more, one ray less,
Had half impair'</span><span style="color: #FF0000;">'d the nameless grace
Which waves in every raven tress,
Or softly lightens o'</span><span style="color: #FF0000;">'er her face;
Where thoughts serenely sweet express
How pure, how dear their dwelling place.
&nbsp;
And on that cheek, and o'</span><span style="color: #FF0000;">'er that brow
So soft, so calm, yet eloquent,
The smiles that win, the tints that glow,
But tell of days in goodness spent,
A mind at peace with all below,
A heart whose love is innocent!'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span>
	<span style="color: #FF0000;">'Christopher Marlowe'</span>,
	<span style="color: #FF0000;">'Come Live With Me'</span>,
	<span style="color: #FF0000;">'Come live with me, and be my love;
And we will all the pleasures prove
That valleys, groves, hills, and fields,
Woods or steepy mountain yields.'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span>
	<span style="color: #FF0000;">'Thomas Campbell'</span>,
	<span style="color: #FF0000;">'Freedom and Love'</span>,
	<span style="color: #FF0000;">'How delicious is the winning
Of a kiss at love'</span><span style="color: #FF0000;">'s beginning,
When two mutual hearts are sighing
For the knot there'</span><span style="color: #FF0000;">'s no untying!
&nbsp;
Yet remember, '</span><span style="color: #FF0000;">'midst your wooing
Love has bliss, but Love has ruing;
Other smiles may make you fickle,
Tears for other charms may trickle.
&nbsp;
Love he comes and Love he tarries
Just as fate or fancy carries;
Longest stays, when sorest chidden;
Laughs and flies, when press'</span><span style="color: #FF0000;">'d and bidden.
&nbsp;
Bind the sea to slumber stilly,
Bind its odour to the lily,
Bind the aspen ne'</span><span style="color: #FF0000;">'er to quiver,
Then bind Love to last for ever.
&nbsp;
Love'</span><span style="color: #FF0000;">'s a fire that needs renewal
Of fresh beauty for its fuel;
Love'</span><span style="color: #FF0000;">'s wing moults when caged and captured,
Only free, he soars enraptured.
&nbsp;
Can you keep the bees from ranging,
Or the ringdove'</span><span style="color: #FF0000;">'s neck from changing?
No! nor fetter'</span><span style="color: #FF0000;">'d Love from dying
In the knot there'</span><span style="color: #FF0000;">'s no untying.'</span>;</pre></td></tr></table></div>

<p>Now we need to create the <a href="http://msdn.microsoft.com/en-us/library/ms187317.aspx">fulltext</a> index;</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('p248code48'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24848"><td class="code" id="p248code48"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Create a fulltext index on title and poem</span>
<span style="color: #0000FF;">CREATE</span> FULLTEXT <span style="color: #0000FF;">INDEX</span> <span style="color: #0000FF;">ON</span> database1.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">poems</span>
<span style="color: #808080;">&#40;</span>
	title
	<span style="color: #0000FF;">Language</span> 0X0,
	poem
	<span style="color: #0000FF;">Language</span> 0X0
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">INDEX</span> PK__poems__00551192 <span style="color: #0000FF;">ON</span> ft_catalog_database1
<span style="color: #0000FF;">WITH</span> CHANGE_TRACK<span style="color: #808080;">IN</span>G AUTO;</pre></td></tr></table></div>

<p>The fulltext index requires a unique non-nullable index to be specified on its creation. Check your primary key name in SSMS and change the above <a href="http://msdn.microsoft.com/en-us/library/ms189826(SQL.90).aspx">TSQL</a> as appropriate (after KEY INDEX). </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image10.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Check Primary Key name in SSMS" border="0" alt="Check Primary Key name in SSMS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb10.png" width="244" height="225" /></a> </p>
<p>You will receive the following warning after the index is created;</p>
<pre>
Warning: Table or indexed view 'Documents' has full-text indexed columns that are of type image, text, or ntext. Full-text change tracking cannot track WRITETEXT or UPDATETEXT operations performed on these columns.
</pre>
<p>Standard UPDATE and INSERT changes are reflected in the fulltext catalog but changes with <a href="http://msdn.microsoft.com/en-us/library/ms186838.aspx">WRITETEXT</a> or <a href="http://msdn.microsoft.com/en-us/library/ms189466.aspx">UPDATETEXT</a> are not. If you’re not using these then you can ignore the warning. Otherwise this would involve some form of manual <a href="http://msdn.microsoft.com/en-us/library/aa214782(SQL.80).aspx">update to the fulltext catalog</a> after using <a href="http://msdn.microsoft.com/en-us/library/ms186838.aspx">WRITETEXT</a> / <a href="http://msdn.microsoft.com/en-us/library/ms189466.aspx">UPDATETEXT</a>. Now the fulltext index is ready to use.</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('p248code49'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24849"><td class="code" id="p248code49"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> 
<span style="color: #0000FF;">FROM</span> poems
<span style="color: #0000FF;">WHERE</span> <span style="color: #0000FF;">CONTAINS</span><span style="color: #808080;">&#40;</span>title, <span style="color: #FF0000;">'&quot;I love you&quot;'</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image11.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Fulltext search results in SSMS" border="0" alt="Fulltext search results in SSMS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb11.png" width="644" height="106" /></a></p>
<p>You can also order by relevance;</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('p248code50'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24850"><td class="code" id="p248code50"><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;">CONTAINSTABLE</span><span style="color: #808080;">&#40;</span>dbo.<span style="color: #202020;">poems</span>, <span style="color: #808080;">*</span>, <span style="color: #FF0000;">'&quot;I love you&quot;'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> t1
<span style="color: #808080;">JOIN</span> dbo.<span style="color: #202020;">poems</span> <span style="color: #0000FF;">AS</span> t2
	<span style="color: #0000FF;">ON</span> t1.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">KEY</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> t2.<span style="color: #202020;">id</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> t1.<span style="color: #808080;">&#91;</span>RANK<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">DESC</span>;</pre></td></tr></table></div>

<p>Note the asterisk in the <a href="http://technet.microsoft.com/en-us/library/ms189760.aspx">CONTAINSTABLE</a> clause. This means search all columns in the fulltext index. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image12.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Fulltext search results with rank in SSMS" border="0" alt="Fulltext search results with rank in SSMS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/07/image_thumb12.png" width="644" height="138" /></a></p>
<p>TSQL has two predicates and two functions we can use with fulltext searching. The table* below details these. Be sure to investigate these to make the most of your fulltext catalogs.</p>
<table border="1" cellspacing="0" cellpadding="5">
<tbody>
<tr>
<td><b>Keyword</b></td>
<td><b>Meaning</b></td>
<td><b>Returns</b></td>
</tr>
<tr>
<td>CONTAINS</td>
<td>Supports complex syntax to search for a word or phrase using Boolean operators, prefix terms</td>
<td>Simple Boolean predicate</td>
</tr>
<tr>
<td>CONTAINSTABLE</td>
<td>Supports CONTAINS syntax and returns document IDs and rank scores for matches</td>
<td>Table containing document IDs and rank scores</td>
</tr>
<tr>
<td>FREETEXT</td>
<td>Automatically performs thesaurus expansions and replacements in a simplified syntax</td>
<td>Simple Boolean predicate</td>
</tr>
<tr>
<td>FREETEXTTABLE</td>
<td>Supports FREETEXT syntax and returns document IDs and rank scores for matches</td>
<td>Table containing document IDs and rank scores</td>
</tr>
</tbody>
</table>
<p>* <em>source: <a title="http://en.wikipedia.org/wiki/SQL_Server_Full_Text_Search" href="http://en.wikipedia.org/wiki/SQL_Server_Full_Text_Search">http://en.wikipedia.org/wiki/SQL_Server_Full_Text_Search</a></em></p>
<p>I’ve just scratched the surface, of what you can do with fulltext searches in <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx">SQL Server</a>. Be sure to check out the concepts around <a href="http://msdn.microsoft.com/en-us/library/ms142551(SQL.90).aspx">Noise words</a>, usage of the <a href="http://msdn.microsoft.com/en-us/library/ms142491(SQL.90).aspx">Thesaurus</a> and <a href="http://msdn.microsoft.com/en-us/library/ms142509(SQL.90).aspx">word-breakers</a> to make the most of your fulltext searches.</p>
<p><map name='google_ad_map_248_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/248?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_248_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=248&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fsql-server-fulltext-search-primer%2F248' title="SQL Server Fulltext Search Primer" alt=" SQL Server Fulltext Search Primer" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248">SQL Server Fulltext Search Primer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/sql-server-fulltext-search-primer/248/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Documenting Databases</title>
		<link>http://www.youdidwhatwithtsql.com/documenting-databases/204</link>
		<comments>http://www.youdidwhatwithtsql.com/documenting-databases/204#comments</comments>
		<pubDate>Tue, 23 Jun 2009 19:05:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Database documentation]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/documenting-databases/204</guid>
		<description><![CDATA[Asking for database documentation in many tech shops will result in blank stares. Other places do see the value of but it forever remains on the to-do list. There are a few commercial products available hoping to help with this; SchemaToDoc &#8211; http://www.schematodoc.com/ SqlSpec &#8211; http://www.elsasoft.org/ SQL Doc &#8211; http://www.red-gate.com/products/SQL_Doc/index.htm I’m not convinced of their [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/documenting-databases/204">Documenting Databases</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Asking for database documentation in many tech shops will result in blank stares. Other places do see the value of but it forever remains on the to-do list. There are a few commercial products available hoping to help with this;</p>
<p>SchemaToDoc &#8211; <a title="http://www.schematodoc.com/" href="http://www.schematodoc.com/">http://www.schematodoc.com/</a>     <br />SqlSpec &#8211; <a title="http://www.elsasoft.org/" href="http://www.elsasoft.org/">http://www.elsasoft.org/</a>     <br />SQL Doc &#8211; <a title="http://www.red-gate.com/products/SQL_Doc/index.htm" href="http://www.red-gate.com/products/SQL_Doc/index.htm">http://www.red-gate.com/products/SQL_Doc/index.htm</a></p>
<p>I’m not convinced of their value especially when important object metadata, or business information,&#160; is missing from your database. </p>
<p>All databases objects should be tagged if appropriate with need-to-know information; Tables should be tagged with some basic information, if that data is licensed and needs annual renewal why not document this inside the database itself? </p>
<p>Columns should have a brief description; it may be obvious to you what the column holds but is it to everyone else? One system I worked with had a <a href="http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html" target="_blank">TINYINT</a> column called phone_permission with values of 0 or 1. My first guess was that ‘1’ meant you could call the telephone number on the record. Luckily I didn&#8217;t run the telesales department so no problem was caused from this misunderstanding.</p>
<p><strong>Adding comments to database tables</strong></p>
<p><font color="#666666">In <a href="http://www.mysql.com" target="_blank">MySQL</a> we can add a comment to a table like so;</font></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('p204code58'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20458"><td class="code" id="p204code58"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> TestTable <span style="color: #990099; font-weight: bold;">COMMENT</span> <span style="color: #008000;">'Data supplied by ACME Corp.'</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Table comments can then be viewed by using the <a href="http://dev.mysql.com/doc/refman/5.0/en/show-create-table.html" target="_blank">SHOW CREATE TABLE</a> syntax.</p>
<pre>mysql&gt; SHOW CREATE TABLE TestTable \G
*************************** 1. row ***************************
       Table: TestTable
Create Table: CREATE TABLE `TestTable` (
  `idnm` int(11) default NULL,
  `fullName` varchar(255) default NULL,
  `old_id` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='The data in this table is supplied by ACME Corp.'
1 row in set (0.00 sec)

mysql&gt;</pre>
<p>Or by querying <a href="http://dev.mysql.com/doc/refman/5.0/en/information-schema.html" target="_blank">INFORMATION_SCHEMA</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('p204code59'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20459"><td class="code" id="p204code59"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- View table comments for the current database</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> TABLE_NAME<span style="color: #000033;">,</span> TABLE_COMMENT
<span style="color: #990099; font-weight: bold;">FROM</span> information_schema.<span style="color: #990099; font-weight: bold;">tables</span>
<span style="color: #990099; font-weight: bold;">WHERE</span> TABLE_SCHEMA <span style="color: #CC0099;">=</span> <span style="color: #000099;">DATABASE</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>For <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> it’s a little more complicated. We have to add an <a href="http://msdn.microsoft.com/en-us/library/ms190243.aspx" target="_blank">extended property</a> to attach a comment onto the 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('p204code60'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20460"><td class="code" id="p204code60"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> sys.<span style="color: #AF0000;">sp_addextendedproperty</span> @name<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'Description'</span>, 
				@<span style="color: #0000FF;">value</span><span style="color: #808080;">=</span>N<span style="color: #FF0000;">'hello table Customer'</span>,
				@level0type<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'SCHEMA'</span>, 
				@level0name<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'dbo'</span>, 
				@level1type<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'TABLE'</span>, 
				@level1name<span style="color: #808080;">=</span>N<span style="color: #FF0000;">'Customer'</span>;</pre></td></tr></table></div>

<p>Viewing this comment isn’t as easy as <a href="http://www.mysql.com" target="_blank">MySQL</a> either. Use the below <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> to view all table level comments in the current database (dbo 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('p204code61'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20461"><td class="code" id="p204code61"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Table comments</span>
<span style="color: #0000FF;">SELECT</span> objtype, objname, name, <span style="color: #0000FF;">value</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #AF0000;">fn_listextendedproperty</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #FF0000;">'schema'</span>, 
	<span style="color: #FF0000;">'dbo'</span>, 
	<span style="color: #FF0000;">'table'</span>, 
	<span style="color: #0000FF;">default</span>, 
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
GO</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image18.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Viewing Table comments in SSMS" border="0" alt="Viewing Table comments in SSMS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb18.png" width="644" height="134" /></a> </p>
<p>Obviously <a href="http://www.microsoft.com/en/us/default.aspx" target="_blank">Microsoft</a> didn’t intend people to hand-code <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> to add comments to their databases, but rather use tool support like <a href="http://msdn.microsoft.com/en-us/library/ms174173.aspx" target="_blank">SSMS</a>. This would probably be rather tedious if you needed to comment a large number of tables. Here’s a suggested method for making this task a little easier. First get the schema and name for all tables in the database you wish to document.</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('p204code62'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20462"><td class="code" id="p204code62"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> TABLE_SCHEMA, TABLE_NAME
<span style="color: #0000FF;">FROM</span> <span style="color: #808080;">IN</span>F<span style="color: #808080;">OR</span>MATION_SCHEMA.<span style="color: #202020;">TABLES</span>
<span style="color: #0000FF;">WHERE</span> TABLE_TYPE <span style="color: #808080;">=</span> <span style="color: #FF0000;">'BASE TABLE'</span>;</pre></td></tr></table></div>

<p>Copy and paste this into Excel, or other <a href="http://www.openoffice.org/product/calc.html" target="_blank">spreadsheet program</a>, so you can comment each table easily.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image19.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Comment your tables in Excel" border="0" alt="Comment your tables in Excel" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb19.png" width="644" height="147" /></a> </p>
<p>When you are finished import this data into <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> and run this <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> statement against it. N.B. This statement assumes you imported the table comment data into a table called <strong>temp_table_comments</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('p204code63'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20463"><td class="code" id="p204code63"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Generate T_SQL to add extended properties to all tables</span>
<span style="color: #008080;">-- in temp_table_comments</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'EXEC sys.sp_addextendedproperty @name=N'</span><span style="color: #FF0000;">'TABLE_COMMENT'</span><span style="color: #FF0000;">', 
					@value=N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> COMMENT <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">',
					@level0type=N'</span><span style="color: #FF0000;">'SCHEMA'</span><span style="color: #FF0000;">', 
					@level0name=N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> TABLE_SCHEMA <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">',
					@level1type=N'</span><span style="color: #FF0000;">'TABLE'</span><span style="color: #FF0000;">', 
					@level1name=N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> TABLE_NAME <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">';'</span>
<span style="color: #0000FF;">FROM</span> temp_table_comments;</pre></td></tr></table></div>

<p>Here’s the <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> it generates if run against the <a href="http://www.microsoft.com/downloads/details.aspx?familyid=e719ecf7-9f46-4312-af89-6ad8702e4e6e" target="_blank">AdventureWorks</a> database table names. Once you have generated the <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">T-SQL</a> then you’re ready to run it against your database and apply your comments to the tables.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image20.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Running the script against the AdventureWorks database" border="0" alt="Running the script against the AdventureWorks database" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb20.png" width="644" height="130" /></a></p>
<p>View the table comments added for the Sales 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('p204code64'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20464"><td class="code" id="p204code64"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Table comments for AdventureWorks</span>
<span style="color: #008080;">-- Sales schema</span>
<span style="color: #0000FF;">SELECT</span> objtype, objname, name, <span style="color: #0000FF;">value</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #AF0000;">fn_listextendedproperty</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #FF0000;">'schema'</span>, 
	<span style="color: #FF0000;">'Sales'</span>, 
	<span style="color: #FF0000;">'table'</span>, 
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #808080;">NULL</span>, 
	<span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TABLE_COMMENT'</span>
GO</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image21.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Table comments in the Sales schema" border="0" alt="Table comments in the Sales schema" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb21.png" width="644" height="202" /></a></p>
<p>It would be fairly easy to take a similar approach for commenting your table columns too. Once your database is nicely documented it’ll be easy to knock up a couple of reports in <a href="http://msdn.microsoft.com/en-us/library/ms159106.aspx" target="_blank">SSRS</a> and surprise the next person who asks for “<em>the documentation”.</em></p>
<p><map name='google_ad_map_204_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/204?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_204_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=204&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fdocumenting-databases%2F204' title="Documenting Databases" alt=" Documenting Databases" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/documenting-databases/204">Documenting Databases</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/documenting-databases/204/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Auditing your SQL Servers with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/auditing-your-sql-server-with-powershell/133</link>
		<comments>http://www.youdidwhatwithtsql.com/auditing-your-sql-server-with-powershell/133#comments</comments>
		<pubDate>Fri, 29 May 2009 07:44:07 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/auditing-your-sql-server-with-powershell/133</guid>
		<description><![CDATA[Being able to know the setup and configuration of your SQL Servers is important for many IT Professionals. Powershell, combined with SMO, makes this task easy. SMO exposes a lot of properties allowing you to easily retrieve things like Processor &#38; RAM Information, Service Pack Level, Operating System information, Collation Settings, number of Databases, and [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/auditing-your-sql-server-with-powershell/133">Auditing your SQL Servers with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Being able to know the setup and configuration of your <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Servers</a> is important for many IT Professionals. <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>, combined with <a href="http://msdn.microsoft.com/en-us/library/ms162169.aspx" target="_blank">SMO</a>, makes this task easy. <a href="http://msdn.microsoft.com/en-us/library/ms162169.aspx" target="_blank">SMO</a> exposes a lot of properties allowing you to easily retrieve things like Processor &amp; RAM Information, <a href="http://support.microsoft.com/kb/913089" target="_blank">Service Pack</a> Level, Operating System information, Collation Settings, number of Databases, and much more. Be sure to explore <a href="http://msdn.microsoft.com/en-us/library/ms162169.aspx" target="_blank">SMO</a> for your specific needs. <a href="http://powergui.org" target="_blank">PowerGUI</a>, from <a href="http://www.quest.com/" target="_blank">Quest Software</a>, has a nice Intellisense feature that makes exploring object properties easy.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image7.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Exploring object properties with PowerGUI" border="0" alt="Exploring object properties with PowerGUI" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image-thumb7.png" width="244" height="119" /></a> </p>
<p>Here’s a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script for documenting your <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Servers</a> setup and configuration. There’s a couple of <a href="http://technet.microsoft.com/en-us/magazine/cc162486.aspx" target="_blank">Powershell variables</a> in the script to configure before you get started;</p>
<p> <strong>$sqlservers – </strong>This points at a text file containing your list of SQL Servers. <strong>$auditDatabases – </strong>If set to <strong>$true</strong> then the script will produce an additional file documenting the databases on each SQL Server. Set to <strong>$false</strong> if you don&#8217;t want to produce this 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('p133code66'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13366"><td class="code" id="p133code66"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Get list of SQL servers</span>
<span style="color: #800080;">$sqlservers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;C:\sqlservers.txt&quot;</span>;
<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>;
<span style="color: #008000;"># Get the datetime to use for the log filename</span>
<span style="color: #800080;">$datetime</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;yyyy-MM-ddThh-mm-ssZ&quot;</span>;
<span style="color: #800080;">$filename</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$datetime.csv&quot;</span>;
<span style="color: #008000;"># If database details should be audited</span>
<span style="color: #800080;">$auditDatabases</span> <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
<span style="color: #008000;"># Flag used to indicate column headers have been added to the database audit file</span>
<span style="color: #800080;">$headerAdded</span> <span style="color: pink;">=</span> <span style="color: #800080;">$false</span>;
&nbsp;
<span style="color: #008000;"># Add the column headers to the log file</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;C:\$filename&quot;</span> <span style="color: #800000;">&quot;sqlserver,Collation,Edition,EngineEdition,OSVersion,PhysicalMemory,Processors,VersionString,Version,ProductLevel,Product,Platform,loginMode,LinkedServerCount,databaseCount,minConfigMem,clrRunValue,clrConfigValue&quot;</span>; 
&nbsp;
<span style="color: #008000;"># For each SQL server listed in $sqlservers</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;">$sqlservers</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Processing sql server: $sqlserver.&quot;</span>;
	<span style="color: #008000;"># Create an instance of SMO.Server for the current sql server</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;">$sqlserver</span>;
&nbsp;
	<span style="color: #800080;">$collation</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Information.Collation;					<span style="color: #008000;"># Server collation</span>
	<span style="color: #800080;">$edition</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Information.Edition;						<span style="color: #008000;"># Server edition</span>
	<span style="color: #800080;">$engineEdition</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Information.EngineEdition;			<span style="color: #008000;"># Engine Edition</span>
	<span style="color: #800080;">$OSVersion</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Information.OSVersion;					<span style="color: #008000;"># OS Version</span>
	<span style="color: #800080;">$PhysicalMemory</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Information.PhysicalMemory;			<span style="color: #008000;"># Physical Memory</span>
	<span style="color: #800080;">$Product</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Information.Product;						<span style="color: #008000;"># Server Product</span>
	<span style="color: #800080;">$Platform</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Information.Platform;						<span style="color: #008000;"># Server Platform</span>
	<span style="color: #800080;">$Processors</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Information.Processors;					<span style="color: #008000;"># Processor count</span>
	<span style="color: #800080;">$VersionString</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Information.VersionString;			<span style="color: #008000;"># Version String</span>
	<span style="color: #800080;">$Version</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Information.Version;						<span style="color: #008000;"># Version</span>
	<span style="color: #800080;">$ProductLevel</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Information.ProductLevel;				<span style="color: #008000;"># Product Level</span>
&nbsp;
	<span style="color: #800080;">$loginMode</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Settings.LoginMode;						<span style="color: #008000;"># Login Mode setting</span>
	<span style="color: #800080;">$linkedServers</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.LinkedServers.Count;					<span style="color: #008000;"># Get the number of linked servers</span>
	<span style="color: #800080;">$databaseCount</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Databases.Count;						<span style="color: #008000;"># Get the number of databases hosted by the sql server</span>
	<span style="color: #800080;">$minMem</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Configuration.MinServerMemory.ConfigValue;	<span style="color: #008000;"># Configured minimum memory</span>
	<span style="color: #800080;">$clrRun</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Configuration.IsSqlClrEnabled.RunValue;		<span style="color: #008000;"># SQLCLR run value</span>
	<span style="color: #800080;">$clrConfig</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Configuration.IsSqlClrEnabled.ConfigValue;<span style="color: #008000;"># SQLCLR config value</span>
&nbsp;
	<span style="color: #008000;"># Write the info for the current sql server</span>
	<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;C:\$filename&quot;</span> <span style="color: #800000;">&quot;$sqlserver,$collation,$edition,$engineEdition,$OSVersion,$PhysicalMemory,$Processors,$VersionString,$Version,$ProductLevel,$Product,$Platform,$loginMode,$linkedServers,$databaseCount,$minMem,$clrRun,$clrConfig&quot;</span>;
&nbsp;
	<span style="color: #008000;"># If $auditDatabases is true then log details of databases</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$auditDatabases</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$dbFilename</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\databases_$filename&quot;</span>;
		<span style="color: #008000;"># Get the databases on the current server</span>
		<span style="color: #800080;">$databases</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Databases;
&nbsp;
		<span style="color: #008000;"># Check to see if the header has been added</span>
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$headerAdded</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$false</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008000;"># Add column headers to the file</span>
			<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800080;">$dbFilename</span> <span style="color: #800000;">&quot;sqlserver,dbName,ActiveConnections,CaseSensitive,Collation,CompatibilityLevel,CreateDate,DefaultSchema,Owner,Size,SpaceAvailable,Status,ProcCount,TableCount,ViewCount,TriggerCount,UDFCount&quot;</span>;
			<span style="color: #008000;"># Set to true so the header isn't added again</span>
			<span style="color: #800080;">$headerAdded</span> <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #008000;"># For each database on the current server</span>
		<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$database</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$databases</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Processing database: $database.&quot;</span>;
			<span style="color: #008000;"># Get database object properties</span>
			<span style="color: #800080;">$dbName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.Name;
			<span style="color: #800080;">$ActiveConnections</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.ActiveConnections;
			<span style="color: #800080;">$CaseSensitive</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.CaseSensitive;
			<span style="color: #800080;">$Collation</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.Collation;
			<span style="color: #800080;">$CompatibilityLevel</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.CompatibilityLevel;
			<span style="color: #800080;">$CreateDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.CreateDate;
			<span style="color: #800080;">$DefaultSchema</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.DefaultSchema;
			<span style="color: #800080;">$Owner</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.Owner;
			<span style="color: #800080;">$Size</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.Size;
			<span style="color: #800080;">$SpaceAvailable</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.SpaceAvailable;
			<span style="color: #800080;">$Status</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$database</span>.Status<span style="color: #000000;">&#41;</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: #800080;">$ProcCount</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.StoredProcedures.Count;
			<span style="color: #800080;">$TableCount</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.Tables.Count;
			<span style="color: #800080;">$ViewCount</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.Views.Count;
			<span style="color: #800080;">$TriggerCount</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.Triggers.Count;
			<span style="color: #800080;">$UDFCount</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.UserDefinedFunctions.Count;
&nbsp;
			<span style="color: #008000;"># Append line to file for the current database</span>
			<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800080;">$dbFilename</span> <span style="color: #800000;">&quot;$sqlserver,$dbName,$ActiveConnections,$CaseSensitive,$Collation,$CompatibilityLevel,$CreateDate,$DefaultSchema,$Owner,$Size,$SpaceAvailable,$Status,$ProcCount,$TableCount,$ViewCount,$TriggerCount,$UDFCount&quot;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Run the script and you should see the output similar to below;</p>
<p>Processing sql server: SQLSERVER1.<br />
  <br />Processing database: [db1]. </p>
<p>Processing database: [db2]. </p>
<p>Processing database: [db3]. </p>
<p>Processing database: [db4]. </p>
<p>Processing database: [db5]. </p>
<p>Processing sql server: SQLSERVER2. </p>
<p>Processing database: [db1]. </p>
<p>Processing database: [db2]. </p>
<p>Processing database: [db3]. </p>
<p>Processing database: [db4]. </p>
<p>Processing database: [db5].</p>
<p>After running this script you will have two datetime stamped <a href="http://en.wikipedia.org/wiki/Comma-separated_values" target="_blank">csv</a> files in the root of your <strong>C:\ </strong>drive. Below I’ve attached some sample files;</p>
<p><a href="http://www.youdidwhatwithtsql.com/?attachment_id=129" target="_blank">SQL Server Audit File</a></p>
<p><a href="http://www.youdidwhatwithtsql.com/?attachment_id=130" target="_blank">SQL Server Databases Audit File</a></p>
<p><map name='google_ad_map_133_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/133?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_133_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=133&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fauditing-your-sql-server-with-powershell%2F133' title="Auditing your SQL Servers with Powershell" alt=" Auditing your SQL Servers with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/auditing-your-sql-server-with-powershell/133">Auditing your SQL Servers with Powershell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/auditing-your-sql-server-with-powershell/133/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tweet-SQL Version 2 Sneak Peek</title>
		<link>http://www.youdidwhatwithtsql.com/tweet-sql-version-2-sneak-peek/45</link>
		<comments>http://www.youdidwhatwithtsql.com/tweet-sql-version-2-sneak-peek/45#comments</comments>
		<pubDate>Sat, 14 Mar 2009 14:14:52 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Tweet-SQL]]></category>
		<category><![CDATA[CLR]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Twitter API]]></category>
		<category><![CDATA[Twitter App]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/tweet-sql-version-2-sneak-peek/45</guid>
		<description><![CDATA[Tweet-SQL is a Twitter Client for Microsoft SQL Server 2005 and above allowing you to interact with the Twitter API with standard T-SQL. The forthcoming version 2 of Tweet-SQL contains a host of new features and improvements. Here’s a sneak peek at what is coming in Tweet-SQL V2; New procedure allowing the use of the [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/tweet-sql-version-2-sneak-peek/45">Tweet-SQL Version 2 Sneak Peek</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.tweet-sql.com" target="_blank">Tweet-SQL</a> is a Twitter Client for Microsoft SQL Server 2005 and above allowing you to interact with the <a href="apiwiki.twitter.com" target="_blank">Twitter API</a> with standard T-SQL. The forthcoming version 2 of <a href="http://www.tweet-sql.com" target="_blank">Tweet-SQL</a> contains a host of new features and improvements. Here’s a sneak peek at what is coming in <a href="http://www.tweet-sql.com" target="_blank">Tweet-SQL</a> V2;</p>
<ul>
<li>New procedure allowing the use of the <a href="apiwiki.twitter.com/Search+API+Documentatio" target="_blank">Twitter Search API</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('p45code70'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4570"><td class="code" id="p45code70"><pre class="t-sql" style="font-family:monospace;">DECLARE @query NVARCHAR(100);
SET @query = 'Tweet-SQL';
&nbsp;
EXEC dbo.tweet_src_search @query, null, null;</pre></td></tr></table></div>

</li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/03/image.png"><img title="Tweet-SQL showing Twitter Search API Results" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="197" alt="Tweet-SQL showing Twitter Search API Results" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/03/image-thumb.png" width="244" border="0" /></a>&#160;</p>
<ul>
<li>New procedures allowing profile modification. </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('p45code71'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4571"><td class="code" id="p45code71"><pre class="t-sql" style="font-family:monospace;">EXEC dbo.tweet_acc_update_profile @name, 
				  @email, 
				  @profile_url,
				  @location, 
				  @description,
				  null;
EXEC tweet_acc_update_profile_colors @profile_background_color,
				     @profile_text_color,
				     @profile_link_color,
				     @profile_sidebar_fill,
				     @profile_sidebar_border_color,
				     null;</pre></td></tr></table></div>

<ul>
<li>Four new procedures implementing the new <a href="http://apiwiki.twitter.com/REST+API+Documentation#SocialGraphMethods" target="_blank">Social Graph</a> API methods either returning data to the user or replicating the data to local tables. </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('p45code72'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4572"><td class="code" id="p45code72"><pre class="t-sql" style="font-family:monospace;">-- Follower ids for the authenticating user
EXEC dbo.tweet_sog_followers null, null;
-- Same as above but copies data to a local table
EXEC dbo.tweet_sog_followersTable null;
-- Friend ids for the authenticating user
EXEC dbo.tweet_sog_friends null, null;
-- Same as above but copies data to a local table
EXEC dbo.tweet_sog_friendsTable;</pre></td></tr></table></div>

<ul>
<li>New procedure to shorten urls with <a href="http://tinyurl.com" target="_blank">TinyURL</a>. </li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/03/image1.png"><img title="Shorten URLs with Tweet-SQL" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="74" alt="Shorten URLs with Tweet-SQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/03/image-thumb1.png" width="244" border="0" /></a> </p>
<ul>
<li>New function tweet_fnc_httpformat allowing easier work with datetime values. </li>
</ul>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/03/image2.png"><img title="HTTP dates with Tweet-SQL" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="71" alt="HTTP dates with Tweet-SQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/03/image-thumb2.png" width="244" border="0" /></a></p>
<p>There’s even more to come in version 2 that will compliment the <a href="http://www.tweet-sql.com/features.php" target="_blank">existing features</a> of Tweet-SQL. There’s a feature freeze in place at the moment but what would you like to see in future versions of <a href="http://www.tweet-sql.com" target="_blank">Tweet-SQL</a>?</p>
<p><map name='google_ad_map_45_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/45?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_45_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=45&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ftweet-sql-version-2-sneak-peek%2F45' title="Tweet SQL Version 2 Sneak Peek" alt=" Tweet SQL Version 2 Sneak Peek" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/tweet-sql-version-2-sneak-peek/45">Tweet-SQL Version 2 Sneak Peek</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/tweet-sql-version-2-sneak-peek/45/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
