sys.dm_exec_sql_text
has a dbid
column, so you can filter on that. For example I took the query from the other answer and added a where clause filtering on queries against master
:
SELECT deqs.last_execution_time AS [Time], dest.TEXT AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
WHERE dest.dbid = DB_ID('master')
ORDER BY deqs.last_execution_time DESC
Note that not all queries have the right database context (or a database context at all). For example, if you have a query that joins two tables that are in different databases, you will only see one dbid - it will either be the executing context and may or may not be one of the databases referenced in the query. So applying the filter might actually hide queries you are interested in.
You may be able to obtain parameters by digging into the XML from other DMOs such as sys.dm_exec_cached_plans
and sys.dm_exec_query_plan
. If you have an execution plan already for a query that you've captured, it is going to be much easier to use a tool like SQL Sentry Plan Explorer than to wade through gobs of XML yourself.
Disclaimer: I work for SQL Sentry, who provides the free tool to the community.