Solved

SQL server indentify then insert missing rows from database to another

Posted on 2011-09-12
5
243 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
  • 2
  • 2
5 Comments
 
LVL 59

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 59

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.

820 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