CREATE    TRIGGER [dbo].[TI_History_Price] ON [dbo].[Anbar]
FOR INSERT
AS 
Begin
   Declare @Price_Kharid_New   Money
   Declare @Price_Omdeh_New  Money
   Declare @Price_Forosh_New  Money

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

   Select @Price_Kharid_New =Sum(Price_Kharid),
              @Price_Omdeh_New=Sum(Price_Omdeh),
              @Price_Forosh_New=Sum(Price_Forosh)     From Inserted i Where A_Code=i.A_Code And K_Code=i.k_Code

   Select @Anbar_Code = A_Code,@Kala_Code = K_Code From Inserted i  Where A_Code=i.A_Code And K_Code=i.k_Code

    IF (ISNULL(@Price_Kharid_New,0) <> 0)
      INSERT Into  Dbo.History_Price(A_Code,K_Code,Price_Kharid,Price_Omdeh,Price_Forosh,ChangePrice )
       Values(@Anbar_Code,@Kala_Code,@Price_Kharid_New,@Price_Omdeh_New,@Price_Forosh_New,'3')

    IF (ISNULL(@Price_Forosh_New,0) <> 0)
      INSERT Into  Dbo.History_Price(A_Code,K_Code,Price_Kharid,Price_Omdeh,Price_Forosh,ChangePrice )
       Values(@Anbar_Code,@Kala_Code,@Price_Kharid_New,@Price_Omdeh_New,@Price_Forosh_New,'5')

    IF (ISNULL(@Price_Omdeh_New,0) <> 0)
      INSERT Into  Dbo.History_Price(A_Code,K_Code,Price_Kharid,Price_Omdeh,Price_Forosh,ChangePrice )
       Values(@Anbar_Code,@Kala_Code,@Price_Kharid_New,@Price_Omdeh_New,@Price_Forosh_New,'9')
    
End



