{"id":271,"date":"2013-03-08T09:44:23","date_gmt":"2013-03-08T09:44:23","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2013\/03\/08\/how-to-get-number-of-rows-in-a-table-without-using-count-function\/"},"modified":"2013-03-08T09:44:23","modified_gmt":"2013-03-08T09:44:23","slug":"how-to-get-number-of-rows-in-a-table-without-using-count-function","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2013\/03\/08\/how-to-get-number-of-rows-in-a-table-without-using-count-function\/","title":{"rendered":"How to get number of rows in a table without using count function"},"content":{"rendered":"

How to get number of rows in a table without using count function?<\/strong><\/p>\n

In SQL Server you can get the approximate number of rows in the table using the below sample script.<\/p>\n

[sql]
\nUSE AdventureWorks
\nGO
\nDECLARE @tablename AS sysname = ‘Production.Product’;<\/p>\n

DBCC UPDATEUSAGE (0, @tablename)
\n WITH NO_INFOMSGS;<\/p>\n

SELECT [ROWS]
\nFROM SYS.indexes AS IND
\n INNER JOIN
\n SYS.partitions AS PRT
\n ON IND.object_id = PRT.object_id
\n AND IND.index_id = PRT.index_id
\nWHERE IND.object_id = OBJECT_ID(@tablename)
\n AND IND.index_id <= 1;
\n[\/sql]<\/p>\n

This script uses sys.partitions to fetch the number of rows in the table.
\nMore about sys.partitions can be found here http:\/\/msdn.microsoft.com\/en-us\/library\/ms175012(v=sql.105).aspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

How to get number of rows in a table without using count function? In SQL Server you can get the approximate number of rows in the table using the below sample script. [sql] USE AdventureWorks GO DECLARE @tablename AS sysname = ‘Production.Product’; DBCC UPDATEUSAGE (0, @tablename) WITH NO_INFOMSGS; SELECT [ROWS] FROM SYS.indexes AS IND INNER…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/271"}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=271"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}