If you're running >= sql2005 you might want to use sys.databases instead... Also there are some dbs that will cause your script to fail so you'd want to eliminate them. Offline dbs will cause an error. Maybe you don't want to back up a read-only db either. Try this where clause:
where status & (
-- FOR EACH DB PROPERTY SHOWN BELOW, UNREM THE LINE ONLY IF YOU DO NOT WANT TO GET A DB WITH THAT PROPERTY
-- + 1 -- autoclose (ALTER DATABASE)
-- + 4 -- select into/bulkcopy (ALTER DATABASE using SET RECOVERY)
-- + 8 -- trunc. log on chkpt (ALTER DATABASE using SET RECOVERY)
-- + 16 -- torn page detection (ALTER DATABASE)
+ 32 -- loading
+ 64 -- pre recovery
+ 128 -- recovering
+ 256 -- not recovered
+ 512 -- offline (ALTER DATABASE)
+ 1024 -- read only (ALTER DATABASE)
-- + 2048 -- dbo use only (ALTER DATABASE using SET RESTRICTED_USER)
-- + 4096 -- single user (ALTER DATABASE)
-- + 32768 -- emergency mode
-- + 4194304 -- autoshrink (ALTER DATABASE)
-- + 1073741824 -- cleanly shutdown
) = 0
and name not in ('master','tempdb','model','msdb','pubs','Northwind')
commented on May 18 2010 2:58PM