Find all tables containing the column name; Filtration

I used SQL found here: Find all tables containing a column with the specified name

to great success. This allows me to find all tables containing a specific column. My problem is that the database I am working on seems to have a lot of empty tables (maybe about half of my results are empty). I was wondering if there is a way to change the code in the link so that empty rows / columns are not presented. Below is the code from the link:

SELECT      c.name  AS 'ColumnName'
            ,t.name AS 'TableName'
FROM        sys.columns c
JOIN        sys.tables  t   ON c.object_id = t.object_id
WHERE       c.name LIKE '%MyName%'
ORDER BY    TableName
            ,ColumnName;

Thank,

+4
source share
1 answer

Something like this can work without tremendous effort:

SELECT      c.name  AS 'ColumnName'
            ,t.name AS 'TableName'
            ,p.rows
FROM sys.columns c
INNER JOIN sys.tables t
ON c.object_id = t.object_id
INNER JOIN sys.partitions p
on t.object_id = p.object_id
WHERE       c.name LIKE '%p%'
            AND p.rows > 0
ORDER BY    TableName
            ,ColumnName;

, sys.partitions ; sys.dm_db_index_physical_stats (. https://dba.stackexchange.com/questions/55124/how-accurate-is-the-sys-partition-rows-column). , , AlwaysOn:

SELECT      c.name  AS 'ColumnName'
            ,t.name AS 'TableName'
            ,ips.record_count
FROM sys.columns c
INNER JOIN sys.tables t
ON c.object_id = t.object_id
CROSS APPLY sys.dm_db_index_physical_stats(DB_ID(), t.object_id, null, null, 'DETAILED') ips
WHERE       c.name LIKE '%p%'           
            AND ips.record_count > 0
ORDER BY    TableName
            ,ColumnName;

100% - , COUNT(*) ( Dynamic SQL), , , , .

+7

Source: https://habr.com/ru/post/1667177/


All Articles