April 24, 2014 at 5:00 am
I have two tables. Table 1 has column "job", table 2 has column "job" and column "item". In table table 2 there are multiple "items" for each "job"
I would like to insert all of the "items" into table 1, based on a join table1.job = table2.job
What is the best way accomplish this task?
Thanks!
April 24, 2014 at 5:07 am
jhinch (4/24/2014)
I have two tables. Table 1 has column "job", table 2 has column "job" and column "item". In table table 2 there are multiple "items" for each "job"I would like to insert all of the "items" into table 1, based on a join table1.job = table2.job
What is the best way accomplish this task?
Thanks!
First create a query joining the two tables and filter the results or whatever needed.
Then construct an insert clause and use the previous query as the source.
insert into mytable(column list)
select [column list]
from mytable1 inner join mytable2
on [join condition]
๐
April 24, 2014 at 5:08 am
Here is an example to further clarify the question;
Table1
Job
1
2
3
Table2
Job Item
1 123
1 456
2 123
2 999
2 121
3 787
I would like to insert all items into table 1 based on the matching job number in table2. i.e.;
table1
Job Item
1 123
1 456
2 123
2 999
2 121
3 787
April 24, 2014 at 5:19 am
Your sample indicates that table1 is an exact copy of two columns of table2 - is this really the case?
Here's one way to do what you want, if table1 is restricted to jobs which already exist in it:
SELECT DISTINCT job
INTO #temp
FROM Table1
TRUNCATE TABLE Table1
INSERT INTO Table1 (job, item)
SELECT job, item
FROM Table2 t2
WHERE EXISTS (SELECT 1 FROM #temp t1 WHERE t1.job = t2.job)
DROP TABLE #temp
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
May 20, 2014 at 2:06 pm
You don't necessarily need to use a join, try the WHERE EXISTS clause. Look it up on MSDN.
----------------------------------------------------
May 20, 2014 at 4:49 pm
MMartin1 (5/20/2014)
You don't necessarily need to use a join, try the WHERE EXISTS clause. Look it up on MSDN.
WHERE EXISTS requires an Equi-Join within the sub-query.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 20, 2014 at 4:50 pm
jhinch (4/24/2014)
Here is an example to further clarify the question;Table1
Job
1
2
3
Table2
Job Item
1 123
1 456
2 123
2 999
2 121
3 787
I would like to insert all items into table 1 based on the matching job number in table2. i.e.;
table1
Job Item
1 123
1 456
2 123
2 999
2 121
3 787
Why are you making a copy of the table? Why not just use Table2?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 21, 2014 at 2:44 am
...
Why are you making a copy of the table? Why not just use Table2?
And if you really want a complete copy of Table2 in Table1 then you can do just that:
drop table Table1
select * into Table1 from Table2
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply