Script that provides the row counts and table names

后端 未结 7 1875
旧巷少年郎
旧巷少年郎 2021-02-05 10:24

Maybe you easily said how to I provide table names and row counts?

Pseudo SQL:

for \"select tablename from system.Tables\" into :tablename
  execute \"se         


        
7条回答
  •  名媛妹妹
    2021-02-05 11:00

    I have adjusted the answer from marc_c with CTE and displaying it with choice of displaying just the schema you are after.

    Should work with SQL Serve 2005 and newer.

    WITH CountRowsInTables (Table_Name, Table_Schema, Row_Counts) AS
    (
    SELECT 
    TableName = t.Name,
    TableSchema = s.Name,
    RowCounts = p.Rows
        FROM 
            sys.tables t
        INNER JOIN 
            sys.schemas s ON t.schema_id = s.schema_id
        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
        WHERE 
            t.is_ms_shipped = 0
        GROUP BY
            s.Name, t.Name, p.Rows
    )
    
    SELECT Table_name, Table_Schema, Row_Counts
        FROM CountRowsInTables
        WHERE Table_Schema = 'Pick_Schema_to_display'; 
    

提交回复
热议问题