This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing the use of sp_MSforeachtable stored procedure of SQL Server.
This is an undocumented object of SQL Server, but still, you can use this stored procedure.
When we require executing same T-SQL command for all the tables, we can use this stored procedure.
Many times we need to run drop/truncate command for all tables or REINDEX all tables, at that point instead of the manual script we can use this stored procedure.
Sharing few examples:
Truncate all tables of current database:
1 |
EXEC sp_MSforeachtable 'TRUNCATE TABLE ?' |
Check the count of all tables:
1 |
EXEC sp_MSforeachtable 'TRUNCATE TABLE ?' |
Drop all tables of current database:
1 |
EXEC sp_MSforeachtable 'DROP TABLE ?' |
REINDEX all tables of current database:
1 |
EXEC sp_MSforeachtable 'DBCC DBREINDEX(''?'')' |