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

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

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

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

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

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

Name                 MirroringStatus
----                 ---------------
db1         		Synchronized
db2                    	Synchronized
db3              	Synchronized
db4			Synchronized
db5    			Synchronized</pre>
<p><map name='google_ad_map_1436_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1436?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1436_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1436&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcheck-mirroring-status-powershell%2F1436' title="Check Mirroring Status with Powershell" alt=" Check Mirroring Status with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/check-mirroring-status-powershell/1436">Check Mirroring Status with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/check-mirroring-status-powershell/1436" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/check-mirroring-status-powershell/1436/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>System Documentation: My Method</title>
		<link>http://www.youdidwhatwithtsql.com/system-documentation-method/319</link>
		<comments>http://www.youdidwhatwithtsql.com/system-documentation-method/319#comments</comments>
		<pubDate>Mon, 10 Aug 2009 13:40:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Database documentation]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=319</guid>
		<description><![CDATA[Jorge Segarra (Blog &#124; Twitter) posed the question System Documentation: What’s your method?. In my experience documentation has either been nonexistent, out of date or even worse, plain wrong. These situations often get blamed on lack of time dedicated to documentation tasks. Therefore I’ve always aimed at making documentation easy or even fun. I’ve blogged [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/system-documentation-method/319">System Documentation: My Method</a></p>
]]></description>
			<content:encoded><![CDATA[<p align="left">Jorge Segarra (<a href="http://sqlchicken.com" target="_blank">Blog</a> | <a href="http://twitter.com/sqlchicken" target="_blank">Twitter</a>) posed the question <a href="http://sqlchicken.com/2009/08/system-documentation-whats-your-method/" target="_blank">System Documentation: What’s your method?</a>. In my experience documentation has either been nonexistent, out of date or even worse, plain wrong. These situations often get blamed on lack of time dedicated to documentation tasks.</p>
<p align="left">Therefore I’ve always aimed at making documentation easy or even fun. I’ve blogged before about <a href="http://www.youdidwhatwithtsql.com/auditing-your-sql-server-with-powershell/133" target="_blank">auditing SQL Servers with Powershell</a> and <a href="http://www.youdidwhatwithtsql.com/auditing-network-adapters-with-powershell/126" target="_blank">auditing network adapters with Powershell</a>. I’m very much a proponent of using <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> to make documentation easy and fun. Of course, it can’t help with everything, so this should only form part of the overall strategy.</p>
<p align="left">I’m also in favour of <a href="http://www.youdidwhatwithtsql.com/documenting-databases/204" target="_blank">Documenting Databases</a> in the sense of actually adding comments, in the form of extended properties, to the database itself. This seems to be a no-brainer to me. Keep it inside the database and generate your documentation from this. I recently produced a few <a href="http://msdn.microsoft.com/en-us/library/ms187956.aspx" target="_blank">views</a> to pull together this information that can be easily exported into a spreadsheet.</p>
<p align="left">Firstly I created a schema to group these views together. This makes their function clear.</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('p319code10'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31910"><td class="code" id="p319code10"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">SCHEMA</span> Documentation;
GO</pre></td></tr></table></div>

<p>Then I placed a comment on this new 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('p319code11'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31911"><td class="code" id="p319code11"><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;">'This schema contains views that exposes extended properties for database tables, column, view and other objects.'</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;">'Documentation'</span>
GO</pre></td></tr></table></div>

<p>Then I created these views based on SQL Server’s <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=2ec9e842-40be-4321-9b56-92fd3860fb32&amp;displaylang=en" target="_blank">system views</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('p319code12'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31912"><td class="code" id="p319code12"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> Documentation.<span style="color: #202020;">ColumnComments</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #008080;">-- Column Comments</span>
<span style="color: #0000FF;">SELECT</span> sch.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> SchemaName,
	   tabs.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> TableName,
	   cols.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> ColumnName,
	   ty.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> DataType,
	   cols.<span style="color: #202020;">max_length</span> <span style="color: #0000FF;">AS</span> MaxLength,
	   ep.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> PropertyName,
	   ep.<span style="color: #0000FF;">value</span> <span style="color: #0000FF;">AS</span> PropertyValue
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">columns</span> cols
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">tables</span> tabs
	<span style="color: #0000FF;">ON</span> cols.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> tabs.<span style="color: #FF00FF;">object_id</span>
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">schemas</span> sch
	<span style="color: #0000FF;">ON</span> sch.<span style="color: #202020;">schema_id</span> <span style="color: #808080;">=</span> tabs.<span style="color: #202020;">schema_id</span>
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">types</span> ty
	<span style="color: #0000FF;">ON</span> ty.<span style="color: #202020;">system_type_id</span> <span style="color: #808080;">=</span> cols.<span style="color: #202020;">system_type_id</span>
	<span style="color: #808080;">AND</span> ty.<span style="color: #202020;">user_type_id</span> <span style="color: #808080;">=</span> cols.<span style="color: #202020;">user_type_id</span>
<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">extended_properties</span> ep
	<span style="color: #0000FF;">ON</span> cols.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> ep.<span style="color: #202020;">major_id</span>
	<span style="color: #808080;">AND</span> ep.<span style="color: #202020;">minor_id</span> <span style="color: #808080;">=</span> cols.<span style="color: #202020;">column_id</span>;
GO
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> Documentation.<span style="color: #202020;">TableComments</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #008080;">-- Table comments</span>
<span style="color: #0000FF;">SELECT</span> sch.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> SchemaName,
	   tabs.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> TableName,
	   ep.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> PropertyName,
	   ep.<span style="color: #0000FF;">value</span> <span style="color: #0000FF;">AS</span> PropertyValue
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">tables</span> tabs
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">schemas</span> sch
	<span style="color: #0000FF;">ON</span> sch.<span style="color: #202020;">schema_id</span> <span style="color: #808080;">=</span> tabs.<span style="color: #202020;">schema_id</span>
<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">extended_properties</span> ep
	<span style="color: #0000FF;">ON</span> tabs.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> ep.<span style="color: #202020;">major_id</span>
	<span style="color: #808080;">AND</span> ep.<span style="color: #202020;">minor_id</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span>;
GO
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> Documentation.<span style="color: #202020;">SchemaComments</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #008080;">-- Schema comments</span>
<span style="color: #0000FF;">SELECT</span> sch.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> SchemaName,
	   ep.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> PropertyName,
	   ep.<span style="color: #0000FF;">value</span> <span style="color: #0000FF;">AS</span> PropertyValue
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">schemas</span> sch
<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">extended_properties</span> ep
	<span style="color: #0000FF;">ON</span> sch.<span style="color: #202020;">schema_id</span> <span style="color: #808080;">=</span> ep.<span style="color: #202020;">major_id</span>
<span style="color: #0000FF;">WHERE</span> sch.<span style="color: #202020;">principal_id</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span>;
GO
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> Documentation.<span style="color: #202020;">ViewComments</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #008080;">-- View Comments</span>
<span style="color: #0000FF;">SELECT</span> sch.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> SchemaName,
	   vw.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> TableName,
	   ep.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> PropertyName,
	   ep.<span style="color: #0000FF;">value</span> <span style="color: #0000FF;">AS</span> PropertyValue
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">views</span> vw
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">schemas</span> sch
	<span style="color: #0000FF;">ON</span> sch.<span style="color: #202020;">schema_id</span> <span style="color: #808080;">=</span> vw.<span style="color: #202020;">schema_id</span>
<span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">extended_properties</span> ep
	<span style="color: #0000FF;">ON</span> vw.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> ep.<span style="color: #202020;">major_id</span>
	<span style="color: #808080;">AND</span> ep.<span style="color: #202020;">minor_id</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span>;
GO</pre></td></tr></table></div>

<p>You should now have the following views in your database. I’ve created these in the <a href="http://msdn.microsoft.com/en-us/library/ms124501.aspx" target="_blank">AdventureWorks</a> sample database.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image9.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Documentation Views in SSMS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb9.png" border="0" alt="image thumb9 System Documentation: My Method" width="244" height="147" /></a></p>
<p><strong>Documentation.ColumnComments</strong></p>
<p>This view contains information about each tables columns, their data types and comments.</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('p319code13'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31913"><td class="code" id="p319code13"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> Documentation.<span style="color: #202020;">ColumnComments</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image10.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Documentation.ColumnComments  View Results" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb10.png" border="0" alt="image thumb10 System Documentation: My Method" width="244" height="106" /></a></p>
<p><strong>Documentation.SchemaComments</strong></p>
<p><span style="color: #666666;">This view shows the schemas in the database along with any comments.</span></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('p319code14'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31914"><td class="code" id="p319code14"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> Documentation.<span style="color: #202020;">SchemaComments</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image11.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Documentation.SchemaComments View Results" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb11.png" border="0" alt="image thumb11 System Documentation: My Method" width="244" height="61" /></a></p>
<p><strong>Documentation.TableComments</strong></p>
<p><span style="color: #666666;">This view exposes comments that have been added to tables.</span></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('p319code15'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31915"><td class="code" id="p319code15"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> Documentation.<span style="color: #202020;">TableComments</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image12.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Documentation.TableComments View Results" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb12.png" border="0" alt="image thumb12 System Documentation: My Method" width="244" height="64" /></a></p>
<p><strong>Documentation.ViewComments</strong></p>
<p>This view exposes comments that have been added to views.</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('p319code16'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p31916"><td class="code" id="p319code16"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> Documentation.<span style="color: #202020;">ViewComments</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image13.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Documentation.ViewComments View Results" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/08/image_thumb13.png" border="0" alt="image thumb13 System Documentation: My Method" width="244" height="106" /></a></p>
<p><map name='google_ad_map_319_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/319?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_319_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=319&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fsystem-documentation-method%2F319' title="System Documentation: My Method" alt=" System Documentation: My Method" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/system-documentation-method/319">System Documentation: My Method</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/system-documentation-method/319" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/system-documentation-method/319/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Practical VBA Examples for the DBA: Part 2</title>
		<link>http://www.youdidwhatwithtsql.com/practical-vba-examples-for-the-dba-part-2/308</link>
		<comments>http://www.youdidwhatwithtsql.com/practical-vba-examples-for-the-dba-part-2/308#comments</comments>
		<pubDate>Wed, 05 Aug 2009 19:53:12 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[VBA]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Visual Basic for Applications]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/practical-vba-examples-for-the-dba-part-2/308</guid>
		<description><![CDATA[In a previous post I demonstrated how a little bit of VBA code can be used&#160; to build some basic user interfaces to deliver data. Continuing on with this the examples here will show how to build some basic user interfaces; Combo box (or drop down list) and a Multi-Select List. Like the previous examples [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/practical-vba-examples-for-the-dba-part-2/308">Practical VBA Examples for the DBA: Part 2</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://www.youdidwhatwithtsql.com/practical-vba-examples-dba-part-1/268" target="_blank">previous post</a> I demonstrated how a little bit of <a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications" target="_blank">VBA</a> code can be used&#160; to build some basic user interfaces to deliver data. Continuing on with this the examples here will show how to build some basic user interfaces; Combo box (or drop down list) and a Multi-Select List. Like the previous examples these use the <a href="http://msdn.microsoft.com/en-us/library/ms124501.aspx" target="_blank">AdventureWorks</a> sample database as a data source.</p>
<p><strong>Using VBA Combo Boxes with SQL Server</strong></p>
<p>This example will present the user with a drop down list so the user can make a selection and return the appropriate data. To get started first open up Excel.</p>
<p>Click the “Visual Basic” button on the Developer ribbon. N.B. If you can’t see it <a href="http://blogs.msdn.com/erikaehrli/archive/2006/06/06/ribbondevelopertab.aspx" target="_blank">enable the Developer ribbon</a>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/excel_2007_developer_ribbon.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="The Developer Ribbon in Excel 2007" border="0" alt="excel 2007 developer ribbon thumb Practical VBA Examples for the DBA: Part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/excel_2007_developer_ribbon_thumb.png" width="644" height="142" /></a> </p>
<p>Firstly we need to add a reference to allow Excel to interact with <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a>. In the Visual Basic editor click Tools &gt; References and tick the box next to “Microsoft ActiveX Data Objects 2.8 Library”. You’ll get runtime errors if this step isn’t done.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/activex_data_objects_reference.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Adding a reference to ADO in Excel." border="0" alt="activex data objects reference thumb Practical VBA Examples for the DBA: Part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/activex_data_objects_reference_thumb.png" width="244" height="197" /></a> </p>
<p>In the project tree right click UserForm &gt; Insert &gt; UserForm. This form should be called UserForm1 (ensure it does as we will be referencing this name in VBA code.)</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/UserForm1_3.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Building simple GUIs with VBA" border="0" alt="UserForm1 3 thumb Practical VBA Examples for the DBA: Part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/UserForm1_3_thumb.png" width="244" height="157" /></a> </p>
<p>In the properties dialog for UserForm1 change the value for Caption to “Select a Product”. From the toolbox palette drag and drop and <a href="http://www.excel-vba.com/vba-forms-3-7-combo-boxes.htm" target="_blank">ComboBox</a> and a <a href="http://www.excel-vba.com/vba-forms-3-4-command-buttons.htm" target="_blank">CommandButton</a> onto the UserForm1. These components should be called <strong>ComboBox1</strong> and <strong>CommandButton1</strong>. You should end up with something that looks like this.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/ComboBox1_4.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Simple form with VBA." border="0" alt="ComboBox1 4 thumb Practical VBA Examples for the DBA: Part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/ComboBox1_4_thumb.png" width="244" height="108" /></a> </p>
<p>Save your work and close the VBA editor. In the Developer ribbon click the Macros button and add a new macro called <strong>comboBox</strong>. Add the following code to the macro. Make sure you change the <a href="http://www.connectionstrings.com/" target="_blank">connection string</a> for your environment.</p>
<pre lang="Visual BASIC">Sub comboBox()

    ' Setup connection string
    Dim connStr As String
    connStr = &quot;driver={sql server};server=localhost\sql2005;&quot;
    connStr = connStr &amp; &quot;Database=AdventureWorks;TrustedConnection=True;&quot;

    ' Setup the connection to the database
    Dim connection As ADODB.connection
    Set connection = New ADODB.connection
    connection.connectionString = connStr
    ' Open the connection
    connection.Open

    ' Open recordset.
    Set Cmd1 = New ADODB.Command
    Cmd1.ActiveConnection = connection
    Cmd1.CommandText = &quot;SELECT Name FROM Production.Product ORDER BY Name&quot;
    Set Results = Cmd1.Execute()

    Results.MoveFirst
    While Not Results.EOF

        ' Fill the Combo box with product names
        UserForm1.ComboBox1.AddItem Results.Fields(&quot;Name&quot;).Value
        Results.MoveNext

    Wend

    UserForm1.Show

End Sub</pre>
<p>This code will select product names from Production.Product and fill the combo box before displaying the form. Go back to UserForm1 and double click the <a href="http://www.excel-vba.com/vba-forms-3-4-command-buttons.htm" target="_blank">CommandButton</a>. This will open up the VBA code editor. Here we can associated an action with the button. Paste the below code into the editor replacing any code already in there. Change the <a href="http://www.connectionstrings.com" target="_blank">connection string</a> to point at your <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a>.</p>
<pre lang="Visual BASIC">Private Sub CommandButton1_Click()

    Dim selection As String
    ' Get the selected product escaping single quotes
    selection = Replace(UserForm1.ComboBox1.Value, &quot;'&quot;, &quot;''&quot;)

    ' Setup connection string
    Dim connStr As String
    connStr = &quot;driver={sql server};server=localhost\sql2005;&quot;
    connStr = connStr &amp; &quot;Database=AdventureWorks;TrustedConnection=True;&quot;

    ' Setup the connection to the database
    Dim connection As ADODB.connection
    Set connection = New ADODB.connection
    connection.connectionString = connStr
    ' Open the connection
    connection.Open

    ' Open recordset.
    Set Cmd1 = New ADODB.Command
    Cmd1.ActiveConnection = connection
    Cmd1.CommandText = &quot;SELECT * FROM Purchasing.PurchaseOrderDetail t1 INNER JOIN Production.Product t2 ON t1.ProductID = t2.ProductID AND t2.Name ='&quot; &amp; selection &amp; &quot;'&quot;
    Set Results = Cmd1.Execute()

    ' Clear the data from the active worksheet
    Cells.Select
    Cells.ClearContents

    ' Add column headers to the sheet
    headers = Results.Fields.Count
    For iCol = 1 To headers
        Cells(1, iCol).Value = Results.Fields(iCol - 1).Name
    Next

    ' Copy the resultset to the active worksheet
    Cells(2, 1).CopyFromRecordset Results

    ' Stop running the macro
    Unload Me

End Sub</pre>
<p>Save the code and return to Excel. The macro should be ready to run. Click the Macros button on the Developer ribbon. Choose <strong>ComboBox</strong> and click Run. If all is ok you will see the combobox populated with product names.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/select_a_product_5.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="ComboBox with VBA" border="0" alt="select a product 5 thumb Practical VBA Examples for the DBA: Part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/select_a_product_5_thumb.png" width="244" height="124" /></a> </p>
<p>Pick “Adjustable Race” and click “Choose Product”. Data should be returned in the open workbook.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/returned_data_6.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Order information for the selected product." border="0" alt="returned data 6 thumb Practical VBA Examples for the DBA: Part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/returned_data_6_thumb.png" width="244" height="149" /></a> </p>
<p><strong>Using VBA Multi-Select Lists (ListBox) with SQL Server</strong></p>
<p><font color="#666666">This example is very similar to the previous</font> one but this time we will allow the user to select more than one product by using a Multi-Select list or <a href="http://msdn.microsoft.com/en-us/library/aa223123(office.11).aspx" target="_blank">ListBox</a>.</p>
<p>Follow the same process above to create a new form. This form should be called <strong>UserForm2</strong>. Add a <a href="http://msdn.microsoft.com/en-us/library/aa223123(office.11).aspx" target="_blank">ListBox</a> and call it <strong>listProducts</strong> then add a button and call it <strong>btnProducts</strong>. You should end up with something looking like below;</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/UserForm2_7.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="ListBox in VBA" border="0" alt="UserForm2 7 thumb Practical VBA Examples for the DBA: Part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/UserForm2_7_thumb.png" width="244" height="149" /></a> </p>
<p>Save and return to Excel and add a new macro called <strong>selectList</strong> . Add the following code to the macro. Change the <a href="http://www.connectionstrings.com" target="_blank">connection string</a> as appropriate.</p>
<pre lang="Visual BASIC">Sub selectList()

    ' Setup connection string
    Dim connStr As String
    connStr = &quot;driver={sql server};server=localhost\sql2005;&quot;
    connStr = connStr &amp; &quot;Database=AdventureWorks;TrustedConnection=True;&quot;

    ' Setup the connection to the database
    Dim connection As ADODB.connection
    Set connection = New ADODB.connection
    connection.connectionString = connStr
    ' Open the connection
    connection.Open

    ' Open recordset.
    Set Cmd1 = New ADODB.Command
    Cmd1.ActiveConnection = connection
    Cmd1.CommandText = &quot;SELECT Name FROM Production.Product ORDER BY Name&quot;
    Set Results = Cmd1.Execute()

    UserForm2.listProducts.MultiSelect = fmMultiSelectMulti
    Results.MoveFirst
    While Not Results.EOF

        UserForm2.listProducts.AddItem Results.Fields(&quot;Name&quot;).Value
        Results.MoveNext

    Wend

    UserForm2.Show
End Sub</pre>
<p>This code will populate the <a href="http://www.excel-vba.com/vba-forms-3-8-list-boxes.htm" target="_blank">ListBox</a> with product names and display the form when the macro is executed. Return to the VBA editor and double click on <strong>btnProducts</strong>. Add the below code and not forgetting to change the <a href="http://www.connectionstrings.com" target="_blank">connection string</a>.</p>
<pre lang="Visual BASIC">Private Sub btnProducts_Click()

    Dim selection As String
    ' Get the selected products escaping single quotes
    'selection = Replace(UserForm2.listProducts.Value, &quot;'&quot;, &quot;''&quot;)
    Dim lItem As Long

    For lItem = 0 To listProducts.ListCount - 1

        If listProducts.Selected(lItem) = True Then

            selection = selection &amp; &quot;'&quot; &amp; Replace(listProducts.List(lItem), &quot;'&quot;, &quot;''&quot;) &amp; &quot;',&quot;
        End If
    Next

    selection = Mid(selection, 1, Len(selection) - 1)

    ' Setup connection string
    Dim connStr As String
    connStr = &quot;driver={sql server};server=localhost\sql2005;&quot;
    connStr = connStr &amp; &quot;Database=AdventureWorks;TrustedConnection=True;&quot;

    ' Setup the connection to the database
    Dim connection As ADODB.connection
    Set connection = New ADODB.connection
    connection.connectionString = connStr
    ' Open the connection
    connection.Open

    ' Open recordset.
    Set Cmd1 = New ADODB.Command
    Cmd1.ActiveConnection = connection
    Cmd1.CommandText = &quot;SELECT * FROM Purchasing.PurchaseOrderDetail t1 INNER JOIN Production.Product t2 ON t1.ProductID = t2.ProductID AND t2.Name IN (&quot; &amp; selection &amp; &quot;)&quot;
    Set Results = Cmd1.Execute()

    ' Clear the data from the active worksheet
    Cells.Select
    Cells.ClearContents

    ' Add column headers to the sheet
    headers = Results.Fields.Count
    For iCol = 1 To headers
       Cells(1, iCol).Value = Results.Fields(iCol - 1).Name
    Next

    ' Copy the resultset to the active worksheet
    Cells(2, 1).CopyFromRecordset Results

    ' Stop running the macro
    Unload Me

End Sub</pre>
<p>Save and return to Excel. The macro should be ready to run. Click the Macros button on the Developer ribbon and run the macro called “selectList”.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/select_list_8.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="VBA ListBox" border="0" alt="select list 8 thumb Practical VBA Examples for the DBA: Part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/select_list_8_thumb.png" width="244" height="186" /></a> </p>
<p>Select some products and “Sheet1” should be populated with the appropriate data.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/Excel_data_9.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Order information for multiple products." border="0" alt="Excel data 9 thumb Practical VBA Examples for the DBA: Part 2" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/Excel_data_9_thumb.png" width="244" height="151" /></a> </p>
</p>
<p>Hopefully this has been easy to follow. I don’t profess to be a <a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications" target="_blank">VBA</a> expert, or enthusiast,&#160; but I do see its use for quickly building interfaces around your data.</p>
<p><map name='google_ad_map_308_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/308?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_308_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=308&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fpractical-vba-examples-for-the-dba-part-2%2F308' title="Practical VBA Examples for the DBA: Part 2" alt=" Practical VBA Examples for the DBA: Part 2" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/practical-vba-examples-for-the-dba-part-2/308">Practical VBA Examples for the DBA: Part 2</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/practical-vba-examples-for-the-dba-part-2/308" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/practical-vba-examples-for-the-dba-part-2/308/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using Powershell to check if Triggers are enabled</title>
		<link>http://www.youdidwhatwithtsql.com/using-powershell-to-check-if-triggers-are-enabled/186</link>
		<comments>http://www.youdidwhatwithtsql.com/using-powershell-to-check-if-triggers-are-enabled/186#comments</comments>
		<pubDate>Wed, 17 Jun 2009 18:57:51 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[Triggers]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/using-powershell-to-check-if-triggers-are-enabled/186</guid>
		<description><![CDATA[I’ve been thinking about how Powershell can be used by the DBA as an extra tool in their armoury. An article that caught my eye was The Daily DBA Checklist, specifically an item about Triggers; &#34;…last week some idiot turned a host of triggers off in our ERP system, causing a cascade of posting problems [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/using-powershell-to-check-if-triggers-are-enabled/186">Using Powershell to check if Triggers are enabled</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I’ve been thinking about how <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> can be used by the <a href="http://en.wikipedia.org/wiki/Database_administrator" target="_blank">DBA</a> as an extra tool in their armoury. An article that caught my eye was <a href="http://www.simple-talk.com/community/blogs/tony_davis/archive/2008/05/13/52769.aspx" target="_blank">The Daily DBA Checklist</a>, specifically an item about <a href="http://en.wikipedia.org/wiki/Database_trigger" target="_blank">Triggers</a>;</p>
<p>&quot;<em>…last week some idiot turned a host of triggers off in our ERP system, causing a cascade of posting problems on dozens of orders before we caught the root cause…</em>&quot;</p>
<p><a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> does contain a system view called <a href="http://msdn.microsoft.com/en-us/library/ms188746(SQL.90).aspx" target="_blank">sys.triggers</a> to provide us with some detailed information on <a href="http://en.wikipedia.org/wiki/Database_trigger" target="_blank">triggers</a> in our databases. Lets view all triggers in the <a href="http://www.codeplex.com/MSFTDBProdSamples" target="_blank">AdventureWorks</a> 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('p186code21'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p18621"><td class="code" id="p186code21"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">triggers</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image13.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sys.triggers from the AdventureWorks database" border="0" alt="image thumb13 Using Powershell to check if Triggers are enabled" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb13.png" width="644" height="149" /></a></p>
<p>View a list of disabled triggers;</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('p186code22'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p18622"><td class="code" id="p186code22"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> name
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">triggers</span> 
<span style="color: #0000FF;">WHERE</span> is_disabled <span style="color: #808080;">=</span> <span style="color: #000;">1</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image14.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Disabled triggers in AdventureWorks" border="0" alt="image thumb14 Using Powershell to check if Triggers are enabled" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb14.png" width="177" height="108" /></a> </p>
<p>View a summary of enabled and disabled triggers;</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('p186code23'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p18623"><td class="code" id="p186code23"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">CASE</span><span style="color: #808080;">&#40;</span>is_disabled<span style="color: #808080;">&#41;</span>
              <span style="color: #0000FF;">WHEN</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Disabled'</span>
              <span style="color: #0000FF;">WHEN</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Enabled'</span>
          <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">state</span><span style="color: #808080;">&#93;</span>,
          <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">count</span><span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">triggers</span>
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> is_disabled;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image15.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Disabled / Enabled trigger summary in AdventureWorks" border="0" alt="image thumb15 Using Powershell to check if Triggers are enabled" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb15.png" width="164" height="88" /></a> </p>
<p>Many people may be happy with these methods but here’s a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> method that I think is much better;</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('p186code24'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p18624"><td class="code" id="p186code24"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Load SMO Extension</span>
<span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.SqlServer.Smo&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>;
&nbsp;
<span style="color: #008000;"># Set sql server &amp; db info</span>
<span style="color: #800080;">$sqlserver</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;RHYS-PC\sql2005&quot;</span>;
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;AdventureWorks&quot;</span>;
&nbsp;
<span style="color: #008000;"># Create sql server and db objects</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>;
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Databases<span style="color: #000000;">&#91;</span><span style="color: #800080;">$database</span><span style="color: #000000;">&#93;</span>;
&nbsp;
<span style="color: #800080;">$triggerCount</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
<span style="color: #800080;">$disabledCount</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
<span style="color: #008000;"># Any Database Triggers?</span>
<span style="color: #800080;">$DatabaseTriggerCount</span> <span style="color: pink;">=</span> <span style="color: #800080;">$db</span>.Triggers.Count;
&nbsp;
<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$DatabaseTriggerCount</span> <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$triggerCount</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$DatabaseTriggerCount</span>;
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$DatabaseTrigger</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.Triggers<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$active</span> <span style="color: pink;">=</span> <span style="color: #800080;">$DatabaseTrigger</span>.IsEnabled;
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$active</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008000;"># Set text to green</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;$DatabaseTrigger IsEnabled = $active.&quot;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">else</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;$DatabaseTrigger IsEnabled = $active.&quot;</span>;
			<span style="color: #008000;"># Increment $disabledCount</span>
			<span style="color: #800080;">$disabledCount</span> <span style="color: pink;">+=</span> <span style="color: #804000;">1</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># For each table</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$table</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.Tables<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># tally up trigger count</span>
	<span style="color: #800080;">$triggerCount</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$table</span>.Triggers.Count;	
	<span style="color: #008000;"># For each trigger on the current table</span>
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$trigger</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$table</span>.Triggers<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$active</span> <span style="color: pink;">=</span> <span style="color: #800080;">$trigger</span>.IsEnabled;
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$active</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008000;"># Set text to green</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;$trigger IsEnabled = $active.&quot;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">else</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;$trigger IsEnabled = $active.&quot;</span>;
			<span style="color: #008000;"># Increment $disabledCount</span>
			<span style="color: #800080;">$disabledCount</span> <span style="color: pink;">+=</span> <span style="color: #804000;">1</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Write summary</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;=======================================&quot;</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;Total triggers in $sqlserver\$database = $triggerCount&quot;</span>;
<span style="color: #800080;">$colour</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Green&quot;</span>;
<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$disabledCount</span> <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># If any triggers are disabled change warning text to red!</span>
	<span style="color: #800080;">$colour</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Red&quot;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> <span style="color: #800080;">$colour</span> <span style="color: #800000;">&quot;Total disabled triggers = $disabledCount.&quot;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image17.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Checking triggers with Powershell" border="0" alt="image thumb17 Using Powershell to check if Triggers are enabled" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb17.png" width="644" height="327" /></a> </p>
<p>In this <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script I have used coloured text to make certain things stand out. The <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> method would be easy to adapt to use with multiple servers or take corrective action, i.e. enabling triggers. A Powershell script would be an excellent choice for that final go-live sanity check. I’m going to focus on more items in <a href="http://www.simple-talk.com/community/blogs/tony_davis/archive/2008/05/13/52769.aspx" target="_blank">The Daily DBA Checklist</a>, and similar articles, for further <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> articles.</p>
<p><map name='google_ad_map_186_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/186?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_186_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=186&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fusing-powershell-to-check-if-triggers-are-enabled%2F186' title="Using Powershell to check if Triggers are enabled" alt=" Using Powershell to check if Triggers are enabled" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/using-powershell-to-check-if-triggers-are-enabled/186">Using Powershell to check if Triggers are enabled</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/using-powershell-to-check-if-triggers-are-enabled/186" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/using-powershell-to-check-if-triggers-are-enabled/186/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cannot resolve the collation conflict</title>
		<link>http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176</link>
		<comments>http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176#comments</comments>
		<pubDate>Fri, 12 Jun 2009 17:57:53 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[collations]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176</guid>
		<description><![CDATA[I do a fair bit of work with Linked Servers and cross-database queries&#160; and sometimes come across the following error when joining between databases with different collations; Msg 468, Level 16, State 9, Line 1 Cannot resolve the collation conflict between 'Latin1_General_CI_AS' and 'SQL_Latin1_General_Pref_CP850_CI_AS'; in the equal to operation. To replicate this error run the [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176">Cannot resolve the collation conflict</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I do a fair bit of work with <a href="http://msdn.microsoft.com/en-us/library/ms188279.aspx" target="_blank">Linked Servers</a> and <a href="http://www.sqlservercentral.com/articles/Advanced/designingcrossdatabasequeries/1753/" target="_blank">cross-database queries</a>&#160; and sometimes come across the following error when joining between databases with different <a href="http://msdn.microsoft.com/en-us/library/aa214408(SQL.80).aspx" target="_blank">collations</a>;</p>
<pre>Msg 468, Level 16, State 9, Line 1

Cannot resolve the collation conflict between 'Latin1_General_CI_AS' and 'SQL_Latin1_General_Pref_CP850_CI_AS'; in the equal to operation.</pre>
<p>To replicate this error run the below <a href="http://msdn.microsoft.com/en-us/library/ms189826.aspx" target="_blank">TSQL</a> to create two databases with tables and data.&#160;</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('p176code29'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17629"><td class="code" id="p176code29"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Create first database</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">DATABASE</span> database1 <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AS;
GO
<span style="color: #008080;">-- Create second database</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">DATABASE</span> database2 <span style="color: #0000FF;">COLLATE</span> SQL_Latin1_General_Pref_CP850_CI_AS;
GO
&nbsp;
<span style="color: #0000FF;">USE</span> database1;
GO
<span style="color: #008080;">-- Create Customer table in database1</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Customer
<span style="color: #808080;">&#40;</span>
	CustID <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> <span style="color: #0000FF;">CLUSTERED</span>,
	FirstName <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;">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;">30</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>,
	StartDate <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>; 
GO
<span style="color: #0000FF;">USE</span> database2;
GO
<span style="color: #008080;">-- Create Customer table in database2</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Customer
<span style="color: #808080;">&#40;</span>
	CustID <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> <span style="color: #0000FF;">CLUSTERED</span>,
	FirstName <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;">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;">30</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>,
	StartDate <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>; 
GO
&nbsp;
<span style="color: #0000FF;">USE</span> database1;
GO
<span style="color: #008080;">-- Insert test data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Customer</span>
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	DOB
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Joe'</span>, <span style="color: #FF0000;">'Bloggs'</span>, <span style="color: #FF0000;">'1975-01-01 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Dave'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1977-10-11 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Fred'</span>, <span style="color: #FF0000;">'Bloggs'</span>, <span style="color: #FF0000;">'1965-11-28 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Sue'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1974-06-17 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Steve'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1981-07-07 00:00:00'</span>;
GO
<span style="color: #0000FF;">USE</span> database2;
GO
<span style="color: #008080;">-- Insert test data</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Customer</span>
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	DOB
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Joe'</span>, <span style="color: #FF0000;">'Bloggs'</span>, <span style="color: #FF0000;">'1975-01-01 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Dave'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1977-10-11 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Fred'</span>, <span style="color: #FF0000;">'Bloggs'</span>, <span style="color: #FF0000;">'1965-11-28 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span><span style="color: #FF0000;">'Sue'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1974-06-17 00:00:00'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Steve'</span>, <span style="color: #FF0000;">'Smith'</span>, <span style="color: #FF0000;">'1981-07-07 00:00:00'</span>;
GO</pre></td></tr></table></div>

<p>Run the following query to observe the collation conflict.</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('p176code30'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17630"><td class="code" id="p176code30"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Cross-database query causing the collation conflict</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Customer</span> c1
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> database1.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">Customer</span> <span style="color: #0000FF;">AS</span> c2
	<span style="color: #0000FF;">ON</span> c1.<span style="color: #202020;">FirstName</span> <span style="color: #808080;">=</span> c2.<span style="color: #202020;">FirstName</span>
<span style="color: #808080;">AND</span> c1.<span style="color: #202020;">LastName</span> <span style="color: #808080;">=</span> c2.<span style="color: #202020;">LastName</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image11.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="SQL Server collation conflict" border="0" alt="image thumb11 Cannot resolve the collation conflict" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb11.png" width="644" height="72" /></a> </p>
<p>You could fix this by changing the collation in one of the databases, i.e.</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('p176code31'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17631"><td class="code" id="p176code31"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Set database2 to the same collation as database1</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">DATABASE</span> database2 <span style="color: #0000FF;">COLLATE</span> Latin1_General_CI_AS;
<span style="color: #008080;">-- Change VARCHAR columns on our existing tables</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> Customer <span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">COLUMN</span> FirstName <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: #0000FF;">COLLATE</span> Latin1_General_CI_AS;
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> Customer <span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">COLUMN</span> LastName <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: #0000FF;">COLLATE</span> Latin1_General_CI_AS;</pre></td></tr></table></div>

<p>Sometimes you may not want, or be able, to change a database in this way. In these situations you can add <a href="http://msdn.microsoft.com/en-us/library/aa258237(SQL.80).aspx" target="_blank">COLLATE</a> DATABASE_DEFAULT to the JOINS or expressions in your query.</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('p176code32'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17632"><td class="code" id="p176code32"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Using COLLATE DATABASE_DEFAULT</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Customer</span> c1
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> database1.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">Customer</span> <span style="color: #0000FF;">AS</span> c2
	<span style="color: #0000FF;">ON</span> c1.<span style="color: #202020;">FirstName</span> <span style="color: #808080;">=</span> c2.<span style="color: #202020;">FirstName</span> <span style="color: #0000FF;">COLLATE</span> DATABASE_DEFAULT
<span style="color: #808080;">AND</span> c1.<span style="color: #202020;">LastName</span> <span style="color: #808080;">=</span> c2.<span style="color: #202020;">LastName</span> <span style="color: #0000FF;">COLLATE</span> DATABASE_DEFAULT;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image12.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Query using COLLATE DATABASE_DEFAULT" border="0" alt="image thumb12 Cannot resolve the collation conflict" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/06/image-thumb12.png" width="644" height="102" /></a></p>
<p><map name='google_ad_map_176_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/176?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_176_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=176&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcannot-resolve-the-collation-conflict%2F176' title="Cannot resolve the collation conflict" alt=" Cannot resolve the collation conflict" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176">Cannot resolve the collation conflict</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/cannot-resolve-the-collation-conflict/176/feed</wfw:commentRss>
		<slash:comments>5</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="image thumb7 Auditing your SQL Servers with Powershell" 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('p133code34'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13334"><td class="code" id="p133code34"><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>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/auditing-your-sql-server-with-powershell/133" size="standard" count="true"></div></div>]]></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>Auditing Network Adapters with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/auditing-network-adapters-with-powershell/126</link>
		<comments>http://www.youdidwhatwithtsql.com/auditing-network-adapters-with-powershell/126#comments</comments>
		<pubDate>Tue, 26 May 2009 17:40:39 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[network settings]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/auditing-network-adapters-with-powershell/126</guid>
		<description><![CDATA[Those boring network auditing tasks you have to do are now going to be a breeze with Powershell. With technologies like WMI accessible from Powershell there is virtually no limit to what you can do. I’m going to publish a series of articles showing how Powershell can be used to document your server and network. [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/auditing-network-adapters-with-powershell/126">Auditing Network Adapters with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Those boring network auditing tasks you have to do are now going to be a breeze with <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>. With technologies like <a href="http://en.wikipedia.org/wiki/Windows_Management_Instrumentation" target="_blank">WMI</a> accessible from <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> there is virtually no limit to what you can do. I’m going to publish a series of articles showing how <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> can be used to document your server and network. First here is a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script that can be used to document networking settings on a host.</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('p126code36'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12636"><td class="code" id="p126code36"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Function used for coalesce</span>
<span style="color: #0000FF;">function</span> coalesce<span style="color: #000000;">&#40;</span><span style="color: #800080;">$param</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;">$param</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$param</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&lt;not configured&gt;&quot;</span>;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">return</span> <span style="color: #800080;">$param</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">function</span> appendToFile
<span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">param</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$computer</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$adapterLine</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$fileName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\$computer&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800000;">&quot;NetworkAdapters.csv&quot;</span>;
	<span style="color: #008000;"># Does the file already exist?</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Test-Path</span> <span style="color: #800080;">$fileName</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># Append text to file</span>
		<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800080;">$fileName</span> <span style="color: #800080;">$adapterLine</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># Create the file</span>
		<span style="color: #008080; font-weight: bold;">New-Item</span> <span style="color: #800080;">$fileName</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> file <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>
		<span style="color: #008000;"># Append the headers</span>
		<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800080;">$fileName</span> <span style="color: #800000;">&quot;adapterName,MACAddress,ipAddress,DefaultIPGateway,DHCPEnabled,DHCPServer,DNSDomain&quot;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>;
		<span style="color: #008000;"># Append text to file</span>
		<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800080;">$fileName</span> <span style="color: #800080;">$adapterLine</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Set computer name here</span>
<span style="color: #800080;">$computer</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost&quot;</span>;
<span style="color: #008000;"># Set to true to show Network adapters with no configured IP address.</span>
<span style="color: #800080;">$showNoIP</span> <span style="color: pink;">=</span> <span style="color: #800080;">$false</span>;
&nbsp;
<span style="color: #800080;">$networkAdapters</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-Class</span> win32_networkadapter <span style="color: #008080; font-style: italic;">-computername</span> <span style="color: #800080;">$computer</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$networkAdapter</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$networkAdapters</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># Win32_NetworkAdapter properties</span>
	<span style="color: #800080;">$adapterName</span> <span style="color: pink;">=</span> coalesce<span style="color: #000000;">&#40;</span><span style="color: #800080;">$networkAdapter</span>.Name<span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$adapterStatus</span> <span style="color: pink;">=</span> coalesce<span style="color: #000000;">&#40;</span><span style="color: #800080;">$networkAdapter</span>.Status<span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$MACAddress</span> <span style="color: pink;">=</span> coalesce<span style="color: #000000;">&#40;</span><span style="color: #800080;">$networkAdapter</span>.MACAddress<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #008000;"># Get the Index of the currrent adapter to retireve its configuration from another WMI class</span>
	<span style="color: #800080;">$adapterIndex</span> <span style="color: pink;">=</span> <span style="color: #800080;">$networkAdapter</span>.Index;
	<span style="color: #800080;">$adapterConfig</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-Class</span> Win32_NetworkAdapterConfiguration <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Index = $adapterIndex AND ipenabled = 'true'&quot;</span> <span style="color: #008080; font-style: italic;">-computername</span> <span style="color: #800080;">$computer</span><span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #008000;"># Win32_NetworkAdapterConfiguration properties</span>
	<span style="color: #800080;">$ipAddress</span> <span style="color: pink;">=</span> coalesce<span style="color: #000000;">&#40;</span><span style="color: #800080;">$adapterConfig</span>.IPAddress<span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$DefaultIPGateway</span> <span style="color: pink;">=</span> coalesce<span style="color: #000000;">&#40;</span><span style="color: #800080;">$adapterConfig</span>.DefaultIPGateway<span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$DHCPEnabled</span> <span style="color: pink;">=</span> coalesce<span style="color: #000000;">&#40;</span><span style="color: #800080;">$adapterConfig</span>.DHCPEnabled<span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$DHCPServer</span> <span style="color: pink;">=</span> coalesce<span style="color: #000000;">&#40;</span><span style="color: #800080;">$adapterConfig</span>.DHCPServer<span style="color: #000000;">&#41;</span>;
	<span style="color: #800080;">$DNSDomain</span> <span style="color: pink;">=</span> coalesce<span style="color: #000000;">&#40;</span><span style="color: #800080;">$adapterConfig</span>.DNSDomain<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #008000;"># Display info only for adapters with configured IP addresses</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$ipAddress</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;&lt;not configured&gt;&quot;</span> <span style="color: #FF0000;">-and</span> <span style="color: #800080;">$showNoIP</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: #800080;">$adapterLine</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$adapterName,$MACAddress,$ipAddress,$DefaultIPGateway,$DHCPEnabled,$DHCPServer,$DNSDomain&quot;</span>;
		appendToFile <span style="color: pink;">-</span>computer <span style="color: #800000;">&quot;$computer&quot;</span> <span style="color: pink;">-</span>adapterLine <span style="color: #800000;">&quot;$adapterLine&quot;</span>;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #008000;"># Display info for all adapters</span>
	<span style="color: #0000FF;">elseif</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$showNoIP</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$adapterLine</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$adapterName,$MACAddress,$ipAddress,$DefaultIPGateway,$DHCPEnabled,$DHCPServer,$DNSDomain&quot;</span>;
		appendToFile <span style="color: pink;">-</span>computer <span style="color: #800000;">&quot;$computer&quot;</span> <span style="color: pink;">-</span>adapterLine <span style="color: #800000;">&quot;$adapterLine&quot;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Just change the <strong>$computer </strong>variable to whichever computer you wish to audit. This will produce a file called <strong>&lt;computer name&gt;_NetworkApaters.csv</strong> and will contain information similar to;</p>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="454">adapterName</td>
<td width="123">MACAddress</td>
<td width="86">ipAddress</td>
<td width="136">DefaultIPGateway</td>
<td width="104">DHCPEnabled</td>
<td width="127">DHCPServer</td>
<td width="136">DNSDomain</td>
</tr>
<tr>
<td>Realtek RTL8187B Wireless 802.11b/g 54Mbps USB 2.0 Network Adapter</td>
<td>00:16:44:6F:FF:65</td>
<td>192.168.0.2</td>
<td>192.168.0.1</td>
<td>TRUE</td>
<td>192.168.0.1</td>
<td>&lt;not configured&gt;</td>
</tr>
</tbody>
</table>
<p>The $<strong>showNoIP </strong>is set to only show adapters configured with <a href="http://en.wikipedia.org/wiki/TCP/IP" target="_blank">TCP/IP</a>. Set this to <strong>$true </strong>to make the script dump details of all adapters. </p>
<p>It would be fairly trivial to get this script to process a list of computers to document an entire network. I have shown this in <a href="http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113" target="_blank">other Powershell scripts</a> but will be demonstrating this again in a future post.</p>
<p><map name='google_ad_map_126_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/126?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_126_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=126&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fauditing-network-adapters-with-powershell%2F126' title="Auditing Network Adapters with Powershell" alt=" Auditing Network Adapters with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/auditing-network-adapters-with-powershell/126">Auditing Network Adapters with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/auditing-network-adapters-with-powershell/126" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/auditing-network-adapters-with-powershell/126/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Altering Database Objects with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/altering-database-objects-with-powershell/119</link>
		<comments>http://www.youdidwhatwithtsql.com/altering-database-objects-with-powershell/119#comments</comments>
		<pubDate>Sat, 23 May 2009 20:54:24 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell Scripting]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/altering-database-objects-with-powershell/119</guid>
		<description><![CDATA[Sometimes it’s necessary to rename tables and databases and this can create a lot of work if it’s referenced by other database objects. I recently came across this situation at work. Developers had introduced a second database into the system and each referenced the other. This didn&#8217;t sit well with our testing environment that had [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/altering-database-objects-with-powershell/119">Altering Database Objects with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Sometimes it’s necessary to rename tables and databases and this can create a lot of work if it’s referenced by other database objects. I recently came across this situation at work. Developers had introduced a second database into the system and each referenced the other. This didn&#8217;t sit well with our testing environment that had multiple copies of customer databases. Here’s a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> Script I wrote to make the process easy.</p>
<p>This script will search for database objects containing the text mentioned in <strong>$matchText</strong>. When a match is found it generates a backup of the object definition and a then a change script. The script will alter the definition by replacing <strong>$matchText </strong>with <strong>$replaceText</strong>. Please be aware that the <strong>match</strong> switch accepts a <a href="http://www.regular-expressions.info " target="_blank">regular expression</a> so some characters have a special meaning. If <strong>$alter</strong> is set to true the script will attempt to alter the database object on the live server. The script can be pointed at any database by setting <strong>$server </strong>and <strong>$database</strong>. Before running ensure you have the directory paths mentioned in <strong>$backupFolder </strong>and <strong>$changeFolder</strong>. These need to contain folders called; ‘procs’, ‘views’, ‘functions’ and ‘triggers&#8217;.</p>
<p>In this example I’ll be using a copy of the <a href="http://codeplex.com/SqlServerSamples" target="_blank">AdventureWorks</a> sample database. Let’s assume for some reason that the <strong>Person.Address </strong>table gets renamed to <strong>Person.Address2</strong>. Create this new table in the <a href="http://codeplex.com/SqlServerSamples" target="_blank">AdventureWorks</a> 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('p119code40'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11940"><td class="code" id="p119code40"><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>Person<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Address2<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>AddressID<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: #808080;">NOT</span> <span style="color: #0000FF;">FOR</span> <span style="color: #0000FF;">REPLICATION</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>AddressLine1<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">60</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">COLLATE</span> Latin1_General_CS_AS <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>AddressLine2<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">60</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">COLLATE</span> Latin1_General_CS_AS <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>City<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">COLLATE</span> Latin1_General_CS_AS <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>StateProvinceID<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: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>PostalCode<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">15</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">COLLATE</span> Latin1_General_CS_AS <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>rowguid<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">uniqueidentifier</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ROWGUIDCOL</span>  <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>DF_Address_rowguid2<span style="color: #808080;">&#93;</span>  <span style="color: #0000FF;">DEFAULT</span> <span style="color: #808080;">&#40;</span>newid<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,
	<span style="color: #808080;">&#91;</span>ModifiedDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">datetime</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>DF_Address_ModifiedDate2<span style="color: #808080;">&#93;</span>  <span style="color: #0000FF;">DEFAULT</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">getdate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>,
 <span style="color: #0000FF;">CONSTRAINT</span> <span style="color: #808080;">&#91;</span>PK_Address_AddressID2<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span> 
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>AddressID<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ASC</span>
<span style="color: #808080;">&#41;</span><span style="color: #0000FF;">WITH</span> <span style="color: #808080;">&#40;</span>PAD_<span style="color: #808080;">IN</span>DEX  <span style="color: #808080;">=</span> <span style="color: #0000FF;">OFF</span>, STATISTICS_N<span style="color: #808080;">OR</span>ECOMPUTE  <span style="color: #808080;">=</span> <span style="color: #0000FF;">OFF</span>, IGN<span style="color: #808080;">OR</span>E_DUP_KEY <span style="color: #808080;">=</span> <span style="color: #0000FF;">OFF</span>, <span style="color: #808080;">ALL</span>OW_ROW_LOCKS  <span style="color: #808080;">=</span> <span style="color: #0000FF;">ON</span>, <span style="color: #808080;">ALL</span>OW_PAGE_LOCKS  <span style="color: #808080;">=</span> <span style="color: #0000FF;">ON</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">ON</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">PRIMARY</span><span style="color: #808080;">&#93;</span>
<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">ON</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">PRIMARY</span><span style="color: #808080;">&#93;</span></pre></td></tr></table></div>

<p>Then run the <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershel</a>l code below with appropriate changes to suit your environment. This script will process any stored procedures, views, functions or triggers containing references to <strong>Person.Address</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('p119code41'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11941"><td class="code" id="p119code41"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$server</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost\sql2005&quot;</span>;					<span style="color: #008000;"># The SQL Server instance name</span>
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;AdventureWorks&quot;</span>;					<span style="color: #008000;"># The database name</span>
<span style="color: #800080;">$matchText</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;\[Person\].\[Address\]&quot;</span>;			<span style="color: #008000;"># Definition text to search .Be aware this accepts a regular expression</span>
<span style="color: #800080;">$replaceText</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;[Person].[Address2]&quot;</span>;			<span style="color: #008000;"># Text to replace $matchText</span>
<span style="color: #800080;">$alter</span> <span style="color: pink;">=</span> <span style="color: #800080;">$false</span>;								    <span style="color: #008000;"># Set to true if you want the script to alter database objects</span>
<span style="color: #800080;">$backupFolder</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;E:\powershell\backup\&quot;</span>;	    <span style="color: #008000;"># Change script folders. Need a \ (back slash) on the end</span>
<span style="color: #800080;">$changeFolder</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;E:\powershell\change\&quot;</span>		    <span style="color: #008000;"># One file per object, backup &amp; change folders</span>
&nbsp;
<span style="color: #008000;"># Load the SQL Management Objects assembly (Pipe out-null supresses output)</span>
<span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.SqlServer.SMO&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">out-null</span>
&nbsp;
<span style="color: #008000;"># Create our SMO objects</span>
<span style="color: #800080;">$srv</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #800000;">&quot;Microsoft.SqlServer.Management.SMO.Server&quot;</span> <span style="color: #800080;">$server</span>;
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.SqlServer.Management.SMO.Database&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008000;"># Get the database</span>
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Databases<span style="color: #000000;">&#91;</span><span style="color: #800080;">$database</span><span style="color: #000000;">&#93;</span>;
&nbsp;
<span style="color: #008000;"># For each stored procedure in the database</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$proc</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.StoredProcedures<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># For each matching stored prcoedure</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$proc</span>.TextBody <span style="color: #FF0000;">-match</span> <span style="color: #800080;">$matchText</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 proc: &quot;</span> <span style="color: #800080;">$proc</span>.Name;
		<span style="color: #008000;"># Backup of the original proc definition</span>
		<span style="color: #800080;">$proc</span>.Script<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$backupFolder</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;procs\&quot;</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;">$srv</span>.name <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\\&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;_&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$db</span>.Name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$proc</span>.name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_backup.sql&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #008000;"># New procedure definition sql</span>
		<span style="color: #800080;">$proc</span>.Script<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$matchtext</span><span style="color: pink;">,</span> <span style="color: #800080;">$replaceText</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$changeFolder</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;procs\&quot;</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;">$srv</span>.name <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\\&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;_&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$db</span>.Name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$proc</span>.name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;.sql&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #008000;"># If set to true this will change the procedure definition on the server!</span>
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$alter</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$proc</span>.TextBody <span style="color: pink;">=</span> <span style="color: #800080;">$proc</span>.TextBody <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$matchtext</span><span style="color: pink;">,</span> <span style="color: #800080;">$replaceText</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #800080;">$proc</span>.Alter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Altered &quot;</span> <span style="color: #800080;">$proc</span>.Name;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># For each view in the database</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$view</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.Views<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># For each matching view</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$view</span>.TextBody <span style="color: #FF0000;">-match</span> <span style="color: #800080;">$matchText</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 view: &quot;</span> <span style="color: #800080;">$view</span>.Name;
		<span style="color: #008000;"># backup the original view definition</span>
		<span style="color: #800080;">$view</span>.Script<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$backupFolder</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;views\&quot;</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;">$srv</span>.name <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\\&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;_&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$db</span>.Name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$view</span>.name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_backup.sql&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #008000;"># New procedure definition sql</span>
		<span style="color: #800080;">$view</span>.Script<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$matchtext</span><span style="color: pink;">,</span> <span style="color: #800080;">$replaceText</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$changeFolder</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;views\&quot;</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;">$srv</span>.name <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\\&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;_&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$db</span>.Name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$view</span>.name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;.sql&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #008000;"># If set to true this will change the view definition on the server!</span>
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$alter</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$view</span>.TextBody <span style="color: pink;">=</span> <span style="color: #800080;">$view</span>.TextBody <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$matchtext</span><span style="color: pink;">,</span> <span style="color: #800080;">$replaceText</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #800080;">$view</span>.Alter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Altered &quot;</span> <span style="color: #800080;">$view</span>.Name;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># For each trigger in the database</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$trigger</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.Triggers<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># for each matching trigger</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$trigger</span>.TextBody <span style="color: #FF0000;">-match</span> <span style="color: #800080;">$matchText</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 trigger: &quot;</span> <span style="color: #800080;">$trigger</span>.Name;
		<span style="color: #008000;"># backup the original trigger definition</span>
		<span style="color: #800080;">$trigger</span>.Script<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$backupFolder</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;triggers\&quot;</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;">$srv</span>.name <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\\&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;_&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$db</span>.Name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$trigger</span>.name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_backup.sql&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #008000;"># New trigger definition sql</span>
		<span style="color: #800080;">$trigger</span>.Script<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$matchtext</span><span style="color: pink;">,</span> <span style="color: #800080;">$replaceText</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$changeFolder</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;triggers\&quot;</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;">$srv</span>.name <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\\&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;_&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$db</span>.Name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$trigger</span>.name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;.sql&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #008000;"># If set to true this will change the trigger definition on the server!</span>
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$alter</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$trigger</span>.TextBody <span style="color: pink;">=</span> <span style="color: #800080;">$trigger</span>.TextBody <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$matchtext</span><span style="color: pink;">,</span> <span style="color: #800080;">$replaceText</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #800080;">$trigger</span>.Alter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Altered &quot;</span> <span style="color: #800080;">$trigger</span>.Name;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># For each UDF in the database</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$udf</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.UserDefinedFunctions<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># for each matching udf</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$udf</span>.TextBody <span style="color: #FF0000;">-match</span> <span style="color: #800080;">$matchText</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 UDF: &quot;</span> <span style="color: #800080;">$udf</span>.Name;
		<span style="color: #008000;"># backup the original udf definition</span>
		<span style="color: #800080;">$udf</span>.Script<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$backupFolder</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;functions\&quot;</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;">$srv</span>.name <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\\&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;_&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$db</span>.Name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$udf</span>.name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_backup.sql&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #008000;"># New udf definition sql</span>
		<span style="color: #800080;">$udf</span>.Script<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$matchtext</span><span style="color: pink;">,</span> <span style="color: #800080;">$replaceText</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$changeFolder</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;functions\&quot;</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;">$srv</span>.name <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\\&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;_&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$db</span>.Name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;_&quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$udf</span>.name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;.sql&quot;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #008000;"># If set to true this will change the udf definition on the server!</span>
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$alter</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$udf</span>.TextBody <span style="color: pink;">=</span> <span style="color: #800080;">$udf</span>.TextBody <span style="color: #FF0000;">-replace</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$matchtext</span><span style="color: pink;">,</span> <span style="color: #800080;">$replaceText</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #800080;">$udf</span>.Alter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Altered &quot;</span> <span style="color: #800080;">$udf</span>.Name;
		<span style="color: #000000;">&#125;</span>	
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Finished processing  $database on $server.&quot;</span>;</pre></td></tr></table></div>

<p>After correctly configuring and running the script you should see output similar to this;</p>
<pre>Processing proc:  pMyApp1AddressAdd
Processing proc:  pMyApp1AddressDelete
Processing proc:  pMyApp1AddressDeleteRecords
Processing proc:  pMyApp1AddressDrillDown
Processing proc:  pMyApp1AddressExport
Processing proc:  pMyApp1AddressGet
Processing proc:  pMyApp1AddressGetList
Processing proc:  pMyApp1AddressGetStats
Processing proc:  pMyApp1AddressUpdate
Processing proc:  pMyApp1CustomerAddressExport
Processing proc:  pMyApp1EmployeeAddressExport
Processing proc:  pMyApp2AddressAdd
Processing proc:  pMyApp2AddressDelete
Processing proc:  pMyApp2AddressDeleteRecords
Processing proc:  pMyApp2AddressDrillDown
Processing proc:  pMyApp2AddressExport
Processing proc:  pMyApp2AddressGet
Processing proc:  pMyApp2AddressGetList
Processing proc:  pMyApp2AddressGetStats
Processing proc:  pMyApp2AddressUpdate
Processing proc:  pMyApp2CustomerAddressExport
Processing view:  vEmployee
Processing view:  vVendor
Processing view:  vIndividualCustomer
Processing view:  vSalesPerson
Processing view:  vStoreWithDemographics
Finished processing  AdventureWorks on localhost\sql2005.</pre>
<p>Some scripts will be produced in the directories you created. The Backup folder contains the original object definition and the Change folder provides a new version with the references to <strong>[Person.Address] </strong>altered to <strong>[Person.Address2]</strong>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image6.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SQL Scripts generated by Powershell" border="0" alt="image thumb6 Altering Database Objects with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image-thumb6.png" width="244" height="98" /></a></p>
<p>The <strong>$alter </strong>variable is set to $false in the <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script above. Once you’re happy with the changes set this to $true and re-run the script. The database objects will then be changed.</p>
<pre>Processing proc:  pMyApp1AddressAdd
Altered  pMyApp1AddressAdd
Processing proc:  pMyApp1AddressDelete
Altered  pMyApp1AddressDelete
Processing proc:  pMyApp1AddressDeleteRecords
Altered  pMyApp1AddressDeleteRecords
Processing proc:  pMyApp1AddressDrillDown
Altered  pMyApp1AddressDrillDown
Processing proc:  pMyApp1AddressExport
Altered  pMyApp1AddressExport
Processing proc:  pMyApp1AddressGet
Altered  pMyApp1AddressGet
Processing proc:  pMyApp1AddressGetList
Altered  pMyApp1AddressGetList
Processing proc:  pMyApp1AddressGetStats
Altered  pMyApp1AddressGetStats
Processing proc:  pMyApp1AddressUpdate
Altered  pMyApp1AddressUpdate
Processing proc:  pMyApp1CustomerAddressExport
Altered  pMyApp1CustomerAddressExport
Processing proc:  pMyApp1EmployeeAddressExport
Altered  pMyApp1EmployeeAddressExport
Processing proc:  pMyApp2AddressAdd
Altered  pMyApp2AddressAdd
Processing proc:  pMyApp2AddressDelete
Altered  pMyApp2AddressDelete
Processing proc:  pMyApp2AddressDeleteRecords
Altered  pMyApp2AddressDeleteRecords
Processing proc:  pMyApp2AddressDrillDown
Altered  pMyApp2AddressDrillDown
Processing proc:  pMyApp2AddressExport
Altered  pMyApp2AddressExport
Processing proc:  pMyApp2AddressGet
Altered  pMyApp2AddressGet
Processing proc:  pMyApp2AddressGetList
Altered  pMyApp2AddressGetList
Processing proc:  pMyApp2AddressGetStats
Altered  pMyApp2AddressGetStats
Processing proc:  pMyApp2AddressUpdate
Altered  pMyApp2AddressUpdate
Processing proc:  pMyApp2CustomerAddressExport
Altered  pMyApp2CustomerAddressExport
Processing view:  vEmployee
Altered  vEmployee
Processing view:  vVendor
Altered  vVendor
Processing view:  vIndividualCustomer
Altered  vIndividualCustomer
Processing view:  vSalesPerson
Altered  vSalesPerson
Processing view:  vStoreWithDemographics
Altered  vStoreWithDemographics
Finished processing  AdventureWorks on localhost\sql2005.</pre>
<p>You can easily reverse the change by changing two lines in the script and re-running it.</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('p119code42'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11942"><td class="code" id="p119code42"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$matchText</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;\[Person\].\[Address2\]&quot;</span>;			<span style="color: #008000;"># Definition text to search .Be aware this accepts a regular expression</span>
<span style="color: #800080;">$replaceText</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;[Person].[Address]&quot;</span>;			<span style="color: #008000;"># Text to replace $matchText</span></pre></td></tr></table></div>

<p><map name='google_ad_map_119_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/119?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_119_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=119&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Faltering-database-objects-with-powershell%2F119' title="Altering Database Objects with Powershell" alt=" Altering Database Objects with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/altering-database-objects-with-powershell/119">Altering Database Objects with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/altering-database-objects-with-powershell/119" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/altering-database-objects-with-powershell/119/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Monitoring &amp; starting Services with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113</link>
		<comments>http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113#comments</comments>
		<pubDate>Thu, 21 May 2009 21:32:25 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[Windows Services]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113</guid>
		<description><![CDATA[Are you part of the DBA crowd that hasn&#8217;t yet checked out Powershell? I&#8217;m really enthusiastic about its potential for server administration. Script out all those mundane jobs you have to do and make life easy. Here&#8217;s a Powershell script that makes checking if services are running, and optionally starting them, on multiple servers really [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113">Monitoring &amp; starting Services with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Are you part of the <a href="http://en.wikipedia.org/wiki/Database_administrator" target="_blank">DBA</a> crowd that hasn&#8217;t yet checked out <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a>? I&#8217;m really enthusiastic about its potential for server administration. Script out all those mundane jobs you have to do and make life easy. Here&#8217;s a <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> script that makes checking if services are running, and optionally starting them, on multiple servers really easy. First, be sure to check <a href="http://en.wikipedia.org/wiki/Windows_service#Managing_services" target="_blank">services.msc</a> for your SQL Server service names. They may be different from the default, i.e. if you’re running <a href="http://msdn.microsoft.com/en-us/library/ms165614.aspx" target="_blank">Named Instances</a>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SQL Server Service Names" border="0" alt="image thumb4 Monitoring &amp; starting Services with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image-thumb4.png" width="217" height="244" /></a>&#160;</p>
<ul>
<li>Create <strong>C:\Computers.txt</strong> with a list of servers to check. </li>
<li>The $servicesArray specifies <strong>SQLSERVERAGENT </strong>and<strong> MSSQLSERVER</strong>. Change these to different services if required. Be sure to use single quotes if a $ symbol is in the service name or they will be treated as variables. </li>
<li>Set $start = $true will make the script start the listed services if they are not running. </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('p113code44'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11344"><td class="code" id="p113code44"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Setup trap to catch exceptions</span>
<span style="color: #0000FF;">trap</span> <span style="color: #000000;">&#91;</span>Exception<span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">write-error</span> $<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;TRAPPED: &quot;</span> <span style="color: pink;">+</span> <span style="color: #000080;">$_</span>.Exception.Message<span style="color: #000000;">&#41;</span>; 
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># read computers from text file</span>
<span style="color: #800080;">$computers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> C:\Computers.txt;
<span style="color: #800080;">$start</span> <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;
&nbsp;
<span style="color: #008000;"># Setup the Service array with the service names we want to check are running</span>
<span style="color: #800080;">$serviceArray</span> <span style="color: pink;">=</span> <span style="color: #800000;">'SQLAgent$SQL2005'</span><span style="color: pink;">,</span> <span style="color: #800000;">'MSSQL$SQL2005'</span>;
&nbsp;
<span style="color: #008000;"># Powershell knows it's an array so working with it is simple</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>
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Checking $computer&quot;</span>;
	<span style="color: #800080;">$objWMIService</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-Class</span> win32_service <span style="color: pink;">-</span>computer <span style="color: #800080;">$computer</span>
&nbsp;
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$service</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$objWMIService</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>	
		<span style="color: #008000;"># Check each service specicfied in the $serviceArray</span>
		<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$srv</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$serviceArray</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;">$service</span>.name <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$srv</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;$srv is present on $computer.&quot;</span>;
				<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$service</span>.state <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;running&quot;</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;$srv is running on $computer&quot;</span>;
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0000FF;">else</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;$srv is not running on $computer&quot;</span>;
					<span style="color: #008000;"># If $start is true the script will attempt to start the service if it is stopped</span>
					<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$start</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						<span style="color: #008000;"># Attempt to start the current service on the current computer</span>
						<span style="color: #800080;">$serviceInstance</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: pink;">-</span>computer <span style="color: #800080;">$computer</span> Win32_Service <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Name='$srv'&quot;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #800080;">$name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$serviceInstance</span>.Name;
						<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Attempting to start $name  on $computer.&quot;</span>
						<span style="color: #800080;">$serviceInstance</span>.StartService<span style="color: #000000;">&#40;</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;"># Refresh the object instance so we get new data</span>
						<span style="color: #800080;">$serviceInstance</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: pink;">-</span>computer <span style="color: #800080;">$computer</span> Win32_Service <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Name='$srv'&quot;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #800080;">$state</span> <span style="color: pink;">=</span> <span style="color: #800080;">$serviceInstance</span>.State;
						<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;$name is &quot;</span><span style="color: #800000;">&quot;$state&quot;</span><span style="color: #800000;">&quot; on  $computer.&quot;</span>;		
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image5.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Monitoring &amp; checking services in Powershell" border="0" alt="image thumb5 Monitoring &amp; starting Services with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/05/image-thumb5.png" width="644" height="379" /></a></p>
<p><map name='google_ad_map_113_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/113?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_113_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=113&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmonitoring-starting-services-with-powershell%2F113' title="Monitoring &amp; starting Services with Powershell" alt=" Monitoring &amp; starting Services with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113">Monitoring &amp; starting Services with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/monitoring-starting-services-with-powershell/113/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Temporary &amp; Custom Database Object Naming</title>
		<link>http://www.youdidwhatwithtsql.com/temporary-custom-database-object-naming/102</link>
		<comments>http://www.youdidwhatwithtsql.com/temporary-custom-database-object-naming/102#comments</comments>
		<pubDate>Tue, 19 May 2009 18:24:11 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[temporary database objects]]></category>
		<category><![CDATA[temporary tables]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/temporary-custom-database-object-naming/102</guid>
		<description><![CDATA[Over the years I’ve experienced various problems with temporary and custom database objects (as in objects created specifically for certain client systems). Development and deployment teams are often distinct and this can create issues. These issues have included; Temporary objects persisting for months or years beyond their initial purpose. Temporary objects spreading from development to [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/temporary-custom-database-object-naming/102">Temporary &amp; Custom Database Object Naming</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Over the years I’ve experienced various problems with temporary and custom database objects (as in objects created specifically for certain client systems). Development and deployment teams are often distinct and this can create issues. These issues have included;</p>
<ul>
<li>Temporary objects persisting for months or years beyond their initial purpose. </li>
<li>Temporary objects spreading from development to live environments. </li>
<li>Client specific objects spreading to other client systems. </li>
</ul>
<p>These issues can be exacerbated as developers leave the organisation and sometimes by reluctance to identify redundant objects. This is especially true in organisations with multiple systems accessing the same data sources. Who knows if that legacy system still running needs that table? Here are some conventions I have developed to try and eliminate these issues with minimal fuss.</p>
<p><strong>Temporary Naming Conventions</strong></p>
<ul>
<li>For objects only required on the created date start table names with ‘temp_’, i.e. </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('p102code48'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10248"><td class="code" id="p102code48"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> temp_customer_import
<span style="color: #808080;">&#40;</span>
	<span style="color: #008080;">/*
	-- Table definition
	*/</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<ul>
<li>For tables required for an indeterminate period of time start with ‘temp_&lt;contact_name&gt;’ where &lt;contact name&gt; is the person to check with as to its status. i.e. </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('p102code49'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10249"><td class="code" id="p102code49"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> temp_rhyscampbell_customer_import
<span style="color: #808080;">&#40;</span>
	<span style="color: #008080;">/*
	-- Table definition
	*/</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>Client Specific Naming Conventions</strong></p>
<p>Often I have seen client specific stored procedures created in development environments which have then been deployed to other client sites.</p>
<ul>
<li>Include the company name in client specific database objects, i.e. </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('p102code50'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10250"><td class="code" id="p102code50"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> usp_ACME_LTD_process_customer
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #008080;">/*
	-- Proc definition
	*/</span>
<span style="color: #0000FF;">END</span></pre></td></tr></table></div>

<p>Adherence to these simple rules should solve many of these issues painlessly. Remember, a clean database, is a happy database!</p>
<p><map name='google_ad_map_102_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/102?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_102_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=102&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ftemporary-custom-database-object-naming%2F102' title="Temporary &amp; Custom Database Object Naming" alt=" Temporary &amp; Custom Database Object Naming" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/temporary-custom-database-object-naming/102">Temporary &amp; Custom Database Object Naming</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/temporary-custom-database-object-naming/102" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/temporary-custom-database-object-naming/102/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

