A couple of days ago, I came across an issue where I had to get the total number of records in the memory optimized table. Finally, I found the solution but during the research, I came across that you can get the total number of records of any tables, views, indexes using OBJECTPROPERTYEX as well. Basically, in SQL Server 2012, a new property namely Cardinality has been added in the OBJECTPROPERTYEX and the purpose of this property is to return the total number of the records in any object.
Given below is the simple script.
--This script is compatible with SQL Server 2012 and above. USE AdventureWorks2012 GO SELECT OBJECT_SCHEMA_NAME(OBJECT_ID) AS [Schema Name] ,name AS [Table Name] ,OBJECTPROPERTYEX(OBJECT_ID, N'Cardinality') AS [Total No of Records] FROM sys.tables --OUTPUT
Let me know your feedback about this approach.