January 16, 2006 at 3:52 am
Hi, I'm still fairly new to SQL server 2000
can anyone tell me how to copy data from a table in one database to another, but only data that isn't in the second database?
January 16, 2006 at 6:37 am
Asssuming there is the same unique key in both tables youcould simply do this using a query like:
INSERT INTO table1
SELECT * FROM table2 WHERE ID NOT IN (SELECT ID FROM table1)
January 16, 2006 at 6:44 am
many thanks for the quick responce, I'll give that a try, although I was told to obtain the results I want I should really use JOIN, something I haven't even tried or even know where to start !!
January 16, 2006 at 9:10 pm
Uh... if you're new, it'll take a bit of study on your part, but you may want to consider setting up "replication"... then, it's nearly "auto-magic" from there. The steps are much to complex to post here. Start by looking up "replication" in Books OnLine... you will need SA privs to do this. If you don't have them, ask your friendly neighborhood DBA to setup replication for you.
If you are a true believer in brute force, you could setup triggers on the tables to do mimic replication on a more realtime basis at the expense of some performance.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 17, 2006 at 2:08 pm
If you want to use a join:
INSERT INTO Table2
SELECT tb1ID, tb1Name, tb1Description
FROM Table1 T1
LEFT OUTER JOIN Table2 T2
ON T1.tb1ID = T2.tb2ID
WHERE tb2id IS NULL
-- the keyword OUTER is optional
If your table is really huge, you might want to test to see which method uses the least resources.
Replication is the answer IF you need to move data on an ongoing basis. If there's not a lot of tables affected, you could do this with a stored procedure and a SQL Agent job. Replication is a bit of a beast. It's not pretty.
January 18, 2006 at 1:52 am
Many thanks people, I'll give all the suggestions a try
January 18, 2006 at 3:13 pm
Might as well give you the not exists option as well
INSERT INTO table1
SELECT * FROM table2 t2 WHERE NOT exists (SELECT 1 FROM table1 t1 where t1.id = t2.id)
January 19, 2006 at 4:04 am
Generally speaking you want to avoid using 'IN ({subquery})' as it can be very inefficient. Stick to using the JOIN method.
(Marvin)
January 19, 2006 at 4:07 am
Many thanks people, it looks like I need to get my head around Joins
January 19, 2006 at 11:02 am
What if the database tables have too many fields to allow replication to work? I'm in a similar situation but replication won't work on my databases which were designed by a solution provider so I can't just 'fix' the tables.
I'm going to explain the whole situation in another thread but am not sure if I should use the Backup, Replication or Newbie forum... Any suggestions?
January 20, 2006 at 7:45 am
The Newbie forum gets a lot of traffic..
January 23, 2006 at 10:19 pm
Try that using a DTS Package..
..hema
January 23, 2006 at 10:24 pm
Then, I'm thinking that someone didn't design the database correctly. It's hard for me to believe that anyone would make a permanent table that has over 520 some odd columns. Like HemaSunder suggested, use DTS instead and fire that solution provider.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply