Get Row Counts of All Tables in Fabric Warehouse
In Microsoft Fabric Warehouse, it's often helpful to get the row counts of all tables in your database — whether for validation, monitoring, or reporting purposes. This tutorial shows you a dynamic and efficient way to accomplish that using T-SQL.
๐ฏ Goal
Dynamically generate and execute a script that counts rows from every BASE TABLE
in the current Fabric Warehouse.
๐งพ T-SQL Script
DECLARE @QueryString NVARCHAR(MAX);
DECLARE @NewLine CHAR(2) = CHAR(13) + CHAR(10); -- Define newline once
SELECT @QueryString = COALESCE(@QueryString + @NewLine + 'UNION ALL' + @NewLine, '')
+ 'SELECT '
+ '''' + t.TABLE_SCHEMA + '.' + t.TABLE_NAME + '''' + ' AS [TableName]' + @NewLine
+ ', COUNT(*) AS [RowCount] FROM '
+ QUOTENAME(t.TABLE_SCHEMA) + '.' + QUOTENAME(t.TABLE_NAME) + @NewLine
FROM INFORMATION_SCHEMA.TABLES AS t
WHERE t.TABLE_TYPE = 'BASE TABLE'
ORDER BY t.TABLE_SCHEMA, t.TABLE_NAME;
-- Optional: print to verify the dynamic SQL
PRINT @QueryString;
-- Execute the dynamic SQL
EXEC sp_executesql @QueryString;
๐ ️ How It Works
- Pulls all
BASE TABLE
names fromINFORMATION_SCHEMA.TABLES
- Builds a
SELECT COUNT(*)
query for each table - Combines them using
UNION ALL
into one dynamic script - Prints and executes the final T-SQL query
๐ Benefits
- No manual table listing needed
- Query adapts to future schema changes
- Can be used for automated audit reporting