Question

I need to copy a database in SQL Server 2008 via script. I found this solution:

BACKUP DATABASE Database1 TO DISK='C:\temp\Database1.bak';

RESTORE DATABASE Database2 FROM DISK='C:\temp\Database1.bak'
WITH MOVE 'Database1' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Database2.mdf',
MOVE 'Database1_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Database2_log.ldf';

This works fine, I get all my tables with all the data, but it doesn't copy my views, stored procedures, functions, security (users, ...).

When I do a restore in the GUI of the management studio (all tasks - restore database) all these objects will be restored. Am I missing some parameters?

Was it helpful?

Solution

When you backup database and restore it from file, it will include all tables, views, stored procedures etc. You ether getting old file or your backup is failing.

Your syntax is correct. Below is full documentation on RESTORE and how to use it to copy database.

http://technet.microsoft.com/en-us/library/ms186858.aspx#copying_db_using_bnr

Delete drop your new database, delete all files that are associated with it, including backup files. And start over again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top