Get Row Counts of All Tables in Fabric Warehouse | Microsoft Fabric Tutorial #fabrictutorial

Get Row Counts of All Tables in Fabric Warehouse | Microsoft Fabric Tutorial

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 from INFORMATION_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

๐ŸŽฌ Watch the Full Tutorial

Blog post written with the help of ChatGPT.