<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-4845744956082769684</atom:id><lastBuildDate>Fri, 16 Dec 2011 02:01:01 +0000</lastBuildDate><category>Virtual Machine</category><category>Scripting</category><category>Book Review</category><category>PowerShell</category><category>VM</category><category>Performance</category><category>MySQL</category><category>SQL</category><category>Cooking</category><category>Sharepoint</category><category>SQL Server</category><category>VSTS DB Pro</category><category>XML</category><category>Powerpivot</category><category>SSIS</category><category>Service Broker</category><title>Schott SQL</title><description /><link>http://schottsql.blogspot.com/</link><managingEditor>noreply@blogger.com (Peter Schott)</managingEditor><generator>Blogger</generator><openSearch:totalResults>26</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/SchottSql" /><feedburner:info uri="schottsql" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-5208106334651134187</guid><pubDate>Fri, 16 Dec 2011 01:59:00 +0000</pubDate><atom:updated>2011-12-15T20:01:01.297-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">MySQL</category><category domain="http://www.blogger.com/atom/ns#">SSIS</category><title>Tales of a Lazy DBA–MySQL, SSIS, and “0” Dates</title><description>&lt;br /&gt;
We’ve recently been tasked with converting a lot of MySQL Data into our system from a multi-tenant MySQL Database. We don’t have a fixed schedule to actually execute the imports because they’ll be on a “per customer” basis.&amp;nbsp; Well, that sounded like a great task for SSIS.&amp;nbsp; I set up the ODBC driver, connected, set the initial “sql_mode” options for our connections to (‘mssql, allow_invalid_dates’) and started to work.&lt;br /&gt;
First problem we ran into with an ADO.NET connection to MySQL and writing a SELECT * from schema.table was when we hit a MySQL “Date” column containing a value of ‘0000-00-00’.&amp;nbsp; SSIS threw an error, not sure what to do.&amp;nbsp; Thanks to some others who have solved this problem, I realized that within the MySQL Select statement, we could do something like: &lt;br /&gt;
&lt;blockquote&gt;
CASE date_created WHEN '0000-00-00' THEN NULL else date_created END as date_created &lt;/blockquote&gt;
to pass those dates over as NULL. That solves the implicit conversion to datetime (SQL 2005) and avoids the invalid dates.&amp;nbsp; I ran something similar for a “Time” column to:  &lt;br /&gt;
&lt;blockquote&gt;
CAST(Time_Created as char(8) ) as Time_Created&lt;/blockquote&gt;
&amp;nbsp; &lt;br /&gt;
So that solved one particular table export with about 20 or so CAST and CASE statements.&amp;nbsp; Needless to say, I wasn’t looking forward to doing this for another 500 tables with a total of almost 6000 columns.  &lt;br /&gt;
I finally set up a really basic query to generate most of the SELECT statements we would need to pull our MySQL data across without too much pain. Admittedly, a small step and I’d still need to copy/paste when I set up each new ADO.NET source, but it worked reasonably well. I’m adding the code snippet for MySQL here in case anyone else has a similar problem. &lt;br /&gt;
&lt;div id="codeSnippetWrapper" style="background-color: #f4f4f4; border-bottom: silver 1px solid; border-left: silver 1px solid; border-right: silver 1px solid; border-top: silver 1px solid; cursor: text; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; height: 335px; line-height: 12pt; margin: 20px 0px 10px; max-height: 500px; overflow: auto; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; padding-top: 4px; text-align: left; width: 107.43%;"&gt;
&lt;div id="codeSnippet" style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;
&lt;pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;&lt;span style="color: blue;"&gt;select&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;CONCAT(&lt;span style="color: blue;"&gt;CASE&lt;/span&gt; &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; ordinal_position = 1 &lt;span style="color: blue;"&gt;THEN&lt;/span&gt; &lt;span style="color: #006080;"&gt;'SELECT '&lt;/span&gt; &lt;span style="color: blue;"&gt;ELSE&lt;/span&gt; &lt;span style="color: #006080;"&gt;', '&lt;/span&gt; &lt;span style="color: blue;"&gt;END&lt;/span&gt;,&lt;/pre&gt;
&lt;pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt; &lt;span style="color: blue;"&gt;CASE&lt;/span&gt; Data_Type &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; &lt;span style="color: #006080;"&gt;'date'&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt; &lt;span style="color: blue;"&gt;THEN&lt;/span&gt; CONCAT(&lt;span style="color: #006080;"&gt;'CASE '&lt;/span&gt;,column_name,&lt;span style="color: #006080;"&gt;' WHEN '&lt;/span&gt;&lt;span style="color: #006080;"&gt;'0000-00-00'&lt;/span&gt;&lt;span style="color: #006080;"&gt;' THEN NULL else '&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;    ,column_name,&lt;span style="color: #006080;"&gt;' END as '&lt;/span&gt;,column_name)&lt;/pre&gt;
&lt;pre style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt; &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; &lt;span style="color: #006080;"&gt;'time'&lt;/span&gt; &lt;span style="color: blue;"&gt;THEN&lt;/span&gt; CONCAT(&lt;span style="color: #006080;"&gt;'CAST('&lt;/span&gt;,column_name,&lt;span style="color: #006080;"&gt;' AS CHAR(8) ) as '&lt;/span&gt;,column_name)&lt;/pre&gt;
&lt;pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt; &lt;span style="color: blue;"&gt;ELSE&lt;/span&gt; column_name &lt;span style="color: blue;"&gt;END&lt;/span&gt;, &lt;span style="color: blue;"&gt;CASE&lt;/span&gt; &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; Ordinal_Position &amp;lt;&amp;gt; MaxOrd &lt;span style="color: blue;"&gt;THEN&lt;/span&gt; &lt;span style="color: #006080;"&gt;''&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt; &lt;span style="color: blue;"&gt;ELSE&lt;/span&gt; CONCAT(&lt;span style="color: #006080;"&gt;'&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;    FROM MySchema.', c.table_name) &lt;span style="color: blue;"&gt;END&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;    ) &lt;span style="color: blue;"&gt;as&lt;/span&gt; Select_Column_Name&lt;/pre&gt;
&lt;pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;&lt;span style="color: blue;"&gt;from&lt;/span&gt; information_schema.columns &lt;span style="color: blue;"&gt;as&lt;/span&gt; c&lt;/pre&gt;
&lt;pre style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;&lt;span style="color: blue;"&gt;JOIN&lt;/span&gt; (&lt;span style="color: blue;"&gt;select&lt;/span&gt; table_name, &lt;span style="color: blue;"&gt;MAX&lt;/span&gt;(ordinal_position) &lt;span style="color: blue;"&gt;as&lt;/span&gt; MaxOrd&lt;/pre&gt;
&lt;pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;        &lt;span style="color: blue;"&gt;from&lt;/span&gt; information_schema.columns&lt;/pre&gt;
&lt;pre style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;        &lt;span style="color: blue;"&gt;WHERE&lt;/span&gt; Table_Schema = &lt;span style="color: #006080;"&gt;'MySchema'&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;        &lt;span style="color: blue;"&gt;GROUP&lt;/span&gt; &lt;span style="color: blue;"&gt;BY&lt;/span&gt; Table_Name) &lt;span style="color: blue;"&gt;as&lt;/span&gt; t1&lt;/pre&gt;
&lt;pre style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;    &lt;span style="color: blue;"&gt;ON&lt;/span&gt; c.Table_Name = t1.Table_Name&lt;/pre&gt;
&lt;pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;&lt;span style="color: blue;"&gt;where&lt;/span&gt; table_schema = &lt;span style="color: #006080;"&gt;'MySchema'&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"&gt;&lt;span style="color: blue;"&gt;order&lt;/span&gt; &lt;span style="color: blue;"&gt;by&lt;/span&gt; c.table_name, ordinal_position &lt;span style="color: blue;"&gt;LIMIT&lt;/span&gt; 0, 50000;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I’d love to hear other ideas if anyone has encountered this before and come up with a more elegant solution for translating “0” Date or Time data from MySQL into SQL Server.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-5208106334651134187?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/T0NU9mFAfOs" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/T0NU9mFAfOs/tales-of-lazy-dbamysql-ssis-and-0-dates.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2011/12/tales-of-lazy-dbamysql-ssis-and-0-dates.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-3963021819500641405</guid><pubDate>Thu, 15 Sep 2011 21:13:00 +0000</pubDate><atom:updated>2011-09-15T16:13:09.407-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">VM</category><category domain="http://www.blogger.com/atom/ns#">Powerpivot</category><category domain="http://www.blogger.com/atom/ns#">Sharepoint</category><category domain="http://www.blogger.com/atom/ns#">SQL Server</category><category domain="http://www.blogger.com/atom/ns#">Virtual Machine</category><title>Installing SQL Server, Sharepoint, PowerPivot on a single server</title><description>I've been trying to set up a single virtual machine for a proof of concept using SQL Server Denali CTP3, Sharepoint 2010, and PowerPivot. I want to get Project "Crescent" running through this as well to prove out some report concepts. I've had a lot of trouble finding the right steps to get everything installed and working correctly on a single box, especially since my Sharepoint knowledge right now is practically non-existent when it comes to BI. Every time I tried to get the Sharepoint / PowerPivot integration working, I hit a roadblock or managed to corrupt my Sharepoint install.&lt;br /&gt;
&lt;br /&gt;
I had the opportunity to speak with Brian Knight ( &lt;a href="http://www.bidn.com/blogs/brianknight/"&gt;blog&lt;/a&gt; | &lt;a href="http://twitter.com/brianknight"&gt;twitter&lt;/a&gt; ) and he pointed me to &lt;a href="http://powerpivotpro.com/"&gt;PowerPivotPro.com&lt;/a&gt; which, in turn, led me to &lt;a href="http://powerpivotgeek.com/"&gt;PowerPivotGeek.com&lt;/a&gt;. They had a &lt;a href="http://powerpivotgeek.com/server-installation/single-server-install-rtm/"&gt;page of instructions for single-server installs&lt;/a&gt; with a pointer to an MS whitepaper and a private cached file of the whitepaper just in case. This gave me the information needed to get PowerPivot working on my VM.&lt;br /&gt;
&lt;br /&gt;
A couple of notes from my personal experience:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Install Sharepoint SP1 right after installing Sharepoint 2010. SP1 is required when you're using Denali&lt;/li&gt;
&lt;li&gt;Do NOT configure Sharepoint until the directions tell you to do so.&lt;/li&gt;
&lt;li&gt;Use a Named Instance in order to get PowerPivot working.&lt;/li&gt;
&lt;li&gt;If you plan to use Crescent, make sure that you set up SSAS with the "Tabular Data" option (or whatever the final name ends up being).&lt;/li&gt;
&lt;/ul&gt;
Once again, thanks to Brian for pointing me towards the right solution. Thanks to PowerPivotGeek for hosting these files. I imagine the instructions would work for Virtual Server, Virtual PC, VMWare, VirtualBox, or similar Virtual Machine apps. I hope this is helpful to anyone else trying to set up their own VM for testing out the Microsoft BI stack.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-3963021819500641405?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/uoXkQc7FPYk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/uoXkQc7FPYk/installing-sql-server-sharepoint.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2011/09/installing-sql-server-sharepoint.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-7238075277541536976</guid><pubDate>Wed, 29 Jun 2011 18:45:00 +0000</pubDate><atom:updated>2011-06-29T13:57:11.294-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Service Broker</category><category domain="http://www.blogger.com/atom/ns#">SQL Server</category><category domain="http://www.blogger.com/atom/ns#">Performance</category><title>Service Broker–WAITFOR and Activated Queues</title><description>&lt;p&gt;We’ve implemented Service Broker for handling a small portion of the transactions we want to catch and apply into our Operational Data Store. Mostly we wanted to make sure we accounted for actual DELETE operations and handled those records correctly. We can use Change Data Capture at this time on our source systems and Service Broker seemed to fit the bill. We still handle the remaining inserts and updates through SSIS.&lt;/p&gt; &lt;p&gt;While running Service Broker, we noticed a pretty constant CPU hum on the receiving server. That seemed odd, but we had a lot of trouble tracking it down. Regular Profiler traces didn’t show any running TSQL, “normal” Service Broker traces weren’t showing much, either. We just saw SQL Server running at a pretty constant 20% even with nothing seemingly happening.&lt;/p&gt; &lt;p&gt;I want to give a public thank you to Mark Hill (&lt;a href="http://twitter.com/ftdba" target="_blank"&gt;twitter&lt;/a&gt;) for doing a little extra digging and catching the root cause of this. In our tinkering with Service Broker, Queues, Activation, Stored Procs, and such we missed some &lt;a href="http://msdn.microsoft.com/en-us/library/ms166135.aspx" target="_blank"&gt;very important information&lt;/a&gt; along the way.&lt;/p&gt; &lt;p&gt;We had written a stored procedure that would be used on the Receiving side of our Service Broker queue to run as the “Activated” proc. Inside the stored procedure, we included a WHILE loop to process anything that was in the queue. If nothing, exit.&amp;nbsp; That seemed pretty simple – if something comes into the queue, process it and stop when nothing is left.&amp;nbsp; End of story, right?&lt;/p&gt; &lt;p&gt;Sadly, this is where we missed a small, but important, fact about Service Broker. If you have an activated stored procedure that doesn’t use the WAITFOR command, Service Broker, we execute that stored proc as many times as possible looking for something to process. We looked at it, turned off the Activation on the queue and saw the CPU drop to nothing. We re-activated the queue and saw the CPU shoot up again. We tweaked the stored procedure after that and added a WAITFOR command with a timeout of 60000. After doing this, we saw the stored procedure run to process everything in the queue, then go idle, which was exactly the intended behavior in the first place.&lt;/p&gt; &lt;p&gt;I’m not going to pretend to be an expert on Service Broker. We had tried to code with the intention of being able to use this stored procedure as an Activated stored procedure or as one called from an external process to work through the queue. While that may be possible, it was unnecessary in our actual usage. Adding a WAITFOR command around our queue processing eliminated our extra, non-essential CPU usage and stopped trying to execute a stored procedure for no reason.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Pro SQL Server 2008 Service Broker ( &lt;a href="http://www.amazon.com/Server-Service-Broker-Books-Professionals/dp/1590599993" target="_blank"&gt;Amazon&lt;/a&gt; | &lt;a href="http://www.apress.com/9781590599990" target="_blank"&gt;Apress&lt;/a&gt; )&lt;/p&gt; &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms187331.aspx" target="_blank"&gt;WAITFOR&lt;/a&gt; (Also see &lt;a href="http://msdn.microsoft.com/en-us/library/ms166135.aspx" target="_blank"&gt;this performance article&lt;/a&gt; – it’s the little details that get you)&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-7238075277541536976?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/AqE1iXZMoVg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/AqE1iXZMoVg/service-brokerwaitfor-and-activated.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2011/06/service-brokerwaitfor-and-activated.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-1027485506554587657</guid><pubDate>Thu, 17 Feb 2011 22:15:00 +0000</pubDate><atom:updated>2011-02-17T16:15:36.801-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server</category><title>Quickly script permissions in SQL 2005+</title><description>&lt;p&gt;I wanted an easy way to generate a script to recreate all DB object and schema permissions for a database. Searching online, I found several different examples, but none quite did everything I needed. I put this together from some examples and added it to my toolkit. This script will generate the appropriate T-SQL to grant permissions to DB Objects and Schemas.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;div style="padding-bottom: 0px; padding-left: 0px; width: 432px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:f928f839-ef42-42b3-b940-deb82dbc322b" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 432px; height: 565px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;span style="color: #0000FF;"&gt;SELECT&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;state_desc &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; permission_name &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; on [&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; ss.name &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;].[&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; so.name &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;]&lt;br /&gt;to [&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdpr.name &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;]&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;COLLATE LATIN1_General_CI_AS &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;as&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;[&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;Permissions T-SQL&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;]&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;FROM&lt;/span&gt;&lt;span style="color: #000000;"&gt; SYS.DATABASE_PERMISSIONS &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdp&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;JOIN&lt;/span&gt;&lt;span style="color: #000000;"&gt; sys.objects &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; so&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;ON&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdp.major_id &lt;/span&gt;&lt;span style="color: #808080;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; so.&lt;/span&gt;&lt;span style="color: #FF00FF;"&gt;OBJECT_ID&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;JOIN&lt;/span&gt;&lt;span style="color: #000000;"&gt; SYS.SCHEMAS &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; ss&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;ON&lt;/span&gt;&lt;span style="color: #000000;"&gt; so.SCHEMA_ID &lt;/span&gt;&lt;span style="color: #808080;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ss.SCHEMA_ID&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;JOIN&lt;/span&gt;&lt;span style="color: #000000;"&gt; SYS.DATABASE_PRINCIPALS &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdpr&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;ON&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdp.grantee_principal_id &lt;/span&gt;&lt;span style="color: #808080;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdpr.principal_id&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;UNION&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;SELECT&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;state_desc &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; permission_name &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; on Schema::[&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; ss.name &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;]&lt;br /&gt;to [&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdpr.name &lt;/span&gt;&lt;span style="color: #808080;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;]&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;COLLATE LATIN1_General_CI_AS &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;as&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;[&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;Permissions T-SQL&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;]&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;FROM&lt;/span&gt;&lt;span style="color: #000000;"&gt; SYS.DATABASE_PERMISSIONS &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdp&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;JOIN&lt;/span&gt;&lt;span style="color: #000000;"&gt; SYS.SCHEMAS &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; ss&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;ON&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdp.major_id &lt;/span&gt;&lt;span style="color: #808080;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ss.SCHEMA_ID&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: #808080;"&gt;AND&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdp.class_desc &lt;/span&gt;&lt;span style="color: #808080;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;Schema&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;JOIN&lt;/span&gt;&lt;span style="color: #000000;"&gt; SYS.DATABASE_PRINCIPALS &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;AS&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdpr&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;ON&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdp.grantee_principal_id &lt;/span&gt;&lt;span style="color: #808080;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; sdpr.principal_id&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;order&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;by&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;[&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;Permissions T-SQL&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;]&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;GO&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;I know that a lot of you may have something that does this already, but figured it can’t hurt to have another snippet available to generate permission statements. If you make any enhancements, let me know. I’d love to keep this up to date so it’s helpful to others.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-1027485506554587657?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/MpSV14cTl9w" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/MpSV14cTl9w/quickly-script-permissions-in-sql-2005.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2011/02/quickly-script-permissions-in-sql-2005.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-7341104230998013980</guid><pubDate>Mon, 07 Feb 2011 22:27:00 +0000</pubDate><atom:updated>2011-02-07T16:27:52.444-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SSIS</category><category domain="http://www.blogger.com/atom/ns#">SQL Server</category><title>SQL 2008 R2 – SSIS Data Export Oddities</title><description>&lt;p&gt;I don’t know if anyone else has had these sorts of issues, but we work quite a bit with conversions of various source data into our SQL Server database. I recently was handed a backup from SQL 2008 R2. I figured that wasn’t a huge problem. Restore onto my local test box, use SSIS to push over to a SQL 2005 instance so it can work its way to Production. No big deal, right?&lt;/p&gt; &lt;p&gt;The first time I tried this using the SQL Native Client 10.0 on both sides, I ran into an errors with the mappings. I can’t quite figure that one out because from what I can see, there’s nothing at all in the source database using a feature that would not be available in SQL 2005.&lt;/p&gt; &lt;p&gt;I tried exporting to an MS Access MDB file. No luck from SQL 2008 R2 with the default settings because of an invalid size error on a varchar to longtext conversion.&lt;/p&gt; &lt;p&gt;I was able to successfully export using SNAC from SQL 2008 R2 to a SQL 2008 instance. So from &lt;em&gt;there&lt;/em&gt; I thought I could upload directly to the SQL 2005 instance. No such luck using SNAC – again. I &lt;em&gt;was&lt;/em&gt; able to export from there to an Access MDB file and pull that into SQL 2005.&amp;nbsp; I don’t quite get why that worked, but figure I’ve got some odd mapping in the XML files defining the defaults that I’m missing.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;I was recently asked to repeat this task and figured there had to be a better way. This time I restored the DB to my R2 instance – no issues. I then used the SNAC client to access my R2 instance, but set up an OLEDB connection to my SQL Server 2005 target. For some reason, the mappings are just different enough that this worked with no issues. I was able to transfer directly. I now wish I’d tried that the first time, but I’d already blown a couple of hours on it.&lt;/p&gt; &lt;p&gt;If anyone else has encountered that and knows why SNAC from 2008 R2 doesn’t seem to work directly to SNAC on 2005, I’d love to know the reasons as well. If not and you encounter something similar, maybe trying the OLEDB connections will work for you.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-7341104230998013980?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/k8c_UtuI1GA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/k8c_UtuI1GA/sql-2008-r2-ssis-data-export-oddities.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2011/02/sql-2008-r2-ssis-data-export-oddities.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-6965670211952326751</guid><pubDate>Mon, 17 Jan 2011 18:24:00 +0000</pubDate><atom:updated>2011-01-17T12:24:35.923-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><title>SQL 2008 Merge and PK/FK Constraints</title><description>We ran into this issue a while back.&amp;nbsp; SQL 2008 has a “feature” that will not allow inserts into a table on either side of a Foreign Key relationship.&amp;nbsp; If you attempt to insert into a table involved in the FK relationship, you get an error something like:&lt;br /&gt;
&lt;blockquote&gt;&lt;span style="color: red;"&gt;The target table 'TableName' of the INSERT statement cannot be on either side of a (primary key, foreign key) relationship when the FROM clause contains a nested INSERT, UPDATE, DELETE, or MERGE statement. Found reference constraint 'ConstraintName'.&lt;/span&gt;&lt;/blockquote&gt;&lt;span style="color: black;"&gt;This is documented in &lt;a href="https://connect.microsoft.com/SQLServer/feedback/details/435031" target="_blank"&gt;Connect 435031&lt;/a&gt;. There is one workaround documented in the ticket – drop the FK constraint, run the MERGE query, then re-create the constraint. Happily, this bug is still open as the requirement to drop a constraint just to run a MERGE statement kind of defeats the purpose of putting a constraint on the table in the first place. While dropping the constraints will work, there is another possible workaround. &lt;span style="color: black;"&gt;I was able to run this without any issues while leaving my FK Constraint in place.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;&lt;span style="color: black;"&gt;Create a temp table that matches the definition of the table into which you want to perform your insert.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="color: black;"&gt;Instead of inserting into your base table like you normally would in a merge, write your code to INSERT #Tablename at the beginning of the MERGE process. You’ll still use your main table and staging table inside the MERGE INTO … USING query.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="color: black;"&gt;After the Merge, insert into the main table using the values in your Temp table.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;span style="color: black;"&gt;Here’s a shell of an example. Line 11 below contains the main difference to the MERGE query from a “normal” MERGE.&lt;/span&gt;&lt;br /&gt;
&lt;div id="codeSnippetWrapper"&gt;&lt;div id="codeSnippet" style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum1" style="color: #606060;"&gt;   1:&lt;/span&gt; &lt;span style="color: green;"&gt;--1: Create Temp table&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum2" style="color: #606060;"&gt;   2:&lt;/span&gt; &lt;span style="color: blue;"&gt;CREATE&lt;/span&gt; &lt;span style="color: blue;"&gt;TABLE&lt;/span&gt; #MyFactTable(&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum3" style="color: #606060;"&gt;   3:&lt;/span&gt;     ID &lt;span style="color: blue;"&gt;INT&lt;/span&gt; &lt;span style="color: blue;"&gt;NULL&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum4" style="color: #606060;"&gt;   4:&lt;/span&gt;     , CustomerName &lt;span style="color: blue;"&gt;VARCHAR&lt;/span&gt;(100) &lt;span style="color: blue;"&gt;NULL&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum5" style="color: #606060;"&gt;   5:&lt;/span&gt;     , SourceID &lt;span style="color: blue;"&gt;INT&lt;/span&gt; &lt;span style="color: blue;"&gt;NULL&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum6" style="color: #606060;"&gt;   6:&lt;/span&gt;     , OutputAction &lt;span style="color: blue;"&gt;VARCHAR&lt;/span&gt;(100) &lt;span style="color: blue;"&gt;NULL&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum7" style="color: #606060;"&gt;   7:&lt;/span&gt; );&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum8" style="color: #606060;"&gt;   8:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum9" style="color: #606060;"&gt;   9:&lt;/span&gt; &lt;span style="color: green;"&gt;--2: INSERT into the temp table instead of your normal target table&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum10" style="color: #606060;"&gt;  10:&lt;/span&gt; &lt;span style="color: green;"&gt;--   Merge query will be the same otherwise&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum11" style="color: #606060;"&gt;  11:&lt;/span&gt; &lt;b&gt;INSERT &lt;span style="color: blue;"&gt;INTO&lt;/span&gt; #MyFactTable (ID, CustomerName, SourceID, OutputAction)&lt;/b&gt;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum12" style="color: #606060;"&gt;  12:&lt;/span&gt; &lt;span style="color: blue;"&gt;SELECT&lt;/span&gt; so.ID, so.CustomerName, so.SourceID, so.output_action&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum13" style="color: #606060;"&gt;  13:&lt;/span&gt; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; (&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum14" style="color: #606060;"&gt;  14:&lt;/span&gt;     MERGE &lt;span style="color: blue;"&gt;INTO&lt;/span&gt; dbo.MyFactTable &lt;span style="color: blue;"&gt;AS&lt;/span&gt; t&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum15" style="color: #606060;"&gt;  15:&lt;/span&gt;     &lt;span style="color: blue;"&gt;USING&lt;/span&gt; Staging.MyFactTable &lt;span style="color: blue;"&gt;AS&lt;/span&gt; s&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum16" style="color: #606060;"&gt;  16:&lt;/span&gt;     &lt;span style="color: blue;"&gt;ON&lt;/span&gt; ( s.ID = t.ID&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum17" style="color: #606060;"&gt;  17:&lt;/span&gt;         &lt;span style="color: blue;"&gt;AND&lt;/span&gt; s.NewLoad = 0 )&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum18" style="color: #606060;"&gt;  18:&lt;/span&gt;         &lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum19" style="color: #606060;"&gt;  19:&lt;/span&gt;     &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; MATCHED &lt;span style="color: blue;"&gt;AND&lt;/span&gt; ( s.SourceID &amp;lt;&amp;gt; t.SourceID )&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum20" style="color: #606060;"&gt;  20:&lt;/span&gt;             &lt;span style="color: blue;"&gt;AND&lt;/span&gt; s.NewLoad = 0            &lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum21" style="color: #606060;"&gt;  21:&lt;/span&gt;     &lt;span style="color: blue;"&gt;THEN&lt;/span&gt; &lt;span style="color: blue;"&gt;UPDATE&lt;/span&gt; &lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum22" style="color: #606060;"&gt;  22:&lt;/span&gt;         &lt;span style="color: blue;"&gt;SET&lt;/span&gt; RecordState = 0&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum23" style="color: #606060;"&gt;  23:&lt;/span&gt;         , UpdatedDate = getdate()&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum24" style="color: #606060;"&gt;  24:&lt;/span&gt;         &lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum25" style="color: #606060;"&gt;  25:&lt;/span&gt;     &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; &lt;span style="color: blue;"&gt;NOT&lt;/span&gt; MATCHED &lt;span style="color: blue;"&gt;BY&lt;/span&gt; TARGET &lt;span style="color: blue;"&gt;AND&lt;/span&gt; s.NewLoad = 0 &lt;span style="color: blue;"&gt;THEN&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum26" style="color: #606060;"&gt;  26:&lt;/span&gt;         INSERT (ID, CustomerName, SourceID)&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum27" style="color: #606060;"&gt;  27:&lt;/span&gt;         &lt;span style="color: blue;"&gt;VALUES&lt;/span&gt; (s.ID, s.CustomerName, s.SourceID)&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum28" style="color: #606060;"&gt;  28:&lt;/span&gt;         &lt;span style="color: blue;"&gt;OUTPUT&lt;/span&gt; $&lt;span style="color: blue;"&gt;action&lt;/span&gt; &lt;span style="color: blue;"&gt;AS&lt;/span&gt; OutputAction&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum29" style="color: #606060;"&gt;  29:&lt;/span&gt;             , ID&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum30" style="color: #606060;"&gt;  30:&lt;/span&gt;             , CustomerName&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum31" style="color: #606060;"&gt;  31:&lt;/span&gt;             , SourceID&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum32" style="color: #606060;"&gt;  32:&lt;/span&gt;         ) &lt;span style="color: blue;"&gt;AS&lt;/span&gt; so (OutputAction, ID, CustomerName, SourceID)&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum33" style="color: #606060;"&gt;  33:&lt;/span&gt;         &lt;span style="color: blue;"&gt;WHERE&lt;/span&gt; OutputAction = &lt;span style="color: #006080;"&gt;'UPDATE'&lt;/span&gt;  ;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum34" style="color: #606060;"&gt;  34:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum35" style="color: #606060;"&gt;  35:&lt;/span&gt;&lt;span style="color: green;"&gt; --3: Perform the final insert into your target table&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum36" style="color: #606060;"&gt;  36:&lt;/span&gt; INSERT &lt;span style="color: blue;"&gt;INTO&lt;/span&gt; MyFactTable (ID, CustomerName, SourceID)&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum37" style="color: #606060;"&gt;  37:&lt;/span&gt; &lt;span style="color: blue;"&gt;SELECT&lt;/span&gt; &lt;span style="color: blue;"&gt;DISTINCT&lt;/span&gt; ID, CustomerName, SourceID&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum38" style="color: #606060;"&gt;  38:&lt;/span&gt; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; #MyFactTable ;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum39" style="color: #606060;"&gt;  39:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum40" style="color: #606060;"&gt;  40:&lt;/span&gt; &lt;span style="color: green;"&gt;--4: Clean up your temp objects.&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 12pt; margin: 0em; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;span id="lnum41" style="color: #606060;"&gt;  41:&lt;/span&gt; &lt;span style="color: blue;"&gt;DROP&lt;/span&gt; &lt;span style="color: blue;"&gt;TABLE&lt;/span&gt; #MyFactTable ;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
I’d love to hear if anyone else has a workaround for this that doesn’t involve dropping and recreating the FK constraint. If this is an issue for you, please vote for the &lt;a href="https://connect.microsoft.com/SQLServer/feedback/details/435031" target="_blank"&gt;Connect Ticket&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-6965670211952326751?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/ZqnmMJOzv4U" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/ZqnmMJOzv4U/sql-2008-merge-and-pkfk-constraints.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>2</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2011/01/sql-2008-merge-and-pkfk-constraints.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-8758965769141209605</guid><pubDate>Tue, 14 Dec 2010 04:00:00 +0000</pubDate><atom:updated>2010-12-13T22:15:34.956-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Book Review</category><title>Book Review: Windows Phone 7 Plain &amp; Simple</title><description>&lt;p&gt;&lt;a href="http://oreilly.com/catalog/9780735643420/" target="_blank"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="cat[1]" border="0" alt="cat[1]" align="left" src="http://lh4.ggpht.com/_SVlKer2O_7A/TQbvQtgnM5I/AAAAAAAAGUg/2QmBVkkZ63k/cat%5B1%5D%5B12%5D.gif?imgmax=800" width="142" height="125"&gt;&lt;/a&gt; Overall, &lt;a href="http://oreilly.com/catalog/9780735643420/" target="_blank"&gt;Windows Phone 7 Plain &amp;amp; Simple&lt;/a&gt; is a good book for people new to Windows Phone 7 and especially for people new to Smart Phones. Michael does a great job illustrating how to use the phone and get up to speed quickly. He walks quickly through the general operation, setting up the phone, and entering text in the first couple chapters. The next several chapters discuss using the most popular features of WP7: calling, e-mail, text messages, calendar, browsing the web, and using the maps/navigation. Next we learn more about music, video, taking pictures and videos, and using the Marketplace. We end with a quick session on using WP7's Office Hub for documents, spreadsheets, and OneNote. This is followed by a discussion on using the Zune software to synchronize files between your phone and your PC. (Michael doesn't mention the Mac software to do this. It's capable, but more limited.)  &lt;p&gt;Having followed the progress of WP7, I found that I knew a lot of the information in this book. There were still a couple interesting bits of information that were news to me, but not many. Michael didn’t write this for people like me who have followed WP7 for some time. He wrote for people who aren’t really familiar with smartphones and especially with the changes that WP7 brings. For those people, this will be a useful read and a quick reference while they get used to their device. I plan to show this to my family members who have WP7 devices and aren't quite sure what to do with them. &lt;p&gt;&lt;strong&gt;The positives:&lt;/strong&gt; &lt;ul&gt; &lt;li&gt;The information is very well presented, easy to follow, and broken out into logical sections.&lt;/li&gt; &lt;li&gt;Michael's casual writing is well-suited for this book.&lt;/li&gt; &lt;li&gt;Readers will not feel intimidated by their new phone and are encouraged to try things out.&lt;/li&gt; &lt;li&gt;Michael presents some information that you may not easily find by reading articles online. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;The negatives:&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;I don't see this as a book that will be a long-lasting reference book. Once the reader has used the phone for a month or so, the lessons from this book should be second-nature.&lt;/li&gt; &lt;li&gt;WP7 already has two planned updates at the time of this review. A minor update in January 2011 that will reportedly enable Copy/Paste functionality and a larger one in February 2011. The errata for this book will need to be updated to take these changes into account as some information may be outdated. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Conclusion:&lt;br&gt;&lt;/strong&gt;If you can find Windows Phone 7 Plain &amp;amp; Simple for a reasonable price and are new to the platform, pick it up. It's a short, but informative, read. If you've followed WP7 for a while, this is not likely the book for you as you'll know most of the information here already. You may still want to pick up a copy to share with family or friends who are new to the phone. It could save you "support" calls.&lt;/p&gt; &lt;p&gt;Please visit the &lt;a href="http://oreilly.com/catalog/9780735643420/" target="_blank"&gt;Windows Phone 7 Plain &amp;amp; Simple page&lt;/a&gt; at O’Reilly for more information about this book. &lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; I received a free copy of this book in electronic format in return for providing an honest review. I was not compensated in any other way.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-8758965769141209605?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/A-nnkFs-Aa8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/A-nnkFs-Aa8/book-review-windows-phone-7-plain.html</link><author>noreply@blogger.com (Peter Schott)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_SVlKer2O_7A/TQbvQtgnM5I/AAAAAAAAGUg/2QmBVkkZ63k/s72-c/cat%5B1%5D%5B12%5D.gif?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/12/book-review-windows-phone-7-plain.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-3606238101023404755</guid><pubDate>Tue, 30 Nov 2010 06:00:00 +0000</pubDate><atom:updated>2010-11-30T00:18:52.422-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Book Review</category><category domain="http://www.blogger.com/atom/ns#">Cooking</category><title>Book Review: Cooking For Geeks</title><description>&lt;p&gt;&lt;em&gt;(I know this isn’t SQL-related, but I think this may be a great read for people with similar interests.)&lt;/em&gt;&lt;/p&gt; &lt;p&gt;This is &lt;em&gt;not&lt;/em&gt; a cookbook. If you’re looking for a new collection of recipes, this is not the book for you. If you like shows like &lt;em&gt;Good Eats&lt;/em&gt; or enjoy seeing how food is prepared and served, you’ll almost certainly love &lt;em&gt;Cooking for Geeks&lt;/em&gt;. If you like experimenting in the kitchen and knowing why food turns out the way it does, pick up this book!&lt;/p&gt; &lt;p&gt;As a geek, I loved Jeff’s analogy: &lt;em&gt;Recipes are code&lt;/em&gt;. Follow a recipe as written and you generally get good results. Forget the where clause and you could have unrecoverable errors. Introduce your own changes and you could get something great or you could get something horrible that requires a lot of cleanup. Recipes may have bugs or need corrections. Perhaps there’s more than one way to the same result. Oh, and don’t forget to comment your recipe. Otherwise you might not be able to recreate something fantastic.&lt;/p&gt; &lt;p&gt;Each chapter of &lt;em&gt;Cooking for Geeks&lt;/em&gt; deals with different concepts, each with their own scientific background. Common utensils, ingredients, time/temperature, baking, additives (chemicals), and even some geeky fun with hardware or unusual cooking techniques – all are included in a way that not only gives some neat recipes, but the science behind the recipes.&lt;/p&gt; &lt;p&gt;To me, the most interesting parts were on baking and the chemical reactions that take place as heat is applied. It was great reading exactly &lt;em&gt;why&lt;/em&gt; food turns out with all of its various nuances. That science got me thinking about ways to tweak the outcome of various recipes I follow and was just fun to read.&lt;/p&gt; &lt;p&gt;You can get your own copy of &lt;em&gt;Cooking for Geeks&lt;/em&gt; &lt;a href="http://oreilly.com/catalog/9780596805883/" target="_blank"&gt;direct from O’Reilly&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;Disclaimer: I received an electronic review copy of this book, though I’d likely have wanted to read and review this anyway.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-3606238101023404755?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/_MNFbGGfcxg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/_MNFbGGfcxg/book-review-cooking-for-geeks.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/11/book-review-cooking-for-geeks.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-6262145336945605366</guid><pubDate>Thu, 28 Oct 2010 16:00:00 +0000</pubDate><atom:updated>2010-10-28T11:00:09.314-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SSIS</category><title>SSIS, OLEDB Data Sources, and Table Variables</title><description>&lt;p&gt;I ran across an interesting problem when trying to tune some SSIS data source performance issues the other day. I had noticed that we were doing a pretty large select out of a table filtered on a handful of date-related columns. We then pulled in most of the data from the table. Well, SQL Server saw this query and apparently figured that since we needed most of the columns, a table scan would be the most efficient way to get that data.&amp;nbsp; Since in real life, we only need a very small percentage of those rows, I decided to rewrite the select to pull in the PK values first, then pull in the data based on that subset.&lt;/p&gt; &lt;p&gt;My first attempt was to use a Temp Table. Sadly, using a temp table does not work in SSIS Data Sources, even if you put that inside a stored procedure. Disappointed, but not deterred, I decided to use a Table Variable. I quickly made the changes and tested them with some values I knew would returned quite a few results. Great – no worries.&amp;nbsp; I was still using a stored procedure at the time and used some of our standard code to start it off so had no issues.&lt;/p&gt; &lt;p&gt;Ultimately, I didn’t want to write a bunch of stored procedures that I would need to maintain outside of SSIS just for this particular load.&amp;nbsp; We had the code already inside of the OLE DB Sources, just not optimized as much as I’d like. I started pulling out the old queries, adding a table variable, populating it with the necessary PK values, the using that to filter out the main data set.&amp;nbsp; Save, deploy, run, see major speed increase.&lt;/p&gt; &lt;p&gt;Then, I looked at the actual insert/update counts. Yes, it ran much faster, but in the process, it didn’t pick up any results.&amp;nbsp; I was pretty confused because the step was successful, the code ran as expected through SSMS, there were no errors or warnings. I re-verified that running that same code got results in SSMS; it did.&amp;nbsp; I started doing some searching to see what might be happening and found &lt;a href="http://www.bidn.com/blogs/DustinRyan/ssis/835/ssis-ole-db-source-using-table-variables-returns-no-rows" target="_blank"&gt;this post by Dustin Ryan&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;In short, it seems that if you have row counts being returned (default behavior), you return a number of rows affected when you insert the table variable, which results in a “Done in Proc” type message, which in turn tells SSIS that the data flow is done and moves on to the next step.&amp;nbsp; The workaround?&amp;nbsp; Add &lt;strong&gt;SET NOCOUNT ON;&lt;/strong&gt; to the beginning of your OLEDB Source query (or make sure this is in your stored proc – we tend to put that in our procs by default).&lt;/p&gt; &lt;p&gt;Once again, thanks to Dustin Ryan for his post on this topic. There’s a good possibility I’d still be struggling with the root cause of this without that assistance.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-6262145336945605366?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/ROKXI24HhJk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/ROKXI24HhJk/ssis-oledb-data-sources-and-table.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>5</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/10/ssis-oledb-data-sources-and-table.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-2446825507205673958</guid><pubDate>Sat, 24 Jul 2010 05:00:00 +0000</pubDate><atom:updated>2010-07-24T00:00:02.849-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Scripting</category><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><title>Review: Windows PowerShell 2.0 Best Practices - Introduction</title><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;a href="http://oreilly.com/catalog/9780735626461" target="_blank"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="PowershellBestPracticesCover" border="0" alt="PowershellBestPracticesCover" align="left" src="http://lh4.ggpht.com/_SVlKer2O_7A/TEkU27NDPPI/AAAAAAAAGSc/VBuwy2pQRbk/PowershellBestPracticesCover%5B5%5D.jpg?imgmax=800" width="162" height="195"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;A while back I received a review copy of &lt;a href="http://oreilly.com/catalog/9780735626461" target="_blank"&gt;Windows PowerShell 2.0 Best Practices&lt;/a&gt; by Ed Wilson (blog | twitter) and the Windows PowerShell Teams at Microsoft. Having finally found the time to sit down and really digest the information within, I thought I’d do a multi-part review of the book.&amp;nbsp; Ed Wilson has compiled an excellent resource not only to show off what PowerShell 2.0 can do, but also to provide practical examples and several useful tips for best practices not only in using PowerShell, but in writing reusable and efficient code.&lt;/p&gt; &lt;p&gt;This book is broken down into 5 major sections:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;strong&gt;Introduction&lt;/strong&gt;  &lt;li&gt;Planning  &lt;li&gt;Designing  &lt;li&gt;Testing and Deploying  &lt;li&gt;Optimizing&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I want to give a quick overview of what’s contained in each chapter, though I’m not going to include the code from the book. You may be able to get it from the main page for the book, but ideally I think this book is well worth buying. You get a PDF file of the book and all of the code included in the book as well. If you plan to do a lot of PowerShell scripting, this book is well worth the money.&amp;nbsp; I will say that this book is not designed to &lt;em&gt;teach&lt;/em&gt; PowerShell.&amp;nbsp; Ed Wilson really does expect that readers have a working knowledge of PowerShell. He builds on that knowledge to show how to write PowerShell scripts with these best practices in mind.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Chapter 1&lt;/strong&gt; contains an overview of PowerShell 2.0. Ed explains some of the differences between PowerShell 1.0 and 2.0, focusing on the new remoting capabilities, WMI enhancements, changes to the way some cmdlets work, and why they have changed the original behavior. He touches on some of the differences between VBScript and PowerShell and why PowerShell might be a better choice than VBScript, despite people’s familiarity with the latter. One of the key advantages of PowerShell over VBScript is that PowerShell was designed to be run from a prompt or as a script. VBScript must be run through something that calls the script.&amp;nbsp; This command-line interaction allows people to work with the script in a much more interactive way and even use native Windows commands with which people are already familiar.&amp;nbsp; Ed closes the chapter by reminding readers of the minimum requirements for PowerShell 2.0, informing readers from where they may &lt;a href="http://support.microsoft.com/kb/968929" target="_blank"&gt;obtain the latest version of PowerShell&lt;/a&gt; and suggesting where PowerShell 2.0 should be installed (pretty much everywhere).&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Chapter 2&lt;/strong&gt; is where readers become more familiar with PowerShell 2.0 capabilities. We’ve already read about the promise of PowerShell 2.0, but now we can start to see it in action. Ed starts by introducing readers to the PowerShell 2.0 interactive command line. The first command shows users running processes inside the PowerShell environment.&amp;nbsp; Ed then shows some of the easiest cmdlets to run and remember, followed by the “most important” cmdlets: &lt;em&gt;&lt;strong&gt;Get-Help&lt;/strong&gt;&lt;/em&gt;, &lt;strong&gt;&lt;em&gt;Get-Command&lt;/em&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;em&gt;Get-Member&lt;/em&gt;&lt;/strong&gt;.&amp;nbsp; I agree that these three cmdlets are key. You can always find out more about what commands are available, how to run them, and what properties are available.&amp;nbsp; Ed then lists several useful cmdlets to enhance filtering, grouping, sorting, and such.&amp;nbsp; Chapter 2 ends with a good introduction to WMI, Remoting, Services, and the Event Log from a PowerShell perspective.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Chapter 3&lt;/strong&gt; concentrates on Active Directory. Ed reminds us that what we typically call “AD” is really a legacy term now and is actually “Active Directory Service Interfaces” or ADSI. We tend to think of AD in terms of users and domains, but it contains much more now than it used to. Ed jumps right in by showing readers how to reference ADSI Users, Groups, and Computers with some quick examples of how to reference each within PowerShell.&amp;nbsp; There’s then a quick “Inside Track” by James Turner showing how PowerShell can be used to automate some common administrative tasks around user accounts. James tells us to use the right tool for the job as well. While the LDAP functionality allows more authentication methods, the WinNT functionality allows more object types and more familiar syntax.&amp;nbsp; The bulk of Chapter 3 really concentrates on building a functional script to create and update Users within ADSI. The reader is guided from the more simple scripts to query data through reading a file to automate the creation and updating of user accounts. We also get to see several ways to accomplish this task to introduce the reader to reading from a connection to Excel.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Chapter 4&lt;/strong&gt; builds on what we’ve already learned from Chapter 3 to work within ADSI.&amp;nbsp; Ed starts by helping us become familiar with the AD schema through PowerShell. As always, Ed actually puts to use the “Best Practices” that he wants his readers to learn. His first script includes a full Help example and several functions, followed by a breakdown of each function. I found it helpful to look through the original script while Ed explains the details so I could keep track of how each function fit into the bigger picture.&lt;/p&gt; &lt;p&gt;Ed then demonstrates how to query AD for information, going beyond merely listing the results – this &lt;em&gt;is&lt;/em&gt; PowerShell after all and the results can be full objects. He gives a pretty good list of ADSI-related properties, shows how to display results that list results, then moves on to true queries – ADO and OLEDB queries. Of course, his overall goal is to really use PowerShell effectively. ADO and OLEDB are useful, but we also want to be efficient. Therefore, the reader is introduced to the DirectorySearcher class and PowerShell 2.0’s [ADSISearcher] to simplify calling DirectorySearcher. Along the way, we see the “FindOne()” method to limit the results to a single row. That is useful to see what data we can expect.&amp;nbsp; Some familiarity with ADSI can really help when going through these chapters.&lt;/p&gt; &lt;p&gt;We’re led from these queries to actually managing user accounts. First, the reader needs to understand ADSI User Account Control values. He then shows using the Bitwise AND operation to compare the various bits set and determine properties of the various user accounts. Ed shows how to find and display disabled user accounts in a short script using bitwise AND and color coding. Readers are then shown how to move objects within AD without too much code. The final example details how to find missing values within AD. These are the values that are expected, but not always completed.&amp;nbsp; Once again, I found it very useful to see the whole script while Ed explains each step of the script.&amp;nbsp; Be sure to include the backtick if you want to type these scripts yourself and try them.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The Introductory section of &lt;a href="http://oreilly.com/catalog/9780735626461" target="_blank"&gt;Windows PowerShell 2.0 Best Practices&lt;/a&gt; is well-written and serves its purpose of getting the readers ready to move through the rest of the book. If I have any complaints about this section it’s that it uses ADSI pretty heavily. For people who don’t work directly with ADSI on a regular basis, a lot of the examples are harder to follow. While still possible to see the best practices for PowerShell, it’s easy to get lost in trying to figure out what Ed is doing with ADSI.&amp;nbsp; The &lt;strong&gt;Planning&lt;/strong&gt; section in chapters 5-8 seem to be less focused on a specific set of functionality and much more on how to plan and use PowerShell effectively.&amp;nbsp; I plan to post a review of those chapters in the near future.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;I’d appreciate feedback on this review. Too lengthy? Too short? Let me know what’s good and what’s not good. I’d like to write a useful and honest review for those considering this book.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-2446825507205673958?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/DmEScYUOz_0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/DmEScYUOz_0/review-windows-powershell-20-best.html</link><author>noreply@blogger.com (Peter Schott)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_SVlKer2O_7A/TEkU27NDPPI/AAAAAAAAGSc/VBuwy2pQRbk/s72-c/PowershellBestPracticesCover%5B5%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/07/review-windows-powershell-20-best.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-8356501820568014222</guid><pubDate>Tue, 13 Apr 2010 17:52:00 +0000</pubDate><atom:updated>2010-04-13T12:52:51.795-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><category domain="http://www.blogger.com/atom/ns#">SQL Server</category><title>Powershell for Database Developers</title><description>I recently presented for the SQLPASS AppDev Virtual Chapter on "Powershell for Database Developers".&amp;nbsp; I promised that I'd get the files used in the demos up to my blog shortly so wanted to make good on that promise. First, some of these depend on having either SQLPSX or the SQL 2008 Snap-in loaded in your Powershell environment. Some of those demos won't work unless that's set up. I think I've got some posts on that already, but if you need help with it, leave me a comment or shoot me a message.&lt;br /&gt;
&lt;br /&gt;
I know that the SQL ISE portions of my demo require &lt;a href="http://sqlpsx.codeplex.com/"&gt;SQLPSX&lt;/a&gt;. Once again, I highly recommend that you download and install these modules for your use. You don't necessarily need them in your startup profile, but they are useful if you work with SQL Server.&lt;br /&gt;
&lt;br /&gt;
I'd also mentioned Red-Gate in my presentation. Here's one link for more details on &lt;a href="http://www.simple-talk.com/sql/sql-tools/doing-schema-comparisons-with-production-servers/"&gt;Doing Schema Compares&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
You can access all of my files used for this presentation on Skydrive.&lt;br /&gt;
&lt;br /&gt;
&lt;iframe frameborder="0" marginheight="0" marginwidth="0" scrolling="no" src="http://cid-285ed9c2763531fb.skydrive.live.com/embedicon.aspx/.Public/Powershell%20for%20DB%20Developers" style="background-color: #fcfcfc; height: 115px; padding: 0pt; width: 98px;" title="Preview"&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-8356501820568014222?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/i3IxrkFgDkQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/i3IxrkFgDkQ/powershell-for-database-developers.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/04/powershell-for-database-developers.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-4338252635933815132</guid><pubDate>Wed, 17 Mar 2010 00:08:00 +0000</pubDate><atom:updated>2010-03-16T19:08:53.606-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><title>Powershell: Writing Text Files</title><description>&lt;p&gt;I was recently trying to generate a bunch of data that I needed to pass through one of our in-house DLLs in order to use some custom encryption algorithms. Thanks to the wonderful Powershell community, I found several examples on using custom DLL methods. However, I ultimately wanted this data to end up in SQL Server. My first attempt used an “Invoke-SQLCmd” Cmdlet from the SQLPS snap-in. However, generating several million rows took a while to insert. I knew that I didn’t want to queue all of this data in memory, so was inserting one row at a time. I stopped this process after a while because it was too time-consuming.&lt;/p&gt; &lt;p&gt;My next attempt was to push these values to a text file using a variable pointing to a file and then using add-content to write each line to the file.&amp;nbsp; This took longer than doing the SQL inserts one at a time.&lt;/p&gt; &lt;p&gt;I remembered coming across an article by Linchi Shea titled &lt;a href="http://sqlblog.com/blogs/linchi_shea/archive/2010/01/04/add-content-and-out-file-are-not-for-performance.aspx" target="_blank"&gt;Add-Content and Out-File are not for performance&lt;/a&gt;. After finding the link to that article, I found that he recommends using System.IO.StreamWriter to write out files in the most performant manner. I definitely recommend that you check out his article for his summary and examples. For my part, I can say that it took me the better part of 6 hours to generate and populate 10 million rows. I just generated 20 million into a file in &amp;lt; 15 minutes!&lt;/p&gt; &lt;p&gt;I’m including a mockup of my code below in case anyone is interested. I’m definitely not a Powershell expert so as always, be careful if you copy any of this code.&amp;nbsp; My first attempt at directly inserting into SQL Server worked, but threw an error at about 3.5 million rows. As the time was already pretty lengthy by then, I stopped the process and moved on to writing the text file, resuming where I’d left off. That ran for hours until I finally killed it and looked for a better way.&amp;nbsp; The uncommented code is what finally ended up working in a timely manner.&lt;/p&gt; &lt;p&gt;I started by loading my DLL. I called it’s “EncryptValue” function (against just the integer – that’s a longer story) and populated both out to a table. There are several reasons for doing this, but this gave me a good working set for a test project and definitely lays some groundwork for the future.&amp;nbsp; If anyone has any ideas on how I can speed this up, feel free to leave me some comments. If I find some, I’ll post an update.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre class="PowerShellColorizedScript"&gt;&lt;span style="color: #008080"&gt;[Reflection.Assembly]&lt;/span&gt;&lt;span style="color: #a9a9a9"&gt;::&lt;/span&gt;&lt;span style="color: #000000"&gt;LoadFile&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #8b0000"&gt;"C:\MyDLL.dll"&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #006400"&gt;# First attempt – insert directly into SQL Server.&lt;br&gt;&lt;/span&gt;&lt;span style="color: #006400"&gt;#foreach ($i IN 1..10000000) { invoke-sqlcmd –query '&lt;br&gt;#&lt;/span&gt;&lt;span style="color: #006400"&gt;("insert dbo.MyTable VALUES(" + "$i" + ",'" + [MyDLLNamespace]::EncryptValue("$i") + "')") -serverinstance "localhost" }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #006400"&gt;#Attempt #2 – Write to file with add-content&lt;br&gt;#$file = New-Item -type File "C:\Values.txt"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #006400"&gt;#foreach ($i IN 1..10000000) { add-content $file ("$i" + "," + [MyDLLNamespace]::EncryptValue("$i") )  }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #ff4500"&gt;$file&lt;/span&gt; &lt;span style="color: #a9a9a9"&gt;=&lt;/span&gt; &lt;span style="color: #0000ff"&gt;New-Object&lt;/span&gt; &lt;span style="color: #8a2be2"&gt;System.IO.StreamWriter&lt;/span&gt; &lt;span style="color: #8b0000"&gt;"E:\Values.txt"&lt;/span&gt;&lt;span style="color: #000000"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #00008b"&gt;foreach&lt;/span&gt; &lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #ff4500"&gt;$i&lt;/span&gt; &lt;span style="color: #00008b"&gt;IN&lt;/span&gt; &lt;span style="color: #800080"&gt;10000001&lt;/span&gt;&lt;span style="color: #a9a9a9"&gt;..&lt;/span&gt;&lt;span style="color: #800080"&gt;30000000&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;/span&gt; &lt;span style="color: #000000"&gt;{&lt;/span&gt; &lt;span style="color: #ff4500"&gt;$file&lt;/span&gt;&lt;span style="color: #a9a9a9"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;Writeline&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #8b0000"&gt;"$i"&lt;/span&gt; &lt;span style="color: #a9a9a9"&gt;+&lt;/span&gt; &lt;span style="color: #8b0000"&gt;","&lt;/span&gt; &lt;span style="color: #a9a9a9"&gt;+&lt;/span&gt; &lt;span style="color: #008080"&gt;[MyDLLNamespace]&lt;/span&gt;&lt;span style="color: #a9a9a9"&gt;::&lt;/span&gt;&lt;span style="color: #000000"&gt;EncryptValue&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #8b0000"&gt;"$i"&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;/span&gt; &lt;span style="color: #000000"&gt;)&lt;/span&gt;  &lt;span style="color: #000000"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #ff4500"&gt;$file&lt;/span&gt;&lt;span style="color: #a9a9a9"&gt;.&lt;/span&gt;&lt;span style="color: #000000"&gt;close&lt;/span&gt;&lt;span style="color: #000000"&gt;(&lt;/span&gt;&lt;span style="color: #000000"&gt;)&lt;/span&gt;&lt;span style="color: #000000"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;&lt;/pre&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-4338252635933815132?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/5vaBRcpOnWI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/5vaBRcpOnWI/powershell-writing-text-files.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/03/powershell-writing-text-files.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-5137149775805457273</guid><pubDate>Sun, 07 Mar 2010 04:19:00 +0000</pubDate><atom:updated>2010-03-06T22:19:57.936-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><title>Powershell Links – 2010-03-06</title><description>&lt;p&gt;Here are some of the more interesting Powershell links I came across recently, along with some thoughts about them.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lunex.spaces.live.com/blog/cns!64CB3857E28BD106!344.entry" target="_blank"&gt;Powershell, String Encryption, and GPG&lt;/a&gt; – &lt;a href="http://sqlservercentral.com" target="_blank"&gt;SQLServerCentral.com&lt;/a&gt; brought this one to my attention. Chad Miller discusses working with &lt;a href="http://chadwickmiller.spaces.live.com/blog/cns!EA42395138308430!930.entry" target="_blank"&gt;Oracle through Powershell&lt;/a&gt;, and the subject of encrypting the connection strings came up. There are times that we need to connect using something other than Windows authentication. Storing an encrypted connection string makes a lot of sense in those cases. I don’t normally work with Oracle, but the idea of passing and saving encrypted connection strings makes a lot of sense. I do a lot of work against SQL Servers that require SQL Logins so plan to investigate this more thoroughly in the near future.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.stevex.net/powershell-cheatsheet/" target="_blank"&gt;Powershell Cheatsheet&lt;/a&gt; – If you don’t follow the &lt;a href="http://twitter.com/#search?q=%23powershell" target="_blank"&gt;#Powershell hashtag on Twitter&lt;/a&gt;, you may have missed out on this useful set of Powershell tips and tricks, including a way to set your current location to a UNC path. This is cool because it’s something you cannot do with a standard command prompt without mapping a drive. There are other tips here that make this well worth bookmarking if you are getting started with Powershell.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://chadwickmiller.spaces.live.com/blog/cns!EA42395138308430!860.entry" target="_blank"&gt;The T-SQL Hammer&lt;/a&gt; – This is taken from Chad Miller’s blog, but is a great reminder that just because your typical DBA knows T-SQL and thinks in T-SQL does not make that the best solution for everything. If you’re wondering why a DBA might want to learn Powershell, give this a read.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://gallery.technet.microsoft.com/ScriptCenter/en-us/ea76e9dd-e6bf-4750-b1a2-de0a0a649797" target="_blank"&gt;Powershell Help Brower&lt;/a&gt; – This script uses Primal Forms and Powershell to build a Treeview Help browser for Powershell. I tested this out and the only entry that seemed to give me fits was trying to get help for Get-ChildItem. That constantly threw an error for me. However, the rest was very nice – an easy way to browse the available commands and see the help for them.&lt;/p&gt;  &lt;p&gt;As with anything else you find on the Internet, don’t just copy these scripts and run them. Check them out carefully and be sure you know what’s going to happen. (Someday I’m just gonna copy Buck Woody’s disclaimer for scripts and give attribution. :)&amp;#160; )&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-5137149775805457273?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/cv0y0DL3Ij8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/cv0y0DL3Ij8/powershell-links-2010-03-06.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/03/powershell-links-2010-03-06.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-1551914394879525962</guid><pubDate>Mon, 08 Feb 2010 20:03:00 +0000</pubDate><atom:updated>2010-02-08T16:48:24.257-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><title>DDL Schema Change Auditing on SQL Server 2005 / 2008</title><description>I’ve run across this before so wanted to write up a solution to keeping track of schema changes we’ve used. We trust our team to make DDL changes, additions, drops in order to get their job done. We also trust them to save those scripts into source control as they are run so we can use them when we release software. However, there are times that people make changes and forget to save the script or just think that it’s a temporary change to test and forget about it. With that in mind, we decided to take advantage of SQL Server 2005’s DDL triggers.&lt;br /&gt;
First, we created a database called [Audit] on our development server.&lt;br /&gt;
Next, we created a table to store the logs.&lt;br /&gt;
&lt;pre style="background-color: #fbfbfb; border: 1px solid rgb(206, 206, 206); min-height: 40px; overflow: auto; padding: 5px; width: 400px;"&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=USE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;USE&lt;/a&gt; Audit
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=GO&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;GO&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=CREATE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;CREATE&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=TABLE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;TABLE&lt;/a&gt; [dbo].[DDL_Audit](
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[DDL_Audit_ID] [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=int&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;int&lt;/a&gt;] &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=IDENTITY&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;IDENTITY&lt;/a&gt;(1,1) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NOT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NOT&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NULL&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NULL&lt;/a&gt;,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[Event_Type] [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;](100) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NULL&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NULL&lt;/a&gt;,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[Database_Name] [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;](100) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NULL&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NULL&lt;/a&gt;,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[SchemaName] [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;](100) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NULL&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NULL&lt;/a&gt;,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[ObjectName] [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;](100) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NULL&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NULL&lt;/a&gt;,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[ObjectType] [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;](100) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NULL&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NULL&lt;/a&gt;,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[EventDate] [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=datetime&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;datetime&lt;/a&gt;] &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NULL&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NULL&lt;/a&gt;,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[SystemUser] [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;](100) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NULL&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NULL&lt;/a&gt;,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[CurrentUser] [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;](100) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NULL&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NULL&lt;/a&gt;,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[OriginalUser] [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;](100) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NULL&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NULL&lt;/a&gt;,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[EventDataText] [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;](&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;max&lt;/a&gt;) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NULL&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NULL&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=ON&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;ON&lt;/a&gt; [&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=PRIMARY&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;PRIMARY&lt;/a&gt;]
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=GO&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;GO&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=GRANT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;GRANT&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=INSERT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;INSERT&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=ON&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;ON&lt;/a&gt; DDL_Audit &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=TO&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;TO&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=public&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;public&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=GO&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;GO&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Note in this example, I granted INSERT permissions to public to avoid needing to give our team any other access to that database. I didn’t want them to read/write rows in that table if I could avoid it. After that, I ran a script in SSMS using Text output to step through all of our databases and generate the trigger create code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #fbfbfb; border: 1px solid rgb(206, 206, 206); min-height: 40px; overflow: auto; padding: 5px; width: 800px;"&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=sp_msforeachdb&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: orange;"&gt;sp_msforeachdb&lt;/a&gt; '&lt;span style="color: darkred;"&gt;SELECT ''use ?
&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=GO&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;GO&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=SET&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;SET&lt;/a&gt; ANSI_PADDING &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=ON&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;ON&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=GO&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;GO&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=CREATE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;CREATE&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=TRIGGER&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;TRIGGER&lt;/a&gt; trg_DDL_Monitor_Change
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=ON&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;ON&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=DATABASE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;DATABASE&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=FOR&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;FOR&lt;/a&gt; DDL_DATABASE_LEVEL_EVENTS
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=AS&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;AS&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=SET&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;SET&lt;/a&gt; NOCOUNT &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=ON&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;ON&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=SET&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;SET&lt;/a&gt; ANSI_PADDING &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=ON&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;ON&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=declare&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;declare&lt;/a&gt; @EventType &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(100)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=declare&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;declare&lt;/a&gt; @SchemaName &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(100)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=declare&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;declare&lt;/a&gt; @DatabaseName &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(100)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=declare&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;declare&lt;/a&gt; @ObjectName &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(100)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=declare&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;declare&lt;/a&gt; @ObjectType &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(100)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=DECLARE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;DECLARE&lt;/a&gt; @EventDataText &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=VARCHAR&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;VARCHAR&lt;/a&gt;(&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=MAX&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;MAX&lt;/a&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=SELECT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;SELECT&lt;/a&gt; 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;@EventType = EVENTDATA().&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=value&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;value&lt;/a&gt;('&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'(/EVENT_INSTANCE/EventType)[1]'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;','&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'n&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;max&lt;/a&gt;)'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;')  
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;,@DatabaseName = EVENTDATA().&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=value&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;value&lt;/a&gt;('&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'(/EVENT_INSTANCE/DatabaseName)[1]'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;','&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'n&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;max&lt;/a&gt;)'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;')  
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;,@SchemaName = EVENTDATA().&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=value&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;value&lt;/a&gt;('&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'(/EVENT_INSTANCE/SchemaName)[1]'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;','&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'n&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;max&lt;/a&gt;)'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;')  
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;,@ObjectName = EVENTDATA().&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=value&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;value&lt;/a&gt;('&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'(/EVENT_INSTANCE/ObjectName)[1]'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;','&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'n&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;max&lt;/a&gt;)'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;')
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;,@ObjectType = EVENTDATA().&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=value&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;value&lt;/a&gt;('&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'(/EVENT_INSTANCE/ObjectType)[1]'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;','&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'n&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;max&lt;/a&gt;)'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;')   
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;,@EventDataText = EVENTDATA().&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=value&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;value&lt;/a&gt;('&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;','&lt;span style="color: darkred;"&gt;''&lt;/span&gt;'n&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;max&lt;/a&gt;)'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;')
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=insert&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;insert&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=into&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;into&lt;/a&gt; Audit.dbo.DDL_Audit (Event_Type, Database_Name, SchemaName, ObjectName, ObjectType
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;, EventDate, SystemUser, CurrentUser, OriginalUser, EventDataText)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=select&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;select&lt;/a&gt; @EventType, @DatabaseName, @SchemaName, @ObjectName, @ObjectType
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;, getdate(), SUSER_SNAME(), &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=CURRENT_USER&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;CURRENT_USER&lt;/a&gt;, ORIGINAL_LOGIN()
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;, @EventDataText
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=GO&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;GO&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;'&lt;span style="color: darkred;"&gt;''&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
We took the results of that script and removed any databases that did not matter for purposes of tracking changes. We ran that and created the appropriate triggers on all of the databases. That left us with the notification messages. We handled this through a SQL Server Agent job using Database Mail.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #fbfbfb; border: 1px solid rgb(206, 206, 206); min-height: 40px; overflow: auto; padding: 5px; width: 1000px;"&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=IF&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;IF&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=EXISTS&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;EXISTS&lt;/a&gt; (&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=SELECT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;SELECT&lt;/a&gt; '&lt;span style="color: darkred;"&gt;x&lt;/span&gt;' &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=FROM&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;FROM&lt;/a&gt; Audit.dbo.DDL_Audit
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=WHERE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;WHERE&lt;/a&gt; Event_Type &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NOT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NOT&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=LIKE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;LIKE&lt;/a&gt; '&lt;span style="color: darkred;"&gt;%statist%&lt;/span&gt;'
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=AND&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;AND&lt;/a&gt; SystemUser &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NOT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NOT&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=IN&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;IN&lt;/a&gt; (&lt;i&gt;service account list here&lt;/i&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=AND&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;AND&lt;/a&gt; EventDate &amp;gt;= &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=convert&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;convert&lt;/a&gt;(&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(10),DATEADD(dd, -1, GETDATE()),101))
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=BEGIN&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;BEGIN&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=DECLARE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;DECLARE&lt;/a&gt; @email_from &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=nvarchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;nvarchar&lt;/a&gt;(100)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;, @email_address &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=nvarchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;nvarchar&lt;/a&gt;(200) 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;, @TheSubject &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=nvarchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;nvarchar&lt;/a&gt;(255)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=SELECT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;SELECT&lt;/a&gt; @Email_Address = &lt;a href="mailto:%27myemail@myemail.com%27"&gt;'&lt;span style="color: darkred;"&gt;myemail@myemail.com&lt;/span&gt;'
&lt;/a&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=SET&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;SET&lt;/a&gt; @email_from = &lt;a href="mailto:%27myemail@myemail.com%27"&gt;'&lt;/a&gt;&lt;a href="mailto:DevServer@server.com"&gt;DevServer@server.com&lt;/a&gt;&lt;a href="mailto:%27myemail@myemail.com%27"&gt;'&lt;/a&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=select&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;select&lt;/a&gt; @email_address &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;as&lt;/a&gt; '&lt;span style="color: darkred;"&gt;To:&lt;/span&gt;' ,  @email_from &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;as&lt;/a&gt; '&lt;span style="color: darkred;"&gt;From:&lt;/span&gt;'
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=set&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;set&lt;/a&gt; @TheSubject = '&lt;span style="color: darkred;"&gt;Recent Schema changes on &lt;/span&gt;' + RTRIM(@@SERVERNAME)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=DECLARE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;DECLARE&lt;/a&gt; @tableHTML  &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NVARCHAR&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NVARCHAR&lt;/a&gt;(&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=MAX&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;MAX&lt;/a&gt;) ;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=SET&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;SET&lt;/a&gt; @tableHTML =
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;N'&lt;span style="color: darkred;"&gt;&amp;lt;H1&amp;gt;DevDB Schema Change&amp;lt;/H1&amp;gt;&lt;/span&gt;' +
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;N'&lt;span style="color: darkred;"&gt;&amp;lt;table border="1"&amp;gt;&lt;/span&gt;' +
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;N'&lt;span style="color: darkred;"&gt;&amp;lt;tr&amp;gt;&amp;lt;th&amp;gt;Database_Name&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;SchemaName&amp;lt;/th&amp;gt;&lt;/span&gt;' +
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;N'&lt;span style="color: darkred;"&gt;&amp;lt;th&amp;gt;ObjectName&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Event_Type&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;ObjectType&amp;lt;/th&amp;gt;&lt;/span&gt;' +
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;N'&lt;span style="color: darkred;"&gt;&amp;lt;th&amp;gt;EventDate&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;SystemUser&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;CurrentUser&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;OriginalUser&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;EventDataText&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;' +
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=CAST&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;CAST&lt;/a&gt; ( ( &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=SELECT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;SELECT&lt;/a&gt; td = Database_Name,       '&lt;span style="color: darkred;"&gt;&lt;/span&gt;',
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;td = SchemaName, '&lt;span style="color: darkred;"&gt;&lt;/span&gt;',
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;td = ObjectName, '&lt;span style="color: darkred;"&gt;&lt;/span&gt;',
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;td = Event_Type, '&lt;span style="color: darkred;"&gt;&lt;/span&gt;',
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;td = ObjectType, '&lt;span style="color: darkred;"&gt;&lt;/span&gt;',
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;td = EventDate, '&lt;span style="color: darkred;"&gt;&lt;/span&gt;',
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;td = SystemUser, '&lt;span style="color: darkred;"&gt;&lt;/span&gt;',
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;td = CurrentUser, '&lt;span style="color: darkred;"&gt;&lt;/span&gt;',
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;td = OriginalUser, '&lt;span style="color: darkred;"&gt;&lt;/span&gt;',
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;td = EventDataText
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=FROM&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;FROM&lt;/a&gt; Audit.dbo.DDL_Audit
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=WHERE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;WHERE&lt;/a&gt; Event_Type &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NOT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NOT&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=LIKE&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;LIKE&lt;/a&gt; '&lt;span style="color: darkred;"&gt;%statist%&lt;/span&gt;'
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=AND&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;AND&lt;/a&gt; SystemUser &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NOT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NOT&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=IN&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;IN&lt;/a&gt; (&lt;i&gt;serviceaccount&lt;/i&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=AND&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;AND&lt;/a&gt; EventDataText &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=not&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;not&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=like&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;like&lt;/a&gt; '&lt;span style="color: darkred;"&gt;%ALTER%INDEX%REBUILD%&lt;/span&gt;'
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=AND&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;AND&lt;/a&gt; EventDate &amp;gt;= &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=convert&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;convert&lt;/a&gt;(&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=varchar&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;varchar&lt;/a&gt;(10),DATEADD(dd, -1, GETDATE()),101)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=ORDER&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;ORDER&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=BY&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;BY&lt;/a&gt; Database_Name, ObjectType, ObjectName, EventDate, Event_Type
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=FOR&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;FOR&lt;/a&gt; XML PATH('&lt;span style="color: darkred;"&gt;tr&lt;/span&gt;'), TYPE 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;) &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=AS&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;AS&lt;/a&gt; &lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=NVARCHAR&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;NVARCHAR&lt;/a&gt;(&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=MAX&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;MAX&lt;/a&gt;) ) +
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;N'&lt;span style="color: darkred;"&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;' ;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=EXEC&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;EXEC&lt;/a&gt; msdb.dbo.sp_send_dbmail
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;@profile_name = '&lt;span style="color: darkred;"&gt;Default&lt;/span&gt;' ,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;@recipients=@email_address,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;@subject = @TheSubject,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;@body = @tableHTML,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;@body_format = '&lt;span style="color: darkred;"&gt;HTML&lt;/span&gt;' ;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;a href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=END&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99" style="color: blue;"&gt;END&lt;/a&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
We scheduled this code in a job step that runs daily at 10am. Any changes in that time frame were then sent to a team of people on a daily basis. If no changes were made, no email was sent. Hopefully this will give someone some ideas on one way to audit changes to their DBs. I make no claims that this is the best way, but it worked well for us.&lt;br /&gt;
&lt;br /&gt;
You can download the script file containing all of this code using the link below. &lt;br /&gt;
&lt;br /&gt;
&lt;iframe frameborder="0" marginheight="0" marginwidth="0" scrolling="no" src="http://cid-285ed9c2763531fb.skydrive.live.com/embedicon.aspx/.Public/SQL%20Scripts/SQL%20Server%20DDL%20Audit.sql" style="background-color: #fcfcfc; height: 115px; padding: 0pt; width: 98px;" title="Preview"&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-1551914394879525962?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/WiEG4LqNRQA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/WiEG4LqNRQA/ddl-schema-change-auditing-on-sql.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>5</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/02/ddl-schema-change-auditing-on-sql.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-7078185929453278852</guid><pubDate>Sat, 30 Jan 2010 20:11:00 +0000</pubDate><atom:updated>2010-01-30T14:11:46.611-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><title>Powershell Community Extensions</title><description>&lt;p&gt;While with the time and knowledge we can write a lot of the things we’ll use in Powershell, it’s often not worth it to re-invent the wheel. There’s a group of Powershell coders who have put together the &lt;a href="http://pscx.codeplex.com/" target="_blank"&gt;Powershell Community Extensions&lt;/a&gt; – a set of Powershell aliases, functions, and other useful code to make working with Powershell a little easier.&lt;/p&gt;  &lt;p&gt;For example, there’s a built-in function to split a string. Functions and DLLs are included to read and write ZIP files. There are even some Cmdlets written to allow easy reading and writing from the clipboard or MSMQ. While not all of this will be immediately useful, it’s definitely worth having around.&lt;/p&gt;  &lt;p&gt;Installation should be pretty straightforward. Make sure all instances of Powershell are closed. The recommended install is performed using the MSI file. If you want the latest version, you’ll have to unzip the file into your Documents\Windows Powershell\Modules folder.&amp;#160; After that, you can import the module using “Import-Module PSCX”.&amp;#160; There is supposed to be a way to customize what you actually import as well so you don’t load items you’ll never use. The details should be on the Powershell Community Extensions site.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;While I’m at it, I’d like to give props to John D. Cook who has written a short pamphlet called &lt;a href="http://www.johndcook.com/PowerShellDay1.pdf" target="_blank"&gt;Day 1 With Powershell&lt;/a&gt; which lists a lot of little things that he wished he’d known before starting. There is some good, basic information in it, including configuration, some basics behind Powershell decisions (like = vs –eq), and some pointers to other Powershell sites. I’d recommend it if you’re just getting started with Powershell as it could give you some pointers on how to proceed past the basics.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-7078185929453278852?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/9j-6oxtqkCk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/9j-6oxtqkCk/powershell-community-extensions.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/01/powershell-community-extensions.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-6908054517104077154</guid><pubDate>Wed, 27 Jan 2010 19:30:00 +0000</pubDate><atom:updated>2010-01-27T13:30:00.622-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Scripting</category><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><title>Powershell – Comparison Operators</title><description>&lt;p&gt;I’m writing this one so I remember these operators. Powershell doesn’t use standard operators such as =, &amp;lt;, &amp;lt;&amp;gt;, !=, etc. These operators are used in the following manner.&lt;/p&gt;  &lt;div align="center"&gt;   &lt;table border="1" cellspacing="0" cellpadding="1" width="437" align="center"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" width="117"&gt;           &lt;p align="center"&gt;Operator&lt;/p&gt;         &lt;/td&gt;          &lt;td valign="top" width="318"&gt;           &lt;p align="center"&gt;Conventional Operator&lt;/p&gt;         &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;-eq&lt;/td&gt;          &lt;td valign="top" width="318"&gt;=&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;-ne&lt;/td&gt;          &lt;td valign="top" width="318"&gt;&amp;lt;&amp;gt; or !=&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;-gt&lt;/td&gt;          &lt;td valign="top" width="318"&gt;&amp;gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;-ge&lt;/td&gt;          &lt;td valign="top" width="318"&gt;&amp;gt;=&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;-lt&lt;/td&gt;          &lt;td valign="top" width="318"&gt;&amp;lt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;-le&lt;/td&gt;          &lt;td valign="top" width="318"&gt;&amp;lt;=&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;-contains&lt;/td&gt;          &lt;td valign="top" width="318"&gt;e.g., 1,2,3 –contains 1 is true&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;-notcontains&lt;/td&gt;          &lt;td valign="top" width="318"&gt;e.g., 1,2,3 –notcontains 1 is false&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;-not !&lt;/td&gt;          &lt;td valign="top" width="318"&gt;e.g., –not ($a –eq $b) Logical Not operator&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;-and&lt;/td&gt;          &lt;td valign="top" width="318"&gt;Logical AND e.g.,            &lt;br /&gt;($age –ge 21) –and ($gender –eq “M”)&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;-or&lt;/td&gt;          &lt;td valign="top" width="318"&gt;Logical OR e.g.,            &lt;br /&gt;($age –le 21) –or ($InSchool –eq $true)&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/div&gt;  &lt;div align="left"&gt;&amp;#160;&lt;/div&gt;  &lt;div align="left"&gt;I think this is one of the areas that is going to trip me up quite a bit. In Powershell, “=” is always used to assign.&amp;#160; “&amp;lt;” and “&amp;gt;” are used for redirection. The names make sense to me in a way, but I can tell I’ll be spending some time with these before I’m completely comfortable with this concept.&lt;/div&gt;  &lt;div align="left"&gt;&amp;#160;&lt;/div&gt;  &lt;div align="left"&gt;The above are the base comparisons.&amp;#160; If you prefix them with “i” the operator becomes case-insensitive.&amp;#160; If you prefix an operator with “c”, the operator will be case-sensitive.&amp;#160; Thus we would get the following:&lt;/div&gt;  &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  &amp;quot;&lt;span style="color: #8b0000"&gt;johnson&lt;/span&gt;&amp;quot; -eq &amp;quot;&lt;span style="color: #8b0000"&gt;Johnson&lt;/span&gt;&amp;quot; #True&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  &amp;quot;&lt;span style="color: #8b0000"&gt;johnson&lt;/span&gt;&amp;quot; -ieq &amp;quot;&lt;span style="color: #8b0000"&gt;Johnson&lt;/span&gt;&amp;quot; #True&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  &amp;quot;&lt;span style="color: #8b0000"&gt;johnson&lt;/span&gt;&amp;quot; -ceq &amp;quot;&lt;span style="color: #8b0000"&gt;Johnson&lt;/span&gt;&amp;quot; #False&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Where-Object&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is a useful Cmdlet to filter out results from the pipeline, but this should also be used with care. The initial object will still contain everything with which it started. The results will then be filtered. If you have a way to filter these results prior to passing them through the pipeline, you’ll often have better performance. The following will find all instances of Firefox running on the machine:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  Get-Process | where-object {$_.&lt;span style="color: #0000ff"&gt;name&lt;/span&gt; -eq 'firefox'}&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We could then pass this through the pipeline to operate on that specific object. Where-Object can be abbreviated with a “&lt;strong&gt;?&lt;/strong&gt;”.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;If, ElseIf, Else&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is a pretty basic statement for Programming and seems to make sense in the way it was implemented in Powershell.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If (condition) {#do something} &lt;br /&gt;  &lt;br /&gt;ElseIf (next condition) {#do something else} &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;Else (final condition) {#final action}&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;As in most uses of &lt;em&gt;If&lt;/em&gt;, the process will work through until it finds a condition that evaluates to $true. Once it hits that, it will execute the command(s) inside the corresponding curly braces and exit out of the If.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Switch&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Sometimes it doesn’t make sense to write out a bunch of IfElse lines to keep checking conditions. Switch can help shorten that. A simple example would be something like this.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;$Value = 15&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;Switch($Value)&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;{&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  1 {&amp;quot;&lt;span style="color: #8b0000"&gt;Value is 1&lt;/span&gt;&amp;quot;}&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  2 {&amp;quot;&lt;span style="color: #8b0000"&gt;Value is 2&lt;/span&gt;&amp;quot;}&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  15 {&amp;quot;&lt;span style="color: #8b0000"&gt;Value is 15&lt;/span&gt;&amp;quot;}&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  default {&amp;quot;&lt;span style="color: #8b0000"&gt;Value is $_&lt;/span&gt;&amp;quot;}&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;}&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Powershell interprets these in order and will evaluate each one in turn. The default line tells Powershell what to do if no match is found.&amp;#160; If you want Powershell to stop evaluating when it finds a match, add a &lt;strong&gt;;break&lt;/strong&gt; inside the {} following the matching condition. Otherwise, it could hit the same matching condition and evaluate each one it finds. This may be desirable at some times when you want to act on all matching statements. Other times you may want to short-circuit the process.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Another advantage of the Switch statement is that you can evaluate expressions instead of just the values.&amp;#160; In the example above, the behavior behind the scenes is performing {$_ –eq 1} and {$_ –eq 2}, etc for the lines in the switch. You can substitute your own expressions in here easily to change the evaluations.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Switch supports character comparisons (defaulting to case-insensitive equals), but can do other types by default with a parameter on the Switch statement (e.g., –regex, –case, –wildcard, etc.)&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The biggest drawback for Switch is that it would be considered a “blocking” component in the pipeline. It needs to wait for all results before it operates. If you just want to do some filtering on the results, you would be better off using Where-Object or filtering up front.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-6908054517104077154?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/XEmOh7tMk44" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/XEmOh7tMk44/powershell-comparison-operators.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/01/powershell-comparison-operators.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-3004320558899815825</guid><pubDate>Wed, 27 Jan 2010 00:15:00 +0000</pubDate><atom:updated>2010-01-26T18:15:00.133-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Scripting</category><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><title>Powershell – Quite note on Objects</title><description>&lt;p&gt;I’m sure this comes as no surprise to anyone who’s dabbled in Powershell, but just about everything in Powershell has an Object underneath. The results we see on screen come from those objects and it’s only when those results are formatted, output, captured, etc that they cease to be an object. This drives much of the power in Powershell. You can set a variable to the contents of an object or part of an object and use that later. &lt;/p&gt;  &lt;p&gt;Typically objects will have properties and methods. Properties define what an object “is” – e.g., Red, Small, Named, etc. Methods define what an object can do – e.g., Write, Read, Slice, Mix, etc. There are quite a few internal methods and properties that are common across the internal Powershell objects. You can find these by piping the object to the “Get-Member” cmdlet.&lt;/p&gt;  &lt;div class="mycode"&gt;&lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  $host | Get-Member&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You can add your own properties and methods to objects easily by calling the Add-Member Cmdlet. The most direct way to do this seems to be by piping the object to the Add-Member Cmdlet.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;p&gt;  $host | Add-Member –Member NoteProperty –Name &lt;em&gt;PropertyName&lt;/em&gt; –Value &lt;em&gt;PropertyValue&lt;/em&gt;&lt;/p&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;As Powershell is based on MS technologies, it makes sense that you can use the .NET Framework to work with objects. I will admit that I’m not as familiar with the wide variety of .NET objects so will not try to go into detail.&amp;#160; For one example, here’s a way to call the .NET framework to look up a DNS name by IP address.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  [system.Net.Dns]::GetHostByAddress(&amp;quot;&lt;span style="color: #8b0000"&gt;207.46.19.190&lt;/span&gt;&amp;quot;)&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Perhaps a more useful class would be System.Environment, first by querying its Static members:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  [System.Environment] | Get-Member -Static&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You can see that there are a lot of members that are useful. If we choose one and don’t enter valid parameter data, we can even get useful error messages at times.&amp;#160; For example, the following will return an error with valid parameters in the message:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  [system.environment]::GetFolderPath(&amp;quot;&lt;span style="color: #8b0000"&gt;z&lt;/span&gt;&amp;quot;)&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We can tell by the error message, that some valid parameters would be Desktop, Programs, MyDocuments, Home, etc. Entering a valid parameter in this case will then return the path stored in the Environment for that variable.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You can even define your own objects using the New-Object Cmdlet. With no parameters, this creates an empty object. You can create objects of various types by passing the type as the first parameter to the New-Object cmdlet. If you set a variable to an object, you need to be careful about type casting.&amp;#160; Setting a variable to a string containing a date will not necessarily cause that variable to be an object of type System.DateTime. It will most likely be a System.String.&amp;#160; You may need to explicitly cast the object as the type you desire or force the value to be of the type you desire in order to cast the object as the correct/desired type.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You can call on COM objects, .NET objects, Powershell objects, Assemblies, and probably more than I’ve listed here as long as you know the name or way to call those objects. I’m still learning this are of Powershell, but hope to expand on this in future posts.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-3004320558899815825?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/qv-PFnuYcOg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/qv-PFnuYcOg/powershell-quite-note-on-objects.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/01/powershell-quite-note-on-objects.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-4060646235461653801</guid><pubDate>Wed, 13 Jan 2010 15:00:00 +0000</pubDate><atom:updated>2010-01-13T09:00:00.672-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Scripting</category><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><title>Powershell – Piping</title><description>One of the most often-used features I’ve seen so far in Powershell has been the concept of piping the results of commands into other commands to ultimately return something formatted, limited, or otherwise morphed into the desired output. The easiest examples might be something like:&lt;br /&gt;
&lt;pre&gt;&lt;pre style="background-color: white; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;dir | more
dir | sort-object length –desc | format-table -autosize

&lt;/pre&gt;
&lt;/pre&gt;&lt;br /&gt;
The first command returns a listing of the files in the current folder one screen at a time, pausing for you to read each screen and tell the command when to advance.&amp;nbsp; The second is a string of commands to get a listing of files, sort by the file size from largest to smallest, then auto-size each column for a best fit based on the data. Because each result set is passed as an object through the pipeline, you don’t have to handle the format of the text or other conversions. Powershell handles the objects and consumes them until you reach the end. By default Powershell appends a hidden “| Out-Default” command to display the results on screen. If you want to see more options for output, you can run:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;pre style="background-color: white; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;Get-Command Out*
Get-Command Export*

&lt;/pre&gt;
&lt;/pre&gt;&lt;br /&gt;
Obviously there are a lot of other commands that can be used in the pipeline, but these seem to be especially useful if you want to output the results to something other than the screen. &amp;nbsp;Also note that if you convert your results along the way using Format-Table or Out-String, you will change the results from an object into an array of text. &amp;nbsp;(Out-String is the only output command that you can insert into a pipeline and not stop the pipeline. All other "Out-" commands will terminate the pipeline.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Blocking vs. Non-Blocking&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
As with other languages, some Cmdlets may be “blocking” other parts of the pipeline. For example, a sort cannot pass its results on until the entire result set has been sorted. The “more” Cmdlet in Powershell is also blocking, but the equivalent “Out-Host –paging” will do the same thing without blocking. When to use various commands is an important consideration if you pipe multiple Cmdlets together, especially if you will be dealing with large objects or result sets.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Filtering&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
Filtering is accomplished by piping your resultset to Cmdlets such as &lt;em&gt;Where-Object&lt;/em&gt;, &lt;em&gt;Select-Object&lt;/em&gt;, &lt;em&gt;ForEach-Object&lt;/em&gt;, or &lt;em&gt;Get-Unique&lt;/em&gt;.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Where-Object allows you to examine all objects and return just those matching your criteria. This is very similar to a WHERE clause in SQL Server.&lt;/li&gt;
&lt;li&gt;Select-Object acts in a manner similar to the SELECT clause of a SQL Statement and allows you to pick which properties are displayed. It can also handle things such as “TOP 5” or “Distinct” (not exact commands), but can also do some interesting handling of values in the array. For example, you can choose to display every other line of the result set or the first, last, and middle rows by examining the array. &amp;nbsp;(Note that this will create a new object in a lot of cases because you're filtering the columns.)&lt;/li&gt;
&lt;li&gt;ForEach-Object operates on each result in the resultset to run commands against them.&lt;/li&gt;
&lt;li&gt;Get-Unique eliminates duplicates in the resultset.&lt;/li&gt;
&lt;/ul&gt;Filtering would be considered a Blocking operation in many cases and can sometimes be done more efficiently through the native commands or Cmdlets. For example, if you need to pull back just the files of type “.txt”, you may be better off filtering that out in your dir command rather than passing the entire resultset of all files to the Where-Object Cmdlet.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Tee-Object&lt;/strong&gt;&lt;br /&gt;
It’s worth mentioning this Cmdlet because it could be really useful in troubleshooting. A simple example would be:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;pre style="background-color: white; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;dir | Tee-Object -variable t1 | Select-Object Name,Length | Tee-Object -variable t2 | Sort-Object Length –descending&lt;/pre&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
This would pipe the output of the “dir” command into a variable called $t1. It would then pass the output to Select-Object to limit the results down to Name and Length, then pass that resultset to a variable called $t2. Finally, it would output your results ordered by length descending.&amp;nbsp; You would now have two variables to examine results as you stepped through the pipeline. If you are getting unexpected results, this would be very valuable in troubleshooting.&amp;nbsp; You can also use “-filepath” instead of&amp;nbsp;“-variable” to store results to a file instead of a variable.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Conclusion&lt;/b&gt;&lt;br /&gt;
There is a &lt;i&gt;lot&lt;/i&gt;&amp;nbsp;of information available for piping commands because this is one of the true strengths of Powershell. I'm not going to go into a lot of details at this point. There will be more examples in future posts and there are lots of other posts detailing the process. &amp;nbsp;Piping takes a set of results and passes that to the next command, and to the next, and to the next, until it gets to the end of the pipeline. At that point, the results are returned, stored, or discarded (if you chose to output to Out-Null). You can store these results along the way for comparison or even store them in a file for later analysis.&lt;br /&gt;
&lt;br /&gt;
I plan to spend a pretty significant amount of time familiarizing myself with the various uses for the pipeline. Because it's used so heavily, this is going to be a key component to understanding and getting the most benefit from Powershell.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-4060646235461653801?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/2QYCB3CsSOc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/2QYCB3CsSOc/powershell-piping.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/01/powershell-piping.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-3593018582324016599</guid><pubDate>Sun, 10 Jan 2010 04:00:00 +0000</pubDate><atom:updated>2010-01-09T22:07:57.245-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Scripting</category><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><title>Powershell – Arrays &amp; Hash Tables</title><description>&lt;p&gt;I don’t have too much to say about Arrays at this point, but as I’m partly blogging this for my own education, I’ll put down what I’m learning.&amp;#160; At this point, I’m expecting the arrays to be useful when trying to build my example project. One piece will be to step through existing files to look for certain flags or traits to tell me to do further processing.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Arrays&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;First of all, any variable that stores the results of a command with multi-line output will result in an array. This includes commands such as &lt;em&gt;dir&lt;/em&gt; and &lt;em&gt;ipconfig&lt;/em&gt;. Arrays start with position [0]. You can create an array by using comma-separated values such as &lt;em&gt;$Variable = 1,2,3,4&lt;/em&gt; or by using &lt;em&gt;$Variable = @(1)&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;The first cool thing I saw was that using negative positions in an array steps through the array in reverse.&amp;#160; That means if I have an array of &lt;em&gt;$Variable = @(0,1,2,3,4)&lt;/em&gt;, that $Variable[1] will be 1.&amp;#160; $Variable[-1] will be 4.&amp;#160; I can see this being very useful when you need to step backwards through the results. This is very intuitive to me and I wish this were easier to implement in other scenarios. However, stepping through the array backwards could be tricky because it could result in creating a copy of the array. For small arrays this wouldn’t necessarily be a problem, but with large arrays, I could see performance becoming an issue. If you need to reverse the entire array, you can simply use [array]::Reverse($Variable) to reverse the array elements in place. (This actually changes the array stored in the variable.)&lt;/p&gt;  &lt;p&gt;You can easily specify to return multiple elements from the array using $Variable[0,4,-12] which would return the 1st element, 4th element, and the element 12th from the end.&lt;/p&gt;  &lt;p&gt;Adding an element is pretty easy. You can type:   &lt;br /&gt;&amp;#160; &lt;em&gt;@Variable += @(NewElement)     &lt;br /&gt;&lt;/em&gt;However, this will create a new copy of the array in order to add another element because arrays in Powershell cannot be resized. This may be something to consider when working with large arrays.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Hash Tables&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Hash tables seem to be pretty much what you’d expect. They store key/value pairs such as “Name”, “SQL Server 2008 Enterprise Edition” stored together. You define these in a manner very similar to that of arrays:   &lt;br /&gt;&amp;#160; &lt;em&gt;$Variable = @{Name=”SQL Server”; Version=”10.0&amp;quot;; Edition=”Enterprise”}&lt;/em&gt;    &lt;br /&gt;That will define a new hash table containing 3 keypairs. Name, Version, and Edition.&amp;#160; Each will have a Name and Value property that will display when you display the variable. You can display just the value of a particular keypair with &lt;em&gt;$Variable.Name&lt;/em&gt; or &lt;em&gt;$Variable.Version&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;Adding a new keypair is easy as well.&amp;#160; Use:   &lt;br /&gt;&amp;#160; &lt;em&gt;$Variable.NewName = “New Value”&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Similarly, you can remove a keypair with:   &lt;br /&gt;&amp;#160; &lt;em&gt;$Variable.Remove(“Name”)&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Hash Tables are used with the Format-Table command as well. You can customize the columns returned by manipulating their respective hash table values: Expression, Width, Label, and Alignment. Use &lt;em&gt;get-help Format-Table&lt;/em&gt; for more information.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;By default, copies of an array or hash table are actually copies of a pointer to the values. That means that if you just set the variable with the normal assign of $Var1 = $Var2, you’ll end up with pointers. Modifying either of those variables will affect the other(s). Modifying the array/hash table or using the .Clone() command on either will make a new copy.&lt;/p&gt;  &lt;p&gt;Finally, if you need to strongly type an array, you can do so by putting the datatype before the array definition:   &lt;br /&gt;&amp;#160; &lt;em&gt;[int[]]$MyArray = @(0,1,2,3)&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;This will ensure that all values stored in the array are of that datatype. Any attempts to add/remove values to the array that are not of that datatype will raise an error.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;There are a lot of uses for arrays and hash tables. This just scratches the surface of what can be done, but it should be enough to get you started.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-3593018582324016599?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/KBjCE33fkZY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/KBjCE33fkZY/powershell-arrays-hash-tables.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/01/powershell-arrays-hash-tables.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-1433707934577012096</guid><pubDate>Sat, 09 Jan 2010 06:51:00 +0000</pubDate><atom:updated>2010-01-13T08:08:58.993-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Scripting</category><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><title>Powershell – Variables</title><description>I’ve just started to play around with variables in Powershell. I used them a little bit before this, but am in the process of figuring out basic commands around variables. First thing to note is that variables are not case-sensitive.&amp;nbsp; That means that a variable called “$MyVariable” is the same as “$MYVARIABLE” or “$mYvARIABLE”.&amp;nbsp; You can declare a variable and assign a value easily using something like:&lt;br /&gt;
&lt;blockquote&gt;&lt;div&gt;&lt;div style="background-color: #e2ecf6;"&gt;$variable = value&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/blockquote&gt;That pretty much covers the majority of basic assignments. You can also use the &lt;em&gt;Set-Variable&lt;/em&gt; command to not only set a variable, but change some options such as making a variable read-only or making it into a constant. Once a constant has been declared/set, you cannot delete it. It will be cleaned up when the Powershell session ends.&amp;nbsp; The only somewhat tricky point around variables is using special characters.&amp;nbsp; All variables should be declared within {}’s if you need to use special characters.&lt;br /&gt;
It’s easy to list all of the variables and their values using:&lt;br /&gt;
&lt;pre&gt;&lt;pre style="background-color: white; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; height: 40px; margin: 0em; width: 103.08%;"&gt;dir variable:
dir variable: | Format-Table Name, Value, Description -autosize

&lt;/pre&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
The first command lists all variables and their values. The second lists the name, value, and a Description property as well and then auto-sizes everything accordingly. Useful if you want to set other properties of your variables besides name and value. You can also use that latter command with a “-wrap” parameter to show the definitions of the system-level variables.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you want to use Windows Environment variables, you’ll want to reference the “env:” virtual drive within Powershell. These are the variables such as your Path or Windows folder that are used within Windows. Any changes you make to these variables are only set within the scope of your Powershell session.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By default, variables stay within the scope of the function or session, but you can override this with &lt;em&gt;$private&lt;/em&gt; to limit a variable to a scope, &lt;em&gt;$local&lt;/em&gt; to use default scoping and enable variables to be read by sessions the current session creates, &lt;em&gt;$script&lt;/em&gt; to allow the variable to work anywhere in the current script, and &lt;em&gt;$global&lt;/em&gt; to allow the variable to be used anywhere, even external functions and scripts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Variable Types and Attributes&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Working with variables is easy with the defaults because they are weakly or loosely typed. If I assign a string to a variable, that variable contains a string. If I assign a bit value, the variable contains a Byte. That means that variables can change types easily as the script runs. However, that also means that if I’m expecting a variable to contain a date value and it somehow gets a string or floating point value, I could be very surprised.&amp;nbsp; Powershell allows for strong data typing of variables as well. If you define a variable in this manner:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;pre style="background-color: white; font-family: consolas,'Courier New',courier,monospace; font-size: 12px; margin: 0em; width: 100%;"&gt;[datetime]$myDate = "&lt;span style="color: darkred;"&gt;2009-12-25&lt;/span&gt;"&lt;/pre&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
You will explicitly declare a variable of type DateTime. If you then attempt to set the variable to a string, you’ll get an error message. The variable types are standard .NET types with a handful of specific Powershell types.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can set other properties using the Attributes for a variable and can even clear out the strong Typing using an Attributes.Clear() command against the variable. Attributes can be used to set constraints around a variable such as whether or not the variable can store $Null values, check constraints, or even RegEx patterns for the data stored in the variable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is by no means a definitely explanation of everything around variables in Powershell, but it’s a good start for most people. You can always run &lt;em&gt;get-help About_Variables&lt;/em&gt; within Powershell for more details or &lt;em&gt;get-help Set-Variable&lt;/em&gt; to read more about setting variables. I may update or post a follow-up if something else pertinent about variables comes up. I’d love to know a way to list out variable datatypes from within Powershell, but haven’t found a way yet.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-1433707934577012096?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/5I5Rd9h6-zE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/5I5Rd9h6-zE/powershell-variables.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/01/powershell-variables.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-452861709707483720</guid><pubDate>Fri, 08 Jan 2010 20:36:00 +0000</pubDate><atom:updated>2010-01-12T06:10:52.751-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Scripting</category><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><title>Powershell – Modules and Profiles</title><description>Probably not technically correct, but pretty close. I’ve been trying to use &lt;a href="http://sqlpsx.codeplex.com/" target="_blank"&gt;SQLPSX for Powershell&lt;/a&gt;, recently updated to v2.0. For the longest time, I’ve tried various methods to import the modules and couldn’t get them to import successfully. I knew that this was likely due to lack of knowledge so spent a little time tracking down the root cause of my problems.&lt;br /&gt;
After quite a bit of searching, I finally figured out why I’ve been having such a hard time loading Powershell modules. By default, the Modules reside in   &lt;br /&gt;
&amp;nbsp; %USERPROFILE%\Documents\WindowsPowershell\Modules&lt;br /&gt;
I verified that this was set within Powershell with quite a bit of different code snippets.&amp;nbsp; For some reason, this folder was never created in my Documents folder.&amp;nbsp; I manually created a folder called “WindowsPowershell” and another inside that called “Modules” and then extracted each of SQLPSX’s module folders into that newly created directory and I finally could use external modules. I restarted Powershell and used&lt;br /&gt;
&lt;blockquote&gt;&lt;span style="color: #333333; font-size: x-small;"&gt;import-module SQLServer&lt;/span&gt;&lt;br /&gt;
&lt;/blockquote&gt;I got a warning that Powershell could not had to set my execution policy to allow remotely signed scripts. That’s not too hard to do, but I couldn’t import new modules without doing that. I ran&lt;br /&gt;
&lt;blockquote&gt;Set-ExecutionPolicy remotesigned&lt;br /&gt;
&lt;/blockquote&gt;&lt;span style="color: #666666;"&gt;and set the default to “Y”. (This is a local test machine so I’m not as concerned about changing this setting.) I re-ran my import-module command and the modules finally imported.&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #666666;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #666666;"&gt;Of course, now I’d like to automatically load these commands whenever I run Powershell, as well as customize the default Powershell environment to pull in the various SQL Powershell modules used in the SQLPS environment that ships with SQL Server 2008.&amp;nbsp; I found &lt;a href="http://msdn.microsoft.com/en-us/library/bb613488%28VS.85%29.aspx" target="_blank"&gt;this MSDN article&lt;/a&gt; that discusses profiles and how to work with them. You can create a default profile easily with:&lt;/span&gt;&lt;br /&gt;
&lt;blockquote&gt;&lt;pre&gt;new-item -path $profile -itemtype file –force
notepad $profile&lt;/pre&gt;&lt;br /&gt;
&lt;/blockquote&gt;&lt;br /&gt;
&lt;br /&gt;
This will create a new profile for all users and all shells on that machine. The second command will open up that new file for editing. Place whatever commands you want to run on startup in this file. For example, &lt;a href="http://blogs.msdn.com/mwories/archive/2008/06/14/SQL2008_5F00_Powershell.aspx" target="_blank"&gt;this article&lt;/a&gt; discusses how you can get all of the behavior of the default SQLPS mini-shell. While not optimal, it looks like a great way to get the full power of Powershell w/ the new SQL functionality.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-452861709707483720?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/OuulRbyvc1s" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/OuulRbyvc1s/powershell-modules-and-profiles.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/01/powershell-modules-and-profiles.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-3698614366811557330</guid><pubDate>Fri, 08 Jan 2010 05:41:00 +0000</pubDate><atom:updated>2010-01-07T23:41:43.526-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Scripting</category><category domain="http://www.blogger.com/atom/ns#">PowerShell</category><title>Getting Started with Powershell</title><description>&lt;p&gt;I’m in the process of learning PowerShell. This seems to be the first scripting language that we can use at the server/desktop level that has some serious backing from Microsoft. I remember VBScript, CScript, Batch files, and similar, but it seemed that if I wanted to run a script, I had to find workarounds or plug-ins to do what I wanted inside of some other language and then mix and match those together.&amp;#160; Powershell seems to drive a lot of processes behind the scenes. That being said, here’s a quick summary of some important things I’m learning in the process.&lt;/p&gt;  &lt;p&gt;1. Powershell can call executables directly, but needs an explicit path to those executables if they aren’t in the system path. This means that you may need to use .\MyProgram.exe instead of just MyProgram.exe.&lt;/p&gt;  &lt;p&gt;2. get-help &lt;em&gt;command&lt;/em&gt;&amp;#160; - I can see myself spending a lot of time with this to figure out syntax and uses for various commands. As I’ve played around with it tonight, I’m really impressed by how much information is available without needing to leave PowerShell. Reminds me a lot of the “man” pages in *IX, but with more examples and in a language I can understand.&lt;/p&gt;  &lt;p&gt;3. get-command –verb &lt;em&gt;verb&lt;/em&gt; – Another useful command to list all Powershell Cmdlets containing that verb.&lt;/p&gt;  &lt;p&gt;4. Parameters can be abbreviated as long as there are enough characters to uniquely ID the parameter. An error message will be thrown if there aren’t enough. Probably better to let auto-complete handle the parameter name in that case to avoid possible ambiguity.&lt;/p&gt;  &lt;p&gt;5. Common Parameters. There are apparently several common parameters that should be available for most Cmdlets.&amp;#160; I can see a lot of use for –ErrorVariable and –OutVariable. These are designed to capture error or output details, respectively.&lt;/p&gt;  &lt;p&gt;6. Aliases. I can see these being both helpful and a little painful. Helpful in that a lot of familiar commands such as &lt;em&gt;dir&lt;/em&gt; and &lt;em&gt;ls&lt;/em&gt; are aliases, but painful in forgetting that I’ve set one and seeing some underlying Cmdlet change. I guess I have some qualms from my days of using a pretty customized BASH shell and forgetting a lot of the basics that were used to create that shell in the first place. Just running the get-help on Alias returned several screens of information. Example command to run to see CmdLet followed by its aliases:    &lt;br /&gt;&amp;#160;&amp;#160; dir alias: | Group-Object definition&lt;/p&gt;  &lt;p&gt;&amp;#160; And just as I figured, there’s a way to export the aliases and re-import them from a file later so you don’t need to define them each time you restart Powershell. Export-Alias and Import-Alias&lt;/p&gt;  &lt;p&gt;7. Virtual Drives. PowerShell has a lot of different virtual drives set up to access Aliases, Registry settings, physical drives, and others. I’ll be exploring those more as I need them. I’m halfway assuming that one of these tied to a server would be related to AD structures, but that may just be a whole new set of Cmdlets.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I think this may be a good place to stop for the day. It seems the next section deals with functions which are reminding of DOS Batch file parameters at first glance. I hope that is not the case, but I’ll know more about that tomorrow. So far I’ve gotten a good handle on some of the basics and learned some ways to help myself during scripting. That’s a great start.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-3698614366811557330?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/6eL46Khm95c" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/6eL46Khm95c/getting-started-with-powershell.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2010/01/getting-started-with-powershell.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-5651850777266947625</guid><pubDate>Wed, 11 Nov 2009 18:14:00 +0000</pubDate><atom:updated>2009-11-11T12:14:15.294-06:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">VSTS DB Pro</category><category domain="http://www.blogger.com/atom/ns#">SQL Server</category><title>Learning to work around DB Pro</title><description>&lt;p&gt;I’ve been trying to get a multi-database solution working correctly with MS Visual Studio Team System for Database Professionals (aka DBPro or DataDude). Anyone who has tried to tie multiple co-dependent databases together within DB Pro has experienced some of the pain points in working with these projects. Hopefully these notes can help you to some extent.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Pain Point #1: Circular References&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This is the largest challenge that we face in our environment. DB Pro doesn’t really understand that DB A can reference DB B which can then reference DB A.&amp;#160; You can easily add a Database Reference to another project in your solution by right-clicking the “references” sub-folder and adding a new Database Reference.&amp;#160; However, once you’ve done that, you can’t add one from that project back to the original.&amp;#160; I understand you don’t want to get caught in an endless loop, but it would be nice to have some sort of Max Level of Recursion set that would stop the program from getting caught in that.&lt;/p&gt;  &lt;p&gt;That being said, there is a way around this. You can generate a .DBSchema file for your existing database using the instructions from &lt;a href="http://portal.sqltrainer.com/2008/10/using-vsdbcmd-utility-to-generate.html" target="_blank"&gt;this site&lt;/a&gt;. Note that you may need to tweak the options a little bit. I think the “ModelType” parameter has been deprecated in GDR2.&amp;#160; Save that DB Schema file to some central folder and repeat for each database you want to reference.&amp;#160; Once done, go back into your projects and add a reference to each of those files. Because these are DBSchema files and not the actual database projects, DB Pro will recognize these and you’ll be able to add a reference as needed to each database.&lt;/p&gt;  &lt;p&gt;Remember that these schema files will not be kept up to date automatically.&amp;#160; As you build your DB Projects, you’ll need to update those schema files in some way.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;     &lt;br /&gt;Pain Point #2: System Objects&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This isn’t a hard problem to work around and there are a lot of hints out there about this.&amp;#160; The easiest way to handle references to the system objects is by adding a reference to the included DBSchema files created when you installed DB Pro.&amp;#160; These can generally be found in &lt;u&gt;%ProgramFiles%\Microsoft Visual Studio 9.0\VSTSDB\Extensions\SqlServer&lt;/u&gt;. There should be sub-folders for 2000, 2005, and 2008.&amp;#160; Simply include these references in your projects and you should be set.&lt;/p&gt;  &lt;p&gt;This may not always help you if you reference another database referencing the system objects, though. I ran into this problem and had to find yet another workaround, which leads me to…&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Pain Point #3: “Unresolved References”&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;When all else fails and you can’t actually get around the “Unresolved Reference” warnings due to the use of temp tables, referencing another database that references the system objects, or some other reason you have a final recourse. That is to tweak the file properties to ignore certain error messages or warnings that occur for that file.&amp;#160; It’s not a great solution because you could miss some valid warnings, but it can help you get around those warnings that keep popping up when you try to build or deploy.&amp;#160; To add an exception just find the file, select it, and edit its properties.&amp;#160; The last option is to “Suppress Warnings” and that will let you bypass the warnings that normally occur when DB Pro can’t figure out what you are trying to do.&amp;#160; This should really only be turned on for code that is definitely working in your environment and the code should be reviewed carefully when enabling this option.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Finally, I don’t necessarily claim that any of these are best practices. We’re learning here and trying to figure out the best way to work around the limitations and quirks in DB Pro to get it working in our environment. I’m more than happy to learn new and better ways to do things so feel free to share your tips or to correct me where I’m wrong.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-5651850777266947625?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/ZHBcLmP3K50" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/ZHBcLmP3K50/learning-to-work-around-db-pro.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2009/11/learning-to-work-around-db-pro.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-5029276974542242123</guid><pubDate>Thu, 01 Oct 2009 20:09:00 +0000</pubDate><atom:updated>2009-10-01T15:09:36.059-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SSIS</category><category domain="http://www.blogger.com/atom/ns#">XML</category><category domain="http://www.blogger.com/atom/ns#">SQL Server</category><title>SSIS and XML Output</title><description>I was pretty surprised by this and am in pretty good company.&amp;nbsp; SSIS 2005 and 2008 do not have an XML Destination adapter. We have another "opportunity" from Microsoft to work out a way to export XML data.&amp;nbsp; I'll disagree that this is a good idea, despite those saying that it's easy to work around in a script component.&amp;nbsp; I'm not a great SSIS Script writer.&lt;br /&gt;
&lt;br /&gt;
My problem - I want to take a simple SQL query that uses FOR XML AUTO to generate XML and put that into a file.&amp;nbsp; If you set this as the only part of your source query, you end up with an output of DT_Image.&amp;nbsp; Converting that to text results in a long string of numbers. While I was amused at the result, it didn't help me generate the necessary XML for our partners.&lt;br /&gt;
&lt;br /&gt;
I came up with a relatively simple workaround that works well for me because I'm only dealing with one XML Data Set at a time.&amp;nbsp; I put something like the following in my OLEDB Source:&lt;br /&gt;
&lt;blockquote&gt;&lt;br /&gt;
&lt;span style="color: blue;"&gt;DECLARE &lt;/span&gt;@XMLOutput &lt;span style="color: blue;"&gt;XML&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: blue;"&gt;SET &lt;/span&gt;@XMLOutput = (&lt;span style="color: blue;"&gt;SELECT &lt;/span&gt;* &lt;span style="color: blue;"&gt;FROM &lt;/span&gt;MyTable &lt;span style="color: blue;"&gt;FOR XML AUTO&lt;/span&gt;)&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: blue;"&gt;SELECT &lt;/span&gt;&lt;span style="color: magenta;"&gt;CAST&lt;/span&gt;(@XMLOutput &lt;span style="color: blue;"&gt;as VARCHAR&lt;/span&gt;(&lt;span style="color: magenta;"&gt;MAX&lt;/span&gt;)) &lt;span style="color: blue;"&gt;as &lt;/span&gt;XMLResults&lt;br /&gt;
&lt;/blockquote&gt;&lt;br /&gt;
I was then able to pipe that into a Delimited Text File destination with a CRLF Delimiter and no column headers. That generated results I could pass on to our partners. While it's not the most elegant solution, it was much easier to me than trying to write and maintain a script component just to handle XML.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-5029276974542242123?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/XoED6UXZ8-g" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/XoED6UXZ8-g/ssis-and-xml-output.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>1</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2009/10/ssis-and-xml-output.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4845744956082769684.post-2570526190695990610</guid><pubDate>Thu, 17 Sep 2009 20:41:00 +0000</pubDate><atom:updated>2009-09-17T15:41:03.762-05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SSIS</category><category domain="http://www.blogger.com/atom/ns#">SQL</category><category domain="http://www.blogger.com/atom/ns#">Performance</category><title>Data Loads</title><description>One of the things I really like about SQL Server Integration Services is that it can load large data sets quickly, especially if you need to look up values along the way.&amp;nbsp; However, there are times that things behave in strange manners that seem hard to explain.&amp;nbsp; We were recently trying to load about 64 million rows into a warehouse-type database and kept hitting a bottleneck once we got past around 30-35 million rows.&amp;nbsp; No matter what I tried, the load would run at about 2 million rows per minute, gradually slowing down until it reached the 10,000 rows per second load speed.&amp;nbsp; I found several helpful posts, including some pointers to check the MaxRowBufferSize and similar settings.&amp;nbsp; Tweaking these helped someone who was on a memory-bound machine with a very similar problem.&amp;nbsp; I tried that and saw my initial load times improve, but still slowed down to a crawl at around the same point.&lt;br /&gt;
&lt;br /&gt;
I learned more about &lt;a href="http://www.sqlis.com/post/Shredding-a-Recordset.aspx"&gt;looping through (aka "shredding") a recordset&lt;/a&gt; of CustomerID values to try to go through that set.&amp;nbsp; I set up a &lt;a href="http://www.sqlis.com/post/For-Loop-Container-Samples.aspx"&gt;FOR Loop&lt;/a&gt; to loop through 5 million records at a time, I checked for processes running against the table. Nothing seemed to help.&amp;nbsp; I had posted information on Twitter, had a co-worker looking at the package with me, and even got some replies from &lt;a href="http://www.twitter.com/PaulRandal"&gt;Paul Randal&lt;/a&gt;.&amp;nbsp; (Thank you for the assist, Paul - sorry I wasn't checking my tweets more regularly that time.)&lt;br /&gt;
&lt;br /&gt;
In the process above, I was challenged trying to set the CustomerID in some dynamic fashion on my source query and eventually resorted to treating the SQL Command as an expression and hacking in my variables into the WHERE clause.&amp;nbsp; It worked, but definitely felt awkward. I am more than open to some suggestions about how to use SSIS variables inside of a FOR loop as part of the OLEDB Source Command.&amp;nbsp; I also learned a little more about watching variable values - like needing to have a breakpoint set before they'll be available.&amp;nbsp; And my co-worker pointed me to a wonderful discussion showing that DtExecUI runs only in 32-bit mode. I'd been using that as a quick/easy way to get my parameters plugged in and to keep an eye on the general progress of the load. &lt;br /&gt;
&lt;br /&gt;
About the same time that Paul suggested checking my index fragmentation levels, I remember seeing a very similar behavior pattern several years ago when a co-worker was trying to update a column in the middle of a somewhat wide clustered index on a multi-million row table. The server was thrashing all over the place, trying to re-order millions of rows as this command took place.&amp;nbsp; I checked the table and sure enough, there was a wide clustered index on the table that would cause exactly that behavior.&amp;nbsp; I don't know why I didn't check it before other than thinking that I'd just created a test table and no indexes on it.&amp;nbsp; In retrospect, that was poor planning on my part.&amp;nbsp; For the future, I'll remember to check clustered indexes before I try to load millions of (unsorted) rows into a new table.&lt;br /&gt;
&lt;br /&gt;
It was a somewhat humbling experience, mostly because one of the basic things I know about loading lots of data was also something that I just completely ignored when doing all of my troubleshooting. Still, a little humility is a good thing and I've now re-learned several programming techniques in the process. The best part about this was seeing a 6 hour load process reduced down to 30 minutes, including dropping and re-creating the clustered index. That's going to make a noticeable difference in our nightly processes.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4845744956082769684-2570526190695990610?l=schottsql.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/SchottSql/~4/S8n61gb-jRw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/SchottSql/~3/S8n61gb-jRw/data-loads.html</link><author>noreply@blogger.com (Peter Schott)</author><thr:total>0</thr:total><feedburner:origLink>http://schottsql.blogspot.com/2009/09/data-loads.html</feedburner:origLink></item></channel></rss>

