Site Wide Message: (current site time 9/9/2010 2:55:46 AM EDT)
  • We want your input! One of our sponsors wants to know your opinion about development related issues. Click here to tell us what you think.
  • Are you an emerging/young developer (aged 18-30)? If so, would you like the chance to affect future developer tools and products?
    If so, then click here to give your feedback.
 

RESTORE ALL DATABASES OF FOLDER ( WITH LOGICAL NAMES !! )

Print
Email
VB icon
Submitted on: 8/7/2008 4:17:49 PM
By: Jose Vicente Rocamora Ballestero 
Level: Advanced
User Rating: Unrated
Compatibility:SQL Server 2000, SQL Server 7.0, Other

Users have accessed this code  3598 times.
 
author picture
(About the author)
 
      Its a script to restore all databases, with the logical names of the databases
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
--**************************************
-- for :RESTORE ALL DATABASES OF FOLDER ( WITH LOGICAL NAMES!! )
--**************************************
 SAY ME. DO YOU WANT TO LIVE FOR EVER ?
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: RESTORE ALL DATABASES OF FOLDER ( WITH LOGICAL NAMES!! )
-- Description:
 Its a script to restore all databases, with the logical names of the databases
-- By: Jose Vicente Rocamora Ballestero
--
-- Inputs:the folder in / out
--
-- Returns:a log, with the results
--
-- Side Effects:
 nothing, but be careful
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1247&lngWId=5--for details.--**************************************

-------------------------------------------------------------------------------------------------------------------
-- Jose Vicente Rocamora Ballestero jvrocamora@gmail.com 
-- 2008
-- only for your eyes ;-)
-- do you like ????.
-------------------------------------------------------------------------------------------------------------------
DECLARE @pathDBOrigen as varchar(2000)
DECLARE @pathDBDestino as varchar(2000)
DECLARE @cmdShellExecute as varchar(4000)
-- THE SOURCE FOLDER WITH THE DATABASE .BAK
Set @pathDBOrigen = 'D:\bbdd\' 
-- THE DESTINATION FOLDER ( MDF AND LDF )
Set @pathDBDestino = 'D:\Archivos de programa\Microsoft SQL Server\MSSQL.1\MSSQL\Data\'
--Set @pathDBDestino = 'C:\program files\Microsoft SQL Server\MSSQL.1\MSSQL\data\'
--Set @pathDBDestino = 'C:\program files\Microsoft SQL Server\MSSQL.1\MSSQL\data\'
Set @cmdShellExecute = 'dir /b /o-d ' + @pathDBOrigen 
-- FILES TO RESTORE
CREATE TABLE #result (output varchar(255) null)
INSERT #result (output) EXEC xp_cmdshell @cmdShellExecute
-- LOGIC NAMES
CREATE TABLE #result2 
(
o1 varchar(255) null,o2 varchar(255) null,o3 varchar(255) null,o4 varchar(255) null,o5 varchar(255) null,o6 varchar(255) null,o7 varchar(255) null,o8 varchar(255) null,o9 varchar(255) null,o10 varchar(255) null,
o11 varchar(255) null,o12 varchar(255) null,o13 varchar(255) null,o14 varchar(255) null,o15 varchar(255) null,o16 varchar(255) null,o17 varchar(255) null,o18 varchar(255) null,o19 varchar(255) null,o20 varchar(255) null,
)
DECLARE @nombreDataBase AS VARCHAR(300)
DECLARE @nombreCompletoDataBase AS VARCHAR(300)
DECLARE @pathDataBase AS VARCHAR(4000)
DECLARE @CADENA AS NVARCHAR(4000)
DECLARE @CADAUX AS NVARCHAR(4000)
DECLARE @LOGICALFILENAME AS VARCHAR(400)
--------------------------------------------
-- INICIALIZACION DE VARIABLES
--------------------------------------------
set @nombreDataBase=''
set @pathDataBase=''
set @CADENA=''
set @CADAUX=''
--------------------------------------------
DECLARE restoreDB CURSOR FOR
select output from #result
OPEN restoreDB 
FETCH NEXT FROM restoreDB INTO @nombreCompletoDataBase
	WHILE @@FETCH_STATUS = 0
	BEGIN
			 
		 set @nombreDataBase = left(@nombreCompletoDataBase,len(@nombreCompletoDataBase)-4)
		 set @pathDataBase = @pathDBOrigen + @nombreDataBase 
		 set @cadaux= N' RESTORE FILELISTONLY FROM DISK = ''' + @pathDataBase + right(@nombreCompletoDataBase,4) + N''' ' 
		
		 set @CADENA = N' print '' [' + @nombreDataBase + N'] '' ' + CHAR(13)
				+ N';restore DATABASE [' + @nombreDataBase + N'] ' + CHAR(13)
				+ N'FROM DISK = '''+ @pathDataBase + N'.BAK'' ' + CHAR(13)
				+ N'WITH replace, ' + CHAR(13)
			set @cadaux= N' RESTORE FILELISTONLY FROM DISK = '''+ @pathDataBase + right(@nombreCompletoDataBase,4) + ''' ' 
			INSERT INTO #result2 EXEC sp_executesql @cadaux
			DECLARE LOGICALFILES CURSOR FOR 
				select o1 from #result2
					OPEN LOGICALFILES 	FETCH NEXT FROM LOGICALFILES INTO @logicalFileName
						WHILE @@FETCH_STATUS = 0
						BEGIN
							set @CADAUX=''
							Set @CADAUX=@CADAUX + N'MOVE ''' + @logicalFileName + N''' TO ''' + @pathDBDestino + @nombreDataBase + N'.mdf'', ' + CHAR(13)
							FETCH NEXT FROM LOGICALFILES INTO @logicalFileName
							Set @CADAUX=@CADAUX + N'MOVE ''' + @logicalFileName + N''' TO ''' + @pathDBDestino + @nombreDataBase + N'.ldf'' ' + CHAR(13)
							FETCH NEXT FROM LOGICALFILES INTO @logicalFileName
						END
			
			CLOSE LOGICALFILES 
			DEALLOCATE LOGICALFILES
				
			-- borramos los datos en la tabla
			exec('DELETE FROM #result2')
		
			set @CADENA=@CADENA + @CADAUX
		 
			PRINT @CADENA
/************************************************************************************************************/
		 EXEC sp_executesql @cadena
/************************************************************************************************************/
		
		FETCH NEXT FROM restoreDB INTO @nombreCompletoDataBase
	END
drop table #result
drop TABLE #result2 
CLOSE restoreDB 
DEALLOCATE restoreDB


Other 2 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify 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

 There are no comments on this submission.
 
Add Your Feedback!

Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.