Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL script to "copy" a database

Tags:

sql-server

I want to write a SQL script that will copy a database on the same server. I could do a backup/restore, but I think it might be faster to just "copy" somehow. Does anyone know if this is possible? Is there a way to write a script that will just detach, copy the file on the HD, and then reattach both copies?

like image 902
skb Avatar asked Jan 19 '10 18:01

skb


2 Answers

@Tony the Lion: Hi - I experienced some problems using your script, so I came up with a hybrid of your script and this post: link

USE master;
GO
-- the original database (use 'SET @DB = NULL' to disable backup)
DECLARE @SourceDatabaseName varchar(200)
DECLARE @SourceDatabaseLogicalName varchar(200)
DECLARE @SourceDatabaseLogicalNameForLog varchar(200)
DECLARE @query varchar(2000)
DECLARE @DataFile varchar(2000)
DECLARE @LogFile varchar(2000)
DECLARE @BackupFile varchar(2000)
DECLARE @TargetDatabaseName varchar(200)
DECLARE @TargetDatbaseFolder varchar(2000)

-- ****************************************************************

SET @SourceDatabaseName = '[Source.DB]'                 -- Name of the source database
SET @SourceDatabaseLogicalName = 'Source_DB'                -- Logical name of the DB ( check DB properties / Files tab )
SET @SourceDatabaseLogicalNameForLog = 'Source_DB_log'  -- Logical name of the DB ( check DB properties / Files tab )
SET @BackupFile = 'C:\Temp\backup.dat'                                  -- FileName of the backup file
SET @TargetDatabaseName = 'TargetDBName'                        -- Name of the target database
SET @TargetDatbaseFolder = 'C:\Temp\'

-- ****************************************************************

SET @DataFile = @TargetDatbaseFolder + @TargetDatabaseName + '.mdf';
SET @LogFile = @TargetDatbaseFolder + @TargetDatabaseName + '.ldf';

-- Backup the @SourceDatabase to @BackupFile location
IF @SourceDatabaseName IS NOT NULL
BEGIN
SET @query = 'BACKUP DATABASE ' + @SourceDatabaseName + ' TO DISK = ' + QUOTENAME(@BackupFile,'''')
PRINT 'Executing query : ' + @query;
EXEC (@query)
END
PRINT 'OK!';

-- Drop @TargetDatabaseName if exists
IF EXISTS(SELECT * FROM sysdatabases WHERE name = @TargetDatabaseName)
BEGIN
SET @query = 'DROP DATABASE ' + @TargetDatabaseName
PRINT 'Executing query : ' + @query;
EXEC (@query)
END
PRINT 'OK!'

-- Restore database from @BackupFile into @DataFile and @LogFile
SET @query = 'RESTORE DATABASE ' + @TargetDatabaseName + ' FROM DISK = ' + QUOTENAME(@BackupFile,'''') 
SET @query = @query + ' WITH MOVE ' + QUOTENAME(@SourceDatabaseLogicalName,'''') + ' TO ' + QUOTENAME(@DataFile ,'''')
SET @query = @query + ' , MOVE ' + QUOTENAME(@SourceDatabaseLogicalNameForLog,'''') + ' TO ' + QUOTENAME(@LogFile,'''')
PRINT 'Executing query : ' + @query
EXEC (@query)
PRINT 'OK!'
like image 137
Jochen van Wylick Avatar answered Oct 04 '22 15:10

Jochen van Wylick


CREATE DATABASE mydatabase_copy AS COPY OF mydatabase;

This will work on an AZURE Database, and will achieve the same end result. Two attached databases - the original and the copied one.

like image 22
timothy Avatar answered Oct 04 '22 16:10

timothy