Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

udf_is_database_available

By Wilfred van Dijk,

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

Total article views: 811 | Views in the last 30 days: 0
 
Related Articles
FORUM

checking database online or not

checking database online or not

FORUM

checking database online or not

checking database online or not

FORUM

Check Database Integrity

Error Check Database Integrity - Maintenance Plans

FORUM

Maintenance Plan freezes on Check Database Integrity

Maintenance Plan freezes on Check Database Integrity

FORUM

script for Checking database online or not

script for Checking database online or not

Tags
 
Contribute