Attach Deattach Database T-SQL SQL Server 2005 2008

— Attach a database

USE master;
GO
CREATE DATABASE
ON (FILENAME = ‘d:\sql data\dbname.mdf’),
(FILENAME = ‘d:\sql data\dbname_Log.ldf’)
FOR ATTACH;
GO

— Dettach database and take offline —
USE master;
GO
ALTER DATABASE
SET OFFLINE WITH ROLLBACK IMMEDIATE
go
EXEC sp_detach_db @dbname = N’dbname’;
GO

Advertisements

Create Snapshot on Mirror Database SQL Server 2008

Create snapshot on mirror database Sql Server 2008

— Run below command to find logical filename of database against which you creating a mirror

select name, type_desc, physical_name from sys.master_files
where database_id = db_id(‘QIIB_MSCRM’)

mscrm ROWS C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\QIIB_MSCRM.mdf
mscrm_log LOG C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\QIIB_MSCRM_log.LDF

— mscrm is name of file. Now go ahead and create snapshot called snapshotdb with filename snpss1.ss in d: drive

CREATE DATABASE snapshotdb
ON
(NAME = ‘mscrm’,FILENAME = ‘d:\snpss1.ss’
)
AS SNAPSHOT OF qiib_mscrm

select name, type_desc, physical_name from sys.master_files
where database_id = db_id(‘QIIB_MSCRM’)