I have a SQL Server 2008 database that I want to copy and create a new database (with the a different name) on the server with. I am not concerned with maintaining data, the new database can be created with no data for starters. What I am looking to do is the following:
As an aside, I have 2 more questions which are not part of my task but would like to consider the following:
In Object Explorer, expand Databases, right-click the database to rename, and then select Rename.
Manual Method to Copy Database from one Server to Another First of all, launch the SQL Server Management Studio from Object Explorer and connect to the Source Server. Right-click on the database, select the option Tasks and then choose the Copy Database option.
You can use SnapCenter to clone a SQL Server database backup. If you want to access or restore an older version of the data, you can clone database backups on demand. Using SnapCenter, you can create clones from a resource group or database.
Source for the script that copies a database.
USE master;
DECLARE
@SourceDatabaseName AS SYSNAME = '<SourceDB>',
@TargetDatabaseName AS SYSNAME = '<TargetDB>'
-- ============================================
-- Define path where backup will be saved
-- ============================================
IF NOT EXISTS (SELECT 1 FROM sys.databases WHERE name = @SourceDatabaseName)
RAISERROR ('Variable @SourceDatabaseName is not set correctly !', 20, 1) WITH LOG
DECLARE @SourceBackupFilePath varchar(2000)
SELECT @SourceBackupFilePath = BMF.physical_device_name
FROM
msdb.dbo.backupset B
JOIN msdb.dbo.backupmediafamily BMF ON B.media_set_id = BMF.media_set_id
WHERE B.database_name = @SourceDatabaseName
ORDER BY B.backup_finish_date DESC
SET @SourceBackupFilePath = REPLACE(@SourceBackupFilePath, '.bak', '_clone.bak')
IF @SourceBackupFilePath IS NULL
RAISERROR ('Could not determine file path for backup file!', 16, 1) WITH LOG
-- ============================================
-- Backup source database
-- ============================================
DECLARE @Sql NVARCHAR(MAX)
SET @Sql = 'BACKUP DATABASE @SourceDatabaseName TO DISK = ''@SourceBackupFilePath'''
SET @Sql = REPLACE(@Sql, '@SourceDatabaseName', @SourceDatabaseName)
SET @Sql = REPLACE(@Sql, '@SourceBackupFilePath', @SourceBackupFilePath)
SELECT 'Performing backup...', @Sql as ExecutedSql
EXEC (@Sql)
-- ============================================
-- Automatically compose database files (.mdf and .ldf) paths
-- ============================================
DECLARE
@LogicalDataFileName as NVARCHAR(MAX)
, @LogicalLogFileName as NVARCHAR(MAX)
, @TargetDataFilePath as NVARCHAR(MAX)
, @TargetLogFilePath as NVARCHAR(MAX)
SELECT
@LogicalDataFileName = name,
@TargetDataFilePath = SUBSTRING(physical_name,1,LEN(physical_name)-CHARINDEX('\',REVERSE(physical_name))) + '\' + @TargetDatabaseName + '.mdf'
FROM sys.master_files
WHERE
database_id = DB_ID(@SourceDatabaseName)
AND type = 0 -- datafile file
SELECT
@LogicalLogFileName = name,
@TargetLogFilePath = SUBSTRING(physical_name,1,LEN(physical_name)-CHARINDEX('\',REVERSE(physical_name))) + '\' + @TargetDatabaseName + '.ldf'
FROM sys.master_files
WHERE
database_id = DB_ID(@SourceDatabaseName)
AND type = 1 -- log file
SELECT
@LogicalDataFileName as LogicalDataFileName,
@LogicalLogFileName as LogicalLogFileName,
@TargetDataFilePath as TargetDataFilePath,
@TargetLogFilePath as TargetLogFilePath
IF @TargetDataFilePath IS NULL OR @TargetLogFilePath IS NULL
RAISERROR ('Could not determine target paths!', 16, 1) WITH LOG
-- ============================================
-- Restore target database
-- ============================================
IF EXISTS (SELECT 1 FROM sys.databases WHERE name = @TargetDatabaseName)
RAISERROR ('A database with the same name already exists!', 20, 1) WITH LOG
SET @Sql = 'RESTORE DATABASE @TargetDatabaseName
FROM DISK = ''@SourceBackupFilePath''
WITH MOVE ''@LogicalDataFileName'' TO ''@TargetDataFilePath'',
MOVE ''@LogicalLogFileName'' TO ''@TargetLogFilePath'''
SET @Sql = REPLACE(@Sql, '@TargetDatabaseName', @TargetDatabaseName)
SET @Sql = REPLACE(@Sql, '@SourceBackupFilePath', @SourceBackupFilePath)
SET @Sql = REPLACE(@Sql, '@LogicalDataFileName', @LogicalDataFileName)
SET @Sql = REPLACE(@Sql, '@TargetDataFilePath', @TargetDataFilePath)
SET @Sql = REPLACE(@Sql, '@LogicalLogFileName', @LogicalLogFileName)
SET @Sql = REPLACE(@Sql, '@TargetLogFilePath', @TargetLogFilePath)
SELECT 'Restoring...', @Sql as ExecutedSql
EXEC (@Sql)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With