Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get Record Count for each table in a database.


Get Record Count for each table in a database.

Author
Message
RBarryYoung
RBarryYoung
SSCrazy Eights
SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)

Group: General Forum Members
Points: 9448 Visits: 9517
Jeff Moden (8/10/2008)
rbarryyoung (8/10/2008)
Gimme time to eat lunch, Jeff! Tongue Smile


Heh... hey! I need to know... does brother Darth have to take off the Chevy look-alike to eat?


Straws. And shakes. Lots of shakes. Milk shakes, oatmeal shakes, yam shakes, broccoli shakes, steak shakes, spinach shakes, etc. Sick

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Jeffrey Williams 3188
Jeffrey Williams 3188
SSCarpal Tunnel
SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)

Group: General Forum Members
Points: 4463 Visits: 9833
Jeff Moden (8/10/2008)
Again, I ask... do you have to update usage for the rowcounts to be accurate?


Well, according to BOL - no, you don't have to update usage.


Reports and corrects pages and row count inaccuracies in the catalog views. These inaccuracies may cause incorrect space usage reports returned by the sp_spaceused system stored procedure. In SQL Server 2005 and later, these values are always maintained correctly. Databases upgraded from SQL Server 2000 may contain invalid counts. We recommend running DBCC UPDATEUSAGE after upgrading to correct any invalid counts.


But, as we all know - BOL is not always correct. There are situations where you will need to update usage, but I am not sure what those are. We just went through a large purge operation and these numbers were updated accurately.

Jeffrey Williams
Problems are opportunities brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs

toniupstny
toniupstny
SSC Veteran
SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)

Group: General Forum Members
Points: 248 Visits: 940
Since I went ahead and made the query which actually does provide the table and associated rowsize without cursors thought I would post it. (though I am sure Barry's does a LOT more ).

SELECT so.name 'Table', si.rows 'Rows'
FROM
(SELECT Name FROM sysobjects WHERE type='U') so
JOIN
(SELECT rows,id,indid FROM sysindexes) si
ON si.id = OBJECT_ID(so.name) AND si.indid < 2




Toni
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45173 Visits: 39925
So... hedging a bet against BOL and changing the code to something a bit more conventional, we end up with this...


DBCC UPDATEUSAGE (0)
SELECT so.Name AS TableName,
si.Rows AS [Rows]
FROM sys.SysObjects so
INNER JOIN sys.SysIndexes si
ON si.ID = so.ID
WHERE si.IndID < 2
AND so.XType = 'U'
AND OBJECTPROPERTY(so.ID,'IsMSShipped') = 0



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45173 Visits: 39925
... and, with SQL Server 2000, we end up with this...


DBCC UPDATEUSAGE (0)
SELECT so.Name AS TableName,
si.Rows AS [Rows]
FROM dbo.SysObjects so
INNER JOIN dbo.SysIndexes si
ON si.ID = so.ID
WHERE si.IndID < 2
AND so.XType = 'U'
AND OBJECTPROPERTY(so.ID,'IsMSShipped') = 0



Yeah... I know... if I didn't use the two part naming convention, they'd both work and they'd both be identical. Actually, the code above will work in both environments... I just have an affinity for the two part naming convention for a lot of reasons. Tongue

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
RBarryYoung
RBarryYoung
SSCrazy Eights
SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)

Group: General Forum Members
Points: 9448 Visits: 9517
Jeffrey Williams (8/10/2008)

Well, according to BOL - no, you don't have to update usage.

Hmm, yeah I see where it says that Jeff. I may be wrong here, when I wrote my view (about 2-3 years ago) I was still working on both 2000 & 2005 most days and I remember having accuracy problems that ultimately caused my to add DBCC UpdateUsage periodically. But it may have been only happening on SQL 2000.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Jeffrey Williams 3188
Jeffrey Williams 3188
SSCarpal Tunnel
SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)

Group: General Forum Members
Points: 4463 Visits: 9833
rbarryyoung (8/10/2008)

Hmm, yeah I see where it says that Jeff. I may be wrong here, when I wrote my view (about 2-3 years ago) I was still working on both 2000 & 2005 most days and I remember having accuracy problems that ultimately caused my to add DBCC UpdateUsage periodically. But it may have been only happening on SQL 2000.


Either way, it doesn't hurt to update usage as long as you have the time. The important thing about any of these techniques (yours, mine and Jeff's) are that they are not cursor based and return the desired results.

Jeffrey Williams
Problems are opportunities brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search