1

I am attempting to write a SQL script that copies and renames a database. I've split it into two parts as features independently, so the restore is giving me a hard time. For example this is approximately what I have now.

BACKUP DATABASE [@dbName] 
TO DISK = 'path' WITH COPY_ONLY;

RESTORE DATABASE [@newDbName] 
FROM DISK = 'path';

How can I alter this to automatically copy?

5
  • 1
    Read this example in the relevant msdn page. You might also want to read this link as well. Commented Jun 30, 2016 at 13:22
  • @ZoharPeled Yeah I found that one earlier, but I don't necessarily have the names of the _Data and _log sections. Is there a way of getting these from the bak file? Commented Jun 30, 2016 at 13:29
  • Not that I know of. Commented Jun 30, 2016 at 13:31
  • But if you perform the back you should be able to get that information. Commented Jun 30, 2016 at 15:09
  • I get what you are asking but you should be more clear about what is not working in the code you posted. Commented Jun 30, 2016 at 15:29

3 Answers 3

1
RESTORE FILELISTONLY   
FROM 'path'
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way I can assign a name to this so it won't conflict with the original name?
1

So the comments and answers were useful to get me here, but this is what I settled on:

BACKUP DATABASE [OldDBName] TO DISK = 'path' WITH COPY_ONLY;
if (objectProperty(object_id('tmp_restore'), 'IsProcedure') is not null)
    drop procedure dbo.tmp_restore
GO
CREATE PROCEDURE dbo.tmp_restore
  @backup_path NVARCHAR(MAX)
AS
BEGIN
    SET NOCOUNT ON;
    RESTORE FILELISTONLY FROM DISK = @backup_path;
END
GO
CREATE TABLE #TEMP(
    LogicalName VARCHAR(64),            PhysicalName VARCHAR(130),
    [Type] VARCHAR(1),                  FileGroupName VARCHAR(64),
    Size DECIMAL(20, 0),                MaxSize DECIMAL(25,0), 
    FileID bigint,                      CreateLSN DECIMAL(25,0), 
    DropLSN DECIMAL(25,0),              UniqueID UNIQUEIDENTIFIER,  
    ReadOnlyLSN DECIMAL(25,0),          ReadWriteLSN DECIMAL(25,0),
    BackupSizeInBytes DECIMAL(25,0),    SourceBlockSize INT,
    filegroupid INT,                    loggroupguid UNIQUEIDENTIFIER,
    differentialbaseLSN DECIMAL(25,0),  differentialbaseGUID UNIQUEIDENTIFIER,
    isreadonly BIT,     ispresent BIT,  TDEThumbpr DECIMAL)

Insert into #TEMP exec dbo.resware_tmp_restore @backup_path = 'path';
GO
DECLARE @NameData VARCHAR(64)
DECLARE @NameLog VARCHAR(64)
Set @NameData = (Select LogicalName from #TEMP t where t.Type like 'D')
Set @NameLog = (Select LogicalName from #TEMP t where t.Type like 'L')
RESTORE DATABASE [newDBName] FROM DISK = 'path'
    WITH RECOVERY,
    MOVE @NameData TO 'path_Data.mdf',
    MOVE @NameLog TO 'path_Log.ldf'
GO
DROP TABLE #TEMP

Obviously Old/NewDBName and path have to be filled in with the proper values, but this should work.

1 Comment

Glad to see you solved the problem. Please accept your own answer so that other people will know that the problem is solved.
0

Another approach to cloning a SQL Server database is to use SQL Server containers.

Windocks supports containers for all editions SQL Server 2008 onward, and you can simply copy mdf, ndf, ldf files to an image and use that to deliver identical but isolated instances.

Windocks has also released integrated support for full database cloning as well as support of large DB environments.

A free Community Edition is available at Windocks

Disclosure: I am the Co-Founder of WinDocks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.