Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favourite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved 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: 779 | Views in the last 30 days: 1
 
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

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones