In many procedures, you'll have to check if a database is online or if it's not read-only, or if it's not in single-user mode. Instead of copy/past this code for in several scripts, I developed a function which does some basic checking for a database. These checks are:
- make sure the database exists
- make sure the status is 'online'
- make sure it's NOT read-only (snapshots are also eliminated)
- make sure access level is multi-user
The function returns true (1) if all these criteria match.
Use it in your code like
if dbo.udf_is_database_available(@databasename) = 0
begin
raiserror('Database %s is not prepared',16,1,@databasename)
return 1
end