How to exclude tempdb from sp_MSforeachdb?
A possibility would be to check for DB_Name = 'Tempdb', and if it's tempdb just end the script.

This is a quick test I did, and it seems to works :

EXEC sp_msforeachdb 'USE [?] IF DB_Name() = ''Tempdb''BEGIN RETURN END SELECT db_Name()'


That will run the code on each db, but if it's tempdb it wont execute a thing, cause it'll return.

It's also known that using sp_msforeachdb is not always the best thing. Aaron has blogged about using a self made replacement procedure you can use, it might be worth your time to go check it out! making-a-more-reliable-and-flexible-spmsforeachdb