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
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

707 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