<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblogcasts.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Martin Croft Musings on SQL Server</title><subtitle type="html" /><id>http://sqlblogcasts.com/blogs/martincroft/atom.aspx</id><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/martincroft/default.aspx" /><link rel="self" type="application/atom+xml" href="http://sqlblogcasts.com/blogs/martincroft/atom.aspx" /><generator uri="http://communityserver.org" version="3.1.20917.1142">Community Server</generator><updated>2010-10-31T07:21:00Z</updated><entry><title>Identity Columns- How close to the edge are you?</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/09/identity-columns-how-close-to-the-edge-are-you.aspx" /><id>http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/09/identity-columns-how-close-to-the-edge-are-you.aspx</id><published>2011-05-09T20:27:00Z</published><updated>2011-05-09T20:27:00Z</updated><content type="html">&lt;p&gt;Day 9. How close is your data to the edge! Integer identity columns that increment by 1, have been known to run out of numbers. I have seen it occur on a few occasions on highly transactional systems. However there is really no excuse for getting caught out. The folwoing code easily shows you were your identity table columns are.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:450px;padding-right:5px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&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;span style="color:#008000;"&gt;--2005&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; SCHEMA_NAME(schema_id) &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; [&lt;span style="color:#0000ff;"&gt;Schema&lt;/span&gt;],name &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; [&lt;span style="color:#0000ff;"&gt;Table&lt;/span&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;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; sys.tables
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; OBJECTPROPERTY(object_id,&amp;#39;&lt;span style="color:#8b0000;"&gt;TableHasIdentity&lt;/span&gt;&amp;#39;) = 1
&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;span style="color:#0000ff;"&gt;ORDER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;BY&lt;/span&gt; [&lt;span style="color:#0000ff;"&gt;Schema&lt;/span&gt;], [&lt;span style="color:#0000ff;"&gt;Table&lt;/span&gt;];
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span style="color:#008000;"&gt;--2000&lt;/span&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;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; Name &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; sysobjects 
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; OBJECTPROPERTY(id,&amp;#39;&lt;span style="color:#8b0000;"&gt;TableHasIdentity&lt;/span&gt;&amp;#39;) = 1&lt;/pre&gt;&lt;/pre&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;The code below will show all your tables that have an identity seed on them and how full they are percentage wise. The trick is to catch them before they hit 100% and bring down the database as it can&amp;#39;t insert any more rows!.  &lt;/p&gt;&lt;p&gt;There are several options to fix, change the data to bigint, reseed the values ( if you don’t keep all the data such as archiving and you don’t need a unique ID) to a lower value,&amp;nbsp; or even start on a negative identity number to double the capacity!, but somehow that seems wrong. If it had been planned correctly it would already be the right data type!. Hey these thing happen when you inherit systems  &lt;/p&gt;&lt;p&gt;Exact number data types that use integer data.  &lt;/p&gt;&lt;p&gt;&lt;b&gt;bigint&lt;/b&gt;  &lt;/p&gt;&lt;p&gt;Integer (whole number) data from -2^63 (-9223372036854775808) through 2^63-1 (9223372036854775807). Storage size is 8 bytes.  &lt;/p&gt;&lt;p&gt;&lt;b&gt;int&lt;/b&gt;  &lt;/p&gt;&lt;p&gt;Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647). Storage size is 4 bytes. The SQL-92 synonym for int is integer.  &lt;/p&gt;&lt;p&gt;&lt;b&gt;smallint&lt;/b&gt;  &lt;/p&gt;&lt;p&gt;Integer data from -2^15 (-32,768) through 2^15 - 1 (32,767). Storage size is 2 bytes.  &lt;/p&gt;&lt;p&gt;&lt;b&gt;tinyint&lt;/b&gt;  &lt;/p&gt;&lt;p&gt;Integer data from 0 through 255. Storage size is 1 byte. &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;&lt;span style="color:#0000ff;"&gt;&lt;/span&gt; SQL 2000 code 
&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;span style="color:#0000ff;"&gt;SELECT&lt;/span&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;    QUOTENAME(USER_NAME(t.uid))+&amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39;+QUOTENAME(t.name)&lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; TableName,
&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;    c.name &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; ColumnName,
&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;span style="color:#0000ff;"&gt;CASE&lt;/span&gt; c.xtype
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 127 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &amp;#39;&lt;span style="color:#8b0000;"&gt;bigint&lt;/span&gt;&amp;#39;
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 56 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &amp;#39;&lt;span style="color:#8b0000;"&gt;int&lt;/span&gt;&amp;#39;
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 52 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &amp;#39;&lt;span style="color:#8b0000;"&gt;smallint&lt;/span&gt;&amp;#39;
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 48 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &amp;#39;&lt;span style="color:#8b0000;"&gt;tinyint&lt;/span&gt;&amp;#39;
&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;span style="color:#0000ff;"&gt;END&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &amp;#39;&lt;span style="color:#8b0000;"&gt;DataType&lt;/span&gt;&amp;#39;,
&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;    IDENT_CURRENT(USER_NAME(t.uid)+&amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39;+ t.name) &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; CurrentIdentityValue,
&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;span style="color:#0000ff;"&gt;CASE&lt;/span&gt; c.xtype
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 127 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; (IDENT_CURRENT(USER_NAME(t.uid)+&amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39;+ t.name)* 100.)/ 9223372036854775807
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 56 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; (IDENT_CURRENT(USER_NAME(t.uid)+&amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39;+ t.name)* 100.)/ 2147483647
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 52 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; (IDENT_CURRENT(USER_NAME(t.uid)+&amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39;+ t.name)* 100.)/ 32767
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 48 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; (IDENT_CURRENT(USER_NAME(t.uid)+&amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39;+ t.name)* 100.)/ 255
&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;span style="color:#0000ff;"&gt;END&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&amp;#39;&lt;span style="color:#8b0000;"&gt;PercentageUsed&lt;/span&gt;&amp;#39;
&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;span style="color:#0000ff;"&gt;FROM&lt;/span&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;    syscolumns &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; c
&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;span style="color:#0000ff;"&gt;INNER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&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;    sysobjects &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; t &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; t.id = c.id
&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;span style="color:#0000ff;"&gt;WHERE&lt;/span&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;    COLUMNPROPERTY(t.id, c.name,&amp;#39;&lt;span style="color:#8b0000;"&gt;isIdentity&lt;/span&gt;&amp;#39;)= 1
&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;span style="color:#0000ff;"&gt;AND&lt;/span&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;    OBJECTPROPERTY(t.id,&amp;#39;&lt;span style="color:#8b0000;"&gt;isTable&lt;/span&gt;&amp;#39;)= 1
&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;span style="color:#0000ff;"&gt;ORDER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;BY&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    PercentageUsed &lt;span&gt;DESC&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&amp;nbsp;&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;span style="color:#0000ff;"&gt;&lt;/span&gt; SQL  2005/2008 
&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;span style="color:#0000ff;"&gt;SELECT&lt;/span&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;    QUOTENAME(SCHEMA_NAME(t.schema_id))+&amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39;+ QUOTENAME(t.name)&lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; TableName,
&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;    c.name &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; ColumnName,
&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;span style="color:#0000ff;"&gt;CASE&lt;/span&gt; c.system_type_id
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 127 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &amp;#39;&lt;span style="color:#8b0000;"&gt;bigint&lt;/span&gt;&amp;#39;
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 56 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &amp;#39;&lt;span style="color:#8b0000;"&gt;int&lt;/span&gt;&amp;#39;
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 52 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &amp;#39;&lt;span style="color:#8b0000;"&gt;smallint&lt;/span&gt;&amp;#39;
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 48 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &amp;#39;&lt;span style="color:#8b0000;"&gt;tinyint&lt;/span&gt;&amp;#39;
&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;span style="color:#0000ff;"&gt;END&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&amp;#39;&lt;span style="color:#8b0000;"&gt;DataType&lt;/span&gt;&amp;#39;,
&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;    IDENT_CURRENT(SCHEMA_NAME(t.schema_id)+&amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39;+ t.name) &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; CurrentIdentityValue,
&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;span style="color:#0000ff;"&gt;CASE&lt;/span&gt; c.system_type_id
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 127 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + &amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39; + t.name)* 100.)/ 9223372036854775807
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 56 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + &amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39; + t.name) * 100.) / 2147483647
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 52 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + &amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39; + t.name)* 100.)/ 32767
&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;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; 48 &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + &amp;#39;&lt;span style="color:#8b0000;"&gt;.&lt;/span&gt;&amp;#39; + t.name)* 100.) / 255
&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;span style="color:#0000ff;"&gt;END&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&amp;#39;&lt;span style="color:#8b0000;"&gt;PercentageUsed&lt;/span&gt;&amp;#39;,
&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;span style="color:#0000ff;"&gt;CAST&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;CAST&lt;/span&gt;(GETDATE()&lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;VARCHAR&lt;/span&gt;(12))&lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;datetime&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; ReportTime
&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;span style="color:#0000ff;"&gt;FROM&lt;/span&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;    sys.columns &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; c
&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;span style="color:#0000ff;"&gt;INNER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&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;    sys.tables &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; t &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; t.[object_id] = c.[object_id]
&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;span style="color:#0000ff;"&gt;WHERE&lt;/span&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;    c.is_identity = 1
&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;span style="color:#0000ff;"&gt;ORDER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;BY&lt;/span&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;    PercentageUsed &lt;span style="color:#0000ff;"&gt;DESC&lt;/span&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;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15610" width="1" height="1"&gt;</content><author><name>MartinCroft</name><uri>http://sqlblogcasts.com/members/MartinCroft.aspx</uri></author><category term="IDENTITY" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/IDENTITY/default.aspx" /><category term="SQL 2008" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/SQL+2008/default.aspx" /><category term="SQL2005" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/SQL2005/default.aspx" /><category term="SQL2000" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/SQL2000/default.aspx" /></entry><entry><title>TempDB–What size was it?</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/08/tempdb-what-size-was-it.aspx" /><id>http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/08/tempdb-what-size-was-it.aspx</id><published>2011-05-08T16:04:00Z</published><updated>2011-05-08T16:04:00Z</updated><content type="html">&lt;p&gt;Day 8. As you might know TempDB is created every time SQL server service is restarted, but do you know what size it will start at next time you start SQL? Well this script shows you, its amazing how many production servers don’t get sized from the default when SQL was installed and its quite suspiring just how small that is, 8Mg!, So if you have an 8Gb data file set to 10% growth, guess how many times it takes 8Mg to get to 8Gb growing by 10% ( Lots), which can cause performance problems.&lt;/p&gt;  &lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="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="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;  alt.filename&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;  ,alt.name&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;  ,alt.&lt;span&gt;size&lt;/span&gt; * 8.0 / 1024.0 &lt;span&gt;as&lt;/span&gt; originalsize_MB&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;  ,files.&lt;span&gt;size&lt;/span&gt;* 8.0 / 1024.0 &lt;span&gt;as&lt;/span&gt; currentsize_MB&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;FROM&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;  master.dbo.sysaltfiles  alt &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;INNER&lt;/span&gt; &lt;span&gt;JOIN&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;  tempdb.dbo.sysfiles files &lt;span&gt;ON&lt;/span&gt; alt.fileid = files.fileid&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;WHERE&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;  dbid = db_id(&amp;#39;&lt;span&gt;tempdb&lt;/span&gt;&amp;#39;)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="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;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15606" width="1" height="1"&gt;</content><author><name>MartinCroft</name><uri>http://sqlblogcasts.com/members/MartinCroft.aspx</uri></author><category term="TempDB" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/TempDB/default.aspx" /></entry><entry><title>Merge–under used statement</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/07/merge-under-used-statement.aspx" /><id>http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/07/merge-under-used-statement.aspx</id><published>2011-05-07T15:54:00Z</published><updated>2011-05-07T15:54:00Z</updated><content type="html">&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Day 7. The MERGE statement is one that quite a few DBA are aware of and use but is under used by developers. I think because they are not aware of all the scenarios that it can be used in. I recently had a variety of scenarios that developers had asked my opinion on , or a review of some code and found its perfect to use MERGE. I put together some examples that show all the different things that can be done with MERGE from the obvious to the more specialist situations.&lt;/p&gt;  &lt;p&gt;This example uses AdventureWorks and also shows some other lesser used bits of T-SQL. The full script can be found &lt;a href="http://sqlblogcasts.com/blogs/martincroft/scripts/MERGE.txt"&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The code to generate the example tables &lt;/p&gt;  &lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:450px;padding-right:5px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&gt;&lt;pre style="background-color:#fbfbfb;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;--=====================================================================================
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;--Populate dummy data &lt;span style="color:#0000ff;"&gt;for&lt;/span&gt; example using CROSS &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt; to generate multiple rows
&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;span style="color:#0000ff;"&gt;and&lt;/span&gt; Row_number to create unique customer numbers 
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;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;&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;INSERT INTO CustomersCache (Name,City,AnnualEarnings) 
&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;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; 
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	Name + &amp;#39; &amp;#39; + CAST(ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Name )AS VARCHAR(3)) ,City,10000
&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;FROM
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;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;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; TOP 10
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;      [Name]
&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; FROM 
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	[AdventureWorks].[Sales].[Store] 
&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;)a
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;CROSS &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&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 style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; DISTINCT TOP 30 
&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;	[City]
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;FROM 
&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;	[AdventureWorks].[Person].[Address]
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;)b 
&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;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;populate script using a cross join to simplify creating dummy data. Its very easy to populate lots of rows using a cross join, but I wanted them to be distinct so used&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name + &amp;#39; &amp;#39; + CAST(ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Name )AS VARCHAR(3))&lt;/p&gt;

