Copy Table Data Across Databases

Hello. Some of you might have already encountered this and some will encounter it in SQL interviews. Here is the solutions for the questions an interviewer might ask you. That is, how to copy or migrate one table's content to another table within a database or across databases.

Copy Table Data with in Database

Let us take a database. Here iShrissDB is my database with a table UserDetails that is to copied.

  1. select * into newTable Name from oldTable Name  

 For example:

  1. use iShrissdb  
  2. select * into dbo.Details from dbo.UserDetails  

Then:

 

Copy Table Data across two Databases

  1. select * into [new Database].newTable from [old database].oldTable  

 Let us take the two databases iShrissDB and DBEngine. As we can see we have a table named UserDetails in the iShrissDB database, we will copy this table from the iShrissDB database to the DBEngine database.

  1. use iShrissdb  
  2. select * into [DBEngine].dbo.newTable from [iShrissdb].dbo.UserDetails  

 

 Copying Table with same Table name. 
  1. select * into [DBEngine].[dbo].UserDetails from [iShrissdb].[dbo].UserDetails  
 
 
 Summary

We learned how to navigate a table within SQL Server databases. 
 
Kudos!