Solved

SQL server indentify then insert missing rows from database to another

Posted on 2011-09-12
5
250 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 400 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 100 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

737 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