We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Quickest way to insert records from one db into another

brdrok asked
Medium Priority
Last Modified: 2010-04-16

I have a DataTable entitled "dtSource" from database 'A'.  I would like to push the data into database 'B'.  Both databases are SQL Server 2005 and both table schemas are the same.

I suppose I could loop through the rows like so:

string sql = "";
foreach(DataRow dr in dt.Rows)
    sql += "INSERT INTO...."
    sql += dr[0].ToString() + ", ";

There has to be a faster and/or more of an elegant way of doing this.

Watch Question

foreach(DataRow dr in dt.Rows)

Then you can use Update to populate the second database

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Let me know if you would like a bit more detail or if this is just the nudge you were looking for

Just execute the following SQL

sql = "Insert INTO dbo.A.dtSource " +
     "SELECT * FROM dbo.b.dtSource";

This is quicker and cleaner because SQL can process it faster than ASP.Net code can loop through a DataTable.


Thank you for this informational gold nugget...

I feel kind of silly asking this but I have a table with 40+ columns, is there anyway to just call the Update() without having to specify a gazillion parameters like the following?

// Create the InsertCommand.
   command = new SqlCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (@CustomerID, @CompanyName)", connection);

    // Add the parameters for the InsertCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");



// Add the parameters for the InsertCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, dr["CustomerID"]);
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, dr["CompanyName"]);

If you are JUST inserting information the code I gave you will not require ANY parameters. If you JUST need to update then i believe this format will work:

UPDATE <table_name> <alias>
SET (<column_name>,<column_name> ) = (
   SELECT (<column_name>, <column_name>)
   FROM <table_name>
   WHERE <alias.column_name> = <alias.column_name>);

if you need to do both, you will need to find out which rows you are inserting and the run an insert on those rows after the update.
In Oracle, you would need to have a database link enabled to copy data from one database to another.
If you can copy from db to db directly in MS SQL then strickdd's approach is a good one

In this approach
foreach(DataRow dr in dt.Rows)

1. Use an OleDataAdapter to fill the source table
2. Use an OleDataAdapter to set the schema in the destination table
3. Populate the destination table by using the import row approach
4. call the OleDataAdapter's Update for the destination table and you should be set.
You will not have to specifiy any parameters at all.

    If you read the initial question it is clearly stated that "Both databases are SQL Server 2005".


Gentleman...thank you very much.  Learned quite a bit today.  

The only thing I had to add is the SetAdded() method

foreach(DataRow dr in dt.Rows)

otherwise my Update() wouldn't be able to find any records to insert.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.