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
218 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now