Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

FASTCOUNT() User-Defined Function (SQL 2000 only)

By Cade Bryant,

SQL Server's COUNT() function is a slow and expensive way to count a table's rows because it scans the entire table or index.  FASTCOUNT(), which I created, is much more efficient for this purpose - its only drawback being that it may return inaccurate results if a nonlogged (e.g., bulk) data modification operation was recently performed against the table.

Syntax: SELECT dbo.FASTCOUNT('')

Because this is a function, you can use it "inline" in any SQL statement or script, just as you would use COUNT(); however, there are some syntax differences.

Unlike COUNT(), FASTCOUNT() takes a table name as its argument (rather than a column name or wildcard), instead of requiring you to suply the table name in a FROM clause.  The advantage of this is that you can pass a variable as table name without resorting to dynamic SQL.  In other words, the following command is legal:

DECLARE @Tbl sysname
SET @Tbl = 'MyTable'
SELECT dbo.FASTCOUNT(@Tbl)

(If you were to accomplish the same results with COUNT(), you would need to use dynamic SQL to pass the table name into a FROM clause).

Total article views: 826 | Views in the last 30 days: 1
 
Related Articles
FORUM
FORUM
FORUM

COUNT

COUNT Title and not IDs

FORUM

Count

Counting Articles for in Incovoice

FORUM

file count

file count

Tags
miscellaneous    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones