When the system tables get out of sync in SQL Server 2000 the following SQL statement must be run to synchronize them.
1 2 | dbcc updateusage(0) GO |
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, these values are always maintained correctly. Databases created on SQL Server 2005 should never experience incorrect counts, however, databases upgraded to SQL Server 2005 may contain invalid counts. We recommend running DBCC UPDATEUSAGE after upgrading to SQL Server 2005 to correct any invalid counts.Googling for information on getting table counts without a row scan yields several posts on the issue, but they involve creating functions and needlessly complicating a simple SQL statement.
There are 2 possible solutions. 1) Common Table Expressions and 2) Use a join. The first option looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | WITH CTETableRowCount AS ( SELECT rows rowCnt , ID ObjectID --, si.* FROM sysindexes si WHERE indid < 2 ) SELECT so.[ name ] , cte.[rowCnt] as [Row Count ] --, so.* FROM sysobjects so LEFT JOIN CTETableRowCount cte ON so.id=cte.ObjectID WHERE type= 'U' and so. name != 'dtproperties' ORDER BY so.[ name ] |
1 2 3 4 5 6 7 | SELECT so.[ name ] , si.[ rows ] as [Row Count ] --, so.* FROM sysobjects so LEFT JOIN sysindexes si ON so.id=si.ID WHERE (type= 'U' ) and (so. name != 'dtproperties' ) and (si.indid < 2) ORDER BY so.[ name ] |
No comments:
Post a Comment