CREATE TRIGGER [dbo].[TU_History_Price] ON [dbo].[Anbar]
After Update
AS 
   Declare @Price_Kharid_New  Money
   Declare @Price_Omdeh_New   Money
   Declare @Price_Forosh_New  Money
   
   Declare @OldPriceKharid    Money
   Declare @OldPriceForosh    Money
   Declare @OldPriceOmdeh     Money
   
   Select @OldPriceKharid = Price_kharid, 
    	  @OldPriceForosh = price_Forosh,
	      @OldPriceOmdeh = Price_Omdeh
   From deleted

   Declare @Anbar_Code VarChar(3)
   Declare @Kala_Code  VarChar(15)

If Update(Price_Kharid) 
Begin
   Declare Cursor_Update Cursor For
   Select A_Code, K_code, Price_Kharid, Price_Forosh, Price_Omdeh From Inserted
   Open Cursor_Update
   Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New
   while @@Fetch_Status = 0 
   Begin
      If (@Price_Kharid_New <> @OldPriceKharid)     
        Insert Into  Dbo.History_Price(A_Code,K_Code,Price_Kharid,Price_Forosh, Price_Omdeh, ChangePrice )
        Values(@Anbar_Code,@Kala_Code,@Price_Kharid_New,@Price_Forosh_New,         @Price_Omdeh_New,'3')
        Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New
   End
   Close Cursor_Update
   Deallocate Cursor_Update
end

If Update(Price_Forosh) 
Begin
   Declare Cursor_Update Cursor For
   Select A_Code, K_code, Price_Kharid, Price_Forosh, Price_Omdeh From Inserted
   Open Cursor_Update
   Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New
   while @@Fetch_Status = 0 
   Begin
      If (@Price_Forosh_New <> @OldPriceForosh)       
        Insert Into  Dbo.History_Price(A_Code,K_Code,Price_Kharid,Price_Forosh, Price_Omdeh, ChangePrice )
        Values(@Anbar_Code,@Kala_Code,@Price_Kharid_New,@Price_Forosh_New,         @Price_Omdeh_New,'5')
        Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New
   End
   Close Cursor_Update
   Deallocate Cursor_Update
end

If Update(Price_Omdeh) 
Begin
   Declare Cursor_Update Cursor For
   Select A_Code, K_code, Price_Kharid, Price_Forosh, Price_Omdeh From Inserted
   Open Cursor_Update
   Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New
   while @@Fetch_Status = 0 
   Begin
      If (@Price_Omdeh_New <> @OldPriceOmdeh)     
        Insert Into  Dbo.History_Price(A_Code,K_Code,Price_Kharid,Price_Forosh, Price_Omdeh, ChangePrice )
        Values(@Anbar_Code,@Kala_Code,@Price_Kharid_New,@Price_Forosh_New,         @Price_Omdeh_New,'9')
        Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New
   End
   Close Cursor_Update
   Deallocate Cursor_Update
end
