Script to get row count for all tables in a SQL Server database
This post helps you in writing a Script to get row count for all tables in a SQL Server database. For any database developer or administrator the most common requirement is to identifying the larger tables based on data size, index size and row count. This script can quickly list out all tables in the given database and retrieves the below details:
- Name: Table Name
- Rows: Total row count
- Reserved: Space reserved for the table – MB
- Data: Total space allocated for data pages – MB
- Index_size: Total space allocated for Index pages – MB
- Unused: Unused space – MB
Here is the script to get row count for all tables in a SQL Server database:
SET NOCOUNT ON;
--Create a temparory table
CREATE TABLE #Tab (
--Capture all tables data allocation information
EXEC sp_msForEachTable 'EXEC sp_spaceused ''?''' ;
--Alter Rows column datatype to BIGINT to get the result in sorted order
ALTER TABLE #Tab ALTER COLUMN [ROWS] BIGINT ;
-- Get the final result: Remove KB and convert it into MB
CAST(LTRIM(RTRIM(REPLACE(Reserved,'KB',''))) AS BIGINT)/1024.0 AS 'Reserved MB',
CAST(LTRIM(RTRIM(REPLACE(Data,'KB',''))) AS BIGINT)/1024.0 AS 'Data MB',
CAST(LTRIM(RTRIM(REPLACE(Index_Size,'KB',''))) AS BIGINT)/1024.0 AS 'Index_Size MB',
CAST(LTRIM(RTRIM(REPLACE(Unused,'KB',''))) AS BIGINT)/1024.0 AS 'Unused MB'
ORDER BY [rows] DESC;
DROP TABLE #Tab;
-- Drop the temparory table
DROP TABLE #Tab;
When you need to get the list of tables along with the row counts, connect to the database instance open a new query window, paste the above script and execute it. It shouldn’t take much time to execute the script as we are using “sp_spaceused”, for us it took maximum 6 sec on a 3.5 TB database.
Remember there are chances where sp_spaced might give the wrong row counts due to several reasons. There are mainly 3 reasons that cause sp_spaceused gives us the wrong counts:
- When there are huge DDL changes
- Statistics are not updated
- Index maintenance is not being taken care
Here you will find more details and resolution.
New Features Added in SQL Server
This post helps you in quickly reviewing the “New Features Added in SQL Server” from 2008 to 2016. The most common interview question is “What are the new features added in SQL Server XXXX?” To simplify the answer we are just giving the single line abbreviations. While preparing for an interview just have a quick look and try to remember 4 to 5 features on current working and last released version. Let’s say you are currently working on 2012 then you need to look for 2012 and 2008 R2. Also people would expect you to know few new features added in the latest version which is SQL Server 2016. Features are categorised for DBA, Developer and Security. That doesn’t mean that SQL Developer need not look into DBA section or vice versa. This is just to make it more readable, when a feature is more related to Developer we added in Developer section when a feature is more relevant administration part it will be in DBA section. Believe this will be helpful for a quick review for version wise feature support.
New Features Added in SQL Server 2008
Activity Monitor: Great tool to showcase resource utilization and performance using GUI.
Policy Based Management: The ability to manage standards on multiple servers
Enhanced Database Mirroring: Automatic data page repair and compressing outgoing log stream
Resource Governor: We can configure it to control SQL Server resource utilization and workload.
External Key Management: Provides a comprehensive solution for encryption and key management.
Hot Add CPU: Adding resources online without downtime.
PowerShell: SQL Server 2008 ships with PowerShell snap-in built on .Net framework 2.0.
Table Compression: Compress data and index pages to save memory and I/O.
Backup Compression: Native backup compression.
Performance data collection: Centralized data repository for storing and reporting performance data
Extended Events: Event collection is easier now compares to running a trace
Microsoft Acquiring LinkedIn
I am very excited to see the news “Microsoft Acquiring LinkedIn” on Monday morning. I have been working on Microsoft products from last 10 years and I love LinkedIn. “Satya Nadella” (the man of inspiration who made Indians proud especially “Telugu” people) announced the news on one of the video from Microsoft website.
“Satya Nadella” has made a first biggest deal as the Microsoft CEO. I believe this is the fantastic step by Microsoft and this is a big deal as Microsoft will be acquiring the world’s largest and the biggest professional social network LinkedIn for $26.2 billion, $196 per share.
Satya Nadella said
“The LinkedIn team has grown a fantastic business cantered on connecting the world’s professionals, I have always had a great admiration for LinkedIn, I have been talking with Reid and Jeff for a while … I have been thinking about this for a long time.”
By this deal Microsoft targeted Organization Growth by integrating Microsoft Office Business suite users with LinkedIn and Targeted advertising.
Jeff Weiner will continue as a CEO for LinkedIn and reports to Satya Nadella. LinkedIn shares surged 47 percent after the announcement on Monday to near $193, Microsoft’s stock was down 3.2 percent.
The deal has been approved by boards from both the companies and it is expected to be closed by end of 2016.