sp_MSforeachtable order by
Asked Answered
G

6

1

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?

Giagiacamo answered 24/8, 2009 at 15:36 Comment(1)
Unfortunately, sp_MSforeachtable is not available for Azure SQL https://mcmap.net/q/120964/-could-not-find-stored-procedure-39-sp_msforeachtable-39Brambly
H
1

I understand this question is over 10 years old, but it has over 3000 visits and a bunch of wrong answers. I'm going to repurpose Chris R.'s answer in hopes of getting this marked as the accepted answer, instead of overly-complicated half-pages of SQL or "you can't" answers. I came here with the exact same question so it's still relevant and obviously not simple.

Use the @whereand parameter to specify an ORDER BY clause, The contents of that parameter are tacked on to the end of the internal SELECT statement via a simple + @whereand in the stored proc. And using 1 in ORDER BY 1 means to order by the first column.

sp_MSforeachtable @command1='SELECT COUNT(*) AS ''?'' FROM ?', @whereand = 'ORDER BY 1'
Hepler answered 30/1, 2020 at 22:20 Comment(1)
this is elegant!!Giagiacamo
S
6

EXEC sp_MSforeachtable @SQL, @whereand = 'ORDER BY 1'

Southward answered 2/7, 2014 at 19:27 Comment(1)
Please add some comments to your answerHirz
T
2

You don't :-)

Just use this SQL script instead - much easier to use and much more configurable - you can sort as you wish!

SELECT 
    t.NAME AS TableName,
    i.name as indexName,
    sum(p.rows) as RowCounts,
    sum(a.total_pages) as TotalPages, 
    sum(a.used_pages) as UsedPages, 
    sum(a.data_pages) as DataPages,
    (sum(a.total_pages) * 8) / 1024 as TotalSpaceMB, 
    (sum(a.used_pages) * 8) / 1024 as UsedSpaceMB, 
    (sum(a.data_pages) * 8) / 1024 as DataSpaceMB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
    t.NAME NOT LIKE 'dt%' AND
    i.OBJECT_ID > 255 AND   
    i.index_id <= 1
GROUP BY 
    t.NAME, i.object_id, i.index_id, i.name 
ORDER BY 
    object_name(i.object_id) 

Marc

Tenderloin answered 24/8, 2009 at 15:45 Comment(1)
+1 for going the right way. Too bad you show all the page counts but no actual row count ;) sum(sys.partitions.rows)Fonda
D
2

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):

CREATE PROCEDURE dbo.listTableRowCounts 
AS 
BEGIN 
    SET NOCOUNT ON 

    CREATE TABLE #foo 
    ( 
        tablename VARCHAR(255), 
        rc INT 
    ) 

    INSERT #foo 
        EXEC sp_msForEachTable 
            'SELECT PARSENAME(''?'', 1), 
            COUNT(*) FROM ? WITH (NOLOCK)' 

    SELECT tablename, rc 
        FROM #foo 
        ORDER BY tablename

    DROP TABLE #foo 
END
GO
Disinter answered 24/8, 2009 at 15:49 Comment(1)
that's a dead link now :-(Rossanarosse
F
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.

Forevermore answered 24/8, 2009 at 15:45 Comment(0)
C
1

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.

Currie answered 4/8, 2014 at 10:47 Comment(0)
H
1

I understand this question is over 10 years old, but it has over 3000 visits and a bunch of wrong answers. I'm going to repurpose Chris R.'s answer in hopes of getting this marked as the accepted answer, instead of overly-complicated half-pages of SQL or "you can't" answers. I came here with the exact same question so it's still relevant and obviously not simple.

Use the @whereand parameter to specify an ORDER BY clause, The contents of that parameter are tacked on to the end of the internal SELECT statement via a simple + @whereand in the stored proc. And using 1 in ORDER BY 1 means to order by the first column.

sp_MSforeachtable @command1='SELECT COUNT(*) AS ''?'' FROM ?', @whereand = 'ORDER BY 1'
Hepler answered 30/1, 2020 at 22:20 Comment(1)
this is elegant!!Giagiacamo

© 2022 - 2024 — McMap. All rights reserved.