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 @userId Smallint
   
   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, Inserted.User_Id From Inserted
   Open Cursor_Update
   Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New, @userId
   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, User_Id )
        Values(@Anbar_Code,@Kala_Code,@Price_Kharid_New,@Price_Forosh_New,         @Price_Omdeh_New,'3', @userId)
        Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New, @userId
   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, User_Id From Inserted
   Open Cursor_Update
   Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New, @userId
   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, User_Id )
        Values(@Anbar_Code,@Kala_Code,@Price_Kharid_New,@Price_Forosh_New,         @Price_Omdeh_New,'5', @userId)
        Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New, @userId
   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,User_Id From Inserted
   Open Cursor_Update
   Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New,@userId
   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, User_Id )
        Values(@Anbar_Code,@Kala_Code,@Price_Kharid_New,@Price_Forosh_New,         @Price_Omdeh_New,'9', @userId)
        Fetch Next From Cursor_Update Into @Anbar_Code, @Kala_Code, @Price_Kharid_New, @Price_Forosh_New, @Price_Omdeh_New, @userId
   End
   Close Cursor_Update
   Deallocate Cursor_Update
end
