SqlServer - 获取所有包含数据的表(非空)

时间:2018-05-22 12:32:56

标签: sql sql-server

问题很简单:

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

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

感谢支持

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

与@ 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