Copy a table from one database to another in SQL Server 2005

If you have a table in a database and you would like to copy the table to another database, use this query:

SELECT * INTO AdventureWorks.dbo.CustomersTemp FROM Northwind.dbo.Customers

Just remember that using this query will only transfer the schema and data. It does not transfer the indexes, foreign keys, statistics etc.

If you want to transfer all the objects from one database to another, open Sql Server Management Studio > Right click on your database > All Tasks > Generate SQL Scripts. Then run these scripts against the new database.

Transfer both schema and data

To copy both data and schema, use the Microsoft SQL Server Database Publishing Wizard 1.1. This tool works for both SQL 2000 and SQL 2005 and generates a single SQL script file which can be used to recreate a database (both schema and data).

27 comments:

  1. Thank you! I was searching the net to copy a table and found ur blog.

    ReplyDelete
  2. I am still looking for pure copy table without using SQL Statement (SQLServer SMO is prefered) but I think I am going to use your tips if none I can find...

    Nice blog :)
    ___________

    Daniel - averagecoder.net

    ReplyDelete
  3. averagecoder: The solution you choose also depends on the size of your table. Using the scripts 'may' not feasible if your table contains a million rows.

    ReplyDelete
  4. thanks for the help :-)

    ReplyDelete
  5. Thanks a lot! Nice blog!

    ReplyDelete
  6. your a retard lmao

    ReplyDelete
  7. Thanks for this. But does this work with large tables?
    Averagecoder - have you tried Import/Export as a solution to copying tables? Unfortunately, this does not work with tables with 1000s of rows.

    ReplyDelete
  8. Thanks for the Help!

    ReplyDelete
  9. Thanx it really saved my time and effort

    ReplyDelete
  10. it creates a new table. i want to copy just data from one table to another

    ReplyDelete
  11. Thankyou so much buddy, I tried in many ways to get this done, but finally your solution helped me.

    Nice blog.

    ReplyDelete
  12. Thank you for sharing knowledge and helping others with it.

    Amir

    ReplyDelete
  13. From db2, type this in your query analyzer:

    INSERT INTO copytable SELECT * FROM db1.dbo.origtable

    ...........................Thankyou

    ReplyDelete
  14. Thanks, it works!

    ReplyDelete
  15. Thank you!

    - venky

    ReplyDelete
  16. Thanks, dude. Still helpful.

    ReplyDelete
  17. insert into Suprotim_Agarwal_comments values('Thanks a lot bro');

    ReplyDelete
  18. Hi,

    http://hightechpost.blogspot.com/2011/10/copying-table-in-sql-server-2005.html

    check it in simple way for:

    1)Copy table from same Database:

    2)Copy table from another Database:

    3)Copy few columns into the destination table:

    regards
    hightechpost

    ReplyDelete
  19. thanks but my databases are present on different machine (server) then what should i do?

    ReplyDelete
  20. thanks but my databases are present on different machine (server) then what should i do?


    my email ID is : aju184@gmail.com

    ReplyDelete