
Create TRIGGER [dbo].[Td_History_Delete_Ghest] ON [dbo].[Ghest] 
FOR DELETE 
AS
Declare @AutoId Numeric(18,0)
Declare @Id_Ghest bigint
Declare @Aghsat_nu int
Declare @Date_Pa SmallDateTime
Declare @Date_Dr SmallDateTime
Declare @Bedeh money
Declare @Bestan money
Declare @User_Id Smallint
Declare @SysTarikh SmallDateTime
Declare @FlagTypeAghsat char (1) 
Declare @NumberFactor varchar (8) 
Declare @Daryaft money
Declare @Status char (1) 
Declare @Act int
Declare @New_status char (1) 
Declare @Old_Status char (1) 
Declare @IdSanad bigint

 Declare Delete_Cursor_Ghest  Cursor Local For
 Select  AutoId, Id_Ghest, Aghsat_Nu, Date_Pa, Date_Dr, Bedeh, Bestan, User_Id,
             SysTarikh, FlagTypeAghsat, NumberFactor, Daryaft, Status, Act, New_Status, Old_Status, IdSanad  From Deleted   

 Open Delete_Cursor_Ghest
 Fetch Next From Delete_Cursor_Ghest Into  @AutoId, @Id_Ghest, @Aghsat_Nu, @Date_Pa, @Date_Dr, @Bedeh, @Bestan, @User_Id,
             @SysTarikh, @FlagTypeAghsat, @NumberFactor, @Daryaft, @Status, @Act, @New_Status, @Old_Status, @IdSanad

 Begin Tran DeleteRecGhest
 While @@Fetch_Status = 0 
 Begin
      Insert Into History_Delete_Ghest  (AutoId, Id_Ghest, Aghsat_Nu, Date_Pa, Date_Dr, Bedeh, Bestan, User_Id,
				          SysTarikh, FlagTypeAghsat, NumberFactor, Daryaft, Status, Act, New_Status, Old_Status, IdSanad,UserIdForDelete)
		                        Values (@AutoId, @Id_Ghest, @Aghsat_Nu, @Date_Pa, @Date_Dr, @Bedeh, @Bestan, @User_Id,
             				          @SysTarikh, @FlagTypeAghsat, @NumberFactor, @Daryaft, @Status, @Act, @New_Status, @Old_Status, @IdSanad,@User_Id)

     IF @@Error > 0 
        RollBack Tran DeleteRecGhest
    
    
    Fetch Next From Delete_Cursor_Ghest Into  @AutoId, @Id_Ghest, @Aghsat_Nu, @Date_Pa, @Date_Dr, @Bedeh, @Bestan, @User_Id,
					              @SysTarikh, @FlagTypeAghsat, @NumberFactor, @Daryaft, @Status, @Act, @New_Status, @Old_Status, @IdSanad
 End
 
 Commit Tran DeleteRecGhest
 Close           Delete_Cursor_Ghest
 Deallocate  Delete_Cursor_Ghest