Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Towards the innovative SQL ideas
Browse by Tags · View All
ms sql server 119
ms sql 118
sql server 116
sql 115
database 102
tsql 81
#SQL Server 78
t-sql 75
#sql 71
sql server general 67

Archive · View All
April 2011 14
July 2011 12
May 2011 12
August 2011 11
June 2011 10
September 2011 8
December 2011 6
November 2011 6
June 2013 5
April 2013 5

Detach and Attach database in SQL Server

Apr 5 2012 12:00AM by Paresh Prajapati   

Recently while working with database move to other location , I used tsql script to move them. There are some methods to move the database files to other like detach and attach files from new location and other one is with modifying database file location.

So, lets see both of them as how can we move database files.

Option #1 : Using Modify file

Here we can use MODIFY clause to move database file after copying required place like as following,

USE master 
Go 
ALTER DATABASE TestDB SET offline 

ALTER DATABASE TestDB 
MODIFY FILE (NAME = TestDB _Data, FILENAME = 'D:\TestDB _Data.mdf') 

ALTER DATABASE TestDB 
MODIFY FILE (NAME = TestDB _Log, FILENAME = 'D:\TestDB _Log.ldf') 

ALTER DATABASE TestDB SET online 

This is one of the fist method to move databases to other location.

Option #2 : Using Detach and Attach

We can use detach and attach method to move database files also. Let’s see that also

ALTER DATABASE TestDB 
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
USE [master]
GO
EXEC master.dbo.sp_detach_db @dbname = N'TestDB'
GO

This will detach database with drop existing connection. After detaching database we can attach database with or without log files like mentioned as following and can be applied any of following methods to attach database again.

#1. Attaching database with both data and log files which will use existing mdf and ldf files,

EXEC sp_attach_db @dbname = N'TestDB', 
    @filename1 = N'D:\TestDB_Data.mdf',  
    @filename2 = N'D:\TestDB_Log.ldf';

#2. We can also generate or rebuild new log file with existing mdf file and attach a database like,

EXEC sp_attach_db @dbname = N'TestDB', 
	@filename1 = N'D:\TestDB_Data.mdf' 
	FOR ATTACH_REBUILD_LOG; 

#3.  Creating a new database with same or different name using same existing mdf file and rebuilding ldf file,

CREATE DATABASE TestDB ON
	(FILENAME =  N'D:\TestDB_Data.mdf') 
	FOR ATTACH_REBUILD_LOG

#4.  Creating a new database with same or different name using same existing mdf and ldf files,

CREATE DATABASE TestDB ON
	(FILENAME = 'D:\TestDB_Data.mdf')  ,
	(FILENAME = 'D:\TestDB_Log.ldf')
	FOR ATTACH;

These all are about to move tsql script, We can also perform the same thing with UI,

a1

2

These are various methods to move databases to other location. Hope you liked this post and also usable for you.

Tags: sql, tsql, sql server, ms sql, ms sql server, t-sql, transaction, #SQL Server, script, #sql,Attach, Modify, detach, move, alter


Paresh Prajapati
6 · 22% · 7054
7
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

2  Comments  

  • One thing you must always remember about is that with detach you lose the database owner of the database. Before deattach always execute sp_helpdb to see who is the database owner. After the attach excute:

    use TestDB 
    go
    sp_changedbowner owner-of-database
    go
    
    commented on Oct 30 2012 1:49PM
    johan.parlevliet
    868 · 0% · 33
  • It is worth noting that detaching a database clears the query cache, so use with caution. Option #1 above is the recommended method of moving databases. There are also potential file permission issues with detaching databases. I cover it here: http://beyondrelational.com/modules/2/blogs/76/posts/13755/windows-file-permissions-for-detached-databases.aspx

    commented on Nov 1 2012 7:05AM
    Mike Lewis
    42 · 4% · 1336

Your Comment


Sign Up or Login to post a comment.

"Detach and Attach database in SQL Server" rated 5 out of 5 by 7 readers
Detach and Attach database in SQL Server , 5.0 out of 5 based on 7 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]