SQL Server – Sp_msforeachtable undocumented stored procedure


SQL Server has sp_msforeachtable as undocumented stored procedure that can be used to run query against all table in single database. Lets say you want to get the record count from each table so in that case you can use below command to generate the list of tables as well as record count.


Exec sp_msforeachtable ‘select ‘?’ Count(*) from ?’

Exec sp_msforeachtable ‘Print ‘?’ DBCC checktables(‘?’)’

Sp_msforeachtable is not supported by Microsoft, You will not find the command in Online book. These undocumented stored procedures like sp_msforeachtable, sp_msforeachdb is present under mater database.

Leave a Reply




− 6 = 2