You can choose one of these approaches based on your requirement:--1EXECUTE sp_MSforeachtable @command1 = N'INSERT INTO ErrorCOunt(TableName, RecCount, ActualCount) SELECT TC.TableName, TC.RecCount, AC.ActualCount FROM (SELECT PARSENAME("?", 1) AS TableName, COUNT(*) AS ActualCount FROM ?) AS AC INNER JOIN TableCounts AS TC ON TC.TableName = AC.TableName AND TC.RecCount <> AC.ActualCount;';
--2INSERT INTO ErrorCOunt(TableName, RecCount, ActualCount) SELECT TC.TableName, TC.RecCount, PS.actual_count FROM sys.objects AS T INNER JOIN TableCounts AS TC ON TC.TableName = T.name CROSS APPLY (SELECT SUM(CASE WHEN P.index_id < 2 AND A.type = 1 THEN P.rows ELSE 0 END) AS actual_count FROM sys.partitions AS P INNER JOIN sys.allocation_units AS A ON P.hobt_id = A.container_id WHERE P.object_id = T.object_id) AS PS WHERE T.type = 'U' AND TC.RecCount <> PS.actual_count;
If it is up to me I would go with the later one.For us, there is only the trying. The rest is not our business. ~T.S. EliotMuhammad Al Pasha