CREATE TRIGGER [dbo].[TU_History_Price] ON [dbo].[Anbar]
After Update
AS 


If Update(Price_Kharid) Or Update(Price_Forosh) or Update(Price_Omdeh)
Begin
   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)

   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)     Or  
         (@Price_Omdeh_New  <> @OldPriceOmdeh)      Or  
         (@Price_Forosh_New <> @OldPriceForosh)       
        Insert Into  Dbo.History_Price(A_Code,K_Code,Price_Kharid,Price_Forosh, Price_Omdeh )
        Values(@Anbar_Code,@Kala_Code,@Price_Kharid_New,@Price_Forosh_New,         @Price_Omdeh_New)
        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
