Forum Moderators: open

Message Too Old, No Replies

Update second table according to the first one

         

tabish

3:48 am on Oct 27, 2009 (gmt 0)

10+ Year Member



OK..

I have two tables.. say

table1
table2

Now.. all the user registrations and updats are happening in table1 while table one contains old data.

Now.. what i want is.. I want to update table2 according to the table1. All the new registrations and updated entries should be inserted/updated into table 2 according to table1.

I want to do it once in a day, how that can happen?

Please help.
Regards

LifeinAsia

3:44 pm on Oct 27, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



while table one contains old data

I assume you meant table2 contains old data?

You'll need 2 steps:
1) Update existing data
2) Add new data

I assume you have some identity field ("key") in both tables, so you can identify matching rows.
For 1, use something like:
INSERT table2
SELECT *
FROM Table1
WHERE Table1.key NOT IN (SELECT Table2.key)

For 2, use something like:
UPDATE table2 SET
Field1=Tabel1.Field1,
Field2=Table1.Field2,
...
FROM Table1 INNER JOIN Table2 ON Table1.key=Table2.key

Alternatively, you can do the following:
DELETE *
FROM Table2

INSERT Table2
SELECT *
FROM Table1