• John Mitchell-245523 (1/21/2014)


    But you don't want it running against databases that don't have the tables - that would be a waste of resources. That's why I suggested scripting it properly. Of course, if databases are being added or removed all the time, that would make it more difficult.

    sp_MSforeachdb is an undocumented stored procedure, so I wouldn't use it for something that will run regularly in production. I know that some take the view that it's been unchanged for many years now and is therefore fairly safe. You'll make your own mind up about that.

    John

    +1

    use a cursor to loop through. If this all user databases you can easily build up a list of them in the cursor FOR clause with 'select name from master.sys.databases where database_id > 4'. That way its flexible and automatically copes with databases being dropped or added. You can also exclude\include based on the database status if say you have offline or read-only databases.

    Complex logic is much easier with cursors than that yukky sp_msforeachdb

    ---------------------------------------------------------------------