SQL Server 2005 - Export table programmatically (run a .sql file to rebuild it)

SqlSql ServerSql Server-2005Database Table

Sql Problem Overview


I have a database with a table Customers that have some data

I have another database in the office that everything is the same, but my table Customers is empty

How can I create a sql file in SQL Server 2005 (T-SQL) that takes everything on the table Customers from the first database, creates a, let's say, buildcustomers.sql, I zip that file, copy it across the network, execute it in my SQL Server and voila! my table Customers is full

How can I do the same for a whole database?

Sql Solutions


Solution 1 - Sql

This functionality is already built in to Sql Server Management Studio 2008.

Just download the trial and only install the client tools (which shouldn't expire). Use Management Studio 2008 to connect to your 2005 database (its backwards compatible).

  1. Right click your database
  2. Choose Tasks > Generate Scripts
  3. Press Next, select your database again
  4. On the 'Choose Script Options' screen, there is an option called Script Data which will generate SQL insert statements for all your data.

(Note: for SQL Server Management Studio 2008 R2, the option is called "Types of data to script" and is the last one in the General section. The choices are "data only", "schema and data", and "schema only")

alt text alt text

Solution 2 - Sql

Use bcp (from the command line) to a networked file and then restore it.

e.g.

bcp "SELECT * FROM CustomerTable" queryout "c:\temp\CustomerTable.bcp" 
     -N -S SOURCESERVERNAME -T 

bcp TargetDatabaseTable in "c:\temp\CustomerTable.bcp" -N -S TARGETSERVERNAME -T 
  • -N use native types
  • -T use the trusted connection
  • -S ServerName

Very quick and easy to embed within code. (I've built a database backup(restore) system around this very command.

Solution 3 - Sql

You can check the following article to see how you can do this by using both SQL Server native tools and the third party tools: SQL Server bulk copy and bulk import and export techniques

Disclaimer: I work for ApexSQL as a Support Engineer

Hope this helps

Solution 4 - Sql

You could always export the data from the Customers table to an Excel file and import that data into your Customers table.

To import/export data:

  1. Right click on database
  2. Go to Tasks
  3. Go to Import Data or Export Data
  4. Change the data source to Microsoft Excel
  5. Follow the wizard

Solution 5 - Sql

If both databases resides in the same instance of SQL Server, ie use same connection, this SQL might be helpful:

INSERT INTO [DestinationDB].[schema].[table] ([column])
SELECT [column] FROM [OriginDB].[schema].[table]
GO

Solution 6 - Sql

Solution 7 - Sql

I just like to add some screen shoots for Sql Server Management Studio 2008. It is correct to use the steps describe previously. When you have the 'Generate and Publish Script' -> 'Set Script Options' then press Advance to see script options:

![Where to find Advanced script options]: image missing because I do not have the right reputation :(

For Sql Server Management Studio 2008 the option to included data is 'Types of data to script'

![Types of data to script]: image missing because I do not have the right reputation :(

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRomjinView Question on Stackoverflow
Solution 1 - SqlThe MattView Answer on Stackoverflow
Solution 2 - SqlUnslicedView Answer on Stackoverflow
Solution 3 - SqlMilica Medic KiraljView Answer on Stackoverflow
Solution 4 - SqlBryan RothView Answer on Stackoverflow
Solution 5 - SqlNordinView Answer on Stackoverflow
Solution 6 - SqlAble AliasView Answer on Stackoverflow
Solution 7 - SqlLars LadegaardView Answer on Stackoverflow