I am using sp_MSforeachtable
to get a rowcount of specific tables in my database. I want these ordered by name.
How do I add an ORDER BY
clause to sp_MSforeachtable
?
I am using sp_MSforeachtable
to get a rowcount of specific tables in my database. I want these ordered by name.
How do I add an ORDER BY
clause to sp_MSforeachtable
?
Either of these should do it;
EXEC sp_MSforeachtable @command1 = "SELECT count(*) as '?' FROM ? ", @whereand = 'ORDER BY 1'
EXEC sp_MSForEachTable 'SELECT ''?'', COUNT(*) FROM ?', @whereand = 'ORDER BY 1'
Kudos to Chris_R for the "@whereand = 'ORDER BY 1'" - I would upvote but do not have the rep to do so.
EXEC sp_MSforeachtable @SQL, @whereand = 'ORDER BY 1'
One way is to create a temp table, then insert / execute in to it. Then do a select / order by on the temp table.
from this link: http://web.archive.org/web/20080701045806/http://sqlserver2000.databases.aspfaq.com:80/how-do-i-get-a-list-of-sql-server-tables-and-their-row-counts.html
This will return correct counts, where methods using the meta data tables will only return estimates.
create this procedure (slightly different than from link):
You don't :-)
Just use this SQL script instead - much easier to use and much more configurable - you can sort as you wish!
Marc