&lt;p&gt;to give me a unique row &lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/unique_rows_01339734.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/unique_rows_thumb_7F82CB5F.jpg" 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="unique_rows" alt="unique_rows" border="0" height="244" width="193" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;So we have two tables &lt;/p&gt;

&lt;p&gt;CustomerCache and Customers. The cache table is populated and the customer table is empty.&lt;/p&gt;

&lt;p&gt;running the following basic MERGE statement&lt;/p&gt;

&lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:450px;padding-right:5px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&gt;&lt;pre style="background-color:#fbfbfb;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;--Simple MERGE statement any rows that dont exist &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; 
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;--TARGET table will be populated 
&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:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;MERGE Customers AS TARGET
&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;USING CustomersCache AS SOURCE  &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt;(TARGET.Name = SOURCE.NAME)
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;WHEN &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; MATCHED &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&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;INSERT (Name, City, [AnnualEarnings], ModifiedDate)
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;VALUES (Name, City, [AnnualEarnings], ModifiedDate);
&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;p&gt;The trigger shows we have inserts occurring and checking both tables we would find they are now in sync.&lt;/p&gt;

&lt;p&gt;Ok lets simulate a scenario where customer accounts no longer exist the easiest approach is to delete some rows out of the cache table&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:450px;padding-right:5px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;DELETE TOP(5) FROM CustomersCache
&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;OUTPUT deleted.*&lt;/pre&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/pre&gt;

&lt;p&gt;Using DELETE&amp;nbsp; TOP(5) we can just delete 5 rows without the need of a WHERE clause and using OUTPUT ( another under rated and used T-SQL function, I’ll blog about this in the next couple of weeks) we can see the rows its deleted.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/deletedrows_0C7CAB71.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/deletedrows_thumb_5D4ABFD9.jpg" 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="deletedrows" alt="deletedrows" border="0" height="93" width="244" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ok lets show how to deactivate the customers&lt;/p&gt;

&lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:450px;padding-right:5px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;MERGE Customers AS TARGET
&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;USING CustomersCache AS SOURCE  &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt;(TARGET.Name = SOURCE.NAME)
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;WHEN &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; MATCHED BY SOURCE &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&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;UPDATE &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; Active =0, ModifiedDate =GETDATE();
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;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;p&gt;So any rows that don’t match between source and destination are now set de-activated and the modified date updated. (shown below). The trigger shows updates occur&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/deactiverows_63919667.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/deactiverows_thumb_55DEDA5F.jpg" 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="deactiverows" alt="deactiverows" border="0" height="238" width="429" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, now lets delete another row and also modify some data. This time i’ll show how you can delete a random row using ORDER BY newiD() and OUTPUT again, notice the placement of the OUTPUT keyword, it comes after the DELETE statement and not at the end of the query.&lt;/p&gt;

&lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:450px;padding-right:5px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;--Delete a Random row from Source 
&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;DELETE TOP(1) CustomersCache
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;OUTPUT deleted.*
&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;WHERE Name = (&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; TOP 1 Name from CustomersCache ORDER BY newiD())
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;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;p&gt;And also add in a new company&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:449px;padding-right:5px;height:318px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;--&lt;span style="color:#0000ff;"&gt;Add&lt;/span&gt; New Company
&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;INSERT INTO CustomersCache
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  TOP 1
&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;#39;Martins New Company 1&amp;#39;,
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	CITY,
&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;	AnnualEarnings,
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	GETDATE() 
&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;FROM 
&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	CustomersCache&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;--ok lets amend some values as well. Random of course.&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;--The code below gives a random value between 1 and 1000,&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;-- also using the 2008 syntax += instead of a=a+1 can put a+=1&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&amp;nbsp;UPDATE 	TOP(1) CC&lt;span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SET&lt;/span&gt; AnnualEarnings+= (1+ CAST(1000*RAND()AS &lt;span&gt;INT&lt;/span&gt;)),&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    ModifiedDate =GETDATE()&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;OUTPUT &lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    deleted.Name,&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    deleted.AnnualEarnings As Before,&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;    inserted.AnnualEarnings AS After&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;FROM CustomersCache CC&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;WHERE ModifiedDate &lt;span&gt;IS&lt;/span&gt; NULL &lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Ok that just updates 1 row, before anyone pulls me I said add some values. If you add&lt;/p&gt;

&lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:450px;padding-right:5px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;G0
&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;50&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;at the end of the code and run it all, you get the code to ran 50 times, this is a very useful bit of code for generating lots of rows quickly, or dropping into someone code at the bottom of a script if they have left their machine unlocked &amp;quot;, only works in Management studio ! as its the batch separator &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;SELECT “I am an IDIOT”&lt;/p&gt;

&lt;p&gt;GO 5000&lt;/p&gt;

&lt;p&gt;you get the idea, not saying I would use the word idiot necessarily!. &lt;/p&gt;

&lt;p&gt;* Warning if you highlight a lot of statements and add a GO 50 it will execute THEM ALL 50 times, that why in my full script you wont find the GO 10, I add it when demonstrating each section *&lt;/p&gt;

&lt;p&gt;Right lets MERGE them into the Customers table ( Sorry couldn’t resist). I am going to use OUTPUT now with the MERGE statement so two cool functions together. However, you can’t use OUTPUT and MERGE if you have triggers on the table so I drop the triggers first.&lt;/p&gt;

&lt;p&gt;He’s the code that will Insert new rows, update and amendments and update the modified date and de-activate the rows that no longer exist.&lt;/p&gt;

&lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:450px;padding-right:5px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&gt;&lt;pre style="background-color:#fbfbfb;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;MERGE Customers AS TARGET
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;USING CustomersCache AS SOURCE  &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt;(TARGET.Name = SOURCE.NAME)
&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:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;WHEN &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; MATCHED &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&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;INSERT (Name, City, [AnnualEarnings], ModifiedDate)
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;VALUES (Name, City, [AnnualEarnings], ModifiedDate)
&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:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;WHEN MATCHED &lt;span style="color:#0000ff;"&gt;AND&lt;/span&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 style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	TARGET.[AnnualEarnings] &amp;lt;&amp;gt; SOURCE.[AnnualEarnings]
&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:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span style="color:#0000ff;"&gt;THEN&lt;/span&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;UPDATE
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; TARGET.[AnnualEarnings]=SOURCE.[AnnualEarnings],
&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;TARGET.ModifiedDate =GETDATE()
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;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;WHEN &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; MATCHED BY SOURCE &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;UPDATE &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; Active =0, ModifiedDate =GETDATE()
&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;OUTPUT $Action AS Action,
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	   Inserted.Custid,
&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;	   Inserted.Name,
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	   Deleted.ModifiedDate,
&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;	   Deleted.AnnualEarnings,
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	   Inserted.AnnualEarnings;
&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:#fbfbfb;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;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;With the OUTPUT and MERGE statement you can use $Action keyword that will allow you see see the changes. Which can be very useful if you want to do another action dependant on what occurred and UPDATE or a DELETE.&amp;nbsp; I did use this mechanism for a complex next customer / policy number scenario based on two table which I’ll look to blog about over the coming weeks.&lt;/p&gt;

&lt;p&gt;which outputs (umm) the following&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/outputAction_542E0E8B.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/outputAction_thumb_248FEFFF.jpg" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="outputAction" alt="outputAction" border="0" height="112" width="443" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conditional MERGE&lt;/p&gt;

&lt;p&gt;Ok, lets say we are interested in Auditing our table to make sure any changes we get to keep a history of, only updates not interested in deletes.&lt;/p&gt;

&lt;p&gt;We create an Auditing table and update 5 rows ( remember the warning) when this works running the entire script and that is because a GO precedes the statement we want to run 5 times! We are outputting again the $Action value. &lt;/p&gt;

&lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:450px;padding-right:5px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&gt;&lt;pre style="background-color:#fbfbfb;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;CREATE TABLE Auditing
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;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;	[AuditID] [smallint] IDENTITY(1,1) &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; NULL,
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	[Name] VARCHAR(50) &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; NULL,
&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;	[City] VARCHAR(30) &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; NULL,
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	[EarningsBefore] &lt;span style="color:#0000ff;"&gt;INT&lt;/span&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;	[EarningsAfter] &lt;span style="color:#0000ff;"&gt;INT&lt;/span&gt;, 
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	[ModifiedDate] [datetime]  NULL,
&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;	[Type] VARCHAR(10),	
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	Active BIT DEFAULT (1)
&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:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	GO
&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:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	--Update 5 records using GO 
&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;	UPDATE 	TOP(1) CC
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	&lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; AnnualEarnings+= (1+ CAST(1000*RAND()AS &lt;span style="color:#0000ff;"&gt;INT&lt;/span&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;		ModifiedDate =GETDATE()
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	OUTPUT deleted.Name,deleted.AnnualEarnings As Before,inserted.AnnualEarnings AS After
&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;	FROM CustomersCache CC
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	WHERE ModifiedDate &lt;span style="color:#0000ff;"&gt;IS&lt;/span&gt; NULL
&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;	GO 5
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;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;&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;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;	INSERT INTO Auditing(Name,City,[EarningsBefore],[EarningsAfter],ModifiedDate, Type)
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; Name,City,EarningsBefore,EarningsAfter,ModifiedDate, Action
&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;	FROM
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;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;	MERGE Customers AS TARGET
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	USING CustomersCache AS SOURCE  &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt;(TARGET.Name = SOURCE.NAME)
&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:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	WHEN &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; MATCHED &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&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;	INSERT (Name, City, [AnnualEarnings], ModifiedDate)
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	VALUES (Name, City, [AnnualEarnings], ModifiedDate)
&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:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	WHEN MATCHED &lt;span style="color:#0000ff;"&gt;AND&lt;/span&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 style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;		TARGET.[AnnualEarnings] &amp;lt;&amp;gt; SOURCE.[AnnualEarnings]
&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:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	&lt;span style="color:#0000ff;"&gt;THEN&lt;/span&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;	UPDATE
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	&lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; TARGET.[AnnualEarnings]=SOURCE.[AnnualEarnings],
&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;	TARGET.ModifiedDate =GETDATE()
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;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;	--Output chanegs to Auditing Table showing before &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; after picture 
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	OUTPUT $Action AS Action,
&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:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;		   Inserted.Name,
&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;		   Inserted.City,
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;		   Deleted.AnnualEarnings AS EarningsBefore,
&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;		   Inserted.AnnualEarnings AS EarningsAfter ,
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;		   Deleted.ModifiedDate
&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:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;) AS D
&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;WHERE Action = &amp;#39;UPDATE&amp;#39;;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt; and&amp;nbsp; Inserting into the Auditing table where the action is an update. The Target table is still getting update but we are capturing all the updates that occur.&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15605" width="1" height="1"&gt;</content><author><name>MartinCroft</name><uri>http://sqlblogcasts.com/members/MartinCroft.aspx</uri></author><category term="DELETE" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/DELETE/default.aspx" /><category term="MERGE" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/MERGE/default.aspx" /><category term="OUTPUT" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/OUTPUT/default.aspx" /><category term="Random Rows" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Random+Rows/default.aspx" /></entry><entry><title>Quick and Simple Deletes</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/06/quick-and-simple-deletes.aspx" /><id>http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/06/quick-and-simple-deletes.aspx</id><published>2011-05-06T17:45:00Z</published><updated>2011-05-06T17:45:00Z</updated><content type="html">&lt;p&gt;Day 6 and a Friday so keep it simple. There are occasions where you want to delete a huge table and truncation is just not an option for various reasons, but deleting from the table creates a huge transaction log. You don’t want to over engineer the solution so heres a simple example thats takes copy of the table and deleted in chunks of 1000 in this case, but you can use what ever suits .&lt;/p&gt;  &lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;--DELETES &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;USE AdventureWorks&lt;br /&gt;&lt;/pre&gt;&lt;pre style="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="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;IF&lt;/span&gt; EXISTS ( &lt;span&gt;SELECT&lt;/span&gt; 1 FROM sysobjects &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;WHERE name =&amp;#39;SalesOrderDetailCopy&amp;#39;) &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;DROP TABLE SalesOrderDetailCopy&lt;br /&gt;&lt;/pre&gt;&lt;pre style="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="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; * INTO SalesOrderDetailCopy &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;FROM Sales.SalesOrderDetail&lt;br /&gt;&lt;/pre&gt;&lt;pre style="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="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="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;WHILE&lt;/span&gt; 1=1&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;BEGIN&lt;br /&gt;&lt;/pre&gt;&lt;pre style="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="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	DELETE TOP(1000)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	FROM SalesOrderDetailCopy  &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;	&lt;span&gt;IF&lt;/span&gt; @@ROWCOUNT &amp;lt; 1000 BREAK&lt;br /&gt;&lt;/pre&gt;&lt;pre style="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="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;END&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="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="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="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="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; COUNT(*) &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;FROM SalesOrderDetailCopy&lt;/pre&gt;&lt;/pre&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15603" width="1" height="1"&gt;</content><author><name>MartinCroft</name><uri>http://sqlblogcasts.com/members/MartinCroft.aspx</uri></author><category term="SQL 2008" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/SQL+2008/default.aspx" /><category term="@@ROWCOUNT" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/_40004000_ROWCOUNT/default.aspx" /><category term="SQL2008" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/SQL2008/default.aspx" /><category term="DELETE" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/DELETE/default.aspx" /></entry><entry><title>Traffic lights in Reporting services</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/05/traffic-lights-in-reporting-services.aspx" /><id>http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/05/traffic-lights-in-reporting-services.aspx</id><published>2011-05-05T18:07:00Z</published><updated>2011-05-05T18:07:00Z</updated><content type="html">&lt;p&gt;Day 5 Pretty easy thing to setup, but can look very effective in a report, you can use gauges but I wanted fore some Health check reports to go for the old traffic light system. To help the Management at work to easily digest if their SQL servers are Critical (Red) Nearly there (Amber) or running ok (Green)&lt;/p&gt;  &lt;p&gt;First things first you need some traffic light images, preferably one with a red light on and a green one, etc I think you get the idea. (a link to the ones I used at bottom of the post)   &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;First in a new report we need to add the images we want going to use. Right click and select Add Image   &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/loadimage_3C897F07.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/loadimage_thumb_62EB6252.jpg" style="border-width:0px;background-image:none;padding-left:0px;padding-right:0px;display:block;float:none;padding-top:0px;" title="loadimage" alt="loadimage" border="0" height="199" width="215" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;browse to the location of the images held on your computer and add them in one by one.&lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/loadedimage_265363B5.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/loadedimage_thumb_257AFDCB.jpg" style="border-width:0px;background-image:none;padding-left:0px;padding-right:0px;display:block;float:none;padding-top:0px;" title="loadedimage" alt="loadedimage" border="0" height="244" width="207" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Here I have loaded 6 images as described above. I have called them the same as the colour of the traffic light to aid clarity.&lt;/p&gt;  &lt;p&gt;Add a table into the report design pane and drag in the red image, into the first column. The image properties box will open up as shown below.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/dropimage_44BDA49E.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/dropimage_thumb_09F5FBC8.jpg" style="border:0px none;background-image:none;padding-left:0px;padding-right:0px;display:block;float:none;padding-top:0px;" title="dropimage" alt="dropimage" border="0" height="363" width="598" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Click on the expression button next to use this image and paste in the following code&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/trafficlightcode_28CC6FA6.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/trafficlightcode_thumb_4DE9BA12.jpg" style="border-width:0px;background-image:none;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;" title="trafficlightcode" alt="trafficlightcode" border="0" height="452" width="494" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In the query from the database I am passing through the colour of the Alert in the Value column, so Red, Green or Amber. Which ever one it is, the image for that colours is displayed simple!&lt;/p&gt;  &lt;p&gt;So you then get the following based on the values passed in.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/trafficlights_6534BE83.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/trafficlights_thumb_71C26B9F.jpg" style="border:0px none;background-image:none;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;" title="trafficlights" alt="trafficlights" border="0" height="228" width="551" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;you can also expand on this with smaller images to give an instant view of what failed using the same principle.&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/minialerts_3766F5BE.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/minialerts_6D629600.jpg" style="border:0px none;background-image:none;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;" title="minialerts" alt="minialerts" border="0" height="120" width="244" /&gt;      &lt;br /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The images used can be accessed from &lt;a href="https://picasaweb.google.com/MysticMart/SqlServer?authkey=Gv1sRgCInc95b3nJqyVQ&amp;amp;feat=directlink"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15601" width="1" height="1"&gt;</content><author><name>MartinCroft</name><uri>http://sqlblogcasts.com/members/MartinCroft.aspx</uri></author><category term="Reporting Services" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Reporting+Services/default.aspx" /><category term="Healthchecks" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Healthchecks/default.aspx" /><category term="Traffic Lights" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Traffic+Lights/default.aspx" /></entry><entry><title>SQL Server Top 10 Queries</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/04/sql-server-top-10-queries.aspx" /><id>http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/04/sql-server-top-10-queries.aspx</id><published>2011-05-04T19:29:00Z</published><updated>2011-05-04T19:29:00Z</updated><content type="html">&lt;h4&gt;&lt;font style="font-weight:normal;" size="2"&gt;Day 4 and still rehashing some old post i needed to migrate from another site. In the garden burning wood and posting the latest blog when the bench collapsed around me .&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/bench_thumb_05E9F03C.jpg" style="border:0px none;background-image:none;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;" title="bench" alt="bench" border="0" height="239" width="357" /&gt;&lt;/p&gt;  &lt;p&gt;This is a list of the top 10 queries I use on a frequent basis that are relatively simple, either one liners or next to one liners. These are in reverse order from 10 through my favourite 1.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;10 Job Information      &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;You want to quickly access job information, you don’t want to have to trawl through joining MSDB.dbo.sysjobs , MSDB.sysjobsteps, just want a quick overview of what jobs are enabled/disabled or what job was updated yesterday? Or when it that job last run or next run &lt;/p&gt;  &lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;--Name,Enabled, description, owner, modeified date,etc&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;EXEC&lt;/span&gt; MSDB.dbo.sp_help_job&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;--Or specify Job_id and get step details, schedule times&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;EXEC&lt;/span&gt; MSDB.dbo.sp_help_job ’34DD4F82-423C-46E9-9E9A-BF7786′&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;9 Quick Search 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Trying to work out where a particular column or table is called in a procedure, you can check the dependency’s or another option is to just quicker check syscomments for the text you are after, there are several procedure out on the web that add a high degree of search ability but this is fast and easy&lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;USE&lt;/span&gt; NorthWind;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;      OBJECT_NAME(iD),&lt;span&gt;text&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;FROM&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;      syscomments&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;WHERE&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;      &lt;span&gt;Text&lt;/span&gt; &lt;span&gt;LIKE&lt;/span&gt; ‘%sales%’&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;8 Traces running 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;I have seen it before your running a trace, the trace hangs is it still running, how can it be you have closed the crashed profiler, always safer to check. or is big brother watching! one way to check! See BOL for the output to this function &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; * &lt;span&gt;FROM&lt;/span&gt; ::&lt;span&gt;fn_trace_getinfo&lt;/span&gt;(&lt;span&gt;default&lt;/span&gt;) &lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;7 Am I sysadmin 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Maybe not useful for everyone, but I have had uses for this multiple times, especially with a variety of access accounts and SQL2005 /2008 ability to switch environments quickly (right click and change connection). I find it useful to know if the account I am logged in has Sysadmin as a usual theme with SQL there are a various ways of finding this out, here is one I use. &lt;/p&gt;

&lt;p&gt;–System Admin 1 yes god like powers 0 no I can’t drop that database &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; is_srvrolemember(‘sysadmin’) [Sysadmin] &lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;6 System Uptime 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Is the system running like a dog? People start asking when was SQL rebooted, I.T’s magic wand lets reboot the server, when was SQL last started. There are several ways of finding out this information ( can use Top tip 5 as well!) but this is an easy approach. Basically see when SPID 1 logged in, also if you useDATEDIFF you can get SQL to tell you how many days, how many DBA’s can count I even use SELECT 10+20 to work out calculations, far too slow opening up calculator. &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;–Logintime &lt;span&gt;for&lt;/span&gt; SPID 1 &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; DATEDIFF(dd,login_time,Getdate())Uptime,Login_time &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;FROM&lt;/span&gt; master..sysprocesses &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;WHERE&lt;/span&gt; spid =1 &lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;5 Errorlogs 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Sometimes useful when evaluating an issue, the SQL error logs can be access from the object explorer, but can be quicker and especially if it’s a long log it open far quicker this way. &lt;/p&gt;

&lt;p&gt;–Read Error log takes Integer value for the error log number &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;exec master..xp_readerrorlog &lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Useful to find out a variety of information quickly, the log gets recycled when the server reboots, header shows were these logs are actually kept, version of SQL and a variety of message. If database are in recovery good place to look to get idea how long its going to take &lt;/p&gt;

&lt;p&gt;&lt;b&gt;4 Statistics 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Lifted directly from the pages of the SQL bible, or BOL as it known. Things are running pants trying to work out what has changed, how up to date are the statistics? This will tell you. &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;–STATS_DATE code from BOL &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;‘Index Name’= i.name,‘Statistics &lt;span&gt;Date&lt;/span&gt;’=STATS_DATE(i.id,i.indid) &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;FROM &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;sysobjects o &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;JOIN &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;sysindexes i &lt;span&gt;ON&lt;/span&gt; o.id = i.id &lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;I tend to use order by 2 DESC added onto the end to order by the tables that were last updated.&lt;b&gt; 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;3 Disk Space 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Another one of those problem solving procedures. Used quite frequently on development system, as you really should have no excuses for production systems running out of space, unless it is the log drive and something untoward has occurred. One of the first procedures run when a developer says my database restore won’t work, 90Gb doesn’t fit on 45Gb free space funnily enough! &lt;/p&gt;

&lt;p&gt;–List disk information, useful for those users filling logs! &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;EXEC Master.dbo.xp_fixeddrives &lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;2 Short Cut Keys 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Life savers when reviewing production incidents, how often you get “it’s not working” with little or no information, multiple systems that you’re unsure of the exact schema, so it is useful to know short cut keys. These are some of the ones I use daily. By assigning to short cuts you can specify, by highlighting SQL I can quickly pull up lots of useful info &lt;/p&gt;

&lt;p&gt;–Get the stored procedure text of system proc sp_who , just highlight sp_who &amp;amp; press Ctrl-F1 &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;USE &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;MASTER; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;GO &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;sp_helptext &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;sp_who &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;– &lt;span&gt;Get&lt;/span&gt; infomation &lt;span&gt;on&lt;/span&gt; tables , Highlight Region &amp;amp; Press ALT +F1 &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;USE NorthWind &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;GO &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;Region &lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;1 Quick Blocking 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;The piece of code that I probably use more than most, so simple but so helpful in times of crisis, which was basically many years ago ripped off from the system stoted procedure EXECsp_blockcnt, which basically just tells you the number of blocked processes on ther server. This querys can be written from memory with no need for fancy solutions on production boxes that your can’t role out code to. &lt;/p&gt;

&lt;p&gt;–Blocking processes &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;* &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;FROM &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;MASTER.dbo.sysprocesses &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;WHERE blocked &amp;lt;&amp;gt; 0&lt;br /&gt;&lt;/pre&gt;&lt;pre style="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;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15591" width="1" height="1"&gt;</content><author><name>MartinCroft</name><uri>http://sqlblogcasts.com/members/MartinCroft.aspx</uri></author><category term="Sysadmin" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Sysadmin/default.aspx" /><category term="Blocking" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Blocking/default.aspx" /><category term="Stats" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Stats/default.aspx" /><category term="Traces" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Traces/default.aspx" /><category term="Tips" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Tips/default.aspx" /><category term="Jobs" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Jobs/default.aspx" /><category term="Uptime" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Uptime/default.aspx" /><category term="Shortcuy Keys" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Shortcuy+Keys/default.aspx" /></entry><entry><title>PowerShell- SQLps</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/03/powershell.aspx" /><id>http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/03/powershell.aspx</id><published>2011-05-03T18:42:00Z</published><updated>2011-05-03T18:42:00Z</updated><content type="html">&lt;p&gt;Day 3, of a blog a day I was hoping for more time for this post but we’ll see how it goes. I looked through some of my Google Documents and found some interesting bits that I’d collected around PowerShell so i thought I’d put them in a blog for future reference and have a play around with it.&lt;/p&gt;  &lt;p&gt;and as it this SQL related I’ll stick to SQLps. It can be evoked from a command prompt or for those not of an older generation with the run command. SQLPS runs a slimmed down PowerShell session with the SQL Server PowerShell provider and cmdlets (more on these later)loaded and registered.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/sqlps_632F0BAE.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/sqlps_thumb_6831495D.jpg" style="border:0px none;background-image:none;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;" title="sqlps" alt="sqlps" border="0" height="233" width="466" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Console window after running SQLps and doing a Dir. &lt;/p&gt;  &lt;p&gt;PowerShell can be used to connect to SQL2008, SQL 2005 from SP2 and SQL 2000 from SP4&lt;/p&gt;  &lt;p&gt;SQL Server 2008 version of SQL Server Management Studio supports starting PowerShell sessions from the Object Explorer tree, &lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/sqlps-mangementstudio_20038D81.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/sqlps-mangementstudio_thumb_6A1E9866.jpg" style="border:0px none;background-image:none;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;" title="sqlps-mangementstudio" alt="sqlps-mangementstudio" border="0" height="244" width="231" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Right click database / table or at any level in object explorer and select PowerShell.&amp;nbsp; In the shot above this open’s a cmd line Powershell application within that database tables, and the ability to change to directory&amp;#39;s for a variety of objects&lt;/p&gt;  &lt;p&gt;Powershell scripts can also be executed through SQL Agent in SQL 2008.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;CmdLets&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;CmdLets are single function command, that can be quite powerful, however security is locked down out of the box and ironically to unlock it uses a CmdLet&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Security&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The OS wont run PowerShell scripts by default. Therefore we need to adjust the ExecutionPolicy to allow Powershell scripts to run. you set the ExecutionPolicy through a cmdlet .To get the current Execution policy run &lt;/p&gt;  &lt;p&gt;&lt;i&gt;get-ExecutionPolicy&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;running the following will give help on the cmdlet&lt;/p&gt;  &lt;p&gt;&lt;i&gt;set-ExecutionPolicy –?&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;S&lt;b&gt;hort cut keys&lt;/b&gt; &lt;/p&gt;  &lt;p&gt;It might be a good point to mention a few shortcut keys , if you typed in &lt;/p&gt;  &lt;p&gt;&lt;i&gt;set-ex &lt;/i&gt;&lt;/p&gt;  &lt;p&gt;and pressed tab, it would auto complete the word. If there was multiple possible combinations you could cycle through all possible options.&lt;/p&gt;  &lt;p&gt;F7 – Will show the history of last 50 commands in a popup window and you can cursor through the command ran.&lt;/p&gt;  &lt;p&gt;F8 - More functionality that arrow up, can search for specific command already ran e.g type in d(F8) and brings up command starting d, to get the next d command press F8 again. &lt;/p&gt;  &lt;p&gt;F9 – Allows entry of a command number &lt;/p&gt;  &lt;p&gt;There are multiple execution policies , the default being Restricted, which permits individual commands but wont run scripts.&amp;nbsp; More details can be found here &lt;i&gt;&lt;a href="http://technet.microsoft.com/en-us/library/dd347641.aspx"&gt;set-ExecutionPolicy –?&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;setting the execution level to remote signed will allow script to be execute.&lt;/p&gt;  &lt;p&gt;&lt;i&gt;set-ExecutionPolicy&amp;nbsp; RemoteSigned &lt;/i&gt;&lt;/p&gt;  &lt;p&gt;you might come across the following issue if running Vista / Windows 7&lt;/p&gt;  &lt;p&gt;&lt;i&gt;&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/sqlps-accessdenied_374E91F2.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/sqlps-accessdenied_thumb_1516866C.jpg" style="border:0px none;background-image:none;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;" title="sqlps-accessdenied" alt="sqlps-accessdenied" border="0" height="253" width="498" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;If this is the case, run SQLps as administrator, start run type SQLps and when it comes up right click and Run As Administrator,&amp;nbsp; once logged in run the set-executionpolicy and when opening back up next time it should be set.&lt;/p&gt;  &lt;p&gt;This was a brief forage into Powershell I’ll look to go more indepth in some future posts, I do have 31 days in May. This task is looking slightly ugly, like the looks the wife has been giving me.&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15580" width="1" height="1"&gt;</content><author><name>MartinCroft</name><uri>http://sqlblogcasts.com/members/MartinCroft.aspx</uri></author><category term="Powershell" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Powershell/default.aspx" /><category term="SQLps" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/SQLps/default.aspx" /></entry><entry><title>Maximum date in a row (not a column)</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/02/maximum-date-in-a-row.aspx" /><id>http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/02/maximum-date-in-a-row.aspx</id><published>2011-05-02T14:04:00Z</published><updated>2011-05-02T14:04:00Z</updated><content type="html">&lt;p&gt;Day 2 of a post every day ! I was asked recently how you could get the latest/earliest date field across a row of data, not the most usual request it tends to be the maximum value in a column and then its just a straight forward GROUP BY and MAX in that case. However&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/martincroft/maximumvalueinrow_5267586F.jpg"&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/maximumvalueinrow_thumb_351200A5.jpg" style="border-width:0px;background-image:none;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;" title="maximumvalueinrow" alt="maximumvalueinrow" border="0" height="88" width="451" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;given the example above the user wanted for each driver to take the minimum date of birth. the following solution could be used.&lt;/p&gt;  &lt;pre style="padding:5px;overflow:auto;min-height:40px;width:471px;height:781px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;USE&lt;/span&gt; tempdb&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;IF&lt;/span&gt;  &lt;span&gt;EXISTS&lt;/span&gt; (&lt;span&gt;SELECT&lt;/span&gt; * &lt;span&gt;FROM&lt;/span&gt; sys.objects &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;WHERE&lt;/span&gt; object_id = OBJECT_ID(N&amp;#39;&lt;span&gt;[dbo].[DriverDetails]&lt;/span&gt;&amp;#39;) &lt;span&gt;AND&lt;/span&gt; type &lt;span&gt;in&lt;/span&gt; (N&amp;#39;&lt;span&gt;U&lt;/span&gt;&amp;#39;))&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;DROP&lt;/span&gt; &lt;span&gt;TABLE&lt;/span&gt; [dbo].[DriverDetails]&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;CREATE&lt;/span&gt; &lt;span&gt;TABLE&lt;/span&gt; DriverDetails &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;( DriverID &lt;span&gt;INT&lt;/span&gt; &lt;span&gt;IDENTITY&lt;/span&gt;(1,1),&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;  DriverName &lt;span&gt;VARCHAR&lt;/span&gt;(20),&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;  Driver1DOB &lt;span&gt;DATETIME&lt;/span&gt;,&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;  Driver2DOB &lt;span&gt;DATETIME&lt;/span&gt;,&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;  Driver3DOB &lt;span&gt;DATETIME&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;)  &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;INSERT&lt;/span&gt; &lt;span&gt;INTO&lt;/span&gt; DriverDetails &lt;span&gt;VALUES&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;(&amp;#39;&lt;span&gt;Bob&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 May 1986&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 June 1986&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 April 1986&lt;/span&gt;&amp;#39;), &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;(&amp;#39;&lt;span&gt;Mary&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 May 1976&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 June 1946&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 April 1986&lt;/span&gt;&amp;#39;), &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;(&amp;#39;&lt;span&gt;John&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 May 1966&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 June 1956&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 April 1946&lt;/span&gt;&amp;#39;), &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;(&amp;#39;&lt;span&gt;Jenny&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 May 1956&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 June 1966&lt;/span&gt;&amp;#39;,&amp;#39;&lt;span&gt;01 April 1986&lt;/span&gt;&amp;#39;) &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;      *&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;FROM&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;      DriverDetails a&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;JOIN&lt;/span&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;(&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;--Unpivot the data and then do a MAX, MIN ON Driver&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; DriverID,&lt;span&gt;MIN&lt;/span&gt;(DOB)&lt;span&gt;AS&lt;/span&gt; MinDOB,&lt;span&gt;MAX&lt;/span&gt;(DOB) &lt;span&gt;AS&lt;/span&gt; MaxDOB&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;FROM&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;   (&lt;span&gt;SELECT&lt;/span&gt; DriverID,Driver1DOB,Driver2DOB,Driver3DOB&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;   &lt;span&gt;FROM&lt;/span&gt; DriverDetails) p&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;UNPIVOT&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;   (DOB &lt;span&gt;FOR&lt;/span&gt; Employee &lt;span&gt;IN&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;      (Driver1DOB,Driver2DOB,Driver3DOB)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;)&lt;span&gt;AS&lt;/span&gt; unpvt&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;GROUP&lt;/span&gt; &lt;span&gt;BY&lt;/span&gt; DriverID&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;) b&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;ON&lt;/span&gt; a.DriverID =b.DriverID;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="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="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;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15577" width="1" height="1"&gt;</content><author><name>MartinCroft</name><uri>http://sqlblogcasts.com/members/MartinCroft.aspx</uri></author><category term="SQL 2008" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/SQL+2008/default.aspx" /><category term="UnPivot" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/UnPivot/default.aspx" /></entry><entry><title>Blog a Day May - Productivity</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/01/blog-a-day-may-productivity.aspx" /><id>http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/01/blog-a-day-may-productivity.aspx</id><published>2011-05-01T16:19:00Z</published><updated>2011-05-01T16:19:00Z</updated><content type="html">&lt;p&gt;&lt;font size="2"&gt;Productivity&amp;nbsp; &lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;  &lt;/font&gt;&lt;p&gt;&lt;font size="2"&gt;I have decided after unsuccessfully not blogging once this year to blog each day in May. The simple premise is that I have got to blog at least once ( can&amp;#39;t see me doing it more than once but who knows) daily and be of useful content, maybe the first one can be excused till&amp;nbsp;I get back into the swing of things.&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;  &lt;/font&gt;&lt;p&gt;&lt;font size="2"&gt;In an attempt to get more productive I have come across several useful tools. &lt;/font&gt;&lt;a title="Drop Box" href="https://www.dropbox.com"&gt;&lt;font size="2"&gt;Drop box&lt;/font&gt;&lt;/a&gt;&lt;font size="2"&gt;&amp;nbsp; you can sign up for a free account and get 2Gb of free space, this allows you to add to every machine you use a folder that keeps all items in that folder in sync across all machines/ phones and in the cloud. So if I drop in a SQL script, word document its sync&amp;#39;d between all my machines and also I can access through my Android phone, also other smart phones are available!. If you get 8 friends to sign up (its free) you can get another 250Mg per friend up to another 8Gb, now it becomes useful unless its blocked through work!&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;  &lt;/font&gt;&lt;p&gt;&lt;font size="2"&gt;There is an action management system called Get things Done &lt;/font&gt;&lt;a title="GTD" href="http://en.wikipedia.org/wiki/Getting_Things_Done"&gt;&lt;font size="2"&gt;GTD&lt;/font&gt;&lt;/a&gt;&lt;font size="2"&gt;&amp;nbsp; which the basic principle of &lt;span style="text-transform:none;text-indent:0px;letter-spacing:normal;word-spacing:0px;white-space:normal;border-collapse:separate;orphans:2;widows:2;font-size-adjust:none;font-stretch:normal;" class="Apple-style-span"&gt;&lt;span style="line-height:19px;font-size:13px;" class="Apple-style-span"&gt;&lt;font face="Verdana"&gt;The Getting Things Done method rests on the principle that a person needs to move tasks out of the&lt;span class="Apple-converted-space"&gt; mind &lt;/span&gt;by recording them externally. That way, the mind is freed from the job of remembering everything that needs to be done, and can concentrate on actually performing those tasks. You set next physical actions which is where &lt;/font&gt;&lt;a title="Springpad" href="http://springpadit.com/how_it_works"&gt;&lt;font face="Verdana"&gt;Springpad&lt;/font&gt;&lt;/a&gt;&lt;font face="Verdana"&gt; comes in, this can be accessed through the web or an Android App and possibly the other one as well !.&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;  &lt;/font&gt;&lt;p&gt;&lt;span style="text-transform:none;text-indent:0px;letter-spacing:normal;word-spacing:0px;white-space:normal;border-collapse:separate;orphans:2;widows:2;font-size-adjust:none;font-stretch:normal;" class="Apple-style-span"&gt;&lt;span style="line-height:19px;font-size:13px;" class="Apple-style-span"&gt;&lt;font size="2" face="Verdana"&gt;One thing I have seen all over the show was delicious but never actually look into what it is &lt;/font&gt;&lt;a title="Delious" href="http://www.delicious.com/MartinCroft"&gt;&lt;font size="2" face="Verdana"&gt;Delicious&lt;/font&gt;&lt;/a&gt;&lt;font size="2" face="Verdana"&gt; , well Its a pretty cool tool for recording bookmarks and finding new and interesting resources. You can add tags to make finding bookmarks easier. You can add to the browser tool bar and click on &amp;quot;Bookmark on delicious &amp;quot; as your about to leave the office and pick it up again at home.&amp;nbsp; Another useful one is ( Android only) is &lt;/font&gt;&lt;a title="Chrome to Phone" href="https://chrome.google.com/extensions/detail/oadboiipflhobonjjffjbfekfjcgkhco"&gt;&lt;font size="2" face="Verdana"&gt;Chrome to Phone&lt;/font&gt;&lt;/a&gt;&lt;font size="2" face="Verdana"&gt; install it into Chrome and with a single click whatever webpage is on display its sent directly to your phone great as your dashing for the train.&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="2"&gt;  &lt;/font&gt;&lt;p&gt;&lt;font size="2"&gt;Anyway I should at least post something SQL related so here it is.&amp;nbsp; &lt;br /&gt;&lt;/font&gt;&lt;/p&gt;      &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;&lt;span style="color:green;font-family:consolas;font-size:9.5pt;"&gt;Retrieve the nth smallest or greatest number in a table&lt;/span&gt;&lt;span style="font-family:consolas;font-size:9.5pt;"&gt;        &lt;/span&gt;&lt;/p&gt;  &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;&lt;span style="color:green;font-family:consolas;font-size:9.5pt;"&gt;        &lt;/span&gt;&lt;/p&gt;  &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;&lt;span style="color:blue;font-family:consolas;font-size:9.5pt;"&gt;DECLARE&lt;/span&gt;&lt;span style="font-family:consolas;font-size:9.5pt;"&gt; &lt;span style="color:teal;"&gt;@item&lt;/span&gt; &lt;span style="color:blue;"&gt;TABLE &lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:teal;"&gt;ItemID&lt;/span&gt; &lt;span style="color:blue;"&gt;INT&lt;/span&gt; &lt;span style="color:blue;"&gt;IDENTITY&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;1&lt;span style="color:gray;"&gt;,&lt;/span&gt;1&lt;span style="color:gray;"&gt;),&lt;/span&gt; &lt;span style="color:teal;"&gt;n&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt; &lt;span style="color:gray;"&gt;)&lt;/span&gt;         &lt;/span&gt;&lt;/p&gt;  &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;&lt;span style="font-family:consolas;font-size:9.5pt;"&gt;        &lt;/span&gt;&lt;/p&gt;  &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;&lt;span style="color:green;font-family:consolas;font-size:9.5pt;"&gt;--Some randomish numbers&lt;/span&gt;&lt;span style="font-family:consolas;font-size:9.5pt;"&gt;        &lt;/span&gt;&lt;/p&gt;  &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;&lt;span style="color:blue;font-family:consolas;font-size:9.5pt;"&gt;INSERT&lt;/span&gt;&lt;span style="font-family:consolas;font-size:9.5pt;"&gt; &lt;span style="color:blue;"&gt;INTO&lt;/span&gt; &lt;span style="color:teal;"&gt;@item&lt;/span&gt;&lt;span style="color:blue;"&gt; &lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:teal;"&gt;n&lt;/span&gt;&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;span style="color:blue;"&gt;Values &lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;1&lt;span style="color:gray;"&gt;),(&lt;/span&gt;2&lt;span style="color:gray;"&gt;),(&lt;/span&gt;4&lt;span style="color:gray;"&gt;),(&lt;/span&gt;8&lt;span style="color:gray;"&gt;),(&lt;/span&gt;56&lt;span style="color:gray;"&gt;),(&lt;/span&gt;128&lt;span style="color:gray;"&gt;),(&lt;/span&gt;256&lt;span style="color:gray;"&gt;)&lt;/span&gt;         &lt;/span&gt;&lt;/p&gt;  &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;&lt;span style="font-family:consolas;font-size:9.5pt;"&gt;        &lt;/span&gt;&lt;/p&gt;  &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;&lt;span style="color:green;font-family:consolas;font-size:9.5pt;"&gt;--(n -1) will return the nth smallest number&lt;/span&gt;&lt;span style="font-family:consolas;font-size:9.5pt;"&gt;        &lt;/span&gt;&lt;/p&gt;  &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;&lt;span style="color:green;font-family:consolas;font-size:9.5pt;"&gt;--changing the &amp;lt; to &amp;gt; can get the largest nth number&lt;/span&gt;&lt;span style="font-family:consolas;font-size:9.5pt;"&gt;        &lt;/span&gt;&lt;/p&gt;  &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;&lt;span style="color:blue;font-family:consolas;font-size:9.5pt;"&gt;SELECT&lt;/span&gt;&lt;span style="font-family:consolas;font-size:9.5pt;"&gt; &lt;span style="color:teal;"&gt;n&lt;/span&gt; &lt;span style="color:blue;"&gt;FROM&lt;/span&gt; &lt;span style="color:teal;"&gt;@item&lt;/span&gt; &lt;span style="color:teal;"&gt;a&lt;/span&gt; &lt;span style="color:blue;"&gt;WHERE &lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;5 &lt;span style="color:gray;"&gt;-&lt;/span&gt; 1&lt;span style="color:gray;"&gt;)&lt;/span&gt;          &lt;/span&gt;&lt;/p&gt;  &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;&lt;span style="color:gray;font-family:consolas;font-size:9.5pt;"&gt;=&lt;/span&gt;&lt;span style="color:blue;font-family:consolas;font-size:9.5pt;"&gt; &lt;/span&gt;&lt;span style="color:gray;font-family:consolas;font-size:9.5pt;"&gt;(&lt;/span&gt;&lt;span style="color:blue;font-family:consolas;font-size:9.5pt;"&gt;select&lt;/span&gt;&lt;span style="font-family:consolas;font-size:9.5pt;"&gt; &lt;span style="color:fuchsia;"&gt;count&lt;/span&gt;&lt;span style="color:gray;"&gt;(*)&lt;/span&gt; &lt;span style="color:blue;"&gt;from&lt;/span&gt; &lt;span style="color:teal;"&gt;@item&lt;/span&gt; &lt;span style="color:teal;"&gt;b&lt;/span&gt; &lt;span style="color:blue;"&gt;where&lt;/span&gt; &lt;span style="color:teal;"&gt;b&lt;/span&gt;&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:teal;"&gt;itemid&lt;/span&gt; &lt;span style="color:gray;"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color:teal;"&gt;a&lt;/span&gt;&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:teal;"&gt;itemid&lt;/span&gt;&lt;span style="color:gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="line-height:normal;margin-bottom:0pt;" class="MsoNormal"&gt;I am sure i&amp;#39;ll be a&amp;nbsp; dab hand at this by the end of May, but I have so much fun with with this first post, chrome does not handle the formatting, and ie9 kept crashing as I was trying to paste in the SQL code and the formatting was horrendous, then I tried Firefox and I eventually managed it.&amp;nbsp; &lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15574" width="1" height="1"&gt;</content><author><name>MartinCroft</name><uri>http://sqlblogcasts.com/members/MartinCroft.aspx</uri></author></entry><entry><title>SQL 2008 top 5 things for a succesful Upgrade and Migration</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/martincroft/archive/2010/10/31/sql-2008-top-5-things-for-a-succesful-upgrade-and-migration.aspx" /><id>http://sqlblogcasts.com/blogs/martincroft/archive/2010/10/31/sql-2008-top-5-things-for-a-succesful-upgrade-and-migration.aspx</id><published>2010-10-31T07:21:00Z</published><updated>2010-10-31T07:21:00Z</updated><content type="html">&lt;span style="text-transform:none;text-indent:0px;letter-spacing:normal;word-spacing:0px;white-space:normal;border-collapse:separate;orphans:2;widows:2;" class="Apple-style-span"&gt;&lt;p style="margin:0px;"&gt;&lt;font class="Apple-style-span" size="3" face="tahoma,arial,helvetica,sans-serif"&gt;We have completed several upgrades from SQL 2000 and SQL2005 to SQL2008 and I thought I would blog about the top 5 things for a&amp;nbsp;successful&amp;nbsp;upgrade. There are lots of things that make an upgrade a success, these are just some of them.&lt;/font&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;font class="Apple-style-span" size="3" face="tahoma,arial,helvetica,sans-serif"&gt;Get a&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;great project team&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;together, this might seem obvious but it can make or break the project. Get expertise in all the areas that the project covers SQL,storage SAN&amp;#39;s, VMware, Infrastructure,SSIS dependent on what the project covers. Have people available on phone support if possible. Get a good cross over of skill sets&lt;/font&gt;&lt;/li&gt;&lt;li&gt;&lt;font class="Apple-style-span" size="3" face="tahoma,arial,helvetica,sans-serif"&gt;Goes without saying&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;TEST&lt;/b&gt;, if your lucky enough to have like for like&amp;nbsp;infrastructures then its easier but in high usage OLTP,OLAP systems which service lots of users and lots of apps this is not always easy. Test to the maximum that achievable, there are usually&amp;nbsp;business&amp;nbsp;constraints, resource constraints. If its not tested it need to be raised as a risk. You could blog entirely on testing&lt;/font&gt;&lt;/li&gt;&lt;li&gt;&lt;font class="Apple-style-span"&gt;&lt;font size="3"&gt;&lt;font face="tahoma,arial,helvetica,sans-serif"&gt;&lt;b&gt;Document everything&lt;/b&gt;. I have a subset of documents I believe are a must for an upgrade / migration. In no&amp;nbsp;particular order as it probably would take me far to long to decide which document has greater requirements&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;font size="3"&gt;&lt;font face="tahoma,arial,helvetica,sans-serif"&gt;&lt;b&gt;&lt;font class="Apple-style-span"&gt;External Influences Document&lt;/font&gt;&lt;/b&gt;&lt;font class="Apple-style-span"&gt;&amp;nbsp;- This was an important document that details all aspects of the SQL server and all external influences, such as applications that connect, services, windows application, web.config files, url for web&amp;nbsp;applications, usage, user accounts for apps. External SQL servers that connect through linked servers, DTS packages, SSIS packages, other non SQL&amp;nbsp;connectivity etc. Any outputs to other SQL Servers, Application servers Webservers, file structures FTP. This document should contain a diagram of all the systems that are touched and a high level of detail around the connectivity. SQL server access to the server, groups, users and permission. During upgrades it can be found the DBA team are the main players in the upgrade and the individual teams who develop the applications on the server take a back role during the project, this is the document you need to get their input into, that way there should be no surprises down the line. This will be used for configuring the test environments, knowing the scope of the project and start of building up requirements for go live steps.&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;p style="margin:0px;"&gt;&lt;font class="Apple-style-span" size="3" face="tahoma,arial,helvetica,sans-serif"&gt;&lt;/font&gt;&lt;/p&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;font size="3"&gt;&lt;font face="tahoma,arial,helvetica,sans-serif"&gt;&lt;b&gt;&lt;font class="Apple-style-span"&gt;Go live steps spread sheet&lt;/font&gt;&lt;/b&gt;&lt;font class="Apple-style-span"&gt;&amp;nbsp;- the list of actions for the migration, numbered, with the server the action is carried out on, what the action is. Reference to scripts, owner, who carried out the step, time , who checked the step (&amp;nbsp;dependent on project!)&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;p style="margin:0px;"&gt;&lt;font class="Apple-style-span" size="3" face="tahoma,arial,helvetica,sans-serif"&gt;&lt;/font&gt;&lt;/p&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;font size="3"&gt;&lt;font face="tahoma,arial,helvetica,sans-serif"&gt;&lt;b&gt;&lt;font class="Apple-style-span"&gt;Lessons Learned Document&lt;/font&gt;&lt;/b&gt;&lt;font class="Apple-style-span"&gt;- This is very useful and should not be seen as a witch hunt at the end of projects, but more for the next project, what went wrong how can we avoid it next time, how can we be more efficient next time. We reviewed previous Lessons Learned Documentation from previous projects when creating the go live steps to give us the best information to&amp;nbsp;succeed in the project.&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/li&gt;&lt;li&gt;&lt;font class="Apple-style-span"&gt;&lt;font size="3"&gt;&lt;font face="tahoma,arial,helvetica,sans-serif"&gt;&lt;b&gt;Risk and Issues logs, Highlight reports&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;- goes without saying we need to keep the focus of the project with the business and cover ourselves when the business puts constraints down that don&amp;#39;t allow you to do everything you would in the perfect world.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;font size="3"&gt;&lt;font face="tahoma,arial,helvetica,sans-serif"&gt;&lt;font class="Apple-style-span"&gt;&lt;b&gt;Learn from experience&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;- I mentioned the Lessons Learned document, learn from these. over the last several months we upgraded 5 core systems from SQL2000 &amp;amp;amp; SQL2005, each one presented different challenges, with different&amp;nbsp;complexness, but each one we learned and took into the next one what we learned.&lt;/font&gt;&lt;font class="Apple-style-span"&gt;&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/li&gt;&lt;li&gt;&lt;font class="Apple-style-span"&gt;&lt;font size="3"&gt;&lt;font face="tahoma,arial,helvetica,sans-serif"&gt;&lt;b&gt;Use the technology available&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;to you once upgraded. DMV&amp;#39;s, Data collector, Resource Governor. I&amp;#39;ll be blogging a set of DBA procedures that utilise the DMV&amp;#39;s to show whats running, who&amp;#39;s connected, what indexes are being used, what indexes aren&amp;#39;t and how much space they save. What procedures are executed , how frequently , average times etc.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;&lt;font size="3" face="tahoma,arial,helvetica,sans-serif"&gt;There are many other elements that contribute to a successful upgrade but I believe these are some of the more important ones. &lt;/font&gt;&lt;br /&gt;&lt;/span&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15000" width="1" height="1"&gt;</content><author><name>MartinCroft</name><uri>http://sqlblogcasts.com/members/MartinCroft.aspx</uri></author><category term="SQL 2008 Migration" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/SQL+2008+Migration/default.aspx" /><category term="Project Management" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Project+Management/default.aspx" /><category term="SQL2008 Upgrade" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/SQL2008+Upgrade/default.aspx" /><category term="Upgrade Advice" scheme="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Upgrade+Advice/default.aspx" /></entry></feed>