Solved

Is there sql script or stored procedure or something simular I can use to create a copy and rename a sql server db?

Posted on 2011-02-26
3
227 Views
Last Modified: 2012-05-11
A lot of my day is spent create new sql dbs via the UI

Is there a sql script or something simular I can use to speed this up for me, i.e. i'd pass in the name of an existing db, and a new name, and thescript would backup and create/restore a new db based on the new name?

THanks
Webbo
0
Comment
Question by:Webbo_1980
3 Comments
 
LVL 13

Expert Comment

by:devlab2012
ID: 34987591
use can use "create database", sp_renamedb stored procedure and "backup database" commands.
0
 

Author Comment

by:Webbo_1980
ID: 34987638
Thanks Devlab2012, but will that do the restore ?

Would it be possible to give me a quick examples if thats ok?
0
 
LVL 7

Accepted Solution

by:
rmm2001 earned 500 total points
ID: 34989749
Try something like this. You'll have to set all of the variables to what you need though. You could put it in a stored proc and just pass in the variables too.
DECLARE @OrigDBName VARCHAR(100)
DECLARE @OrigDBDataName VARCHAR(100)
DECLARE @OrigDBLogName VARCHAR(100)
DECLARE @OrigDBNameBackup VARCHAR(100)

SET @OrigDBName = 'OrigDB'
SET @OrigDBDataName = 'OrigDB_Data'
SET @OrigDBLogName = 'OrigDB_Log'
SET @OrigDBNameBackup = 'OrigDBBackup'

DECLARE @RestoreDBName VARCHAR(100)
DECLARE @RestoreDBDataLocation VARCHAR(100)
DECLARE @RestoreDBLogLOcation VARCHAR(100)
DECLARE @RestoreDBNameBackup VARCHAR(100)

SET @RestoreDBName = 'NewDB'
SET @RestoreDBDataName = 'RestoreDB_Data'
SET @RestoreDBLogName = 'RestoreDB_Log'
SET @RestoreDBDataLocation = 'C:\Data\RestoreDB.mdf'
SET @RestoreDBNameLogLocation = 'C:\Data\RestoreDB.ldf'

DECLARE @SqlCmd VARCHAR(8000)

SET @SqlCmd = '
BACKUP DATABASE [' + @OrigDbName + ']
   TO ['+ @OrigDBNameBackup +']

RESTORE FILELISTONLY 
   FROM ['+ @OrigDBNameBackup +']

RESTORE DATABASE ['+ @RestoreDBName +'] 
   FROM ['+ @OrigDBNameBackup +'] 
   WITH MOVE ''' + @OrigDBDataName + ''' TO ''' + @RestoreDBDataLocation + ''',
   MOVE ''' + @OrigDBLogName + ''' TO ''' + @RestoreDBNameLogLocation'''
GO'

EXEC (@SqlCmd)

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question