Transfer a SQL Server database without data - sql-server

I'm trying to move a database from a server in one data centre to a server in my local data centre. The pipe between the two is very slow. Luckily I only need to transfer the schema, not the data. Is there an easy way in SQL Server to transfer just the schema of a database without the data?

Go to Management Studio's Object Explorer and select Tasks > Generate Scripts ...
Then step through the wizard, select those objects you need, select whether to script out to a single file or to a file per table / view - whatever you need.
Then transfer the .sql script(s) to the target system, launch Management Studio there, run the scripts - done!

I would use the SQL Server Publishing Wizard for this.

One approach would be to generate scripts of your existing database (in SSMS right-click the database, then Tasks, then Generate Scripts ...). Go through the wizard to generate your script file(s). Then run the script(s) on the target system.

There are a variety of third party tools which can do this for you pretty easily, such as Red Gate's SQL Compare and DB Ghost. ApexSQL has a free, community version of ApexSQL Diff. Devart has a dbForge Studio Express for SQL Server which has schema compare capabilities.
There are also several other questions on Stack Overflow which provide some ideas:
Free Tool to compare Sql Server tables [closed]
Best tool for auto-generating SQL change scripts for SQL Server [closed]
Anyone know of any good Database Diff tools? [duplicate]
Microsoft's Visual Studio can even do schema comparisons now.
But you should be able to script the entire database using SQL Server Management Studio (SSMS) as Marc S's answer now describes. This option was also proposed by Chris Brandsma in his answer to Script entire database SQL-Server.

Related

The database 'xxx' cannot be opened because it is version 904

I can't attach my database. When I try to attach a database in SQL Server Management Studio, I get this error:
The database 'C:\FILES\ACCOUNTING.MDF' cannot be opened because it is version 904. This server supports version 852 and earlier. A downgrade path is not supported. Could not open new database 'C:\FILES\ACCOUNTING.MDF'. CREATE DATABASE is aborted. (.Net SqlClient Data Provider)
and I have tried these commands:
cd "C:\Program Files\Microsoft SQL Server\130\LocalDB\Binn"
SqlLocalDB.exe delete "MSSQLLocalDB"
SqlLocalDB.exe create "MSSQLLocalDB"
but it still has an error
You CANNOT do this - you cannot attach/detach or backup/restore a database from a newer version of SQL Server (v904 = SQL Server 2019) down to an older version (v852 which is SQL Server 2016) - the internal file structures are just too different to support backwards compatibility.
You can either get around this problem by
using the same version of SQL Server on all your machines - then you can easily backup/restore databases between instances
otherwise you can create the database scripts for both structure (tables, view, stored procedures etc.) and for contents (the actual data contained in the tables) either in SQL Server Management Studio (Tasks > Generate Scripts) or using a third-party tool
or you can use a third-party tool like Red-Gate's SQL Compare and SQL Data Compare to do "diffing" between your source and target, generate update scripts from those differences, and then execute those scripts on the target platform; this works across different SQL Server versions.
The error message in the problem statement occurs because the SQL Server database files (*.mdf, *.ndf and *.ldf) and backups are not backward compatible. Backward compatibility is why we cannot restore or attach a database created from a higher version of SQL Server to a lower version of SQL Server. However, there are a few options that can help us to downgrade the database from a higher version of SQL Server to a lower version SQL Server. These options include:
Use the Generate Scripts Wizard in SQL Server Management Studio
Use SQL Server Integration Services
Create Custom Scripting and BCP
In this tip we will use the Generate Scripts Wizard in SQL Server Management Studio.
Here are the basic steps we need to follow:
Script the database schema and data from the higher version of SQL Server by using the Generate Scripts Wizard in SSMS.
Connect to the lower version of SQL Server, and run the SQL scripts that were generated in the previous step, to create the database schema and data.
In the next section, I will demonstrate the steps for downgrading a SQL Server 2012 database to SQL Server 2008 R2 database.
Steps to Downgrade a SQL Server Database Using SSMS Generate Scripts Wizard
Step 1 Script the schema of the OUTLANDER database on the SQL Server
2012 instance (IITCUK\DEV01) using the Generate Scripts wizard in
SSMS.
In Object Explorer connect to IITCUK\DEV01, right-click on the
OUTLANDER database, expand Tasks and choose "Generate Scripts...".
This launches Generate and Publish Scripts wizard. Click Next, to skip the Introduction screen and proceed to the Choose Objects page.
On the Choose Objects page, choose option "Script entire database and all database objects", and then click Next to proceed to "Set Scripting Options" page.
n the Advanced Scripting Options dialog box,
set Script for Server Version to SQL Server 2008 R2 (or whatever version you want)
under the Table/View Options, set Script Triggers, Script Indexes and Script Primary Keys to True
and set Types of data to script to Schema and Data - this last option is key because this is what generates the data per table.
Once done, click OK, to close the Advanced Scripting Options dialog box and return to Set Scripting Options page. In Set Scripting Options page, click Next to continue to Summary page.
After reviewing your selections on Summary page, click Next to generate scripts.
Once scripts are generated successfully, choose the Finish button to close the Generate and Publish Scripts wizard.
Step 2 Connect to the SQL Server 2008 R2 instance
(IITCUK\SQLSERVER2008), and then run the SQL scripts that were
generated in Step 1, to create the OUTLANDER database schema and data.
In Object Explorer connect to IITCUK\SQLServer2008, then in SQL Server
Management Studio, open the SQL Server script you saved in Step 1
Modify the script, to specify the correct location for the OUTLANDER database data and log files. Once done, run the script to create the OUTLANDER database on IITCUK\SQLServer2008 instance.
Upon successful execution, refresh the Database folder in Object Explorer. As you can see in the following image, the OUTLANDER database has been successfully downgraded.
Notes
There are a few things to be aware of when using this approach.
This solution creates one large SQL file that has the scripts to create the database objects and also INSERT statements for the data in the tables.
For a large databases, the SQL file can get very large if you script out both the schema and the data and could be hard to load into an editor. Also, you may get a memory related error message from the editor if the file is too big.
For large databases, around 1GB or more, if this approach does not work, then you should look at using SSIS to migrate the database or create custom scripts to script out the objects and BCP out the data for each of the tables. You can use this Generate Scripts wizard to just generate the schema without the data and use SSIS or BCP to export and import the data.
This approach works for SQL Server 2017 to SQL Server 2005. Some of the scripting options might be a bit different in newer versions, but the process is still the same.
Before just executing the script, you should review the script to make sure everything looks correct such as the path of the database files, database options, etc.
Also if you are using new functionality that does not exist in the lower version, SQL Server won't be able to create the objects and you will need to review the scripts that were generated and update the code accordingly.
For a very simple database this approach should work pretty easliy, but you might need to spend some time making some modifications to the script for a more complex database.
Below is a list of all of the scripting options. If you click on an item, the bottom part of the screen gives you a short definition of the option.
Next Steps
To avoid this issue, always make sure that you perform a full backup of the database before you upgrade the SQL Server and database to a higher version of SQL Server. In addition, be sure to thoroughly test the application prior to releasing the application to the users.
Consider this downgrade option as your last option to rollback from an upgrade because the time and storage needed can be very large.
With a very large database be sure you have sufficient storage to support the data needs.
Be sure to verify row and object counts as well as test your application before releasing to production.
Additional Resources:
Why Can't I Restore a Database to an Older Version of SQL Server?
SQL Server Database Engine Backward Compatibility
SQL Server Upgrade Tips

How can migrate all databases from one server to another server in SQL Server

I want to migrate my all databases from SQL Server 2012 to SQL Server 2014. Is there any way to do all in one short instead of one by one?
you can simply do in-place upgrade in the same server, instead of migration, if possible. It will one-shot operation.
You can do side by side migration, by taking backup in source environment(MSSQL 2012) and restore databases in target environment(MSSQL 2014).
This is a very important operation and many things have to be planned. You have to have migration plan for this.
You can use upgrade advisor tool for this. It will tell you whether everything is fine on your instance for initial analysis: https://msdn.microsoft.com/en-us/library/dn236457(v=sql.120).aspx
Also, read below articles.
https://thomaslarock.com/2014/06/upgrading-to-sql-server-2014-a-dozen-things-to-check/
https://blogs.technet.microsoft.com/meamcs/2013/12/14/what-to-consider-when-creating-sql-server-database-migration-plan/
There are lots of guides available online. Just search internet. you will find many guides.
You can do it in 2 ways:
1.Create a backup of your database and restore it in new server.From Management Studio you can Right-click on your database then select Tasks -> Back up.
2.Generate script, create the database in the new server and run the script.From Management Studio you can Right-click on your database then select Tasks -> Generate Scripts.

