Web Technologies

BLOG for Web Technologies

Freewares, Free E-Books Download, SEO, Tips, Tricks, Tweaks, Latest News, .Net, PHP, ASP, ASP.Net, CSP, MS SQL Server, MySQL, Database
earnptr.com
Saturday, November 10, 2007
SQL SERVER - 2005 - T-SQL Script to Attach and Detach Database
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
GO

Labels: , , , ,

posted by WebTeks @ 3:35 AM  
0 Comments:
Post a Comment
<< Home
 
Previous Post
Archives
Links
Template by

Free Blogger Templates

BLOGGER

Subscribe in NewsGator Online Subscribe in Rojo Add to Google Add to netvibes Subscribe in Bloglines Web Developement Blogs - BlogCatalog Blog Directory Blogarama - The Blog Directory Blog Directory & Search engine Computers Blogs - Blog Top Sites Top Computers blogs