Thursday 4 October 2012

Loop Through All Tables in a Database

The following built in System Stored Procedure can be very useful when you want to loop through all the tables on a Database:

sp_msforeachtable


Example/Syntax - Needs to be run at the context of the Database.

The below will do a row count on all your tables in a Database:

sp_msforeachtable "SELECT COUNT(*) AS [?] FROM ?"

Output would look something like:


 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.