List of Databases and their Status
select
NAME,
DATABASEPROPERTY(name, N'IsDetached') as DETACHED,
(case when DATABASEPROPERTY(name, N'IsShutdown')
is null then -1
else
DATABASEPROPERTY(name, N'IsShutdown')
end) as ISSHUTDOWN,
DATABASEPROPERTY(name, N'IsSuspect') as ISSUSPECT,
DATABASEPROPERTY(name, N'IsOffline') as ISOFFLINE,
DATABASEPROPERTY(name, N'IsInLoad') as ISINLOAD,
(case when DATABASEPROPERTY(name, N'IsInRecovery')
is null then -1
else
DATABASEPROPERTY(name, N'IsInRecovery')
end) as ISINRECOVERY,
(case when DATABASEPROPERTY(name, N'IsNotRecovered')
is null then -1
else
DATABASEPROPERTY(name, N'IsNotRecovered')
end) as ISNOTRECOVERED,
DATABASEPROPERTY(name, N'IsEmergencyMode') as ISEMERGENCYMODE,
DATABASEPROPERTY(name, N'IsInStandBy'),
has_dbaccess(name) as ISINSTANDBY
from master.dbo.sysdatabases
Check whether a Database Belongs to PANTHEON
select NAME
from dbname.dbo.SYSOBJECTS
where NAME = 'PARAMSYS'
ATTACH Database
The database must be located on the server in the form of .MDF and .LDF
files, but must also be attached to the server.
You can attach the database with the SP_ATTACH_DB command.
sp_attach_db @dbname='database_name',
@filename1= 'path_and_MDF_file_name',
@filename2= 'path_and_LDF_file_name'
database_name - the name of the database that you wish to attach.
path_and_MDF_file_name - the path and file name of the database
file (.mdf).
path_and_LDF_file_name - the path and file name of the database log
file (.ldf).
Example:
sp_attach_db @dbname='msDataLab',
@filename1= 'c:\mssql7\data\msDataLab.MDF',
@filename2= 'c:\mssql7\data\msDataLab.LDF'
DETACH Database
The database file and log file (.MDF and .LDF) cannot be manipulated as long
as they are attached to the server. You have to detach them before you can move,
delete, etc. them.
sp_detach_db @DbName='database_name'
database_name - the name of the database that you want to detach from
the server.
Example:
sp_detach_db @DbName='msDataLab'
Log Size
This query returns the size of the log file (.LDF).
DBCC sqlperf(logspace)