triggers - table adds the updates sql server -


i have table tab, , want create table history insert updates made in tab table . i've created trigger tr_update, doens't work correctly.

create table tab  ( id_tab char(5), data_tab int )  create table history  (id_modify       char(3), old_data             int, new_data             int, )   create trigger tr_update on tab after update declare @id char(3) declare @old int declare @new int  select @id=id_tab, @new=data_tab inserted select @old=data_tab deleted  insert history (id_modify,old_data,new_data) values (@id,@old,@new) 

triggers works sets of data, meaning setsinsertedanddeletedcan contain more 1 row , simple assignments won't work. instead can useinsertedand deletedas source tables (and join them if want both new , old data, in case).

create trigger tr_update on tab after update  insert history (id_modify,old_data,new_data) select i.id_tab, d.data_tab, i.data_tab  inserted inner join deleted d on i.id_tab = d.id_tab 

on side note, seems theid_datafiled in history table 3 chars wide, while original table has 5 chars wide. won't work; field in history table needs same size (or wider) or else server won't allow insert data might truncated.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -