Home Forums Programming General How to collect data from different databases within a stored procedure? RE: How to collect data from different databases within a stored procedure?

  • The use of sp_MSforeachdb can have unintended results. It will occasionally miss databases. There are better methods (such as a cursor - and considering msforeachdb is just a loop, a cursor is ok).

    Here is an article talking about the behavior and a workaround.

    http://sqlblog.com/blogs/aaron_bertrand/archive/2010/12/29/a-more-reliable-and-more-flexible-sp-msforeachdb.aspx

    Here is a different version of msforeachdb by Gianluca that is what I prefer to use.

    http://spaghettidba.com/2011/09/09/a-better-sp_msforeachdb/

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events