<?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">My two cents</title><subtitle type="html">Tips, opinions, tests (and misspellings) from a Brazilian DBA in UK</subtitle><id>http://sqlblogcasts.com/blogs/leopasta/atom.aspx</id><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/leopasta/default.aspx" /><link rel="self" type="application/atom+xml" href="http://sqlblogcasts.com/blogs/leopasta/atom.aspx" /><generator uri="http://communityserver.org" version="3.1.20917.1142">Community Server</generator><updated>2007-08-13T21:36:07Z</updated><entry><title>How atomic is a SELECT INTO?</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/leopasta/archive/2012/05/22/how-atomic-is-a-select-into.aspx" /><id>http://sqlblogcasts.com/blogs/leopasta/archive/2012/05/22/how-atomic-is-a-select-into.aspx</id><published>2012-05-22T22:47:00Z</published><updated>2012-05-22T22:47:00Z</updated><content type="html">&lt;p&gt;Last week I got an interesting situation that prompted me to challenge a long standing assumption. I always thought that a &lt;em&gt;SELECT INTO&lt;/em&gt; was an atomic statement, i.e. it would either complete successfully or the table would not be created.&lt;/p&gt;

&lt;p&gt;So I got very surprised when, after a “select into” query was chosen as a deadlock victim, the next execution (as the app would handle the deadlock and retry) would fail with:&lt;/p&gt;

&lt;p&gt;&lt;font color="#ff0000" face="Courier New"&gt;Msg 2714, Level 16, State 6, Line 1 
    &lt;br /&gt;There is already an object named &amp;#39;#test&amp;#39; in the database.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;The only hypothesis we could come up was that the “create table” part of the statement was committed independently from the actual “insert”. We can confirm that by capturing the “Transaction Log” event on Profiler (filtering by SPID0). The result is that when we run:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; * &lt;span class="kwrd"&gt;INTO&lt;/span&gt; #results &lt;span class="kwrd"&gt;FROM&lt;/span&gt; master.sys.objects&lt;/pre&gt;

