SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 
        
Home       Members    Calendar    Who's On



Raw count for all tables in databsae Expand / Collapse
Author
Message
Posted Tuesday, June 30, 2009 6:40 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, September 04, 2009 9:35 AM
Points: 166, Visits: 443
Hi all,

how can i get report to get row count for all tables in database please!! guide me!!
Post #744360
Posted Tuesday, June 30, 2009 8:00 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, October 09, 2009 4:46 AM
Points: 450, Visits: 131
IF EXISTS (SELECT [name] From sysobjects WHERE [name] ='spReporttbleInfo' and xType ='P')
BEGIN
DROP PROCEDURE spReporttbleInfo
END


GO

CREATE PROCEDURE spReporttbleInfo
(
@Database Varchar(100)
)
AS
BEGIN


DECLARE @SQLStr NVARCHAR(4000)
DECLARE @TblName VARCHAR(155)


--Create a temp table and Insert all the User tables in the database

IF EXISTS( SELECT [name] from tempdb..sysObjects WHERE [name] ='#Store' and xtype ='U')
BEGIN
DROP TABLE #Store
END


CREATE TABLE #store (tblName VARCHAR(255))

SET @SQLStr = N'INSERT INTO #Store
SELECT obj.[Name]
FROM ' +@Database + '..sysObjects obj
WHERE xType =''U'''

EXEC sp_ExecuteSQL @SQLStr


--Create a temp table to Store all the info returned by sp_spaced used

IF EXISTS( SELECT [name] from tempdb..sysObjects WHERE [name] ='#details' and xtype ='U')
BEGIN
DROP TABLE #details
END

CREATE TABLE #Details ([Name] VARCHAR(255),[Rows] BIGINT, Reserved VARCHAR(20), [data] VARCHAR(10),indexSize VARCHAR(10), unused VARCHAR(10))


--Cursor to execute sp_spaced used for each of the tables in the database

DECLARE tblCSR CURSOR FOR
SELECT * FROM #store
OPEN tblCSR

FETCH NEXT FROM tblCSR INTO @TblName

WHILE @@FETCH_STATUS = 0
BEGIN

SET @SQLstr =N'EXECUTE ' + @Database + '..sp_spaceUsed ' + @TblName

INSERT INTO #Details
EXEC sp_executeSQL @SQLstr


FETCH NEXT FROM tblCSR INTO @TblName


END

CLOSE tblCSR
DEALLOCATE tblCSR

SELECT * FROM #Details



END






Post #744427
Posted Tuesday, June 30, 2009 9:26 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Today @ 4:00 AM
Points: 776, Visits: 1,909
pat (6/30/2009)
how can i get report to get row count for all tables in database

Hi,
Try Another method

USE DB
GO
SELECT
a.name, object_name (i.id) TableName, rows as RowCnt
FROM sysindexes i INNER JOIN sysobjects o ON (o.id = i.id AND o.xType = 'U')
inner join sysusers a on o.uid = a.uid
WHERE indid < 2
ORDER BY 3 desc, TableName

ARUN SAS
Post #745043
Posted Tuesday, June 30, 2009 9:34 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 12:19 PM
Points: 1,261, Visits: 1,501
arun.sas (6/30/2009)
pat (6/30/2009)
how can i get report to get row count for all tables in database

Hi,
Try Another method

USE DB
GO
SELECT
a.name, object_name (i.id) TableName, rows as RowCnt
FROM sysindexes i INNER JOIN sysobjects o ON (o.id = i.id AND o.xType = 'U')
inner join sysusers a on o.uid = a.uid
WHERE indid < 2
ORDER BY 3 desc, TableName

ARUN SAS


Arun,

Thanks for this... it's something I hadn't seen before.


Wayne
For better assistance in answering your questions, click here
For performance problems, please read this.
Post #745046
Posted Tuesday, June 30, 2009 9:38 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 12:19 PM
Points: 1,261, Visits: 1,501
SimonH (6/30/2009)

IF EXISTS( SELECT [name] from tempdb..sysObjects WHERE [name] ='#Store' and xtype ='U')
BEGIN
DROP TABLE #Store
END


Simon,
Just as an FYI, this is not the correct way to check for a temp table. As coded above, it won't find the temp table and if it does exist, you'll get an error when trying to create it again.

Instead, you should run:
if object_id('tempdb..#Store') is not null DROP TABLE #Store

Take a look at this article - it talks about it.


Wayne
For better assistance in answering your questions, click here
For performance problems, please read this.
Post #745048
Posted Tuesday, June 30, 2009 10:24 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 6:34 PM
Points: 1,509, Visits: 3,988
This script will give the row counts and space used for each table in a database.

Script to analyze table space usage
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61762
Post #745063
Posted Tuesday, June 30, 2009 10:32 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 6:34 PM
Points: 1,509, Visits: 3,988
WayneS (6/30/2009)
SimonH (6/30/2009)

IF EXISTS( SELECT [name] from tempdb..sysObjects WHERE [name] ='#Store' and xtype ='U')
BEGIN
DROP TABLE #Store
END


Simon,
Just as an FYI, this is not the correct way to check for a temp table. As coded above, it won't find the temp table and if it does exist, you'll get an error when trying to create it again.

Instead, you should run:
if object_id('tempdb..#Store') is not null DROP TABLE #Store

Take a look at this article - it talks about it.


This is a better way to check for the existence of a temp table:
if object_id('tempdb..#Store','U') is not null DROP TABLE #Store

When you add the second argument, 'U', to the object_id function, it makes sure it is a table and not some other type of temporary object, like a stored procedure. This works with all versions of SQL Server from 7.0 forward.





Post #745065
Posted Wednesday, July 01, 2009 1:07 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, October 09, 2009 4:46 AM
Points: 450, Visits: 131
Wayne,

Sorry it should have read Like '#store%'


Perhaps still not the best way to to do it.


Simon
Post #745128
Posted Sunday, July 19, 2009 11:47 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:32 PM
Points: 18,143, Visits: 12,163
Ummmmm.... be careful folks. If you don't run DBCC UPDATEUSAGE(0), you can get some seriously incorrect information. I'll try not to bad mouth the cursor/loop solutions on this too badly... And, we don't need any temp tables for this either. The simple solutions using SysIndexes are good but you must use the DBCC command I mentioned above first.

As Micheal pointed out in code, rowcounts are the tip of the iceberg. So, try this... details are where they usually are... in the comments in the code...
--_______________________________________________________________________________________________________________________
/**********************************************************************************************************************
Purpose:
Returns a single result set similar to sp_Space used for all user tables at once.
&#160;
Notes:
1. May be used as a view, stored procedure, or table-valued funtion.
2. Must comment out 1 "Schema" in the SELECT list below prior to use. See the adjacent comments for more info.
&#160;
Revision History:
Rev 00 - 22 Jan 2007 - Jeff Moden
- Initital creation for SQL Server 2000
Rev 01 - 11 Mar 2007 - Jeff Moden
- Add automatic page size determination for future compliance
Rev 02 - 05 Jan 2008 - Jeff Moden
- Change "Owner" to "Schema" in output. Add optional code per Note 2 to find correct schema name
**********************************************************************************************************************/
--===== Ensure that all row counts, etc is up to snuff
-- Obviously, this will not work in a view or UDF and should be removed if in a view or UDF. External code should
-- execute the command below prior to retrieving from the view or UDF.
DBCC UPDATEUSAGE(0) WITH COUNT_ROWS, NO_INFOMSGS
&#160;
--===== Return the single result set similar to what sp_SpaceUsed returns for a table, but more
SELECT DBName = DB_NAME(),
--SchemaName = SCHEMA_NAME(so.UID), --Comment out if for SQL Server 2000
SchemaName = USER_NAME(so.UID), --Comment out if for SQL Server 2005
TableName = so.Name,
TableID = so.ID,
MinRowSize = MIN(si.MinLen),
MaxRowSize = MAX(si.XMaxLen),
ReservedKB = SUM(CASE WHEN si.IndID IN (0,1,255) THEN si.Reserved ELSE 0 END) * pkb.PageKB,
DataKB = SUM(CASE WHEN si.IndID IN (0,1 ) THEN si.DPages ELSE 0 END) * pkb.PageKB
+ SUM(CASE WHEN si.IndID IN ( 255) THEN ISNULL(si.Used,0) ELSE 0 END) * pkb.PageKB,
IndexKB = SUM(CASE WHEN si.IndID IN (0,1,255) THEN si.Used ELSE 0 END) * pkb.PageKB
- SUM(CASE WHEN si.IndID IN (0,1 ) THEN si.DPages ELSE 0 END) * pkb.PageKB
- SUM(CASE WHEN si.IndID IN ( 255) THEN ISNULL(si.Used,0) ELSE 0 END) * pkb.PageKB,
UnusedKB = SUM(CASE WHEN si.IndID IN (0,1,255) THEN si.Reserved ELSE 0 END) * pkb.PageKB
- SUM(CASE WHEN si.IndID IN (0,1,255) THEN si.Used ELSE 0 END) * pkb.PageKB,
Rows = SUM(CASE WHEN si.IndID IN (0,1 ) THEN si.Rows ELSE 0 END),
RowModCtr = MIN(si.RowModCtr),
HasTextImage = MAX(CASE WHEN si.IndID IN ( 255) THEN 1 ELSE 0 END),
HasClustered = MAX(CASE WHEN si.IndID IN ( 1 ) THEN 1 ELSE 0 END)
FROM dbo.SysObjects so,
dbo.SysIndexes si,
(--Derived table finds page size in KB according to system type
SELECT Low/1024 AS PageKB --1024 is a binary Kilo-byte
FROM Master.dbo.spt_Values
WHERE Number = 1 --Identifies the primary row for the given type
AND Type = 'E' --Identifies row for system type
) pkb
WHERE si.ID = so.ID
AND si.IndID IN (0, --Table w/o Text or Image Data
1, --Table with clustered index
255) --Table w/ Text or Image Data
AND so.XType = 'U' --User Tables
AND PERMISSIONS(so.ID) <> 0
GROUP BY so.Name,
so.UID,
so.ID,
pkb.PageKB
ORDER BY ReservedKB DESC
&#160;

Basically, I did what MS should have done with sp_SpaceUsed. In fact, I stole most of the code from that computational slice of heaven. Expect it to take a minute or two the first time you run it because people don't normally include the DBCC command in their normal maintanence. I use the "Rows" and "RowModCtr" columns to decide when I want to update statistics. I also schedule a proc with this in it to take occasional "snapshots" of the databaseto see where growth is occurring and what the growth is being caused by. Comes in handy when it's time to justify more disk space.


--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."

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #755511
« Prev Topic | Next Topic »


Permissions Expand / Collapse