Introduction
Recently, I was working on the database and there is the situation in which I have to insert the values from one database table to the another database table.
Well, if the table have 2 or 3 values then we can write the Insert Query to insert the values in the table but suppose if there are too many values, then? The same thing happened to me. I have two tables in the database named State and City. There are 25 records in State and 590 records in the City table.
I got the solution to do this.
Solution
The structure of the Query is as follows:
- USE Target_Database
- GO
- INSERT INTO dbo.Target_Table(Column1, Column2, Column3)
- SELECT Column1, Column2, Column3
- FROM Source_Database.dbo.Source_Table
I have Converted this to the following query:
I have to insert values in two different tables:
- Use Country
- INSERT INTO dbo.State(State_Name)
- SELECT State_Name
- FROM CollegeDb.dbo.State
- INSERT INTO dbo.City(State_ID, City_Name)
- SELECT State_ID, City_Name
- FROM CollegeDb.dbo.City
Output

That's it. Happy Coding!!

शुभम राऊतPosted Sep 12, 2020, 12:48 AM
Is there any way to do the same but for 2 different sql servers? as in taking data from one table from one server and using it as insert for other table on other server?
Ashirwad SatapathiPosted Jan 20, 2020, 4:27 PM
How to write data or push it between multiple databases in Azure SQL. As external tables doesn't support DML operation is there any other way to do it?
Govind KumawatPosted Jan 11, 2018, 1:57 AM
How to get autometically user idhow to get autometically user id from one databse to another? from one databse to another?
Arjun DhilodPosted Jul 20, 2015, 8:24 AM
Yes very nice
Saineshwar BageriPosted Jul 19, 2014, 12:32 AM
Nice article