【问题标题】:SqlServer - Get all tables that has data ( are not empty )Sql Server - 获取所有有数据的表(不为空)
【发布时间】:2018-11-21 10:47:51
【问题描述】:

问题很简单:

是否可以检索所有非空表?

我需要一个查询来列出这些表。有什么办法吗?

感谢支持

【问题讨论】:

标签: sql sql-server


【解决方案1】:

试试这个脚本来获取所有非空记录的表

    USE [Your database Name]
    Go
    SELECT SCHEMA_NAME(schema_id) AS [SchemaName],
            [Tables].name AS [TableName]
            --SUM([Partitions].[rows]) AS [TotalRowCount]
    FROM sys.tables AS [Tables]
    JOIN sys.partitions AS [Partitions]
        ON [Tables].[object_id] = [Partitions].[object_id]
        AND [Partitions].index_id IN ( 0, 1 )
    -- WHERE [Tables].name = N'name of the table'
    GROUP BY SCHEMA_NAME(schema_id), [Tables].name
    HAVING SUM([Partitions].[rows]) >0

【讨论】:

    【解决方案2】:

    与@Sreenu131 的答案略有不同,因为它使用 sys.partitions .rows 属性来查找

    p.rows > 0

    SELECT 
        sch.name as SchemaName,
        t.NAME AS TableName,
        p.rows AS RowCounts
    FROM 
        sys.tables t
    INNER JOIN 
        sys.partitions p ON t.object_id = p.OBJECT_ID 
    INNER JOIN sys.schemas sch
        on t.schema_id = sch.schema_id
    WHERE 
        t.NAME NOT LIKE 'dt%' 
        AND t.is_ms_shipped = 0
        AND p.rows > 0
    GROUP BY 
        sch.name,t.Name, p.Rows
    ORDER BY 
        sch.name,t.Name
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      • 2014-03-22
      • 1970-01-01
      • 2011-04-12
      • 2017-03-09
      • 1970-01-01
      相关资源
      最近更新 更多