Can SQL Server synchronise a live database and a create script to make an update script?

We are currently updating our SQL Server database create and update scripts by hand, and I'm looking for a better way.
I've worked extensively with MySQL Workbench before, and it has functionality to update a database creation script to match a live database, and in the process also produce an update script.
Is there similar functionality available in SQL Server? If so, can you please provide references to it?
If you're using Visual Studio, the answer is SQL Server Data Tools (SSDT). It comes with SQL Schema Compare functionality. Check it out here: http://msdn.microsoft.com/en-us/data/tools.aspx
SSDT can also do a lot of other stuff, like automated database testing, automated build and deployment of databases, etc.
There are also 3rd party alternatives, such as Red Gate's SQL Compare: http://www.red-gate.com/products/sql-development/sql-compare/

What is a good Microsoft SQL Database Export / Import tool?

I need a tool that will look at a Microsoft SQL Server database and export the data as a SQL script. I want to be able to run the script on another box with an empty database and insert all the data from the previous one without having to muck with the index's due to ref-integ constraints.
Try a tool like Red Gate Data Compare or DBGhost. Both have trials I believe.
http://www.innovartis.co.uk/
http://www.red-gate.com/products/sql_data_compare/index.htm
Use Microsoft SQL Server Database Publishing Wizard.
Depending on what version of SQL Server you have, you can right click the database, choose generate scripts, choose the tables you want, and uncheck the option to script structure and choose the option to script the data.
If you're using Visual Studio 2008 or above you should be able to script out the database when you do a publish of the website/web application. Scott Guthrie has a few examples of how to do this out of Visual Studio 2005: Deploying a Database Part I and SQL Server Database publishing kit.
Hope this works for you.
I decided to go with Redgate SQL Packager it create a very nice script for the entire DB or an exe that will install it if necessary

Easiest way to copy an entire SQL server Database from a server to local SQL Express

I need to copy an entire database from a SQL Server 2005 on my server over to my local SQL Express in order to run my application for a presentation. What is the fastest/easiest way to get this done?
EDIT: I have very limited access to my server so I don't think I can access the backup file that I could create so that is out.
If the database is not too big, you could use the Database Publishing Wizard.
This is a free tool from Microsoft which creates a complete SQL script of a database for you (not only the tables and stuff, but all data as well).
You can install the tool on your machine, connect to a remote server and let the tool create the script directly on your machine.
You can download the Database Publishing Wizard here.
Apparently the link above doesn't work anymore in 2019.
That's probably because in newer versions of SQL Server Management Studio, the functionality of the Database Publishing Wizard is included out-of-the-box, so there's no need to install it separately.
It's now called the Generate and Publish Scripts Wizard, but it does exactly the same.
You can right click the database -> Tasks -> Generate scripts. Here you can select one, multiple, or all objects. Then in the 'Set Scripting Options' step of the wizard, click Advanced. In here set the property 'Types of Data to script; to Schema and Data.
Having done these steps, make sure you publish to a file. Because only file can handle large amounts of data.
Now you should have all your objects, tables, and data scripted. Now start running the scripts and viola!
Back up the database on the server and then restore it locally in SQL Express.
EDIT: If this is part of your work, surely you can get someone in networks to get you a backup..?
If you can login to both servers (the Express and the 05 Server) using SQL Server Management Studio then you can do a DB Restore from one database to the other. No need for backup files at all.
You can use SSIS's database copy wizard, but it's not quick at all. Is there a DBA there that you can ask for the backup file? That will probably turn out to be the fastest way.
Depending which versions of SQL Server you are using, you might get some mileage out of the SQL Server Database Publishing Wizard from Microsoft. I've had mixed results with this tool in complex environments, but for most simple database scenarios it is a great tool.
An example of a scenario where I ran into difficulties was a DB with multiple distinct schemas, each with their own owner and extreme separation between the tables (don't ask...). As I said, though, most other scenarios have been fine.
Save your database as a sql script
EMS SQL Manager (for example) allows you to backup your database as a script in a .sql file. It is then possible to run this file against any other SQL server. Just update the first line of the script (CREATE DATABASE ....).
You can even fully parameter the script in order to include data from complete or filtered tables.

Resources