sp_MSforeachtable order by

时间:2009-08-24 15:36:35

标签: sql sql-server sql-server-2008 tsql sp-msforeachtable

我使用sp_MSforeachtable来获取数据库中特定表的行数。我希望这些按名称排序。

如何向ORDER BY添加sp_MSforeachtable子句?

6 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:3)

你没有: - )

只需使用此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) 

马克

答案 2 :(得分:2)

一种方法是创建一个临时表,然后插入/执行它。然后在临时表上执行select / order by。

答案 3 :(得分:2)

来自此链接: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

答案 4 :(得分:1)

我知道这个问题已有10多年的历史了,但它已有3000多次访问,并且给出了许多错误的答案。我将重新定义Chris R.的答案,以期将其标记为可接受的答案,而不是将SQL的半页或“您不能”的答案弄得过于复杂。我来到这里的是完全相同的问题,因此它仍然很重要,而且显然并不简单。

使用 #define LAMBERT mat4 rotationX( in float angle ) { return mat4( 1.0, 0, 0, 0, 0, cos(angle), -sin(angle), 0, 0, sin(angle), cos(angle), 0, 0, 0, 0, 1); } mat4 rotationY( in float angle ) { return mat4( cos(angle), 0, sin(angle), 0, 0, 1.0, 0, 0, -sin(angle), 0, cos(angle), 0, 0, 0, 0, 1); } mat4 rotationZ( in float angle ) { return mat4( cos(angle), -sin(angle), 0, 0, sin(angle), cos(angle), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); } // instanced attribute vec3 instanceOffset; attribute vec3 instanceColor; attribute vec3 instanceRotation; varying vec3 vLightFront; varying vec3 vIndirectFront; #ifdef DOUBLE_SIDED varying vec3 vLightBack; varying vec3 vIndirectBack; #endif #include <common> #include <uv_pars_vertex> #include <uv2_pars_vertex> #include <envmap_pars_vertex> #include <bsdfs> #include <lights_pars_begin> #include <color_pars_vertex> #include <fog_pars_vertex> #include <morphtarget_pars_vertex> #include <skinning_pars_vertex> #include <shadowmap_pars_vertex> #include <logdepthbuf_pars_vertex> #include <clipping_planes_pars_vertex> void main() { #include <uv_vertex> #include <uv2_vertex> #include <color_vertex> // vertex colors instanced #include <beginnormal_vertex> #include <morphnormal_vertex> #include <skinbase_vertex> #include <skinnormal_vertex> #include <defaultnormal_vertex> #include <begin_vertex> // position instanced transformed += instanceOffset; #include <morphtarget_vertex> #include <skinning_vertex> #include <project_vertex> mvPosition = rotationX(250.0) * rotationY(90.0) * rotationZ(25.0) * vec4(position, 1.0); #include <logdepthbuf_vertex> #include <clipping_planes_vertex> #include <worldpos_vertex> #include <envmap_vertex> #include <lights_lambert_vertex> #include <shadowmap_vertex> #include <fog_vertex> } ` 参数指定一个@whereand子句,该参数的内容通过一个简单的ORDER BY插入到内部SELECT语句的末尾。存储的过程。在+ @whereand中使用1意味着要按第一列进行排序。

ORDER BY 1

答案 5 :(得分:0)

其中任何一个都应该这样做;

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'” - 我会赞成,但没有代表这样做。