Monday, 12 February 2018

Trigger for update in sql server

     
  create trigger updates on stu             
     for update as begin declare
  @id int
  declare @Nname nvarchar(49),@Oname nvarchar(49),
   @Nage int,@Oage int,@Nsalary int,@Osalary int
  ,@ides int  ,@names nvarchar(49),@ages int,@salarys int,@dates nvarchar(30)
 
   select * into #temptable from inserted
   while(exists(select id from #temptable))
   begin
   select top 1 @id=id,@Nname=name,@Nage=age,@Nsalary=salary from #temptable
   select @Oname= name,@Oage=age,@Osalary=salary from deleted
 
   if(@Oname <> @Nname)
   
   set @names=@Nname
   
   if(@Oage <> @Nage)
   
   set @ages=@Nage
   
  if(@Osalary <> @Nsalary)
 
  set @salarys=@Nsalary
   
   set @dates=cast(getdate() as nvarchar(30))
   insert into backs values(@id,@names,@ages,@salarys,@dates)
   delete from #temptable where id=@id
   end
   end
 

No comments:

Post a Comment