Restored database missing views, stored procs and foreign keys - sql-server

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.

Related

How can I backup and then restore SQL Server DBs to different names without overwriting existing DBs?

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.

Restore from .bak only imports system tables

I was given a .bak from a MS SQL database and am needing to import the database onto a different workstation. I created a blank database in SQL Management Studio and attempted to restore from the .bak. The restore lists as successful, but only system tables appear in the restored database.
Am I missing a step?
Per the conversation in the comments, you had a backup of msdb rather than the database you wanted. You can do a restore with headeronly from disk = 'name of file here' to see what database is contained in the backup.

SQL Server Management Studio: Backup and restore database without data

Is there a way to backup and restore a database without the data. I just want the tables, scheme, stored procedures, etc. without the data.
How to backup the database?
How to restore it in SSMS?
If I'm not mistaken you can do this:
Right click the database
Select Script Database As
Select Create to
Select file
This gives you a script that you can run on a different server to set up the db.
Update:
You probably need to follow the steps here

Database copy doesn't copy views, stored procedures, [duplicate]

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.

Opening a SQL Server .bak file (Not restoring!)

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/

Resources