特定模式中表的行数

时间:2013-11-17 21:10:18

标签: sql oracle-sqldeveloper

如何获取一个sql查询,它将在一个模式中获得所有关于行计数的表

1 个答案:

答案 0 :(得分:0)

以下是用于检索所有表的Microsoft SQL Server查询,表示该表的架构和行数。

用于特定模式只需取消注释where子句的最后一行(remove / ** /)。并在where子句的最后一行的括号中输入您要查找的模式名称,您可以输入2以上,只需按照模式'SchemaName','SchemaName','SchemaName'。

 SELECT 
        t.NAME AS TableName,
        s.name AS SchemaName,
        p.[Rows]
    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.schemas s on s.schema_id = t.schema_id
    WHERE 
        t.NAME NOT LIKE 'dt%' AND
        i.OBJECT_ID > 255 AND   
        i.index_id <= 1 /* AND
        s.name in ('Schemaname1','Schemaname2') */
    GROUP BY 
        t.NAME, i.object_id, i.index_id, i.name,s.name, p.[Rows]
    ORDER BY 
        object_name(i.object_id) 

如果你想这样查询  SELECT owner, table_name, NUM_ROWS FROM ALL_TABLES where owner like 'Schema_name'

我建议创建一个类似的视图。

CREATE VIEW ALL_TABLES
AS
      SELECT 
            t.NAME AS table_name,
            s.name AS owner,
            p.[Rows] as NUM_ROWS
        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.schemas s on s.schema_id = t.schema_id
        WHERE 
            t.NAME NOT LIKE 'dt%' AND
            i.OBJECT_ID > 255 AND   
            i.index_id <= 1 /* AND
            s.name in ('Schemaname1','Schemaname2') */
        GROUP BY 
            t.NAME, i.object_id, i.index_id, i.name,s.name, p.[Rows]
        ORDER BY 
            object_name(i.object_id) 

然后如果你运行以下查询它将起作用

SELECT owner, table_name, NUM_ROWS FROM ALL_TABLES where owner like 'Schema_name'

如果这有帮助,请将答案标记为正确