<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'DMVs', 'good habits', 'SQL Server &amp;quot;Denali&amp;quot;', and 'denali'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=DMVs,good+habits,SQL+Server+%26quot%3BDenali%26quot%3B,denali&amp;orTags=0</link><description>Search results matching tags 'DMVs', 'good habits', 'SQL Server &amp;quot;Denali&amp;quot;', and 'denali'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>SQL Server v.Next (Denali) : Breaking change to sys.dm_exec_sessions</title><link>http://sqlblog.com/blogs/aaron_bertrand/archive/2011/07/19/sql-server-v-next-denali-breaking-change-to-sys-dm-exec-sessions.aspx</link><pubDate>Tue, 19 Jul 2011 11:22:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:37059</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;If you're not OCD about prefixing every single column in a query, this is one that might bite you. Today you might have code that assumes that, because database_id is in sys.dm_exec_requests but not in sys.dm_exec_sessions, you don't need to prefix database_id in the following example:&lt;/p&gt;&lt;blockquote&gt;&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT TOP &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;s.session_id&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;database_id&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;FROM &lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="green"&gt;sys&lt;/font&gt;&lt;font&gt;&lt;font color="black"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;font color="green"&gt;dm_exec_sessions &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;s&lt;br&gt;&lt;/font&gt;&lt;font color="magenta"&gt;LEFT &lt;/font&gt;&lt;font color="gray"&gt;OUTER &lt;/font&gt;&lt;font color="blue"&gt;JOIN &lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="green"&gt;sys&lt;/font&gt;&lt;font&gt;&lt;font color="black"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;font color="green"&gt;dm_exec_requests&lt;/font&gt;&lt;font color="black"&gt; &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;r&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;ON &lt;/font&gt;&lt;font color="black"&gt;s.session_id = &lt;/font&gt;&lt;font color="black"&gt;r.session_id&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;&lt;p&gt;In SQL Server 2005, 2008 and 2008 R2, the above code works perfectly fine, because the server can determine quite easily that the database_id column comes from the sys.dm_exec_requests DMV. However, if you run this same code on Denali CTP3, you get the following error:&lt;/p&gt;&lt;blockquote&gt;&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;&lt;tr&gt;&lt;td&gt;&lt;div style="color:red;padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;p&gt;Msg 209, Level 16, State 1, Line 3&lt;br&gt;Ambiguous column name 'database_id'. &lt;br&gt;&lt;/p&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;&lt;p&gt;Why is that? Well, we're finally getting indication of some database context even when the session is idle. Back in SQL Server 2005, they deprecated the sysprocesses system table in favor of the new DMVs sys.dm_exec_sessions, sys.dm_exec_sessions and sys.dm_exec_requests, though they made a compatibility view called sys.sysprocesses. They couldn't eliminate sysprocesses immediately because of the volume of code that it would break; and they quickly found that they couldn't eliminate it in the longer run because some crucial information was missing (see &lt;a href="http://connect.microsoft.com/SQLServer/feedback/details/257502/deprecation-of-sysprocesses-dmvs-doesnt-fully-replace-all-columns" title="http://connect.microsoft.com/SQLServer/feedback/details/257502/deprecation-of-sysprocesses-dmvs-doesnt-fully-replace-all-columns" target="_blank"&gt;Connect #257502&lt;/a&gt; for more info). In sys.sysprocesses you can see database context, for example, in the dbid column; in sys.dm_exec_sessions, there is no equivalent. There is a database_id column in the sys.dm_exec_requests DMV, but this only helps you get database context information for sessions with an active request.&lt;/p&gt;&lt;p&gt;Now in Denali, we have some new columns in sys.dm_exec_sessions, including database_id. This allows us to determine database context even for a session that currently doesn't have an active request; but it comes at the potential cost of breaking code, as demonstrated above. To make the above code sample work, you'll need to properly prefix the database_id column, maybe even with a COALESCE:&lt;/p&gt;&lt;blockquote&gt;&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT TOP &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;s.session_id&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font&gt;&lt;font color="black"&gt;database_id&lt;/font&gt;&lt;/font&gt;&lt;font color="gray"&gt; = &lt;/font&gt;&lt;font&gt;&lt;font color="magenta"&gt;COALESCE&lt;/font&gt;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;r.database_id, s.database_id&lt;/font&gt;&lt;font color="gray"&gt;)&lt;/font&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;FROM &lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="green"&gt;sys&lt;/font&gt;&lt;font&gt;&lt;font color="black"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;font color="green"&gt;dm_exec_sessions &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;s&lt;br&gt;&lt;/font&gt;&lt;font color="magenta"&gt;LEFT &lt;/font&gt;&lt;font color="gray"&gt;OUTER &lt;/font&gt;&lt;font color="blue"&gt;JOIN &lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="green"&gt;sys&lt;/font&gt;&lt;font&gt;&lt;font color="black"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;font color="green"&gt;dm_exec_requests&lt;/font&gt;&lt;font color="black"&gt; &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;r&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;ON &lt;/font&gt;&lt;font color="black"&gt;s.session_id &lt;/font&gt;&lt;font&gt;&lt;font color="gray"&gt;=&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt; &lt;/font&gt;&lt;font color="black"&gt;r.session_id&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;&lt;p&gt;There are some other interesting columns that have been added, such as authenticating_database_id - which will allow you to determine the original database even in cases where ORIGINAL_DB_NAME() is not available. This one warrants some testing and further treatment in a future blog post.&lt;br&gt;&lt;/p&gt;&lt;p&gt;Now don't get me wrong, database_id is a fantastic and welcome addition - I can stop looking at sys.sysprocesses to determine the database for sessions that aren't currently running a query. But if you're not currently in the habit of properly qualifying column names throughout your queries, here's one more reason you should start. This is another one of those "maybe breaking changes" that is almost certainly not going to show up using the Upgrade Advisor or Best Practices Analyzer, so it can be a real problem if the first time you see it is after you upgrade.&lt;br&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp; &lt;br&gt;&lt;/p&gt;</description></item></channel></rss>