Copy Table Data Across Databases

Introduction

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

Copy Table Data within the Database

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

select * into newTable Name from oldTable Name  

 For example

Copy Table Data Across Databases

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

Copy Table Data Across Databases

Then

Copy Table Data Across Databases 

Copy Table Data across two Databases

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.

Copy Table Data Across Databases

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

Copy Table Data Across Databases

Copying Table with the same Table name

select * into [DBEngine].[dbo].UserDetails from [iShrissdb].[dbo].UserDetails  

Copy Table Data Across Databases

Summary

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


Similar Articles