Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
Reply With Quote
  #2 (permalink)  
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
Reply With Quote
  #3 (permalink)  
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,102
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
Reply With Quote
  #4 (permalink)  
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.
Reply With Quote
  #5 (permalink)  
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,102
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
Reply With Quote
  #6 (permalink)  
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
Reply With Quote
  #7 (permalink)  
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)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:02 AM.


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