<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tag 'Scripts'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Scripts&amp;orTags=0</link><description>Search results matching tag 'Scripts'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>The Windows Azure Software Development Kit (SDK) and the Windows Azure Training Kit (WATK)</title><link>http://sqlblog.com/blogs/buck_woody/archive/2012/09/12/the-windows-azure-software-development-kit-sdk-and-the-windows-azure-training-kit-watk.aspx</link><pubDate>Wed, 12 Sep 2012 13:40:40 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:45165</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;Windows Azure is a platform that allows you to write software, run software, or use software that we've already written. We provide lots of resources to help you do that - many can be found right here in this blog series. There are two primary resources you can use, and it's important to understand what they are and what they do.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://officeimg.vo.msecnd.net/en-us/images/MH900441285.jpg"&gt;&lt;img width="121" height="107" style="float:left;max-width:550px;" alt="" src="http://officeimg.vo.msecnd.net/en-us/images/MH900441285.jpg" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;The Windows Azure Software Development Kit (SDK)&lt;/h1&gt;
&lt;p&gt;Actually, this isn't one resource. We have SDK's for multiple development environments, such as Visual Studio and also Eclipse, along with SDK's for iOS, Android and other environments. Windows Azure is a "back end", so almost any technology or front end system can use it to solve a problem.&lt;/p&gt;
&lt;p&gt;The SDK's are primarily for development. In the case of Visual Studio, you'll get a runtime environment for Windows Azure which allows you to develop, test and even run code all locally - you do not have to be connected to Windows Azure at all, until you're ready to deploy.&lt;/p&gt;
&lt;p&gt;You'll also get a few samples and codeblocks, along with all of the libraries you need to code with Windows Azure in .NET, PHP, Ruby, Java and more.&lt;/p&gt;
&lt;p&gt;The SDK is updated frequently, so check this location to find the latest for your environment and language - just click the bar that corresponds to what you want:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.windowsazure.com/en-us/develop/downloads/" target="_blank"&gt;http://www.windowsazure.com/en-us/develop/downloads/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://officeimg.vo.msecnd.net/en-us/images/MH900438678.jpg"&gt;&lt;img width="151" height="163" style="margin:2px 5px;border:0px currentColor;float:left;max-width:550px;" src="http://officeimg.vo.msecnd.net/en-us/images/MH900438678.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;The Windows Azure Training Kit (WATK)&lt;/h1&gt;
&lt;p&gt;Whether you're writing code, using Windows Azure Virtual Machines (VM's) or working with Hadoop, you can use the WATK to get examples, code, PowerShell scripts, PowerPoint decks, training videos and much more. This should be your second download after the SDK. This is all of the training you need to get started, and even beyond. The WATK is updated frequently - and you can find the latest one here:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.windowsazure.com/en-us/develop/net/other-resources/training-kit/" target="_blank"&gt;http://www.windowsazure.com/en-us/develop/net/other-resources/training-kit/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;There are many other resources - again, check the &lt;a href="http://windowsazure.com"&gt;http://windowsazure.com&lt;/a&gt; site, the &lt;a href="http://www.windowsazure.com/en-us/community/newsletter/2012/june/" target="_blank"&gt;community newsletter (which introduces the latest features)&lt;/a&gt;, and &lt;a href="http://sqlblog.com/b/buckwoody/rss.aspx" target="_blank"&gt;my blog for more&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>Reading temporary table from another session</title><link>http://sqlblog.com/blogs/michael_zilberstein/archive/2012/03/01/42048.aspx</link><pubDate>Thu, 01 Mar 2012 17:30:57 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:42048</guid><dc:creator>mz1313</dc:creator><description>&lt;p&gt;It happens to me at least once a week – I want to check progress of some heavy script that runs in chunks over big dataset and find out that it writes intermediate data to temporary table only. Last time it happened 3 days ago when I wanted to analyze 50GB trace table on my notebook. I wrote a script that was taking 200 thousand rows at a time, parameterizing them and aggregating by different keys – host name, application etc. Usual trace analysis stuff. After an hour I wanted to check the progress but found out that intermediate results are written to temp table and of course I forgot to add debug prints. Took me some thought and ~5 minutes of coding to find the solution. How? &lt;/p&gt;  &lt;p&gt;My trace analysis script ran over 200K rows chunks and among other things aggregated them grouping by parameterized query text. One of the measurement columns was “TotalQueries” – counter of rows in a group that could be later used to calculate average values for Reads, Writes, Duration and CPU. Every 200k rows chunk inserted rows into #BA temp table. Last step in a script, when all chunks have already been processed, was aggregation of #BA table’s data into permanent table. So in order to monitor the progress I could SUM all values of “TotalQueries” column in #BA table and compare it to number of rows in my 50GB trace table (which can be easily verified using &lt;code style="font-size:12px;"&gt;&lt;span style="color:darkred;"&gt;&lt;font size="2"&gt;sp_spaceused&lt;/font&gt;&lt;/span&gt;&lt;/code&gt;).&lt;/p&gt;  &lt;p&gt;Of course, I new the temp table name – I wrote the initial script. So first thing was to find its object_id.&lt;/p&gt;  &lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;font size="2"&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;span style="color:black;"&gt;[object_id] &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;span style="color:black;"&gt;tempdb.sys.tables &lt;/span&gt;&lt;span style="color:blue;"&gt;WHERE &lt;/span&gt;&lt;span style="color:black;"&gt;name &lt;/span&gt;&lt;span style="color:gray;"&gt;LIKE &lt;/span&gt;&lt;span style="color:red;"&gt;'#BA%'&lt;/span&gt;&lt;/font&gt;&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;It brought me negative number: –1546597904 in my current demo. I don’t have access to temp table created by another session. But as admin on my own notebook I sure have access to every page in every database. So next step is to find all pages that belong to #BA table. For this task we have undocumented but widely known (and used) DBCC IND command. Since digging in every page manually and wasting hour on it wasn’t exactly my purpose, I kept DBCC IND output in the table variable and used it later in the script.&lt;/p&gt; &lt;code style="font-size:12px;"&gt;&lt;font size="2"&gt;&lt;span style="color:blue;"&gt;DECLARE &lt;/span&gt;&lt;span style="color:#434343;"&gt;@BATablePages &lt;/span&gt;&lt;/font&gt;&lt;span style="color:blue;"&gt;&lt;font size="2"&gt;TABLE        &lt;br /&gt;&lt;/font&gt;&lt;/span&gt;&lt;font size="2"&gt;&lt;span style="color:gray;"&gt;(        &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:black;"&gt;PageFID &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;PagePID &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;IAMFID &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;IAMPID &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;ObjectID BIGINT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;IndexID &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;PartitionNumber &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:gray;"&gt;,        &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:black;"&gt;PartitionID BIGINT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;iam_chain_type &lt;/span&gt;&lt;span style="color:blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;64&lt;/span&gt;&lt;span style="color:gray;"&gt;), &lt;/span&gt;&lt;span style="color:black;"&gt;PageType TINYINT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;IndexLevel TINYINT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;NextPageFID &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:gray;"&gt;,        &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:black;"&gt;NextPagePID &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;PrevPageFID &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;PrevPagePID &lt;/span&gt;&lt;/font&gt;&lt;span style="color:blue;"&gt;&lt;font size="2"&gt;INT        &lt;br /&gt;&lt;/font&gt;&lt;/span&gt;&lt;font size="2"&gt;&lt;span style="color:gray;"&gt;)        &lt;br /&gt;        &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;INSERT INTO &lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:#434343;"&gt;@BATablePages        &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;EXEC&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:red;"&gt;'DBCC IND(2, -1546597904, 1)'&lt;/span&gt;&lt;/font&gt;&lt;span style="color:gray;"&gt;&lt;font size="2"&gt;)&lt;/font&gt;       &lt;br /&gt;&lt;/span&gt;&lt;/code&gt;  &lt;p&gt;Actually we need only PagePID column and only for leaf level data pages e.g. PageType = 1. Next step is to loop over those pages and insert their content into another table. For viewing page’s data I used another widely known (and undocumented as well) DBCC PAGE command. It is less known that DBCC PAGE can be used along with WITH TABLERESULT suffix. Note: column definitions in the following script are accidental – I just wanted them to be wide enough so that script can complete successfully.&lt;/p&gt; &lt;code style="font-size:12px;"&gt;&lt;font size="2"&gt;&lt;span style="color:blue;"&gt;DECLARE &lt;/span&gt;&lt;span style="color:#434343;"&gt;@rc &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:#434343;"&gt;@v_PageID &lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:#434343;"&gt;@sql &lt;/span&gt;&lt;span style="color:blue;"&gt;NVARCHAR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:magenta;"&gt;MAX&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:gray;"&gt;)        &lt;br /&gt;        &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;CREATE TABLE &lt;/span&gt;&lt;span style="color:#434343;"&gt;#ind&lt;/span&gt;&lt;span style="color:gray;"&gt;( &lt;/span&gt;&lt;span style="color:black;"&gt;RowNum &lt;/span&gt;&lt;span style="color:blue;"&gt;INT &lt;/span&gt;&lt;span style="color:gray;"&gt;NOT NULL &lt;/span&gt;&lt;span style="color:#434343;"&gt;IDENTITY&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;), &lt;/span&gt;&lt;span style="color:black;"&gt;PageNum &lt;/span&gt;&lt;span style="color:blue;"&gt;INT &lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:gray;"&gt;NOT NULL )        &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;CREATE TABLE &lt;/span&gt;&lt;span style="color:#434343;"&gt;#page&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;ParentObject &lt;/span&gt;&lt;span style="color:blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;128&lt;/span&gt;&lt;span style="color:gray;"&gt;), &lt;/span&gt;&lt;span style="color:black;"&gt;[Object] &lt;/span&gt;&lt;span style="color:blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;512&lt;/span&gt;&lt;span style="color:gray;"&gt;), &lt;/span&gt;&lt;span style="color:black;"&gt;Field &lt;/span&gt;&lt;span style="color:blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;128&lt;/span&gt;&lt;span style="color:gray;"&gt;), &lt;/span&gt;&lt;span style="color:black;"&gt;[Value] &lt;/span&gt;&lt;span style="color:blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;8000&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:gray;"&gt;) )        &lt;br /&gt;        &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;INSERT INTO &lt;/span&gt;&lt;span style="color:#434343;"&gt;#ind&lt;/span&gt;&lt;span style="color:gray;"&gt;( &lt;/span&gt;&lt;span style="color:black;"&gt;PageNum &lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:gray;"&gt;)        &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:black;"&gt;PagePID        &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:#434343;"&gt;@BATablePages        &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;WHERE &lt;/span&gt;&lt;span style="color:black;"&gt;PageType &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:black;"&gt;1        &lt;br /&gt;        &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;SET &lt;/span&gt;&lt;span style="color:#434343;"&gt;@rc &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:#434343;"&gt;@@ROWCOUNT        &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;WHILE &lt;/span&gt;&lt;span style="color:#434343;"&gt;@rc &lt;/span&gt;&lt;span style="color:gray;"&gt;&amp;gt; &lt;/span&gt;&lt;/font&gt;&lt;span style="color:black;"&gt;&lt;font size="2"&gt;0        &lt;br /&gt;&lt;/font&gt;&lt;/span&gt;&lt;font size="2"&gt;&lt;span style="color:blue;"&gt;BEGIN        &lt;br /&gt;&amp;#160;&amp;#160; SELECT &lt;/span&gt;&lt;span style="color:#434343;"&gt;@v_PageID &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:black;"&gt;PageNum &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;span style="color:#434343;"&gt;#ind &lt;/span&gt;&lt;span style="color:blue;"&gt;WHERE &lt;/span&gt;&lt;span style="color:black;"&gt;RowNum &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:#434343;"&gt;@rc        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;SET &lt;/span&gt;&lt;span style="color:#434343;"&gt;@sql &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:red;"&gt;'DBCC PAGE(2, 1, ' &lt;/span&gt;&lt;span style="color:gray;"&gt;+ &lt;/span&gt;&lt;span style="color:magenta;"&gt;CONVERT&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;32&lt;/span&gt;&lt;span style="color:gray;"&gt;), &lt;/span&gt;&lt;span style="color:#434343;"&gt;@v_PageID&lt;/span&gt;&lt;span style="color:gray;"&gt;) + &lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:red;"&gt;', 3) WITH TABLERESULTS'        &lt;br /&gt;        &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;INSERT INTO &lt;/span&gt;&lt;span style="color:#434343;"&gt;#page&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;ParentObject&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;[Object]&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;Field&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;[Value]&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:gray;"&gt;)        &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;EXEC&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:#434343;"&gt;@sql&lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:gray;"&gt;)        &lt;br /&gt;        &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;SET &lt;/span&gt;&lt;span style="color:#434343;"&gt;@rc &lt;/span&gt;&lt;span style="color:blue;"&gt;= &lt;/span&gt;&lt;span style="color:#434343;"&gt;@rc &lt;/span&gt;&lt;span style="color:gray;"&gt;- &lt;/span&gt;&lt;/font&gt;&lt;span style="color:black;"&gt;&lt;font size="2"&gt;1        &lt;br /&gt;&lt;/font&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&lt;font size="2"&gt;END        &lt;br /&gt;&lt;/font&gt;&lt;/span&gt;&lt;/code&gt;  &lt;p&gt;So now we have all data pages inside single table but not in a format we’re used to:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/michael_zilberstein/image_21445ED5.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblog.com/blogs/michael_zilberstein/image_thumb_5B237BF4.png" width="1097" height="235" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;But actually, we now have everything we need. The rest is simple:&lt;/p&gt; &lt;code style="font-size:12px;"&gt;&lt;font size="2"&gt;&lt;span style="color:blue;"&gt;DELETE FROM &lt;/span&gt;&lt;span style="color:#434343;"&gt;#page &lt;/span&gt;&lt;span style="color:blue;"&gt;WHERE &lt;/span&gt;&lt;span style="color:black;"&gt;Field &lt;/span&gt;&lt;span style="color:gray;"&gt;&amp;lt;&amp;gt; &lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:red;"&gt;'TotalQueries'        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;span style="color:magenta;"&gt;SUM&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:magenta;"&gt;CONVERT&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;[Value]&lt;/span&gt;&lt;span style="color:gray;"&gt;)) &lt;/span&gt;&lt;span style="color:blue;"&gt;AS &lt;/span&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;span style="color:black;"&gt;TotalQueries        &lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;span style="color:#434343;"&gt;#page&lt;/span&gt;&lt;/font&gt;&lt;/code&gt;</description></item><item><title>SYS2 Scripts Updated – Scripts to monitor database backup, database space usage and memory grants now available</title><link>http://sqlblog.com/blogs/davide_mauri/archive/2011/02/13/sys2-scripts-updated-scripts-to-monitor-database-backup-database-space-usage-and-memory-grants-now-available.aspx</link><pubDate>Sun, 13 Feb 2011 17:30:22 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:33451</guid><dc:creator>manowar</dc:creator><description>&lt;p&gt;I’ve just released three new scripts of my “sys2” script collection that can be found on CodePlex:&lt;/p&gt;  &lt;p&gt;Project Page: &lt;a title="http://sys2dmvs.codeplex.com/" href="http://sys2dmvs.codeplex.com/"&gt;http://sys2dmvs.codeplex.com/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Source Code Download: &lt;a title="http://sys2dmvs.codeplex.com/SourceControl/changeset/view/57732" href="http://sys2dmvs.codeplex.com/SourceControl/changeset/view/57732"&gt;http://sys2dmvs.codeplex.com/SourceControl/changeset/view/57732&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The three new scripts are the following&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;sys2.database_backup_info.sql &lt;/li&gt;    &lt;li&gt;sys2.query_memory_grants.sql &lt;/li&gt;    &lt;li&gt;sys2.stp_get_databases_space_used_info.sql &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Here’s some more details:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;database_backup_info&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;This script has been made to quickly check if and when backup was done. It will report the last full, differential and log backup date and time for each database. Along with these information you’ll also get some additional metadata that shows if a database is a read-only database and its recovery model:&lt;/p&gt;    &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/davide_mauri/image_4B56D20D.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://sqlblog.com/blogs/davide_mauri/image_thumb_269594C9.png" width="1028" height="109" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;By default it will check only the last seven days, but you can change this value just specifying how many days back you want to check.&lt;/p&gt;    &lt;p&gt;To analyze the last seven days, and list only the database with FULL recovery model without a log backup&lt;/p&gt;    &lt;p&gt;&lt;font face="Courier New"&gt;select * from sys2.databases_backup_info(default)        &lt;br /&gt;where recovery_model = 3 and log_backup = 0&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;To analyze the last fifteen days, and list only the database with FULL recovery model with a differential backup&lt;/p&gt;    &lt;p&gt;&lt;font face="Courier New"&gt;select * from sys2.databases_backup_info(15)        &lt;br /&gt;where recovery_model = 3 and diff_backup = 1&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;I just love this script, I use it every time I need to check that backups are not too old and that t-log backup are correctly scheduled.&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;query_memory_grants&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;This is just a wrapper around sys.dm_exec_query_memory_grants that enriches the default result set with the text of the query for which memory has been granted or is waiting for a memory grant and, optionally, its execution plan&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;stp_get_databases_space_used_info&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;This is a stored procedure that list all the available databases and for each one the overall size, the used space within that size, the maximum size it may reach and the auto grow options. This is another script I use every day in order to be able to monitor, track and forecast database space usage.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;As usual feedbacks and suggestions are more than welcome!&lt;/p&gt;</description></item><item><title>PowerShell for the DBA: Search the Windows Event Logs for Errors</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/08/04/powershell-for-the-dba-search-the-windows-event-logs-for-errors.aspx</link><pubDate>Wed, 04 Aug 2010 13:43:30 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:27605</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;This is a very simple script - but it's one I run each morning. It searches the Windows System Event Log for an error condition. You can replace "System" here with "Application" or "Security", or any of the other logs that are created on your Windows Server. This is run at the server, since I have each server check itself and make a file of the results - then I swing by and pick up the files each morning with another script that builds a web page.&lt;/p&gt;
&lt;p&gt;&lt;span style="color:#339966;"&gt;&amp;nbsp;# Parse for errors&lt;br /&gt;Get-eventlog System | Where-Object { $_.entryType -eq "error" }&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;"&gt;&lt;span style="font-family:Calibri;"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:Calibri;mso-ansi-language:EN;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:Calibri;mso-ansi-language:EN;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:Calibri;mso-ansi-language:EN;"&gt;&lt;span style="font-family:Calibri;"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. Yes, there are always multiple ways to do things, and this script may not work in every situation, for everything. It&amp;rsquo;s just a script, people. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea&amp;nbsp;during the operation of this script, see a physician immediately.&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;</description></item><item><title>Viewing how much memory is used by not reused query plan</title><link>http://sqlblog.com/blogs/davide_mauri/archive/2010/07/23/viewing-how-much-memory-is-used-by-not-reused-query-plan.aspx</link><pubDate>Fri, 23 Jul 2010 08:48:54 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:27251</guid><dc:creator>manowar</dc:creator><description>&lt;p&gt;One interesting observation that may help developers get convinced that they should parametrize query and that they must check that the ORM they use does it correctly is show how much memory can be wasted by plans that cannot be effectively reused.&lt;/p&gt;  &lt;p&gt;The following query can help on this:&lt;/p&gt;  &lt;p&gt;&lt;font size="2" face="Courier New"&gt;with cte as (     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; select       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reused = case when usecounts &amp;gt; 1 then 'reused_plan_mb' else 'not_reused_plan_mb' end,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; size_in_bytes,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cacheobjtype,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; objtype      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; from       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sys.dm_exec_cached_plans      &lt;br /&gt;), cte2 as      &lt;br /&gt;(      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; select       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reused,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; objtype,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cacheobjtype,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; size_in_mb = sum(size_in_bytes / 1024. / 1024.)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; from       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cte       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; group by       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reused, cacheobjtype, objtype      &lt;br /&gt;), cte3 as      &lt;br /&gt;(      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; select      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; *      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; from      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cte2 c      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; pivot       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ( sum(size_in_mb) for reused in ([reused_plan_mb], [not_reused_plan_mb])) p      &lt;br /&gt;)      &lt;br /&gt;select      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; objtype, cacheobjtype, [reused_plan_mb] = sum([reused_plan_mb]), [not_reused_plan_mb] = sum([not_reused_plan_mb])      &lt;br /&gt;from      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; cte3      &lt;br /&gt;group by      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; objtype, cacheobjtype      &lt;br /&gt;with rollup      &lt;br /&gt;having      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; (objtype is null and cacheobjtype is null) or (objtype is not null and cacheobjtype is not null)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The result is something like this:&lt;/p&gt;  &lt;p&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="image" border="0" alt="image" src="http://sqlblog.com/blogs/davide_mauri/image_578A81C8.png" width="418" height="233" /&gt; &lt;/p&gt;  &lt;p&gt;as you can see this server (SQL Server 2005 SP2) is using near 1.8GB of memory for Plan Caching and one third is memory that contains plans that are never reused (column usecount = 1 as reported by DMV &lt;em&gt;sys.dm_exec_cached_plans&lt;/em&gt;)&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Monitoring the result of this query can also show how much impact the usage of the new (SQL Server 2008) “&lt;a href="http://msdn.microsoft.com/en-us/library/cc645587.aspx"&gt;Optimize for ad-hoc workload&lt;/a&gt;” option can have on a system.&lt;/p&gt;  &lt;p&gt;PS&lt;/p&gt;  &lt;p&gt;I’ve also updated my &lt;em&gt;sys2dmv&lt;/em&gt; project on CodePlex with this new script:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://sys2dmvs.codeplex.com/" href="http://sys2dmvs.codeplex.com/"&gt;http://sys2dmvs.codeplex.com/&lt;/a&gt;&lt;/p&gt;</description></item><item><title>PowerShell and Extended Properties</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/07/20/powershell-and-extended-properties.aspx</link><pubDate>Tue, 20 Jul 2010 12:43:33 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:27176</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="font-family:Calibri;font-size:small;"&gt;I use Extended Properties on databases and their objects all the time. They are a great way to include information about the object &amp;ndash; I use them for versioning the database, detailing what a column is used for and so on. They can be a little tricky to set, but it&amp;rsquo;s really not bad once you learn how.&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;Ken Simmons, a SQL Server MVP has a fantastic article here that explains more: &lt;/span&gt;&lt;a href="http://cybersql.blogspot.com/2010/07/extended-property-awesomeness.html"&gt;&lt;span style="font-family:Times New Roman;color:#0000ff;font-size:small;"&gt;http://cybersql.blogspot.com/2010/07/extended-property-awesomeness.html&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Calibri;"&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family:Calibri;font-size:small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Calibri;"&gt;Although it&amp;rsquo;s not as simple as I would like to set the properties using PowerShell, it&amp;rsquo;s really trivial to read them. I do this to make system documentation in an HTML page. Open a SQL Server 2008 Powershell provider (sqlps.exe) and CD to any &amp;ldquo;directory&amp;rdquo; of a database or tables, views, whatever. Then you can just type this: &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent:0.5in;margin:0in 0in 0pt;"&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&lt;span style="color:#00b050;"&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Calibri;"&gt;DIR | select-object &amp;ndash;property name, Extendedproperties&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent:0.5in;margin:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family:Calibri;font-size:small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Calibri;"&gt;As always, do this on a test system.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family:Calibri;font-size:small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;"&gt;&lt;span style="font-family:Calibri;"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:Calibri;mso-bidi-font-size:11.0pt;mso-ansi-language:EN;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:Calibri;mso-ansi-language:EN;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin:0in 0in 0pt;"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:Calibri;mso-ansi-language:EN;"&gt;&lt;span style="font-family:Calibri;"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. Yes, there are always multiple ways to do things, and this script may not work in every situation, for everything. It&amp;rsquo;s just a script, people. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea&amp;nbsp;during the operation of this script, see a physician immediately.&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;span style="font-family:'Times New Roman','serif';font-size:12pt;mso-ansi-language:EN;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Check Script</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/06/30/check-script.aspx</link><pubDate>Wed, 30 Jun 2010 14:10:04 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26606</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;Someone contacted me yesterday and said they were getting blocked when they tried to create a FileStream data column type. On investigation, I found they were Mirroring that database – and the two aren’t compatible.&lt;/p&gt;  &lt;p&gt;Which got me to thinking – it’s probably a good idea to make a “check script” as you investigate tacking on a new feature, column type and so on to ensure that they are compatible with what you want to do. For instance, in the case of the client that contacted me, I just ran this script as part of the troubleshooting process: &lt;/p&gt; &lt;font color="#0000ff"&gt;   &lt;p&gt;SELECT&lt;/p&gt;   &lt;font color="#000000"&gt; &lt;/font&gt;&lt;font color="#808080"&gt;*&lt;/font&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff"&gt;   &lt;p&gt;FROM&lt;/p&gt;   &lt;font color="#000000"&gt; &lt;/font&gt;&lt;font color="#008000"&gt;sys&lt;/font&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;font color="#008000"&gt;database_mirroring&lt;/font&gt;&lt;font color="#808080"&gt;;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff"&gt;   &lt;p&gt;GO&lt;/p&gt;    &lt;p&gt;&lt;/p&gt; You could easily add logic there to test for a partner and report on the process before you tried to implement the change. That’s pretty trivial – but how about creating a “check script” each time you investigate something like this, and put it in your library of scripts? That way you don’t have to recreate the wheel each time you want to make a change in a new system. &lt;/font&gt;  &lt;p&gt;Very important – these scripts will change as new releases and service packs come out, so make sure you enter that as comments or even better as a test condition using CASE or other statements.&lt;/p&gt;</description></item><item><title>Create and Track Your Own License Keys with PowerShell</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/04/15/create-and-track-your-own-license-keys-with-powershell.aspx</link><pubDate>Thu, 15 Apr 2010 13:10:45 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:24316</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;SQL Server used to have&amp;#160; cool little tool that would let you track your licenses. Microsoft didn’t use it to limit your system or anything, it was just a place on the server where you could put that this system used this license key. I miss those days – we don’t track that any more, and I want to make sure I’m up to date on my licensing, so I made my own.&lt;/p&gt;  &lt;p&gt;Now, there are a LOT of ways you could do this. You could add an extended property in SQL Server, add a table to a tracking database, use a text file, track it somewhere else, whatever. This is just the route I chose; if you want to use some other method, feel free. Just sharing here.&lt;/p&gt;  &lt;p&gt;&lt;font color="#ff0000"&gt;&lt;strong&gt;Warning&lt;/strong&gt; Serious problems might occur if you modify the registry incorrectly by using Registry Editor or by using another method. These problems &lt;strong&gt;might require that you reinstall the operating system&lt;/strong&gt;. Microsoft cannot guarantee that these problems can be solved. Modify the registry &lt;em&gt;&lt;u&gt;at your own risk&lt;/u&gt;&lt;/em&gt;.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;And this is REALLY important. I include a disclaimer at the end of my scripts, but in this case you’re modifying your registry, and that could be EXTREMELY dangerous – only do this on a test server – and I’m just showing you how I did mine. It isn’t an endorsement or anything like that, and this is a “Buck Woody” thing, NOT a Microsoft thing. &lt;a href="http://support.microsoft.com/kb/256986" target="_blank"&gt;See this link first&lt;/a&gt;, and then you can read on.&lt;/p&gt;  &lt;p&gt;OK, here’s my script:&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:navy;font-size:11pt;"&gt;# Track your own licenses&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:navy;font-size:11pt;"&gt;# Write a New Key to be the License Location&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;mkdir HKCU:\SOFTWARE\Buck&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;&amp;#160;&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:navy;font-size:11pt;"&gt;# Write the variables - one sets the type, the other sets the number, and the last one holds the key &lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;New-ItemProperty HKCU:\SOFTWARE\Buck -name &amp;quot;SQLServerLicenseType&amp;quot; -value &amp;quot;Processor&amp;quot;&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:navy;font-size:11pt;"&gt;# Notice the Dword value here - this one is a number so it needs that. Keep this on one line!&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;New-ItemProperty HKCU:\SOFTWARE\Buck -name &amp;quot;SQLServerLicenseNumber&amp;quot; -propertytype DWord -value 4&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;New-ItemProperty HKCU:\SOFTWARE\Buck -name &amp;quot;SQLServerLicenseKey&amp;quot; -value &amp;quot;ABCD1234&amp;quot;&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;&amp;#160;&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:navy;font-size:11pt;"&gt;# Read them all&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;$LicenseKey = Get-Item HKCU:\Software\Buck&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;$Licenses = Get-ItemProperty $LicenseKey.PSPath&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;foreach ($License in $LicenseKey.Property) { $License + &amp;quot;=&amp;quot; + $Licenses.$License }&lt;/p&gt;  &lt;p style="margin:0in;font-family:calibri;color:green;font-size:11pt;"&gt;&amp;#160;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;" class="MsoNormal"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;" class="MsoNormal"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;         &lt;p&gt;&lt;/p&gt;       &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. Yes, there are always multiple ways to do things, and this script may not work in every situation, for everything. It’s just a script, people. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea during the operation of this script, see a physician immediately.&lt;/span&gt;&lt;/i&gt;&lt;span style="font-family:'Times New Roman','serif';font-size:12pt;mso-ansi-language:en;"&gt;     &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;</description></item><item><title>Rejuvenated: Script Creates and Drops for Candidate Keys and Referencing Foreign Keys</title><link>http://sqlblog.com/blogs/adam_machanic/archive/2010/04/04/rejuvinated-script-creates-and-drops-for-candidate-keys-and-referencing-foreign-keys.aspx</link><pubDate>Mon, 05 Apr 2010 02:48:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:24043</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Once upon a time it was 2004, and I wrote what I have to say was &lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/script-out-pks-unique-constraints-and-referencing-fks.aspx"&gt;a pretty cool little script&lt;/a&gt;. (Yes, I know the post is dated 2006, but that's because I dropped the ball and failed to back-date the posts when I moved them over here from my prior blog space.)&lt;/p&gt;&lt;p&gt;The impetus for creating this script was (and is) simple: Changing keys can be a painful experience. Sometimes you want to make a clustered key nonclustered, or a nonclustered key clustered. Or maybe you want to add a column to the key. Or remove a column. Or change a data type. Whatever you want to do, you need to remember to drop all of the referencing keys, drop the key you want to change, then put everything back the way it was.&lt;/p&gt;&lt;p&gt;... well, that's not quite true. I was talking with &lt;a href="http://sqlskills.com/blogs/kimberly/"&gt;Kimberly Tripp&lt;/a&gt; this week and she happened to mention that she was working on a script for the same scenario I was once concerned with. Her script is based on the idea that you don't have to drop the constraints--you can just disable them. I thought back to my own script, and decided to give it a much-needed major update. But I decided that if Kimberly is working on a version based on disabling constraints, I'll leave mine as-is. Then you can use either script--or both--depending on your scenario. If you're just changing a clustering key, Kimberly's may be easier to work with. If you're moving columns in or out of a key, my script will probably get you there a bit faster. More tools means better options! Watch her blog for her version, which should be released shortly.&lt;br&gt;&lt;/p&gt;&lt;p&gt;So what have I updated in my own script?&lt;/p&gt;&lt;ul&gt;&lt;li&gt;First of all, the new version is written entirely against the SQL Server 2005+ catalog views. I tried writing the original against INFORMATION_SCHEMA with some goal of cross-compatibility in mind and failed miserably in that pursuit, even then. I've since given up hope of ever writing code that will run (properly) on multiple DBMSs, so it's catalog views all the way from here on out.&lt;/li&gt;&lt;li&gt;The old version only scripts those indexes that are actually declared as "constraints". But a unique index can act as a candidate key just as well as a unique constraint. And with the addition of SQL Server 2005's "included columns" feature, 
there are lots of great reasons to use a unique index instead of a 
constraint. So those are now scripted as well.&amp;nbsp;&lt;/li&gt;&lt;li&gt;The old version didn't properly handle system-named constraints. The new version tries to keep things consistent, scripting an explicit name only when one was specified at create time.&lt;/li&gt;&lt;li&gt;The old version didn't handle the various index options such as fill factor, padding, etc, that were around in SQL Server 2000, let alone the newer features added in SQL Server 2005 and 2008. The new version handles all of them: fill factor, padding, ignore duplicate keys, allow row or page locks, and data compression are all supported. &lt;br&gt;&lt;/li&gt;&lt;li&gt;Partitioning? Check--in the new version.&lt;/li&gt;&lt;li&gt;Disabled constraints? The old version may have scripted them in some cases. The new version ignores them, and rightfully so.&lt;/li&gt;&lt;li&gt;The old version didn't properly quote the table names. Oops! Fixed.&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;There are a few areas that I'm still not touching: XML and FTS indexes, as well as other less-used features that can make the script not work quite as intended. In most cases should you be using these features the drops will simply fail, so you won't wind up with too much of a mess on your hands. But as always, test carefully before using this script. I'll happily fix any bugs you report, but I provide this with absolutely no warranty or guarantee.&lt;/p&gt;&lt;p&gt;To use the script, simply put SSMS into "results in text" mode, specify your target table, and hit F5, CTRL-E, or the Play button. The result should be a nicely-formatted script containing all of the drops and creates, in the correct order. (Foreign keys dropped first, then nonclustered indexes, then the clustered index, if appropriate. Creates are scripted in the reverse order.)&lt;/p&gt;&lt;p&gt;&lt;b&gt;The script is attached to the post&lt;/b&gt;. Download, take a look, and let me know if you encounter any issues. Or if you wish to lavish me with praise.&lt;/p&gt;&lt;p&gt;Enjoy! &lt;br&gt;&lt;/p&gt;&lt;p&gt;Note: Don't forget to configure the maximum text size in SSMS before using. The default is 256 characters--not enough for many cases. To configure, use the following sequence:&lt;br&gt;&lt;/p&gt;&lt;blockquote&gt;Tools-&amp;gt;Options-&amp;gt;Query Results-&amp;gt;Results to Text-&amp;gt;Maximum number of characters-&amp;gt;8192&lt;/blockquote&gt;&amp;nbsp;</description></item><item><title>List SQL Server Instances using the Registry</title><link>http://sqlblog.com/blogs/buck_woody/archive/2010/03/31/list-sql-server-instances-using-the-registry.aspx</link><pubDate>Wed, 31 Mar 2010 13:41:24 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:23906</guid><dc:creator>BuckWoody</dc:creator><description>&lt;p&gt;I read &lt;a href="http://thepowershellguy.com/blogs/posh/archive/2007/06/20/remote-registry-access-and-creating-new-registry-values-with-powershell.aspx" target="_blank"&gt;this interesting article on using PowerShell and the registry&lt;/a&gt;, and thought I would modify his information a bit to list the SQL Server Instances on a box. The interesting thing about listing instances this was is that you can touch remote machines, find the instances when they are off and so on. Anyway, here’s the scriptlet I used to find the Instances on my system:&lt;/p&gt;  &lt;p&gt;$MachineName = '.'&lt;/p&gt;  &lt;p&gt;$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $MachineName)&lt;/p&gt;  &lt;p&gt;$regKey= $reg.OpenSubKey(&amp;quot;SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL&amp;quot; )&lt;/p&gt;  &lt;p&gt;$regkey.GetValueNames()&lt;/p&gt;  &lt;p&gt;You can read more of his article to find out the reason for the remote registry call and so forth – there are also security implications here for being able to read the registry.&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;" class="MsoNormal"&gt;&lt;font face="Calibri"&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-size:11.0pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;Script Disclaimer, for people who need to be told this sort of thing: &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;b&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;           &lt;p&gt;&lt;/p&gt;         &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p style="margin:0in 0in 0pt;" class="MsoNormal"&gt;&lt;i style="mso-bidi-font-style:normal;"&gt;&lt;span style="color:maroon;font-size:10pt;mso-bidi-font-family:calibri;mso-ansi-language:en;"&gt;&lt;font face="Calibri"&gt;Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. Yes, there are always multiple ways to do things, and this script may not work in every situation, for everything. It’s just a script, people. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or diarrhea during the operation of this script, see a physician immediately.&lt;/font&gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;</description></item></channel></rss>