on 2017 Jan 03 7:47 PM
What would be the correct SQL statement to determine if a database is running or stopped?
Thanks
You can only connect to a running database, so there is no direct way via SQL statements to query the database itself whether it is running. (You can however analyse the error code of a failing connect attempt to find out whether the database is not running in contrast to wrong credentials, missing authentication or the like.)
If there are several databases running on the same server, you can connect to one of them and use the sa_db_list() builtin procedure or the db_name()/next_database() functions to query the names of all running databases on that server. - Note, for v16 and above, querying properties of other databases requires particular privileges.
For v12 and below, you might also use the phantom utility_db database to query for databases running on a given server if using that has been enabled.
Additionally, the DBLOCATE (for network servers) and DBPING tools may be of help here.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, see the docs, here's how to use sa_db_list() to get the names of the running databases:
SELECT DB_NAME(Number) FROM sa_db_list();
and return either the current status, or at a minimum, if it's actually running or not
Are you coming from an ASE/MS SQL Server background - where user databases are managed by one central master database, so you know what databases "belong" to a particular server? - It's different with SQL Anywhere, here each database is on its own, and so any database can not "know" whether another database is "active or not", it can just tell whether there are other databases running on the same server (when the querying connection has appropriate privileges) or not. So there's no "status" for databases on a server.
But here's a query to return 1 if the desired database name is running on that engine, and 0 otherwise (and should work with v10 and above):
select count(*) as db_is_running
from sa_db_list()
where db_name(Number) = 'MyDbName';
It looks like you were editing your reply when I posted my earlier comment. Have now added this concept to a utility that lists all DB's. Sometimes when a customer runs a lockbox file against several DB's it throws an error - prior to running a backup is made of each DB and the software then attempts a restore, but for some reason, and only intermittently, it will fail to start the last DB on the list - finding that and starting it again has been a pain in the butt! Now I can see graphically which one is stopped
User | Count |
---|---|
62 | |
10 | |
7 | |
7 | |
6 | |
6 | |
6 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.