Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two tables in that first table have 5 columns and second table have 10 columns then my question is i want to insert values into first table that is only two columns i have to insert and remaining three columns should be take from second table that first table id = second table id matchable in that first table and second table have that three columns name are same i dont know how to i solve this problem

What I have tried:

i dont know how to solve this problem
Posted
Updated 6-Jun-17 7:16am

First off, that's a bad idea because it means you have duplicated data - and depending on what the data is that could mean significant wasted space in your DB. If you already have a foreign key relationship between the two tables it's a lot easier (and safer, as the data can't get out of sync if it isn't duplicated) to use a JOIN when you try to retrieve the info:
SQL
SELECT a.Col1, a.Col2, b.Col1, b.Col2, b.Col3 FROM MyFirstTable a
JOIN MySecondTable b ON a.Id = b.OtherTableId

If you must do the INSERT in a dangerous and poor design manner though, you can use an SQL INSERT INTO SELECT Statement[^].
 
Share this answer
 
v2
 
Share this answer
 
Maybe something like this:
SQL
INSERT INTO table1 ([ID],[Name],[Address])  
	SELECT [ID],[Name],[Address] FROM table2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900