Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / December 2007

Tip: Looking for answers? Try searching our database.

Looping tables?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paulo - 27 Dec 2007 11:26 GMT
Hi, I would like to know if is possible to loop through all tables (via pure
sql statement or SP) on a database and gives me the record count from each
one...

Because I need to know the table wich has more records on it! Can you help
me ?

Thanks!
Tom Moreau - 27 Dec 2007 12:00 GMT
You can use the undocumented sp_foreachtable:

sp_foreachtable ('select ''?'', count (*) from ?')

Signature

  Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau

Hi, I would like to know if is possible to loop through all tables (via pure
sql statement or SP) on a database and gives me the record count from each
one...

Because I need to know the table wich has more records on it! Can you help
me ?

Thanks!
Paulo - 27 Dec 2007 12:07 GMT
On SQL 2005?

> You can use the undocumented sp_foreachtable:
>
[quoted text clipped - 9 lines]
>
> Thanks!
Tom Moreau - 27 Dec 2007 13:28 GMT
It works for 7.0, 2000 and 2005.

Signature

  Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau

On SQL 2005?

"Tom Moreau" <tom@dont.spam.me.cips.ca> escreveu na mensagem
news:e8MQlAISIHA.6036@TK2MSFTNGP03.phx.gbl...
> You can use the undocumented sp_foreachtable:
>
[quoted text clipped - 9 lines]
>
> Thanks!
Dan Guzman - 27 Dec 2007 13:49 GMT
> On SQL 2005?

Here's a SQL 2005-specific method:

SELECT
   t.name,
   SUM(rows) AS Rows
FROM sys.tables t
JOIN sys.partitions p ON
t.object_id = p.object_id
WHERE
   p.index_id IN(0,1)
GROUP BY
   t.name

Signature

Hope this helps.

Dan Guzman
SQL Server MVP

> On SQL 2005?
>
[quoted text clipped - 13 lines]
>>
>> Thanks!
Madhivanan - 27 Dec 2007 13:27 GMT
> You can use the undocumented sp_foreachtable:
>
[quoted text clipped - 16 lines]
>
> Thanks!

That should be

sp_msforeachtable 'select ''?'', count (*) from ?'
SB - 27 Dec 2007 12:02 GMT
> Hi, I would like to know if is possible to loop through all tables (via pure
> sql statement or SP) on a database and gives me the record count from each
[quoted text clipped - 4 lines]
>
> Thanks!

This is a bit of a hack but works for me:

select so.name,rowcnt
from sysindexes si, sysobjects so
where si.id = so.id
and so.type in ('U')
and si.status = 2066
order by so.name
Paulo - 27 Dec 2007 12:11 GMT
Very very good man!

Thanks a lot !

On Dec 27, 5:26 pm, "Paulo" <prbs...@uol.com.br> wrote:
> Hi, I would like to know if is possible to loop through all tables (via
> pure
[quoted text clipped - 5 lines]
>
> Thanks!

This is a bit of a hack but works for me:

select so.name,rowcnt
from sysindexes si, sysobjects so
where si.id = so.id
and so.type in ('U')
and si.status = 2066
order by so.name
Madhivanan - 27 Dec 2007 13:29 GMT
> Very very good man!
>
[quoted text clipped - 20 lines]
> and si.status = 2066
> order by so.name

Also refer
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/02/different-ways-to-co
unt-rows-from-a-table.aspx

TheSQLGuru - 27 Dec 2007 15:26 GMT
sysindexes is not guaranteed to be maintained to actual row count values.
See DBCC UPDATEUSAGE in BOL.

Signature

Kevin G. Boles
TheSQLGuru
Indicium Resources, Inc.
kgboles a earthlink dt net

On Dec 27, 5:26 pm, "Paulo" <prbs...@uol.com.br> wrote:
> Hi, I would like to know if is possible to loop through all tables (via
> pure
[quoted text clipped - 5 lines]
>
> Thanks!

This is a bit of a hack but works for me:

select so.name,rowcnt
from sysindexes si, sysobjects so
where si.id = so.id
and so.type in ('U')
and si.status = 2066
order by so.name
Kyle R. Hanrahan - 27 Dec 2007 13:48 GMT
Since I don't like to use undocumented procedures, since they can go away at
any time, here is the script I use for this

/* Start Script */
Create table #tmpRowCounts (
TblName varchar(128),
RowCt int
)

DECLARE crgetrows CURSOR
FOR select name from sysobjects where xtype = 'U' order by name

DECLARE @tblname varchar(128)
OPEN crgetrows

FETCH NEXT FROM crgetrows INTO @tblname
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN
 Execute('
 Declare @rowcount int
 select @rowcount = count(*) from ' + @tblname + '
 Insert into #tmpRowCounts values(''' + @tblname + ''',@rowcount)
 ')
END
FETCH NEXT FROM crgetrows INTO @tblname
END

CLOSE crgetrows
DEALLOCATE crgetrows

Select * from #tmpRowCounts

Drop table #tmpRowCounts

GO
/* End Script */

> Hi, I would like to know if is possible to loop through all tables (via
> pure sql statement or SP) on a database and gives me the record count from
[quoted text clipped - 4 lines]
>
> Thanks!
DXC - 27 Dec 2007 15:25 GMT
Take your pick.............

select o.name tablename ,i.rows tblrowcount
from sysobjects o
inner join sysindexes i on (o.id = i.id)
where o.xtype = 'U' and o.name <> 'dtproperties'
and i.indid < 2 Order by tablename

> Hi, I would like to know if is possible to loop through all tables (via pure
> sql statement or SP) on a database and gives me the record count from each
[quoted text clipped - 4 lines]
>
> Thanks!
Madhivanan - 29 Dec 2007 08:36 GMT
> Take your pick.............
>
[quoted text clipped - 14 lines]
>
> - Show quoted text -

You need to refer this as well
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/02/different-ways-to-co
unt-rows-from-a-table.aspx

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.