[dba-SQLServer] For Each

Arthur Fuller fuller.artful at gmail.com
Wed Aug 27 07:56:44 CDT 2008

I was positive that at some point I had encountered a sproc that would let
me do things like this (pseudo-code):

exec sp_foreach @objectType = 'Table', @command = 'UPDATE STATISTICS ?'

where the question mark indicates the object name in the iterative process.
So the statement above would update the stats on all user tables and require
no adjustment should I add or remove any tables. Given this construct, I
could also rebuild all indexes on all tables or defrag all tables, etc.
without having to write the statements explicitly.

But I can't find this procedure and I'm beginning to think I only dreamed of
it rather than encountered it in the real world. Have you encountered this
particular beast anywhere? I've looked through the system procedures and
searched MSDN and Googled and I can't find anything, with the exception of a
ForEach construct in SQL 2008 SSIS packages.


More information about the dba-SQLServer mailing list