How to get the row count of a table?

  • I am using ADO in VC++ to access my SQL server database. How do I get the number of rows of a particular table in the database?

    Thanks,

    Krishnan

  • Hi Krishnan,

    quote:


    I am using ADO in VC++ to access my SQL server database. How do I get the number of rows of a particular table in the database?


    you mean something like

    iRetVal = recSet->GetRecourdCount(); ?

    Cheers,

    Frank

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • No, what I want is to now the number of rows(records) stored in a table, without executing a query to the database like SELECT * FROM CUSTOMERS. For eg, I want to know the number of customer records stored in the CUSTOMERS table. Is there any way to get this info?

    BTW, I have found out one way and that is to use SELECT COUNT(*) from CUSTOMERS. But, I feel there must be a better and more efficient way to get the same result!

    Thanks,

    Krishnan

  • Hi Krishnan,

    quote:


    BTW, I have found out one way and that is to use SELECT COUNT(*) from CUSTOMERS. But, I feel there must be a better and more efficient way to get the same result!


    I don't think that there is a more efficient way than SELECT COUNT(.), but if you come across one, I will be happy to know

    Cheers,

    Frank

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • You can use sp_spaceused @table_name which gives various bits of information about a table including the number of rows and is probably quicker than select count(*) from ....

    However, it is not always accururate but you can correct the information with sp_spaceused <table_name>, 'TURE'. I forget when it gets out of sync with the actual table but I'm sure someone can remind me.

    Jeremy

  • quote:


    You can use sp_spaceused @table_name which gives various bits of information about a table including the number of rows and is probably quicker than select count(*) from ....

    However, it is not always accururate but you can correct the information with sp_spaceused <table_name>, 'TRUE'. I forget when it gets out of sync with the actual table but I'm sure someone can remind me.


    sp_spaceused gets its data from (among other places) sysindexes. This data may be out of date if, for example, you've just deleted an index.

    Thomas Rushton
    blog: https://thelonedba.wordpress.com

  • Thank you guys I think this should work!

  • And thats like what it does:

    select i.rows as [rows]

    from sysindexes i

    where i.indid < 2

    and i.id = object_id('Customers')

    Cheers.

  • Exec sp_mstablespace <tablename>

    paul


    paul

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply