title: Why are these DMVs returning rows for all databases except one?
body: <p>I am trying to query the DMVs in SQL Server 2008 R2.</p>
  <p>On this server are two user databases called <em>histrx</em> and <em>OpenLink</em>.  To prove I have their names correct:</p>
  <pre><code>select db_id('histrx') -- Returns 5
  select db_id('OpenLink') -- Returns 7
  </code></pre>
  <p>If I run the following query, picking out entries for the <em>histrx</em> database, I get 25 rows in the result set:</p>
  <pre><code>select top 25 
      total_worker_time/execution_count as avg_worker_time,
      total_logical_reads/execution_count as avg_logical_reads,
      db_name(s.dbid) as [db_name],
      object_name(s.objectid, s.dbid) as [object_name],
      execution_count,
      plan_generation_num,
      last_execution_time,
      creation_time,
      [text],
      p.query_plan
  from
      sys.dm_exec_query_stats qs
      CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle) s
      CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) p
  where
      db_name(s.dbid) = 'histrx'
  order by
      avg_logical_reads desc
  </code></pre>
  <p>If I then change the <code>where</code> clause to the following, no rows are returned:</p>
  <pre><code>where
      db_name(s.dbid) = 'OpenLink'
  </code></pre>
  <p>I know that there is a significant amount of activity on the <em>OpenLink</em> database.  If I look at <strong>Recent Expensive Queries</strong> in the Activity Monitor, I can see entries for <em>OpenLink</em>, and I'm pretty sure this is using the DMVs underneath.</p>
  <ul>
  <li>I'm running the Activity Monitor and the DMV query under the same
  login</li>
  <li>That login is the owner of the <em>OpenLink</em> database</li>
  <li>If I run <code>select * from fn_my_permissions (NULL, 'server');</code> then I can see I have <em>VIEW SERVER STATE</em> permissions</li>
  <li>If I remove the <code>where</code> clause, I see entries for other databases such as <em>msdb</em> and <em>distribution</em></li>
  <li>Here is a screenshot of the mappings for my login.  I'm pretty sure I shouldn't be the owner, but that's a different question.</li>
  </ul>
  <p><img src="https://i.stack.imgur.com/3YVcW.png" alt="Screenshot of SQL Server permissions showing db_owner role membership" /></p>
  <p>Can anyone tell me why my DMV query is returning zero rows for this database?</p>