sql - updated record is inserting into the history table not the old record -


i have 2 tables test , testhistory

create table [dbo].[test](     [id] [int] null,     [name] [varchar](10) null,     [status] [char](1) null,     [createddate] [datetime] null ) on [primary]  go  create table [dbo].[test_history](     [id] [int] null,     [name] [varchar](10) null,     [status] [char](1) null,     [createddate] [datetime] null ) on [primary]  go  insert test ([id],[name],[status],[createddate])values (1,'mohan','a',getdate()) 

created trigger :

alter trigger [dbo].[trg_test]       on [dbo].[test]      update             declare @id int;      declare @name varchar(10);      declare @status char(2);      declare @createddate datetime;        select @id = i.id inserted      select @name = i.name inserted      select @status = i.status inserted      select @createddate = i.createddate inserted        insert [dbo].[test_history]            ([id]            ,[name]            ,[status]            ,[createddate]            )            select @id,                  @name,                  @status,                  getdate()                      inserted             @id = [id]  

when i'm updating record

update [test] set status = 'i' old record status = 'a' should inserted ever i'm updating has been inserting testhistory table not old record i'm doing wrong , how insert old value

like if updating status = 'i' , in history table status = 'a' shoul inserted

you need insert deleted not inserted.

see examples here understanding sql server inserted , deleted tables dml triggers.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -