Quote from Books Online 2008 R2 > sys.dm_exec_sql_text:
dbid smallint ID of database. Is NULL for ad hoc and prepared SQL statements.
1) So, for this type of SQL statements, where db_name(s.dbid) = 'OpenLink'
means where NULL = 'OpenLink'
and this predicate is evaluated to UNKNOWN
.
2) dbid
column is NOT NULL
for "non-adhoc and non-prepared SQL" statements (ex. for stored procedures).
3) For SQL 2008R2 you might get the dbid
for "ad hoc and prepared SQL statements" from sys.dm_exec_plan_attributes ( plan_handle )
function (link) using this query:
SELECT
...
ISNULL(src.dbid,CONVERT(SMALLINT,att.value)) AS my_dbid,
DB_NAME(ISNULL(src.dbid,CONVERT(SMALLINT,att.value))) my_dbname,
...
FROM
sys.dm_exec_query_stats qs
...
CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) att
WHERE att.attribute='dbid'