sp_MSforeachtable 排序依据

发布于 2024-08-02 22:44:11 字数 129 浏览 7 评论 0原文

我正在使用 sp_MSforeachtable 来获取数据库中特定表的行计数。我想要这些按名字排序。

如何向 sp_MSforeachtable 添加 ORDER BY 子句?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

無心 2024-08-09 22:44:11

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

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

只有影子陪我不离不弃 2024-08-09 22:44:11

通过此链接: 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

这将返回正确的计数,其中使用元数据表的方法只会回报估计。

创建此过程(与链接略有不同):

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

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
那一片橙海, 2024-08-09 22:44:11

您不需要 :-)

只需使用此 SQL 脚本即可 - 更易于使用且更易于配置 - 您可以根据需要进行排序!

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) 

马克

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

终难愈 2024-08-09 22:44:11

我知道这个问题已经有 10 多年的历史了,但它有超过 3000 次访问和一堆错误的答案。我将重新调整 Chris R. 的答案,希望将其标记为已接受的答案,而不是过于复杂的半页 SQL 或“你不能”的答案。我带着完全相同的问题来到这里,所以它仍然相关,而且显然并不简单。

使用 @whereand 参数指定 ORDER BY 子句,该参数的内容通过以下方式附加到内部 SELECT 语句的末尾存储过程中的简单 + @whereand 。在 ORDER BY 1 中使用 1 表示按第一列排序。

sp_MSforeachtable @command1='SELECT COUNT(*) AS ''?'' FROM ?', @whereand = 'ORDER BY 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'
空气里的味道 2024-08-09 22:44:11

一种方法是创建一个临时表,然后插入/执行其中。然后在临时表上执行选择/排序。

One way is to create a temp table, then insert / execute in to it. Then do a select / order by on the temp table.

眉目亦如画i 2024-08-09 22:44:11

其中任何一个都应该可以做到;

EXEC sp_MSforeachtable @command1 = "SELECT count(*) as '?' FROM ? ", @whereand = 'ORDER BY 1'

EXEC sp_MSForEachTable 'SELECT ''?'', COUNT(*) FROM ?', @whereand = 'ORDER BY 1'

感谢 Chris_R 的“@whereand = 'ORDER BY” 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文