What would be the correct SQL statement to determine if a database is running or stopped?
asked 03 Jan, 19:47
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.