Quick and easy way to know if SQL table has any records?

A form in my vb.net program needs to know if the LocInv table has any records.  I have been using this code:

        sSQL = "SELECT COUNT(*) FROM LocInv" 'check to see if this file is in use...
        sSQLresult = RunSQL_Select(sSQL, True)
        If Val(sSQLresult) > 0 Then
            pbC1_UseInv = True
            pbC1_UseInv = False
        End If

The problem is that LocInv has almost 100,000 records, and that is taking a few seconds to 'count' on my (slow) portable device.  So I need to know if there is an easy way to code this to more quickly determine whether or not the LocInv table has any records in it--without having to do a 'count' command.  Note that this is the compact framework environment, meaning I have a limited number of commands available to me.  TIA
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
Quickerer and dirtier...
SELECT row_count FROM sys.dm_db_partition_stats WHERE OBJECT_NAME(object_id) = 'LocInv'

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
This code looks somewhat awkward but it's the most accurate way that's also extremely fast:

"SELECT (SELECT CASE WHEN EXISTS(SELECT TOP (1) NULL FROM LocInv) THEN 1 ELSE 0 END)" 'check to see if this file is in use...

I've seen cases where sys.dm_db_partition_stats is not reliable, particularly when a large table has been truncated.
sasllcAuthor Commented:
When I tried to use these in my program I got numerous syntax errors.  I'm guessing that it is due to a combination of this program being developed in the compact framework environment with its numerous restrictions, plus my lack of knowledge.

But it occurred to me that I might be able to try and read the first record of the file to see if I was already at the end of file--meaning it is empty.  This code worked for me:

        sSQL = "SELECT Bale FROM LocInv"

        cmd = New SqlCeCommand(sSQL, cn)
        cmd.CommandType = CommandType.Text
        dtr = cmd.ExecuteReader(CommandBehavior.Default)

        CkEOF = dtr.Read()
        If CkEOF = False Then 'if false, then there are no more records to read...
            pbC1_UseInv = False
            pbC1_UseInv = True
        End If
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.