代码之家  ›  专栏  ›  技术社区  ›  DarioN1

sqlserver-获取所有具有数据的表(不为空)

  •  -2
  • DarioN1  · 技术社区  · 6 年前

    问题很简单:

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

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

    多亏了支持

    2 回复  |  直到 6 年前
        1
  •  1
  •   Sreenu131    6 年前

    尝试使用此脚本获取具有非空记录的所有表

        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
  •  1
  •   jitin14    6 年前

    与@sreenu131答案稍有不同,因为它使用sys.partitions.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