Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2003, 10:23 AM
Authorized User
 
Join Date: Jun 2003
Location: London, , United Kingdom.
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default Row Count

How can I count a row in a table can anyone out there help me?
I made a mistake in the previous message.

Regards,


CarlWalden
__________________
CarlWalden
 
Old June 9th, 2003, 10:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: London, , United Kingdom.
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If all you want is the number of rows in the table :

Select count(*) from TableA

If you want the number of a specific row, you're out of luck because SQLserver (or any Relational db) doesn't have the notion of row numbers

HTH
 
Old June 9th, 2003, 10:32 AM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I would use

Select count(primarykeycolumnname) from TableA..

This gets rid of the performance hit you take when using a * in a query.

Hal Levy
Daddyshome, LLC
 
Old June 9th, 2003, 11:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: London, , United Kingdom.
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually, there is no performance hit here. All you are doing is asking the query engine to count rows, and return the answer as a singleton. using '*' in queries slows things down mainly because of the marshalling and bandwidth overheads incurred with moving all the extra data around.
 
Old June 9th, 2003, 11:32 AM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Chris,

That goes against everything I have ever been told.

I was told that the query engine, when getting a * request, will go out and get a listing of all the columns first and then look at what has to be done. So, It would get the * and then go "oh, I need to count rows".. Rather than know it's counting rows and not do the lookup for the columns. Perhaps I was misinformed or something changed in later versions of SQL server.

EDIT: So I just ran the query in Query Analyzer and got an execution plan and it certainly seems like it knows it's counting and does not take the performance hit I was expecting. I was missinformed.

Hal Levy
Daddyshome, LLC
 
Old June 9th, 2003, 12:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

As you've seen, COUNT(*) is special cased by the optimizer. The asterisk in COUNT(*) doesn't mean 'all the columns' but rather 'all the rows'. It means 'all the columns' only when it is all by itself or prepended with a table name or alias.

COUNT(*) is actually quite fast, as the number of rows in a table is a statistic that is readily available to the optimizer.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old June 9th, 2003, 12:47 PM
Authorized User
 
Join Date: Jun 2003
Location: Colombo 04, WP, Sri Lanka.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Preethi Send a message via Yahoo to Preethi
Default

You can use
select rows from sysindexes where id = object_id('TableA') and indid<2


Note that TableA is enclosed within quotation marks ('TableA')


Preethi
(G.R.Preethiviraj Kualsingham)

Preethi
(G.R.Preethiviraj Kulasingham)




Similar Threads
Thread Thread Starter Forum Replies Last Post
SQLDataSource row count areid ASP.NET 2.0 Basics 3 June 5th, 2010 06:02 AM
Row Count in each table ayazhoda Access VBA 0 April 15th, 2008 04:31 PM
advance count (2 counts in a row) avishni SQL Language 3 November 23rd, 2006 05:04 AM
Row count Discrepancy bph SQL Server 2000 9 January 26th, 2006 02:04 PM
SELECT COUNT as a ROW?? t0ny SQL Language 3 December 8th, 2004 05:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.