Following script can be used to detach or attach database. If database is to be from one database to another database following script can be used detach from old server and attach to new server.
Process to move database :
--Step 1 : Detach Database using following script USE [master] GO EXEC master.dbo.sp_detach_db @dbname = N'MyDB', @keepfulltextindexfile=N'true' GO
--Step 2 : Move Data files and Log files to new location
--Step 3 : Attach Database using following script USE [master] GO CREATE DATABASE [MyDB] ON ( FILENAME = N'C:\Data\MyDB_Data.mdf' ), ( FILENAME = N'C:\Data\MyDB_Log.ldf' ) FOR ATTACH GO IF EXISTS ( SELECT name FROM master.sys.databases sd WHERE name = N'MyDB' AND SUSER_SNAME(sd.owner_sid) = SUSER_SNAME() ) EXEC [MyDB].dbo.sp_changedbowner @loginame=N'sa', @map=false GOLabels: Attach, Database, Detach, SQL Server, T-SQL |