Just Code‎ > ‎

Log Changes made in database tables, not that hard as i thought

posted Dec 16, 2009, 9:14 AM by Peter Henell   [ updated Dec 16, 2009, 9:23 AM ]
create table MyTable(
id int identity
, namn varchar(20)
, primary key(id)
)

insert mytable values ('Peter')
insert mytable values ('Johanna')
insert mytable values ('Ilsa')
insert mytable values ('Molgan')
insert mytable values ('Joe')



select * from mytable

create table MyTable_Log
(
    action varchar(20),
    id int,
    OldName varchar(20),
    DateChanged datetime,
primary key(id)
)

delete MyTable OUTPUT 'Deleted', Deleted.*, getdate() into MyTable_Log
where id = 2

update MyTable 
    Set Namn = 'Görgen' 
    OUTPUT 'Updated', DELETED.*, getdate() into MyTable_Log
where 
    id = 3

select * from MyTable_Log
Comments