Original: "SQL" counts the rows of all tables
Environment:MSSQL ENT 2k8 R2
principle: traverse all user tables, get the number of rows per table with the sp_spaceused process, write to the temp table, and finally return to the temporary table
IF object_id('tempdb: #TableRowCount','U') is not NULL DROP TABLE#TableRowCountGOCREATE TABLE#TableRowCount (Name sysnamePRIMARY KEY, rowcntDECIMAL( One,0), ReservedVARCHAR( -), DataVARCHAR( -), index_sizeVARCHAR( -), UnusedVARCHAR( -))GODECLARECurcntalltablerowsCURSORLOCAL Fast_forward for SELECTName fromsys.tablesOPENcurcntalltablerowsDECLARE @TableNamesysnameFETCH NEXT fromCurcntalltablerows into @TableName while @ @FETCH_STATUS=0BEGIN INSERT into#TableRowCountEXECSys.sp_spaceused@TableName FETCH NEXT fromCurcntalltablerows into @TableNameENDCLOSEcurcntalltablerowsdeallocatecurcntalltablerowsSELECTName,rowcnt from#TableRowCountWHERERowcnt>0ORDER byRowcntDESC, Name
Wenbi.
"SQL" counts the number of rows for all tables