Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL server indentify then insert missing rows from database to another

Posted on 2011-09-12
5
Medium Priority
?
267 Views
Last Modified: 2012-08-13
There are 9031 ids in one table that i need to find the ROWS with those ids from another table then insert those rows into another table..


What would be the best way to do this without manullay typing out 9031 times?
0
Comment
Question by:wilko100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1600 total points
ID: 36524266
Hi.

So it sounds like you have table A (9031 ids), table B (9031 ids), and then table C (missing some ids and needs updating) -- that correct?

If so, then you can do this:

INSERT INTO C({column list})
SELECT {column list}
FROM A
JOIN B ON A.id = B.id
/* check for those id values that are not in C yet */
WHERE NOT EXISTS (
   SELECT 1
   FROM C
   /* or whatever unique key(s) connect C to B|A */
   WHERE C.id = A.id 
)
;

Open in new window


Hope that helps!
0
 
LVL 23

Assisted Solution

by:Racim BOUDJAKDJI
Racim BOUDJAKDJI earned 400 total points
ID: 36524294
Additionally to the above comment, don't forget to set the IDENTITY to OFF before attempting to insert.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36524316
Thanks, Racimo! Good point. wilko100, if you need anything else, just let us know. Between Racimo and I, we should be able to help you through understanding what you need to do.
0
 

Author Comment

by:wilko100
ID: 36525524
Cheers, i'll try that
0
 

Author Closing Comment

by:wilko100
ID: 36937837
Sorry for the delay responding, just got a pre prod enviorment setup. Works a treat. Dont think i would of done it with the help

 Thanks for the advice
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question