This process is draws upon a couple posts in different user forums who's links are provided at the end of this post. This process will work better on smaller databases.
The high-level approach uses SQL Server Management Studio's Generate Scripts task to create separate schema and data scripts, then creates required log-ins in the target database, runs the schema script to create the users\schema, temporarily disables all constraints, executes the data script, and finally enables the constraints.
The steps below walk through the process:
On the machine running source database (in our case SQL Server 2008 R2 Express)
- Open SQL Server Management Studio
- Select your source Database
- Select Tasks->Generate Scripts.
- Select 'Script entire database and all database objects', press 'Next'
- Select 'Save to File' and enter a file name like “Objects_Schema.sql”
- Click on the 'Advanced' button
- Select 'Script for Server Version' and select the version you want (in our case SQL Server 2008)
- Select 'Type of data to Script' and select Schema Only
- Click 'OK', then Next and create the script
- Run the wizard again to export only the data
- Select your source Database
- Select Task->Generate Scripts.
- Select 'Script entire database and all database objects', press 'Next'
- Select 'Save to File' and enter a file name like “Data.sql”
- Click on the 'Advanced' button
- Select 'Script for Server Version' and select the version you want (same version as above)
- Select 'Type of data to Script' and select Data Only
- Click 'OK', then Next and create the script
- Copy the resulting files to the target machine
- Edit the Schema Only script to set the location of the data files to be created.
- Create any log-ins referenced in the source database
- Run the Schema Only Script
- The data script does not follow referential ordering when inserting data. Therefore we have a choice to edit the data script to put the inserts in referential order, or to do the DBA cringe-worthy task of disabling all constraints. If you choose the quick path, execute the following T-SQL command as the object owner to disable all of the constraints. Be careful you know what you're doing here!
- exec sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"
- Run the Data Script using SQLCMD as they can be really large and not load in SQL Server Management Studio.Log the output to a file.
- Resolve any errors if they are reported
- If you previously disabled all the constraints, Run the following T-SQL command to enable them
- exec sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
- Perform typical; smoke testing of your database\application to ensure everything transferred OK
Source References: