tags:

views:

432

answers:

4

I need to copy the data from one database to another only if the table schema is same... a program to do that is possible but its too much work any quick short method?

+2  A: 

you can use Bulk Insert. Or Import and export in SQL Server

Shoban
A: 

If the schema is same then take a database dump from one database and restore in the other database.

Other option is to export the data in file or excel and then import them into the other database.

Otherway is create a dblink in the second database and execute a query like insert into ... (select from ...)

Bhushan
there would be only be some tables that would have same schema... i would want to copy data in those tables and left the others.
Umair Ahmed
+2  A: 

You can use BCP utility for MSSQL Server 2000, or SSIS (SQL Server Integration Services) from MSSQL 2005 or 2008.

Alex_L
We have a couple processes that run quarterly or annually (and the person doing them would have forgotten both how to do them, and that they should be done). Consequently, we set up SSIS packages (they used to be called DTS) to do them in a consistent, repeatable manner.
Tangurena
A: 

A crude work-around: In SQL Server 2005 and up, you could write a direct INSERT...SELECT statement (using linked servers if they're on different SQL instances), and then wrap it in a TRY/CATCH block. If it copies, good, if it fails, the error gets caught and managed.

Further issues will crop up, such as if the target table has an extra column that's nullable or has a default, but its a start.

-- Adding this in response to your comment --

How to write "INSERT...SELECT" statements for all your tables in under a minute:

First, each command looks like so:

INSERT TargetDB.dbo.xxx select * from xxx

This does use SELECT *, but since you're looking for exactly matching tables that shouldn't be a problem. Next, to write one of these for each user-defined table in the currently selected database, run this:

SELECT 'INSERT TargetDB.dbo.' + name + ' select * from ' + name
 from sys.tables

Replace "TargetDB" with the target database, run, cut and paste the results, and then work in the TRY/CATCH block.

Philip Kelley
but that would require me to write insert queries for all of my tables!!!
Umair Ahmed
Added the second half of the post in response to your comment.
Philip Kelley