This question already has answers here:
Restored database missing views, stored procs and foreign keys
(2 answers)
Closed 9 years ago.
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?
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.
Related
I'm working on a small project to backup and restore databases to different name(s), and I can't seem to figure it out.
I'm using SSMS v18.11.1 with SQL Server 2016, and I have seven databases, let's call them DBCompanyA_1 ... DBCompanyA_7. Their .mdf and .ldf files are stored in the default location of
C:\Program Files\Microsoft SQL Server/MSSQL13.MSSQLSERVER\MSSQL\DATA
I want to make a full backup of each of those 7 databases (with logs) and then restore them to a different name. Using my example above, let's say that I want to restore them to DBCompanyB_1 ... DBCompanyB_7.
The problem I keep encountering is that all attempts (using MOVE) overwrites the existing DBCompanyA_n databases, which is bad. I want to use backup and restore so that the DBs originals' permissions will carry over to the new DBs.
I don't care if the backup procedure is via UI or script, but the restore must be using T-SQL.
I'm curious what inside of a SQL Server database (a .mdf file) since it has been 4 gb and how do I open it to find something interesting in it?
Extracted from data-recovery-solutions:
MDF files are the primary database files of SQL database server and
they are saved as .MDF extension. Being the primary database, MDF
files store all the user database and other database objects like
tables, keys, indices, views, functions, columns, triggers etc. Also,
these files include all the startup info of the SQL Server database.
In an MS SQL Server, each database contains one primary MDF file.
It is a binary kind of file which can’t be opened normally. It is the
main database file that can be attached to a running SQL Server to
access the database using SQL transaction Statements. But, these MDF
files are prone to corruption. Even a smaller corruption in MDF files
makes your entire database contents inaccessible.
See here for a MDF viewer
This question already has answers here:
Is there any way to generate database scripts from a SQL query in SQL Server?
(2 answers)
Closed 4 years ago.
I am presently using SQL Server 2008. I want to move the entire applications and database to SQL Server 2016 without data. i.e. I don't want to copy the entire data but I need all the tables and everything from the previous server. Can please anyone help me with this?
You'll want to script out each database. To do so:
right click on the database
tasks
generate scripts
using the wizard, choose all the data objects you want (tables, views, etc.) or all of them
save to a location
open the file from that location
boom, there is your code set up all your tables, etc. for your database!
Just repeat for each database you want to 'copy'. Just remember (as requested) each table will have no data in it. Alternatively you can backup and restore each database and then truncate each table, that is probably a lot more work though.
I'm trying to create a copy of a database in SQL Management Studio (SQL Server Express) using Backup-Restore. Local to local. When I do it it copies tables and data, but ignores views, stored procedures and foreign keys.
Object Explorer -> write-click "Databases" -> "Restore Database..." -> put in a name -> select a "from" database -> go
This is the script that gets generated:
RESTORE DATABASE [DbName_raw]
FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Backup\DbName.bak.1'
WITH FILE = 1,
MOVE N'GeoDo' TO N'c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\DbName_raw.mdf',
MOVE N'GeoDo_log' TO N'c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\DbName_raw_1.ldf',
NOUNLOAD, STATS = 10
GO
Database appears in Object Explorer but with views, stored procs ad foreign keys missing. Can't see anything obvious in the settings of the wizard. What am I missing?
Backup and restore will transfer anything in that database, even views and procedures. You must be doing wrong something else. Are you looking at the wrong database? Or is the restore failing?
This was because of an old backup file. I was under the impression that backing up from a database would just copy what was there currently, but it turns out that the backups come from snapshots taken of the database instead. Those backups did not contain the missing views, stored procedures and foreign keys.
I have been reading a LOT of google posts and StackOverflow questions about how to restore a database in SQL Server from a .bak file.
But none of them states how to just READ the tables in the database-backup.
(None that I could find anyway?)
I just want to check out some old information which now has been deleted, without actually restoring the full database.
Is this possible?
.
EDIT:
I just wanted to post my T-SQL solution to the problem, so others may use it and I can go back and look it up ;)
First I created a new database called backup_lookup and took it offline.
After this I could restore my old database mydb to the new one, without ever touching my original.
USE master
GO
RESTORE DATABASE backup_lookup
FROM DISK = 'D:\backup\mydb.bak'
WITH REPLACE,
MOVE 'mydb' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup.mdf',
MOVE 'mydb_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup_log.ldf'
GO
I hope this helps :)
From SQL Server 2008 SSMS (SQL Server Management Studio), simply:
Connect to your database instance (for example, "localhost\sqlexpress")
Either:
a) Select the database you want to restore to; or, alternatively
b) Just create a new, empty database to restore to.
Right-click, Tasks, Restore, Database
Device, [...], Add, Browse to your .bak file
Select the backup.
Choose "overwrite=Y" under options.
Restore the database
It should say "100% complete", and your database should be on-line.
PS: Again, I emphasize: you can easily do this on a "scratch database" - you do not need to overwrite your current database. But you do need to RESTORE.
PPS: You can also accomplish the same thing with T-SQL commands, if you wished to script it.
The only workable solution is to restore the .bak file. The contents and the structure of those files are not documented and therefore, there's really no way (other than an awful hack) to get this to work - definitely not worth your time and the effort!
The only tool I'm aware of that can make sense of .bak files without restoring them is Red-Gate SQL Compare Professional (and the accompanying SQL Data Compare) which allow you to compare your database structure against the contents of a .bak file. Red-Gate tools are absolutely marvelous - highly recommended and well worth every penny they cost!
And I just checked their web site - it does seem that you can indeed restore a single table from out of a .bak file with SQL Compare Pro ! :-)
There is no standard way to do this. You need to use 3rd party tools such as ApexSQL Restore or SQL Virtual Restore. These tools don’t really read the backup file directly. They get SQL Server to “think” of backup files as if these were live databases.
Just to add my TSQL-scripted solution:
First of all; add a new database named backup_lookup.
Then just run this script, inserting your own databases' root path and backup filepath
USE [master]
GO
RESTORE DATABASE backup_lookup
FROM DISK = 'C:\backup.bak'
WITH REPLACE,
MOVE 'Old Database Name' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup.mdf',
MOVE 'Old Database Name_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup_log.ldf'
GO
It doesn't seem possible with SQL Server 2008 alone. You're going to need a third-party tool's help.
It will help you make your .bak act like a live database:
http://www.red-gate.com/products/dba/sql-virtual-restore/