solving sql server problems for millions of dbas and developers since 2006


Identify and resolve SQL Server problems BEFORE they happen with SQL diagnostic manager

SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Whitepapers SQL Server Tools SQL Server Webcasts SQL Server Questions and Answers SQL Server Questions and Answers









SQL Server Resource Database Values in DMVs

By: | Read Comments | Print

Chad is an Architect, Administrator and Developer with technologies such as SQL Server, .NET, and Windows Server.

Related Tips: More

Problem
Multiple queries against system dynamic management views and catalog views result in database ID's and names with a NULL value. Is something wrong with my queries?  How can the database name or ID be NULL?  Can I modify my queries to capture the correct values?

Solution
The answer is due to the new system database introduced in SQL Server 2005 called the
RESOURCE database. This database will always show a database_id value of 32767 when exposed within system DMVs or Catalog Views.  Note - The Resource database is not exposed in all DMVs or Catalog Views, as evident by a simple query against the sys.databases Catalog View, where you won't see an entry for it.  This is often the cause for retrieving NULL database name or database id values when joining to other system views/functions. One such example is as follows:

select distinct db_name(database_id)
from sys.dm_os_buffer_descriptors b with(nolock);

If you change this query to join to the sys.databases catalog view, you will notice that the NULL database name values disappear, however so do the resulting records from the sys.dm_os_buffer_descriptors where the prior statement was returning a NULL db_name() value:

select distinct d.name
from sys.dm_os_buffer_descriptors b with(nolock)
join sys.databases d with(nolock)
on b.database_id = d.database_id

If you run the first query without using the db_name() function, you'll notice that the database_id values for some of the entries in the sys.dm_os_buffer_descriptors DMV are the value of 32767 - the ID value for the RESOURCE database.

A simple tweak to the query will provide you with the appropriate results:

select distinct case when database_id = 32767 then 'resourceDb' else db_name(database_id) end
from sys.dm_os_buffer_descriptors b with(nolock);

Or the 2nd query to something like this:

select distinct case when b.database_id = 32767 then 'resourceDb' else d.name end
from sys.dm_os_buffer_descriptors b with(nolock)
left join sys.databases d with(nolock)
on b.database_id = d.database_id

Next Steps



Related Tips: More | Become a paid author


Last Update: 2/16/2007

Share: Share 






Comments and Feedback:


Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
"Amazing, Amazing, Amazing! SQL doctor is truly one of the most powerful tools I have seen."

Optimize your SQL Server storage: compress live databases by up to 90%. Download a free trial.

We fill in the gaps... SQL Server Training, Development, Performance Tuning, SSIS and more

Solving SQL Server problems for millions of DBAs and Devs since 2006. Join now.

Free Web Cast - Database development best practices by SQL Server MVPs Grant Fritchey and Jeremy Kadlec


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com