CREATE TRIGGER [dbo].[TU_TotPriceHeadH] ON [dbo].[headH] 
For Update AS  
Declare @Tot_Price_Kharid   Money 
Declare @Tot_Price_Omdeh    Money 
Declare @Tot_Price_Forosh   Money 
Declare @Tot_Price          Money 
Declare @Nu                 Bigint 
Declare @Kind_flag          Char 
If Update(Tot_Price_Forosh)Or Update(Tot_Price_Kharid)  Or Update(Tot_Price_Omdeh) 
begin   
  Select @Nu=i.Id_Havaleh,@Tot_Price_Forosh=i.Tot_Price_Forosh  ,  
         @Tot_Price_Kharid=i.Tot_Price_Kharid,@Tot_Price_Omdeh=i.Tot_Price_Omdeh,  
         @Kind_Flag=i.Kind_Flag from inserted i where Id_Havaleh=i.Id_havaleh  
  Set @Tot_Price=0 
  if  @Kind_Flag= '0'  Set @Tot_Price=@Tot_Price_Forosh 
  if  @Kind_Flag= '2'  Set @Tot_Price=@Tot_Price_Kharid 
  if  @Kind_Flag= '3'  Set @Tot_Price=@Tot_Price_Kharid 
  if  @Kind_Flag= '4'  Set @Tot_Price=@Tot_Price_Kharid 
  if  @Kind_Flag= '5'  Set @Tot_Price=@Tot_Price_Forosh 
  if  @Kind_Flag= '6'  Set @Tot_Price=@Tot_Price_Forosh 
  if  @Kind_Flag= '7'  Set @Tot_Price=@Tot_Price_Forosh 
  if  @Kind_Flag= '8'  Set @Tot_Price=@Tot_Price_Kharid 
  if  @Kind_Flag= '9'  Set @Tot_Price=@Tot_Price_Forosh 
  If @Tot_Price <>0 
  begin  
    Update   dbo.sanad set Bedeh=@Tot_Price  Where (Nu_Havaleh=@Nu) and (Bestan=0) and (Nu_Over_under is Null) And (Nu_Ghest is Null)  
    Update dbo.sanad set Bestan=@Tot_Price  Where (Nu_Havaleh=@Nu) and (Bedeh=0) and (Nu_Over_under is Null) And (Nu_Ghest is Null) 
  end 
 end
