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?
-
There was a question about this yesterday, and the best solution was the Database Publishing Wizard.Forgotten Semicolon– Forgotten Semicolon2008-08-21 16:13:54 +00:00Commented Aug 21, 2008 at 16:13
-
If you're using Visual Studio 2008 Team Edition for Database Professionals, this can be done with a simple wizard: <msdn.microsoft.com/en-us/library/aa833411(VS.80).aspx>Slavo– Slavo2008-08-21 16:22:01 +00:00Commented Aug 21, 2008 at 16:22
-
Another way would be to use Redgate's excellent SqlCompare tooledosoft– edosoft2009-05-19 07:59:22 +00:00Commented May 19, 2009 at 7:59
-
to choose certain table to script data , try SQL Formatter Add-In for SSMSMandoleen– Mandoleen2012-03-07 11:44:04 +00:00Commented Mar 7, 2012 at 11:44
5 Answers
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).
- Right click your database
- Choose Tasks > Generate Scripts
- Press Next, select your database again
- 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")

5 Comments
true you will find that option missing. The option is Types of data to script and you will need to set it to "Schema and Data", as this screenshot shows.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.
2 Comments
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
1 Comment
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:

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