 CREATE TRIGGER [dbo].[TI_NU_Manual] ON [dbo].[HeadS] FOR INSERT AS 
 Begin
  Declare @TmpIdentity BigInt
  Declare @Tmp_Nu_Manual BigInt
  Set @TmpIdentity=@@Identity
  select  @Tmp_Nu_Manual =Nu_Manual from inserted if ((@Tmp_Nu_Manual ) = 0)  Or (@Tmp_Nu_Manual is null) Or (@Tmp_Nu_Manual =dbo.samsi_Date(GetDate()))
   Begin
     Select @TmpIdentity=Max(Nu_Manual)  From Dbo.HeadS Where (Id_Sanad <> @TmpIdentity) and (Nu_Manual <999990)
     Update Dbo.HeadS Set Nu_Manual=IsNull(@TmpIdentity,1) + 1,
     Note ='سند خودكار مورخ ' + SUBSTRING(dbo.MiladiToShamsi(GETDATE()),1,4)+'/'+
       SUBSTRING(dbo.MiladiToShamsi(GETDATE()),6,2)+'/'+
       SUBSTRING(dbo.MiladiToShamsi(GETDATE()),9,2)
     Where  (Id_Sanad=@@Identity)
   end
 End
