Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 update/insert based on timestamp field

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-09-30 : 17:01:10
hi,

i have two tables:

create table test
(id int identity(1,1) not null
,name nvarchar(10)
,time_stamp timestamp)



insert into test (name) values ('John')
insert into test (name) values ('Bon')
insert into test (name) values ('Jovi')


and second one "test2", which is identical to "test".

What i want is to track:
- if there is change on existing data then update
- if there are new rows than insert new data
to table "test2" from table "test".

timestamp would be nice solution. does anyone have any sample on this case, how to start? is there better way to do it?

thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-09-30 : 17:08:20
Use a trigger and look at the trigger tables inserted and deleted.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-09-30 : 17:12:54
i was thinking of using procedure in a job. no triggers
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-09-30 : 17:20:14
Why?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-09-30 : 19:34:21
--Update
UPDATE t2
SET t2.field1 =t.field1,..........
FROM test t
INNER JOIN test2 t2
ON t2.id=t.id
AND t2.name=t.name
AND t2.time_stamp =t.time_stamp

---Insert if different.
INSERT INTO test2 (fields...)
SELECT t.*
FROM Test t
LEFT JOIN test2 b
ON b.id=t.id
AND b.name=t.name
AND b.time_stamp =t.time_stamp
WHERE b.name IS NULL
AND b.time_stamp IS NULL


Go to Top of Page
   

- Advertisement -