views:

269

answers:

4

I have a table in an MS SQL Server db. I want to create a script that will put the table and all records into another db. So I right-click the table in Management Studio and select Create-To new query editor... but all I get is the table structure.

How exactly do I get the values too?

+3  A: 

One of the things I really like about the tools for MySQL that SQL Server is missing out of the box to be certain.

You can use a script to do it however.

cfeduke
+2  A: 

If both are on the same machine (or on different machines but the servers are linked) you can create the table with the script you can generate automatically and do this to copy the data:

INSERT INTO [destinationdb].[dbo].[destinationtable] SELECT * 
FROM [originaldb].[dbo].[originaltable]

(Prepend [servername] to the database name if you'll be using linked servers)

Another option is to enable xp_cmdshell (do with care, it's relaxing security constraints) and use the bcp command line utility from the management studio to create copies you can then import into the other database/server. You can do that directly from the shell as well and do not need to enable xp_cmdshell in that case, of course.

Vinko Vrsalovic
+2  A: 

You might also want to consider using something like Red-Gate SQL Compare and Red-Gate SQL Data Compare. They aren't cheap tools, priced at $395 each (for the standard editions), but there are 14 day free trials available for download, and they make copying schema and data from one SQL Server to another very easy.

Mun
+1  A: 

it doesn't really create a "SQL script" but it does the job :

  • select the database in the object explorer

  • right click

  • select import/export data

  • follow the wizard

  • at the end of the process you can save the "integration service package" to reuse it

  • later you can modify the details by opening the .dtsx

  • (it will take care of security, and won't cost one more penny, it's seems we have to compete with other answers :) )

hope it helps.

call me Steve