foreachdatabase query

SELECT TOP 10
        [Total Reads] = SUM(total_logical_reads)
        ,[Execution count] = SUM(qs.execution_count)
        ,DatabaseName = DB_NAME(qt.dbid)
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as qt
GROUP BY DB_NAME(qt.dbid)
ORDER BY [Total Reads] DESC;

I was using this query to return output for queries with most reads. I would execute it on master database. But I need to use exec sp_msforeachdb statement to get outputs for each database and put a clause so that it only gets results for user databases. pls help
LVL 1
sqlserverdbaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chapmandewCommented:
this is how you do it:

create table #results(reads int, execount int, dbname sysname)

insert into #results
execute sp_msforeachdb 'use [?] SELECT TOP 10
        [Total Reads] = SUM(total_logical_reads)
        ,[Execution count] = SUM(qs.execution_count)
        ,DatabaseName = ''?''
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as qt
GROUP BY DB_NAME(qt.dbid)
ORDER BY [Total Reads] DESC'

select * from #results
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sqlserverdbaAuthor Commented:
Thank you..
The query executes, there is output, but there is also a syntax error. This is the error I get
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near '.'.
also I need to get only user databases in the output.
0
chapmandewCommented:
did you use the exact query I provided?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

sqlserverdbaAuthor Commented:
yes i did
0
chapmandewCommented:
Hmmm...I just ran it on 3 different servers and it worked fine on all of them.  You're on 2005, right?
0
sqlserverdbaAuthor Commented:
yes I am. pls try parsing it on a different database no need of executing it even. In master database, it parses successfully and on a user database it says incorrect syntax.
0
chapmandewCommented:
works fine for me everywhere.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.