Useful SQL Query (Part 2) – Count of records in each table from SQL

This is very useful query while doing upgrade, you can get the count of records in each table from source and target environment. This helps you with initial level of testing. I found it useful to share.

 DECLARE @QueryString NVARCHAR(MAX) ;  
 SELECT @QueryString = COALESCE(@QueryString + ' UNION ALL ','')  
            + 'SELECT '  
            + '''' + QUOTENAME(SCHEMA_NAME(sOBJ.schema_id))  
            + '.' + QUOTENAME(sOBJ.name) + '''' + ' AS [TableName]  
            , COUNT(*) AS [RowCount] FROM '  
            + QUOTENAME(SCHEMA_NAME(sOBJ.schema_id))  
            + '.' + QUOTENAME(sOBJ.name) + ' WITH (NOLOCK) '  
 FROM sys.objects AS sOBJ  
 WHERE  
    sOBJ.type = 'U'  
    AND sOBJ.is_ms_shipped = 0x0  
 ORDER BY SCHEMA_NAME(sOBJ.schema_id), sOBJ.name ;  
 EXEC sp_executesql @QueryString  
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: