Enumerate Tables and Execute T-SQL for each with sp_MSforeachtable
Joe Webb - The undocumented sp_MSforeachtable procedure
"...For example, we may want to disable check constraints for all tables prior to loading a bunch of data; or perhaps we need to run DBCC CHECKTABLE for every table in our database to ensure the integrity of all pages and structures that make up the tables.
We could, of course, create a script that uses a cursor to loop through all of the tables in the database to do our bidding through dynamic SQL. Yuck!
Fortunately, there's a better way. It's the undocumented sp_MSforeachtable stored procedure in the master database. It's like a cursor in that it loops through each table in the current database and executes a script that you define ...
..."
Every time I need this stored procedure I forget what it's called. Maybe if I blog it I'll remember it (or at least be able to find easier in the future... ;)
No comments:
Post a Comment