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