![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have 2 tables, Table1 and Table2. I have copied all data from Table1 to Table2. However Table1 is dynamic it has new rows added and some old rows modified everyday or every other day... How can I continue to keep Table2 up to date without always having to copy everything from Table1? Basically from now on I would only like to copy new rows or modified rows in Table1 to Table2 and skip rows that are already present and have not been modified in Table1. I would like to not do anything for any rows that were removed in Table1 and continue to keep a copy of them in Table2. Is using a DTS package the best way to automate this update of Table2 to make sure Table2 is always up-to-date with Table1? |
#3
| |||
| |||
|
|
The first question is why do you want to do this in the first place? It seems funny that you would want to have two identical tables in the same database? Or ar the tables in different databases on different servers? |
|
If the tables are on the same server, a trigger would be the best way to do it. |
#4
| |||
| |||
|
|
Hi, sorry perhaps I should have been a bit more clear. Well, Table2 is essentially a Master table that will have a record of all users that were ever added to Table1. So even if at a later date userA and userB were removed from Table1, a record of UserA and UserB will always be there in Table2. So yes right now Table1 and 2 are identical and that seems pointless...however soon Table2 will be different in that it will have a record of rows that are no longer present in Table1. I'm keeping track of them via another method which checks if a row has been removed from Table1 if so it adds the date of removal to a column of that row in Table2. This is why I dont want to update Table2 if a row is removed in Table1...only if a new row is added or an existing one modified. I hope that explains what I'm trying to do :-) can I still use Triggers to do this? |
#5
| |||
| |||
|
|
Yas (yas... (AT) gmail (DOT) com) writes: Hi, sorry perhaps I should have been a bit more clear. Well, Table2 is essentially a Master table that will have a record of all users that were ever added to Table1. So even if at a later date userA and userB were removed from Table1, a record of UserA and UserB will always be there in Table2. So yes right now Table1 and 2 are identical and that seems pointless...however soon Table2 will be different in that it will have a record of rows that are no longer present in Table1. I'm keeping track of them via another method which checks if a row has been removed from Table1 if so it adds the date of removal to a column of that row in Table2. This is why I dont want to update Table2 if a row is removed in Table1...only if a new row is added or an existing one modified. I hope that explains what I'm trying to do :-) can I still use Triggers to do this? Since the tables are in the same database, triggers is definitely the way to go. |
#6
| |||
| |||
|
|
Since the tables are in the same database, triggers is definitely the way to go. Thanks. This is what I'm trying to do now... do you know how I can refer to the row that has just been added or modified? |
#7
| |||
| |||
|
|
On Thu, 16 Aug 2007 08:13:47 -0700, Yas <yas... (AT) gmail (DOT) com> wrote: Since the tables are in the same database, triggers is definitely the way to go. Thanks. This is what I'm trying to do now... do you know how I can refer to the row that has just been added or modified? Read up on the INSERTED and DELETED virtual tables that are available to triggers. |
#8
| |||
| |||
|
|
CREATE TRIGGER my_Trigger ON [dbo].[Table2] FOR INSERT AS INSERT INTO Table2(STATUS,attribute15,email,lastname1,lastname 2,name,company,startDate) SELECT 'Active' AS STATUS, b.Attribute15, b.email, b.lastname1, b.lastname2, b.name, b.company, b.startDate FROM Inserted b LEFT OUTER JOIN Table2 a ON b.Attribute15 = a.Attribute15 WHERE a.Attribute15 IS NULL GO The syntax according to MS SQL server is correct but nothing happens when a new row is inserted into Table1. |
#9
| |||
| |||
|
|
Yas (yas... (AT) gmail (DOT) com) writes: CREATE TRIGGER my_Trigger ON [dbo].[Table2] FOR INSERT AS INSERT INTO Table2(STATUS,attribute15,email,lastname1,lastname 2,name,company,startDate) SELECT 'Active' AS STATUS, b.Attribute15, b.email, b.lastname1, b.lastname2, b.name, b.company, b.startDate FROM Inserted b LEFT OUTER JOIN Table2 a ON b.Attribute15 = a.Attribute15 WHERE a.Attribute15 IS NULL GO The syntax according to MS SQL server is correct but nothing happens when a new row is inserted into Table1. Well, the code you posted is a trigger on Table2, so... |
#10
| |||
| |||
|
|
DOH!!! what a silly mistake. :-) |
|
do you think apart from that its fine for inserting new rows into Table2 from Table1 trigger? |
![]() |
| Thread Tools | |
| Display Modes | |
| |