&lt;p&gt;we get the following output on Profiler:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/leopasta/image_188315F8.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/leopasta/image_thumb_2C2FFF8C.png" width="689" height="99" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;It is easy to see the two independent transactions. Although this behaviour was a surprise to me, it is very easy to workaround it if you feel the need (as we did in this case). You can either change it into independent “CREATE TABLE / INSERT SELECT” or you can enclose the SELECT INTO in an explicit transaction:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SET&lt;/span&gt; XACT_ABORT &lt;span class="kwrd"&gt;ON&lt;/span&gt;
&lt;span class="kwrd"&gt;BEGIN&lt;/span&gt; &lt;span class="kwrd"&gt;TRANSACTION&lt;/span&gt;
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; * &lt;span class="kwrd"&gt;INTO&lt;/span&gt; #results &lt;span class="kwrd"&gt;FROM&lt;/span&gt; master.sys.objects
COMMIT&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/leopasta/image_3CC7FA7A.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://sqlblogcasts.com/blogs/leopasta/image_thumb_4D5FF568.png" width="679" height="142" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16215" width="1" height="1"&gt;</content><author><name>leo.pasta</name><uri>http://sqlblogcasts.com/members/leo.pasta.aspx</uri></author><category term="SQL Server" scheme="http://sqlblogcasts.com/blogs/leopasta/archive/tags/SQL+Server/default.aspx" /><category term="SELECT INTO" scheme="http://sqlblogcasts.com/blogs/leopasta/archive/tags/SELECT+INTO/default.aspx" /><category term="Temp Tables" scheme="http://sqlblogcasts.com/blogs/leopasta/archive/tags/Temp+Tables/default.aspx" /></entry><entry><title>Coming back from (blog) retirement</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/leopasta/archive/2012/05/22/coming-back-from-blog-retirement.aspx" /><id>http://sqlblogcasts.com/blogs/leopasta/archive/2012/05/22/coming-back-from-blog-retirement.aspx</id><published>2012-05-22T21:30:54Z</published><updated>2012-05-22T21:30:54Z</updated><content type="html">&lt;p&gt;So, it has been more than 3 years without a single blog post. I wished I could have a decent excuse for it, but in the end, I guess it boils down to laziness and procrastination. :-)&lt;/p&gt;  &lt;p&gt;Even though I learned a lot in that period (and added a feel tricks to my bag), I couldn’t find the will to sit down and write. I hope all my readers (yes mom and dad, I’m talking to you) have not been disappointed.&lt;/p&gt;  &lt;p&gt;I will try really hard not to let routine take over. I don’t expect I will be the most active blogger in the community, but hopefully a couple of posts per month is a good target to aim.&lt;/p&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=16214" width="1" height="1"&gt;</content><author><name>leo.pasta</name><uri>http://sqlblogcasts.com/members/leo.pasta.aspx</uri></author><category term="Personal" scheme="http://sqlblogcasts.com/blogs/leopasta/archive/tags/Personal/default.aspx" /></entry><entry><title>Getting all the Clustered keys in the database</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/leopasta/archive/2009/03/30/getting-all-the-clustered-keys-in-the-database.aspx" /><id>http://sqlblogcasts.com/blogs/leopasta/archive/2009/03/30/getting-all-the-clustered-keys-in-the-database.aspx</id><published>2009-03-30T15:47:00Z</published><updated>2009-03-30T15:47:00Z</updated><content type="html">Jeremiah Peschka (http://twitter.com/peschkaj) asked on twitter if anyone had a script to list all clustered keys in the database. This is my take on it, I hope it helps someone: SELECT i.name , &amp;#39;(&amp;#39; + STUFF ( ( SELECT &amp;#39;,&amp;#39; + c.name + CASE WHEN is_descending_key = 0 THEN &amp;#39; ASC &amp;#39; ELSE &amp;#39; DESC &amp;#39; END FROM sys.index_columns ic JOIN sys.columns c ON ic. OBJECT_ID = c. OBJECT_ID AND ic.column_id = c.column_id WHERE i. OBJECT_ID = ic. OBJECT_ID AND i.index_id = ic.index_id AND...(&lt;a href="http://sqlblogcasts.com/blogs/leopasta/archive/2009/03/30/getting-all-the-clustered-keys-in-the-database.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=11502" width="1" height="1"&gt;</content><author><name>leo.pasta</name><uri>http://sqlblogcasts.com/members/leo.pasta.aspx</uri></author></entry><entry><title>Reducing Transaction Log fragmentation</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/leopasta/archive/2008/09/02/reducing-transaction-log-fragmentation.aspx" /><id>http://sqlblogcasts.com/blogs/leopasta/archive/2008/09/02/reducing-transaction-log-fragmentation.aspx</id><published>2008-09-02T22:09:25Z</published><updated>2008-09-02T22:09:25Z</updated><content type="html">I am taking Kimberly Tripp and Paul Randal&amp;#39;s course on SQL Server Performance &amp;amp; Availability this week in Hatfield and we had an interesting discussion on Transaction Log internal fragmentation. Kimberly summarized the issue, together with other Transaction Log best practices, here (check item 8) . Based on that discussion, and as I was bitten by this issue before, I thought it might be interesting to write a procedure to help verify and fix it. The procedure is called sp_DBA_DefragLog and...(&lt;a href="http://sqlblogcasts.com/blogs/leopasta/archive/2008/09/02/reducing-transaction-log-fragmentation.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=10772" width="1" height="1"&gt;</content><author><name>leo.pasta</name><uri>http://sqlblogcasts.com/members/leo.pasta.aspx</uri></author></entry><entry><title>Which queries are missing indexes?</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/leopasta/archive/2008/05/08/which-queries-are-missing-indexes.aspx" /><id>http://sqlblogcasts.com/blogs/leopasta/archive/2008/05/08/which-queries-are-missing-indexes.aspx</id><published>2008-05-08T20:48:28Z</published><updated>2008-05-08T20:48:28Z</updated><content type="html">One of the things I really enjoy when doing performance tuning on 2005 (I still work on a mix of several SQL 2000 and some SQL 2005) is the sys.dm_db_missing_index* DMVs. As the query processor evaluates queries, it detects if that specific query could benefit from an index and how much it expect that index would reduce the cost (in terms of IO), exposing these information as views that we can query. I won&amp;#39;t delve in the structure of it, but you can use my procedure sp_dba_missingindex as an...(&lt;a href="http://sqlblogcasts.com/blogs/leopasta/archive/2008/05/08/which-queries-are-missing-indexes.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=10381" width="1" height="1"&gt;</content><author><name>leo.pasta</name><uri>http://sqlblogcasts.com/members/leo.pasta.aspx</uri></author></entry><entry><title>SQLBits</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/leopasta/archive/2008/03/02/sqlbits.aspx" /><id>http://sqlblogcasts.com/blogs/leopasta/archive/2008/03/02/sqlbits.aspx</id><published>2008-03-02T21:47:24Z</published><updated>2008-03-02T21:47:24Z</updated><content type="html">Yesterday I attended the SQL Server focused conference called SQLBits , which this year took place in Birmingham. First of all, many thanks to Tony Rogerson , Simon Sabin and all organisers. You did a hell of a good job!!! As a suggestion for the next ones, I would find useful to have an indication if it is a session intended for beginners or if it will be more in-depth sessions. I got a couple of sessions &amp;quot;wrong&amp;quot; because I misjudge it from the Session title and description. In this post...(&lt;a href="http://sqlblogcasts.com/blogs/leopasta/archive/2008/03/02/sqlbits.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=8452" width="1" height="1"&gt;</content><author><name>leo.pasta</name><uri>http://sqlblogcasts.com/members/leo.pasta.aspx</uri></author></entry><entry><title>Data purity</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/leopasta/archive/2007/11/26/data-purity.aspx" /><id>http://sqlblogcasts.com/blogs/leopasta/archive/2007/11/26/data-purity.aspx</id><published>2007-11-26T22:39:41Z</published><updated>2007-11-26T22:39:41Z</updated><content type="html">Reviewing DBCC CHECKDB syntax for SQL Server 2005, I found a [DATA_PURITY] option added to its syntax. This option enable the following checks on each column value of those datatypes: Unicode character - The data length should be a multiple of 2. Datetime - The days field should be between Jan 1 1753 and Dec 31 9999. The time field must be earlier than '11:59:59:999PM' . Real and Float - Check for existence of invalid floating point values like SNAN, QNAN, NINF, ND, PD, PINF. For a database created...(&lt;a href="http://sqlblogcasts.com/blogs/leopasta/archive/2007/11/26/data-purity.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=4776" width="1" height="1"&gt;</content><author><name>leo.pasta</name><uri>http://sqlblogcasts.com/members/leo.pasta.aspx</uri></author><category term="SQL Server" scheme="http://sqlblogcasts.com/blogs/leopasta/archive/tags/SQL+Server/default.aspx" /><category term="Recovery" scheme="http://sqlblogcasts.com/blogs/leopasta/archive/tags/Recovery/default.aspx" /></entry><entry><title>Losing your Transaction log</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/leopasta/archive/2007/11/20/losing-your-transaction-log.aspx" /><id>http://sqlblogcasts.com/blogs/leopasta/archive/2007/11/20/losing-your-transaction-log.aspx</id><published>2007-11-20T00:17:50Z</published><updated>2007-11-20T00:17:50Z</updated><content type="html">Technorati Tags: SQL Server , Recovery Some days ago, my boss got on my desk asking if I could take a look at one database that was not opening. That DB was in a virtual machine used for our sales presentations. For some reason, several files got corrupted in that VM, and between those was our beloved transaction log file. It was a perfect opportunity to sharpen my database recovery skills, there was quite some time that I didn’t played with those tools and to be honest I had forgot almost everything...(&lt;a href="http://sqlblogcasts.com/blogs/leopasta/archive/2007/11/20/losing-your-transaction-log.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=4348" width="1" height="1"&gt;</content><author><name>leo.pasta</name><uri>http://sqlblogcasts.com/members/leo.pasta.aspx</uri></author><category term="SQL Server" scheme="http://sqlblogcasts.com/blogs/leopasta/archive/tags/SQL+Server/default.aspx" /><category term="Recovery" scheme="http://sqlblogcasts.com/blogs/leopasta/archive/tags/Recovery/default.aspx" /></entry><entry><title>The sound of silence</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/leopasta/archive/2007/09/06/the-sound-of-silence.aspx" /><id>http://sqlblogcasts.com/blogs/leopasta/archive/2007/09/06/the-sound-of-silence.aspx</id><published>2007-09-06T22:14:01Z</published><updated>2007-09-06T22:14:01Z</updated><content type="html">This one isn't related to SQL Server. But do you know when sometimes you get stuck in an almost infinite loop of dialog boxes and for some reason you don't want to kill the application? "Sorry &amp;lt;beep&amp;gt; guys, this &amp;lt;beep&amp;gt; should &amp;lt;beep&amp;gt; end in &amp;lt;beep&amp;gt; any mome&amp;lt;beep&amp;gt;nt now &amp;lt;beep&amp;gt;." Well it happened to me these days and I decided that enough was enough and I would request the help of Saint Google! It turns out that at least on XP and Vista there is a nice registry key...(&lt;a href="http://sqlblogcasts.com/blogs/leopasta/archive/2007/09/06/the-sound-of-silence.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=2501" width="1" height="1"&gt;</content><author><name>leo.pasta</name><uri>http://sqlblogcasts.com/members/leo.pasta.aspx</uri></author><category term="Windows" scheme="http://sqlblogcasts.com/blogs/leopasta/archive/tags/Windows/default.aspx" /></entry><entry><title>Page splits</title><link rel="alternate" type="text/html" href="http://sqlblogcasts.com/blogs/leopasta/archive/2007/08/13/page-splits.aspx" /><id>http://sqlblogcasts.com/blogs/leopasta/archive/2007/08/13/page-splits.aspx</id><published>2007-08-13T20:36:07Z</published><updated>2007-08-13T20:36:07Z</updated><content type="html">These days, investigating a fairly simple insert which was taking longer than what I would consider reasonable for that particular piece of code, I explored several possible causes. One thing that raised my attention was that the "Pages split/sec" perfmon counter was constantly above 0, so I began to wonder which index was causing most of it. I ended up reading an interesting blog entry from Greg Linwood which inspired me to write a small stored procedure to monitor it for a longer period: USE master...(&lt;a href="http://sqlblogcasts.com/blogs/leopasta/archive/2007/08/13/page-splits.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=2198" width="1" height="1"&gt;</content><author><name>leo.pasta</name><uri>http://sqlblogcasts.com/members/leo.pasta.aspx</uri></author><category term="SQL Server" scheme="http://sqlblogcasts.com/blogs/leopasta/archive/tags/SQL+Server/default.aspx" /></entry></feed>
