Important alert: (current site time 7/15/2013 8:00:34 PM EDT)
 

VB icon

MOVE DATABASE

Email
Submitted on: 1/16/2007 2:23:06 PM
By: Jose Vicente Rocamora Ballestero 
Level: Advanced
User Rating: By 4 Users
Compatibility: SQL Server 2000, SQL Server 7.0
Views: 9117
author picture
(About the author)
 
     MOVE DATABASE FILES MDF AND LDF !!!!!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: MOVE DATABASE
-- Description:MOVE DATABASE FILES MDF AND LDF !!!!!
-- By: Jose Vicente Rocamora Ballestero
--
-- Assumes:MOVE DATABASE, ITS FOR YOU. 
1) READ_ONLY
2) BACKUP
3) RESTORE IN THE OTHER PATH
4) READ_WRITE
PLEASE VOTE ME !!!! SEE YOU LATER
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1117&lngWId=5--for details.--**************************************

USE [MASTER]
GO
Declare @bbdd varchar(50)
Declare @cadB nvarchar(2000)
Declare @cadR nvarchar(2000)
Declare @cadRAux nvarchar(2000)
Declare @cont int
Declare @cadReadOnly nvarchar(500)
Declare @cadReadWrite nvarchar(500)
Declare @cadEnd nvarchar(4000)
Set @bbdd='[@_PICO_E0003E0006]'
Set @cadB=''
Set @cadR=''
Set @cadRAux=''
Set @cadB= 'BACKUP LOG ' + @bbdd + ' WITH TRUNCATE_ONLY ' + CHAR(13)
set @cadB= @cadB + N'BACKUP DATABASE ' + @bbdd + ' TO DISK=''e:\program files\backup\' + replace(replace(replace(@bbdd,'[',''),']',''),'@_','') + '.BAK'' ' + CHAR(13)
-- PRIMERA PARTE DE LA CADENA DE RESTAURACION !!!!
--------------------------------------------------------
 Set @cadR= 'RESTORE DATABASE ' + @bbdd + CHAR(13)
 Set @cadR= @cadR + 'FROM DISK = ''e:\program files\backup\' + replace(replace(replace(@bbdd,'[',''),']',''),'@_','') + '.BAK'' ' + CHAR(13)
 Set @cadR= @cadR + 'WITH replace, ' + CHAR(13)
--------------------------------------------------------
--PRINT 'e:\program files\backup\' + replace(replace(replace(@bbdd,'[',''),']',''),'@_','') + '.BAK' 
Set @cont=0
 DECLARE @NOMBRELOG VARCHAR(500) 
 DECLARE logicalName CURSOR FOR 
 SELECT cast(F.NAME as VARCHAR(500)) as name FROM SYSALTFILES F LEFT JOIN SYSDATABASES D ON F.DBID=D.DBID WHERE D.NAME=replace(replace(@bbdd,'[',''),']','')
	OPEN logicalName 
	FETCH NEXT FROM logicalName INTO @NOMBRELOG 
	WHILE @@FETCH_STATUS = 0 
	BEGIN 
		--PRINT @NOMBRELOG	
	
		Set @cont=@cont+1	
		if (@cont=1) 			
		BEGIN
			Set @cadRAux= @cadRAux + 'MOVE ''' + RTRIM(LTRIM(@NOMBRELOG)) + ''' TO ''e:\program files\Data\' + replace(replace(replace(@bbdd,'[',''),']',''),'@_','') + '.mdf'', ' + CHAR(13)
	--		Set @cadRAux= @cadRAux + 'ALTER DATABASE ' + @BBDD + '	MODIFY FILE (NAME = ''' + RTRIM(LTRIM(@NOMBRELOG)) + ''', FILENAME = ''e:\program files\data\' + replace(replace(replace(@bbdd,'[',''),']',''),'@_','') + '.mdf'') ' + CHAR(13)
		END
		
		if (@cont=2)
		BEGIN
			Set @cadRAux= @cadRAux + 'MOVE ''' + RTRIM(LTRIM(@NOMBRELOG)) + ''' TO ''e:\program files\Data\' + replace(replace(replace(@bbdd,'[',''),']',''),'@_','') + '.ldf'' '
	--		Set @cadRAux= @cadRAux + 'ALTER DATABASE ' + @BBDD + '	MODIFY FILE (NAME = ''' + RTRIM(LTRIM(@NOMBRELOG)) + ''', FILENAME = ''e:\program files\data\' + replace(replace(replace(@bbdd,'[',''),']',''),'@_','') + '.ldf'') ' + CHAR(13)
		END 		
		
		FETCH NEXT FROM logicalName INTO @NOMBRELOG 
	END
 
CLOSE logicalName 
DEALLOCATE logicalName 
Set @cadR=@cadR + @cadRAux
Set @cadReadOnly='ALTER database ' + @bbdd + ' set READ_ONLY ' + CHAR(13)
Set @cadReadWrite='ALTER database ' + @bbdd + ' set READ_WRITE' + CHAR(13)
Set @cadEnd = 'PRINT '' STARTING !!! '' ' + CHAR(13)
Set @cadEnd = @cadEnd + @cadReadOnly + CHAR(13) + CHAR(13)
Set @cadEnd = @cadEnd + 'PRINT '' STEP 1 READ ONLY '' ' + CHAR(13) + CHAR(13)
Set @cadEnd = @cadEnd + @cadB + CHAR(13) + CHAR(13) 
Set @cadEnd = @cadEnd + 'PRINT '' STEP 2 BACKUP '' ' + CHAR(13) + CHAR(13)
Set @cadEnd = @cadEnd + @cadR + CHAR(13) + CHAR(13) + CHAR(13)
Set @cadEnd = @cadEnd + 'PRINT '' STEP 3 RESTORE '' ' + CHAR(13) + CHAR(13)
Set @cadEnd = @cadEnd + @cadReadWrite + CHAR(13) + CHAR(13)
Set @cadEnd = @cadEnd + 'PRINT '' STEP 4 READ WRITE '' ' + CHAR(13) + CHAR(13)
--Set @cadEnd = @cadEnd + 'COMMIT TRANSACTION ' + CHAR(13)
--PRINT @cadEnd
EXEC (@cadEnd)


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/17/2007 3:12:36 AMRamirez

Good job!
(If this comment was disrespectful, please report it.)

 
1/17/2007 4:42:27 AMJVRB

ITS POSIBLE TO DECLARE ANOTHER VAR FOR THE NEW PATH
(If this comment was disrespectful, please report it.)

 
1/17/2007 2:14:32 PMCharles Kincaid

You cold make this a stored procedure that takes three parameters: (a) data base name, (b) path for MDF file, and (c) pathe for LDF file.

Best to put the MDF and the LDF on different physical drives (not just different drive letters) for speed and recovery.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.