Etiket arşivi: mssql rowcount

SQL tablo kayıt sayılarını bulmak

Mssql tablodaki satır sayısını bulmak.. sp_MSForEachTable  kullanımı örnek.

SELECT SCHEMA_NAME(schema_id) AS [SchemaName],
[Tables].name AS [TableName],
SUM([Partitions].[rows]) AS [TotalRowCount]
FROM sys.tables AS [Tables]
JOIN sys.partitions AS [Partitions]
ON [Tables].[object_id] = [Partitions].[object_id]
AND [Partitions].index_id IN ( 0, 1 )
-- WHERE [Tables].name = N'name of the table'
GROUP BY SCHEMA_NAME(schema_id), [Tables].name

-- yada sp_MSForEachTable ile güncel rowcount alabilirsiniz.(büyük dblerde kullanmayın,sistemi yorabilir)
CREATE TABLE #counts
( table_name varchar(255),
row_count int)
EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'
SELECT table_name, row_count FROM #counts ORDER BY row_count DESC
DROP TABLE #counts