Skip to content

Instantly share code, notes, and snippets.

@ronnysuero
Last active October 25, 2018 17:32
Show Gist options
  • Save ronnysuero/10d0434c89df328af251d3d13cb176e0 to your computer and use it in GitHub Desktop.
Save ronnysuero/10d0434c89df328af251d3d13cb176e0 to your computer and use it in GitHub Desktop.
Total Rows By Table Sql Server
WITH UnUsedTables (
TableName
,TotalRowCount
,CreatedDate
,LastModifiedDate
)
AS (
SELECT DBTable.NAME AS TableName
,PS.row_count AS TotalRowCount
,DBTable.create_date AS CreatedDate
,DBTable.modify_date AS LastModifiedDate
FROM sys.all_objects DBTable
JOIN sys.dm_db_partition_stats PS ON OBJECT_NAME(PS.object_id) = DBTable.NAME
WHERE DBTable.type = 'U'
AND NOT EXISTS (
SELECT OBJECT_ID
FROM sys.dm_db_index_usage_stats
WHERE OBJECT_ID = DBTable.object_id
)
)
-- Select data from the CTE
SELECT TableName
,TotalRowCount
,CreatedDate
,LastModifiedDate
FROM UnUsedTables
ORDER BY TotalRowCount ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment