Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
A fast RECCOUNT() for SQL Server
Message
 
 
To
26/02/2003 18:03:11
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
00758494
Message ID:
00758547
Views:
23
This message has been marked as the solution to the initial question of the thread.
Hi Ryan,

There're two ways to get total records count in SQL Server table:
1. SELECT COUNT(*) FROM mytable
Produces correct count but may take long on tables with a lot of records.
2. SELECT rowcnt FROM sysindexes WHERE id=OBJECT_ID('dbo.mytable') and indid < 2
Fast but result isn't relaible because it's based on data stored in sysindexes tables which may or may not be current.

With parameters you specified SP_TABLE_VALIDATION uses the first method (SELECT COUNT(*)) to get total record count. So you may just use SELECT COUNT(*).

>Doug's post about a general counting SP got me thinking about a function I wrote a long time ago in VFP that will do a fast total record count on a table in SQL Server (sorry, not filtered but more like the RECCOUNT() function of VFP). I thought I would share it.
>
>I am not sure how this could be made to work as stored procedure, but as a VFP function it works great. It would be interesting if someone couLd make this a SP that either returned a result set of the record count or an output parameter.
>
>
>** gnSql is assumed to be a public connection handle to SQL Server.
>**
>** The same system SP is called twice once with 0 as the count parameter and
>** then as null for the second call. SQL Server could report incorrect
>** results if only one call is made, see SP_TABLE_VALIDATION in BOL.
>**
>FUNCTION SQLRecCount(cTableName)
>LOCAL nCount
>nCount = 0
>IF SQLEXEC(gnSql,;
>    "{Call SP_TABLE_VALIDATION ('" + cTableName + "',?@nCount)}") > 0
>    nCount = NULL
>    IF SQLEXEC(gnSql,;
>       "{Call SP_TABLE_VALIDATION ('" + cTableName + "',?@nCount)}") > 0
>       RETURN INT(nCount)
>    ENDIF
>ENDIF
>
--sb--
Previous
Reply
Map
View

Click here to load this message in the networking platform