This article is half-done without your Comment! *** Please share your thoughts via Comment ***
One of the junior database developer of our team asked me one question that how can we copy table data from one database to another database in SQL Server.
This post is dedicated to all junior database developer and sharing full demonstration to copy table data from one database to another database.
Create a First Database with Sample Table:
1 2 3 4 5 6 7 8 9 10 11 12 |
CREATE DATABASE Database_A; GO CREATE TABLE [Database_A].[dbo].[tbl_Employee] ( EmpID INTEGER IDENTITY(1,1) ,EmpName VARCHAR(255) ,Gender CHAR(1) ,DOB DATETIME ,CONSTRAINT pk_tbl_Employee_EmpID PRIMARY KEY(EmpID) ) GO |
Create a Second Database with Sample Table:
1 2 3 4 5 6 7 8 9 10 11 12 |
CREATE DATABASE Database_B; GO CREATE TABLE [Database_B].[dbo].[tbl_Employee] ( EmpID INTEGER IDENTITY(1,1) ,EmpName VARCHAR(255) ,Gender CHAR(1) ,DOB DATETIME ,CONSTRAINT pk_tbl_Employee_EmpID PRIMARY KEY(EmpID) ) GO |
Insert sample data into First Database’s Table:
1 2 3 4 5 6 7 8 |
INSERT INTO [Database_A].[dbo].[tbl_Employee] (EmpName,Gender,DOB) VALUES ('Anvesh','M','19880126') ,('Roy','M','19930214') ,('Jenny','F','19920320') ,('Martin','M','19850616') GO |
Copy First Database’s Table Data into another Database:
1 2 3 4 5 6 7 8 |
INSERT INTO [Database_B].[dbo].[tbl_Employee] (EmpName,Gender,DOB) SELECT [EmpName] ,[Gender] ,[DOB] FROM [Database_A].[dbo].[tbl_Employee] GO |