Optimize sql query for sql server 2019

I am very new to sql server and I did a lot of research on optimization of sql query. I have a query which is very long and coded with lot’s of cursor. Since cursor takes lots of time in it’s execution, I wish someone with great experience in sql could help me in making the code run on sql server 2019 as fast and as optimized as possible. Being a new sql developer, I need someone for guidance as the code is very big and complex.


/****** Object:  StoredProcedure [dbo].[ps_Epilator]  ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO

ALTER PROCEDURE [dbo].[ps_Epilator]
AS
BEGIN    
  SET NOCOUNT ON    
      
  DECLARE    
   @lsReportingDate     VARCHAR(8)    
  ,@ldReportingDate  DATE      
  ,@EPILATOR   VARCHAR(4)    
  ,@pNbInsertionPersActuel INT    
  ,@pNbInsertionPersAncien INT    
      
  ,@lCompte   VARCHAR(11)    
  ,@lMaj    INT = 0    
  ,@COMPTE    VARCHAR(11)    
  ,@DATECH    DATE    
  ,@NbInsertionPRET INT = 0    
  ,@DOSS_CODE   VARCHAR(10)    
  ,@LIGN_DEVI   VARCHAR(3)    
  ,@nominalSociete  DECIMAL(15,2)    
  ,@mntBilan   DECIMAL(15,2)    
  ,@ICNE    DECIMAL(15,2)    
  ,@mntHBilan   DECIMAL(15,2)    
  ,@DATE_SIGNATURE  DATE    
  ,@CLIE_ID   VARCHAR(7)    
  ,@nbTypeAmort  VARCHAR(1)    
  ,@nbAmortInt   VARCHAR(1)      
      
  --variables for the fields Pret    
  ,@PRE_ID_PRET   VARCHAR(20)    
  ,@PRE_VA_NFE   VARCHAR(8)    
  ,@PRE_VA_DEVISE   VARCHAR(3)    
  ,@PRE_VN_MNTAUTOR  DECIMAL(19,6)    
  ,@PRE_VN_NOMINALPRET  DECIMAL(15,2)    
  ,@PRE_VN_MNTBILAN  DECIMAL(19,6)    
  ,@PRE_VN_ICNE   DECIMAL(19,6)    
  ,@PRE_VN_MNTHORSBILAN DECIMAL(19,6)    
  ,@PRE_DT_ECHEANCE  DATE    
  ,@PRE_VN_DUREEM   INT    
  ,@PRE_DT_DEBLOCAGE  DATE    
  ,@PRE_VA_TYPAMORT  VARCHAR(20)    
  ,@PRE_VA_RUBCPT   VARCHAR(6)    
  ,@PRE_BO_ISINFINE  VARCHAR(1)    
  ,@PRE_CE_DOSSIERALIS  VARCHAR(20)    
  ,@PRE_LI_ImpOuDechu  VARCHAR(10)    
  ,@PRE_LI_ObjFinance  VARCHAR(50)    
  ,@PRE_VA_NbEchImpaye SMALLINT    
  ,@PRE_IMP_MntEch  DECIMAL(19,6)    
  ,@PRE_TypTaux   VARCHAR(1)    
  ,@PRE_MargeComm   DECIMAL(15,10)    
      
  ----Variables for the fields  compte    
  ,@CMT_VA_RUBCPT   VARCHAR(6)    
  ,@CMT_ID_COMPTE   VARCHAR(11)    
  ,@CMT_VA_DEVISE   VARCHAR(3)    
  ,@CMT_VN_MNTAUTOR  DECIMAL(19,6)    
  ,@CMT_VN_SOLDE   DECIMAL(19,6)    
  ,@CMT_BO_ISDECOVER  VARCHAR(1)    
  ,@CMT_DT_DATOUV   DATE    
  ,@CMT_CodComExt   VARCHAR(20)    
  ,@CMT_LibCom    VARCHAR(100)    
      
  --Variables for the fields  détail    
  ,@DET_TYPE_ENGA      VARCHAR(25) = null    
  ,@DET_ID_ENGA        VARCHAR(20) = null    
  ,@DET_VA_NFE         VARCHAR(8)  = null    
  ,@DET_VA_DEVISEENGA  VARCHAR(3) = null    
  ,@DET_VN_AUTOR       DECIMAL(22,2) = null    
  ,@DET_VN_BILAN      DECIMAL(22,2) = null    
  ,@DET_VN_HORSBILAN  DECIMAL(22,2) = null    
  ,@DET_DT_ECHEANCE   DATE = null    
  ,@DET_DT_DEBLOCAGE  DATE = null    
  ,@DET_VN_DUREEM     INT = null    
  ,@DET_VA_CPTSUPPDEC VARCHAR(11) = null    
  ,@DET_VA_COMPTE     VARCHAR(11) = null    
  ,@DET_VA_RUBCPT     VARCHAR(6) = null    
  ,@Det_VA_CONTRACTANT VARCHAR(7) = null    
  ,@DET_VA_LIBENGA    VARCHAR(100) = null    
  ,@DET_VA_LIBNFE  VARCHAR(30)= null    
  ,@DET_VN_SUMBILAN   DECIMAL(19,2) = null    
  ,@DET_VA_DELEGATION VARCHAR(100) = null    
  ,@DET_VA_TYPENGA    VARCHAR(50) = null    
  ,@DET_VA_IDNEWSRCLIENT VARCHAR(8) = null    
  ,@DET_VA_NEWSRCLIENT VARCHAR(50) = null    
  ,@DET_VN_MNTENGACLIENT DECIMAL(22,2)= null    
  ,@DET_VN_NBENGACLIENT INT =null    
  ,@DET_VA_NOMEXPL   VARCHAR(25) = null    
  ,@DET_VA_DIRECTION VARCHAR(25) = null    
  ,@DET_VA_NETCLIENT VARCHAR(8)  = null    
  ,@DET_VA_WLCLI    VARCHAR(1)  = null    
  ,@DET_VA_NIVWLCLI  VARCHAR(1)= null    
  ,@DET_DT_NOTECLIENT DATE = null    
  ,@DET_VA_LIBELLENET VARCHAR(30) =null    
  ,@DET_ID_TG   VARCHAR(7)  = null    
  ,@DET_VA_NOMTG  VARCHAR(80) = null     
  ,@DET_VA_NETTG  VARCHAR(8)  = null    
  ,@DET_VA_WLTDG  VARCHAR(1)  = null    
  ,@DET_VA_NIVWLTDG VARCHAR(1)  = null    
  ,@DET_DT_NOTEGROUPE DATE  = null    
  ,@DET_LI_NETTG  VARCHAR(30) = null    
  ,@DET_VA_IDNEWSRTDG VARCHAR(8) = null    
  ,@DET_VA_NEWSRTDG VARCHAR(50) = null    
  ,@DET_ID_CLIENT  VARCHAR(7)  = null    
  ,@DET_VA_NOMCLIENT VARCHAR(25) = null    
  ,@DET_VA_SYSSOURCE VARCHAR(6)  = null    
  ,@DET_VN_MNTENGATG INT   = null    
  ,@DET_VA_REVISION VARCHAR(10)  = null    
  ,@DET_CE_DOSSIERALIS VARCHAR(20)    
  ,@DET_VN_NOMPRET DECIMAL(15,2)    
  ,@DET_VN_ICNE  DECIMAL(19,6)    
  ,@DET_VA_TYPAMORT VARCHAR(20)    
  ,@DET_BO_ISINFINE VARCHAR(1)    
  ,@DET_BO_ISDECOVER VARCHAR(1)    
  ,@DET_VN_NBENGATG DECIMAL(18,0)    
  ,@DET_LI_ImpOuDechu VARCHAR(10)    
  ,@DET_LI_ObjFinance  VARCHAR(50)     
  ,@DET_VA_NbEchImpaye SMALLINT    
  ,@DET_IMP_MntEch  DECIMAL(19,6)    
  ,@DET_TypTaux   VARCHAR(1)    
  ,@DET_MargeComm   DECIMAL(15,10)    
      
  --Initialization of variables    
  SET @EPILATOR ='EPILATOR'     
  SELECT @ldReportingDate = dbo.getReportingDate (@EPILATOR)    
  SELECT @lsReportingDate = convert(char(8),@ldReportingDate,112)    
  print '@lsReportingDate : '+ @lsReportingDate    
  print '@ldReportingDate : '+ convert(char(10),@ldReportingDate)    
    
 
    
    -- We Flag all accounts that correspond to overdrafts that have not been repaid on their due date
    
    -- We select all the accounts that had an overdraft that expired less than 3 months ago  
    -- (then we do not have the history of the positions of the account)    
    DECLARE CurrentCompte CURSOR FOR    
       (SELECT CMT_ID_COMPTE AS COMPTE,GRT_DATECH as DATECH    
           FROM   tra.BAL_GRT_Garant    
             JOIN tra.BAL_CMT_COMPTE     
             ON GRT_PGCPT1 = CMT_ID_COMPTE    
             JOIN par.BAL_PRC_ParamRubCompt3    
             ON  GRT_CODE_FICHIER = PRC_IDNT_FLUX    
           WHERE  GRT_CODE_FICHIER = @EPILATOR    
           AND    PRC_TYPE_ENGAGEMENT = 'DECOUVERT'    
           AND    GRT_DATECH BETWEEN @ldReportingDate and DATEADD(mm,-3,@ldReportingDate)    
           AND    GRT_DATECH IS NOT NULL)    
    
    OPEN CurrentCompte     
    FETCH CurrentCompte INTO @COMPTE, @DATECH    
    WHILE @@FETCH_STATUS = 0     
  BEGIN    
            -- We initialize the variables   
            SET @lCompte = null    
    
            -- We check that this overdraft has not had a positive balance since the end of his auto   
                
                SELECT @lCompte= ENG_COMPTE    
                FROM   tra.BAL_ENG_SLDENGC     
                WHERE  ENG_COMPTE = @COMPTE    
                AND    @DATECH < ENG_DATPOS    
                AND    ENG_POSDISP >= 0    
                GROUP BY ENG_COMPTE    
                IF @@ROWCOUNT =0    
                     -- We check if the last known position of the account is negative   
                     BEGIN    
                          SELECT @lCompte=SLD.ENG_COMPTE    
                          FROM   tra.BAL_ENG_SLDENGC SLD     
                               JOIN (SELECT ENG_COMPTE,MAX(ENG_DATPOS) AS DATPOS FROM tra.BAL_ENG_SLDENGC  GROUP BY ENG_COMPTE) SLDMAX    
                               ON SLD.ENG_COMPTE = SLDMAX.ENG_COMPTE    
         AND ENG_DATPOS = SLDMAX.DATPOS    
                          WHERE  SLD.ENG_COMPTE = @COMPTE    
                          AND    ENG_DATPOS <= @DATECH    
                          AND    ENG_POSDISP > 0    
                          GROUP  BY SLD.ENG_COMPTE    
                          IF @@ROWCOUNT =0    
                            SET @lCompte = null    
                      END    
               
    
            -- We check and update if necessary  
            IF   @lCompte is not null    
    BEGIN    
     UPDATE tra.BAL_CMT_COMPTE     
       SET CMT_BO_ISDECOVER = 'O'    
       WHERE CMT_ID_COMPTE = @COMPTE    
       -- We update the compteur    
       SET @lMaj = @lMaj + 1    
    END     
    
   FETCH CurrentCompte     
   INTO  @COMPTE, @DATECH    
  END    
    CLOSE CurrentCompte    
    DEALLOCATE CurrentCompte            
    
    
    
    -- 2* Filling the Prêt table for credits with several envelopes       
        
     -- Cursor initialization  
     SET @DOSS_CODE = NULL    
     SET @LIGN_DEVI = NULL    
     SET @nominalSociete = NULL    
  SET @mntBilan = NULL    
  SET @ICNE = NULL    
  SET @mntHBilan = NULL    
  SET @DATE_SIGNATURE = NULL    
  SET @CLIE_ID = NULL    
        
    DECLARE  CurrentDoublePret CURSOR FOR    
  (SELECT     
           CRI.CRD_LIGN_ID,    
           CRI.CRD_LIGN_DEVI,    
           sum(CRI.CRD_TIRA_NOMINAL_SOCIETE) as nominalSociete,    
           sum(abs((ISNULL(CRI.CRD_MntEncoursReel,0) + ISNULL(CRI.CRD_IMP_MntEch,0) + ISNULL(CRI.CRD_MntIntReportITC,0)) * dbo.getCours(CRI.CRD_LIGN_DEVI, @EPILATOR,'M'))) as mntBilan,    
           CRI.CRD_MntIntReportITC as ICNE,               
           sum(CRI.CRD_ENGAGEMENT * dbo.getCours(CRI.CRD_LIGN_DEVI, @EPILATOR,'M')) as mntHBilan,    
           CRI.CRD_DATE_SIGNATURE,    
           CRI.CRD_CLIE_ID    
        FROM   (SELECT * FROM tra.BAL_DVJ_DEVJ WHERE DVJ_CODE_FICHIER=@EPILATOR) DEVJ    
                           JOIN (SELECT * FROM tra.BAL_CRD_CRISTD WHERE CRD_CODE_FICHIER=@EPILATOR) CRI    
                           ON    DEVJ.DVJ_DEVISE = CRI.CRD_LIGN_DEVI    
    LEFT OUTER JOIN (SELECT * FROM tra.BAL_EXT_CREDOSEXT WHERE EXT_CODE_FICHIER=@EPILATOR) CREDOSEXT    
         ON    CRI.CRD_DOSS_CODE = CREDOSEXT.EXT_REFEXT    
                           JOIN (SELECT CRD_DOSS_CODE FROM tra.BAL_CRD_CRISTD WHERE CRD_CODE_FICHIER = @EPILATOR GROUP BY CRD_DOSS_CODE HAVING COUNT(*) > 1) COUNT_CRI    
                           ON    CRI.CRD_DOSS_CODE = COUNT_CRI.CRD_DOSS_CODE    
        AND NOT EXISTS (SELECT 1 FROM tra.BAL_PRE_PRET WHERE CREDOSEXT.EXT_NOOPER = PRE_ID_PRET)    
    
        group by  CRI.CRD_LIGN_ID, CRI.CRD_LIGN_DEVI,     
     CRI.CRD_MntIntReportITC, CRI.CRD_DATE_SIGNATURE, CRI.CRD_CLIE_ID)     
    OPEN CurrentDoublePret     
    FETCH CurrentDoublePret INTO @DOSS_CODE, @LIGN_DEVI, @nominalSociete,     
         @mntBilan, @ICNE, @mntHBilan, @DATE_SIGNATURE, @CLIE_ID    
    WHILE @@FETCH_STATUS = 0     
     BEGIN    
    
                          
    
             -- Case where the periodicity of interest on a loan is not the same on the two lines of credit 
             IF   @nbAmortInt = 1    
      SET @PRE_BO_ISINFINE=(SELECT DISTINCT CASE CRD_PCR_PeriodCalcInt    
                  WHEN 'IF' THEN 'O'    
                  ELSE null    
                   END     
               FROM   tra.BAL_CRD_CRISTD    
               WHERE  CRD_CODE_FICHIER=@EPILATOR    
               AND    CRD_LIGN_ID = @DOSS_CODE)    
             ELSE    
                 SET @PRE_BO_ISINFINE = null    
                 
    
             -- The duration is calculated according to the start and due dates retrieved    
             SET @PRE_VN_DUREEM = (SELECT  ROUND(DATEDIFF(MM,@PRE_DT_DEBLOCAGE,@PRE_DT_ECHEANCE),0))                 
    
             -- We determine the RC. We take that of the loan with the largest outstanding amount   
                 
             SET @PRE_VA_RUBCPT = (SELECT DISTINCT CRD_TYPR_ID    
          FROM   tra.BAL_CRD_CRISTD CRI    
            JOIN (SELECT CRD_LIGN_ID,MAX(CRD_MntEncoursReel) ENCOURS FROM tra.BAL_CRD_CRISTD WHERE CRD_CODE_FICHIER=@EPILATOR GROUP BY CRD_LIGN_ID) MAX_CRI    
            ON CRI.CRD_LIGN_ID = MAX_CRI.CRD_LIGN_ID AND CRI.CRD_MntEncoursReel = MAX_CRI.ENCOURS    
          WHERE  CRI.CRD_CODE_FICHIER=@EPILATOR    
          AND CRI.CRD_LIGN_ID = @DOSS_CODE )    
     
             IF @@ROWCOUNT <> 1    
                 SET @PRE_VA_RUBCPT = null    
    
             -- Insertion in the table prêt    
             INSERT into tra.BAL_PRE_PRET     
             (    
     PRE_ID_PRET    
    ,PRE_CE_DOSSIERALIS    
    ,PRE_VA_RUBCPT    
    ,PRE_VA_DEVISE    
    ,PRE_VA_NFE    
    ,PRE_VN_NOMINALPRET    
    ,PRE_VN_MNTBILAN    
    ,PRE_VN_ICNE    
    ,PRE_VN_MNTHORSBILAN    
    ,PRE_VN_MNTAUTOR    
    ,PRE_DT_DTSIGNATURE    
    ,PRE_CE_CLIENT    
    ,PRE_DT_DEBLOCAGE    
    ,PRE_DT_ECHEANCE    
    ,PRE_VA_TYPAMORT    
    ,PRE_BO_ISINFINE    
    ,PRE_VN_DUREEM    
             )     
             values     
             (    
      @DOSS_CODE    
     ,@DOSS_CODE    
     ,@PRE_VA_RUBCPT    
     ,@LIGN_DEVI    
     ,'ZN'    
     ,@nominalSociete    
     ,@mntBilan    
     ,@ICNE     
     ,@mntHBilan    
     ,0    
     ,@DATE_SIGNATURE    
     ,@CLIE_ID    
     ,@PRE_DT_DEBLOCAGE    
     ,@PRE_DT_ECHEANCE    
     ,@PRE_VA_TYPAMORT    
     ,@PRE_BO_ISINFINE    
     ,@PRE_VN_DUREEM    
             )    
             -- Update of the table compteur    
             SET @NbInsertionPRET = @NbInsertionPRET+1    
                 
             UPDATE tra.BAL_PRE_PRET    
             SET    
     PRE_LI_ObjFinance = CRD_ObjFinance    
    ,PRE_VA_NbEchImpaye = CRD_NbEchImpaye    
    ,PRE_LI_ImpOuDechu = CASE CRD_FlgActif    
           WHEN 1 THEN     
            CASE CRD_FlgDechu    
             WHEN 1 THEN 'Déchu'    
             ELSE 'Impayé'    

            END    
          END    
    ,PRE_IMP_MntEch = CRD_IMP_MntEch    
    ,PRE_TypTaux = CRD_TypTaux    
    ,PRE_MargeComm = CRD_MargeComm       
             FROM    
    tra.BAL_PRE_PRET    
    INNER JOIN tra.BAL_CRD_CRISTD    
    ON(PRE_ID_PRET = CRD_LIGN_ID AND CRD_CODE_FICHIER=@EPILATOR)    
   WHERE    
    PRE_ID_PRET = @DOSS_CODE    
    
   FETCH CurrentDoublePret     
   INTO @DOSS_CODE, @LIGN_DEVI, @nominalSociete,     
     @mntBilan, @ICNE, @mntHBilan, @DATE_SIGNATURE, @CLIE_ID    
  END    
  print 'Number of rows inserted for tra.BAL_PRE_PRET            : ' + convert(char(6),@NbInsertionPRET)    
      
    CLOSE CurrentDoublePret    
    DEALLOCATE CurrentDoublePret    
   
    
            /*********************************************************************/    
            /*              We loop on all the overdrafts            */    
            /*********************************************************************/    
            --DECLARATION     
            DECLARE    
            @DEC_ID_DECOUVERT  VARCHAR(11) = null    
            ,@DEC_VA_RUBCPT   VARCHAR(6) = null    
            ,@DEC_VA_NFE   VARCHAR(8) = null    
            ,@DEC_VN_MNTAUTOR  DECIMAL(19,6) = null    
            ,@DEC_VN_MNTBILAN  DECIMAL(19,6) = null    
            ,@DEC_VN_MNTHORSBILAN DECIMAL(19,6) = null    
            ,@DEC_VA_DEVISE   VARCHAR(3)   = null    
            ,@DEC_DT_ECHEANCE  DATE = null    
            ,@DEC_DT_DEBLOCAGE  DATE = null    
            ,@DEC_VA_CPTSUPP  VARCHAR(11) = null    
            ,@DEC_VN_DUREEM   DECIMAL(19,6) = null    
                          
            -- On récupère les découverts qui ne sont pas clôts    
            DECLARE CurrentDecouvert CURSOR FOR (SELECT DEC_ID_DECOUVERT,DEC_VA_RUBCPT,DEC_VA_NFE,DEC_VA_DEVISE,DEC_VN_MNTAUTOR,    
              DEC_VN_MNTBILAN,DEC_VN_MNTHORSBILAN,DEC_DT_ECHEANCE,DEC_DT_DEBLOCAGE,    
              DEC_VA_CPTSUPP,DEC_VN_DUREEM     
                                     FROM   tra.BAL_DEC_DECOUVERT    
                                     WHERE  DEC_CE_CLIENT = @CUS_ID_CLIENT    
                                     AND    (DEC_DT_ECHEANCE is null OR DEC_DT_ECHEANCE >= @ldReportingDate))    
            OPEN CurrentDecouvert     
   FETCH CurrentDecouvert INTO @DEC_ID_DECOUVERT,@DEC_VA_RUBCPT,@DEC_VA_NFE,@DEC_VA_DEVISE,@DEC_VN_MNTAUTOR,    
          @DEC_VN_MNTBILAN,@DEC_VN_MNTHORSBILAN,@DEC_DT_ECHEANCE,@DEC_DT_DEBLOCAGE,    
          @DEC_VA_CPTSUPP,@DEC_VN_DUREEM    
   WHILE @@FETCH_STATUS = 0    
                -- On récupère les libellés de l'engagement et de la note de l'engagement    
                BEGIN    
                    SELECT @DET_VA_TYPENGA= PRC_TYPE_ENGAGEMENT2,    
                            @DET_VA_LIBENGA= PRC_LIB_ENGAGEMENT,    
                            @DET_VA_LIBNFE = NOT_LI_NOTE    
                    FROM   par.BAL_PRC_ParamRubCompt3,    
                           par.BAL_NOT_NOTE NOTE    
                    WHERE  @DEC_VA_RUBCPT = PRC_RUB_COMPT    
                     AND    PRC_IDNT_FLUX = @EPILATOR    
                     AND    ISNULL(@DEC_VA_NFE,'ZN') = NOT_ID_NOTE    
                       
    
                    -- Valorisation of the variables    
                    SET @DET_TYPE_ENGA      = 'DECOUVERT'    
                    SET @DET_ID_ENGA        = @DEC_ID_DECOUVERT    
                    SET @DET_VA_NFE         = @DEC_VA_NFE    
                    SET @DET_VA_DEVISEENGA  = @DEC_VA_DEVISE    
                    SET @DET_VN_AUTOR       = @DEC_VN_MNTAUTOR    
                    SET @DET_VN_BILAN       = abs(@DEC_VN_MNTBILAN)    
                    SET @DET_VN_HORSBILAN   = @DEC_VN_MNTHORSBILAN    
                    SET @DET_DT_ECHEANCE    = @DEC_DT_ECHEANCE    
                    SET @DET_DT_DEBLOCAGE   = @DEC_DT_DEBLOCAGE    
                    SET @DET_VN_DUREEM      = @DEC_VN_DUREEM    
                    SET @DET_VA_CPTSUPPDEC  = @DEC_VA_CPTSUPP    
                    SET @DET_VA_COMPTE      = @DEC_VA_CPTSUPP    
                    SET @DET_VA_RUBCPT      = @DEC_VA_RUBCPT    
                    SET @DET_VN_SUMBILAN    = abs(@DEC_VN_MNTBILAN) + @DEC_VN_MNTHORSBILAN    
                    SET @DET_VA_DELEGATION  = dbo.NivDelegataire(@CUS_ID_CLIENT)    
                        
                    -- It is determined with which entity the commitment was contracted(BPSD / CIE1818, BP1818)    
                    --IF    @DEC_DT_DEBLOCAGE < '01/07/2009'    
                    IF(DATEDIFF(DAY,@DEC_DT_DEBLOCAGE,convert(datetime,'20090701',102))>0)    
                      SET @Det_VA_CONTRACTANT = 'BPSD'    
                    ELSE      
       SET @Det_VA_CONTRACTANT = 'BP1818'    
                        
                    -----------------------------------------------------------------------------    
                    
                        
     FETCH CurrentDecouvert INTO @DEC_ID_DECOUVERT,@DEC_VA_RUBCPT,@DEC_VA_NFE,@DEC_VA_DEVISE,@DEC_VN_MNTAUTOR,    
          @DEC_VN_MNTBILAN,@DEC_VN_MNTHORSBILAN,@DEC_DT_ECHEANCE,@DEC_DT_DEBLOCAGE,    
          @DEC_VA_CPTSUPP,@DEC_VN_DUREEM    
                -- End of the loop on the découverts    
                END    
            CLOSE CurrentDecouvert    
            DEALLOCATE CurrentDecouvert    
    
          
                    -----------------------------------------------------------------------------    
                    -- Insertion in the table tra.BAL_DET_DETAIL    
                    INSERT INTO tra.BAL_DET_DETAIL    
        (DET_CE_DOSSIERALIS        
        ,DET_TYPE_ENGA             
        ,DET_ID_ENGA               
        ,DET_VA_NFE                
        ,DET_VA_DEVISEENGA         
        ,DET_VN_AUTOR              
        ,DET_VN_NOMPRET            
        ,DET_VN_BILAN              
        ,DET_VN_ICNE               
        ,DET_VN_HORSBILAN          
        ,DET_DT_ECHEANCE           
        ,DET_VN_DUREEM             
        ,DET_DT_DEBLOCAGE          
        ,DET_VA_TYPAMORT           
        ,DET_VA_COMPTE             
        ,DET_VA_RUBCPT             
        ,DET_VN_SUMBILAN           
        ,DET_BO_ISINFINE           
        ,DET_VA_DELEGATION     
        ,DET_VA_REVISION    
        ,DET_ID_CLIENT    
        ,DET_VA_NOMCLIENT    
        ,DET_VA_SYSSOURCE    
        ,DET_VA_IDNEWSRTDG    
        ,DET_VA_NEWSRTDG    
        ,DET_LI_NETTG    
        ,DET_VA_NETTG    
        ,DET_VA_WLTDG    
        ,DET_VA_NIVWLTDG    
        ,DET_DT_NOTEGROUPE    
        ,DET_ID_TG    
        ,DET_VA_NOMTG    
        ,DET_VA_LIBELLENET    
        ,DET_VA_NETCLIENT    
        ,DET_VA_WLCLI    
        ,DET_VA_NIVWLCLI    
        ,DET_DT_NOTECLIENT    
        ,DET_VA_DIRECTION    
        ,DET_VA_NOMEXPL    
        ,DET_VN_MNTENGACLIENT    
        ,DET_VN_NBENGACLIENT    
        ,DET_VA_IDNEWSRCLIENT    
        ,DET_VA_NEWSRCLIENT    
        ,Det_VA_CONTRACTANT    
        ,DET_VA_LIBENGA     
        ,DET_VA_LIBNFE     
        ,DET_VA_TYPENGA     
        ,DET_LI_ImpOuDechu    
    ,DET_LI_ObjFinance    
        ,DET_VA_NbEchImpaye     
        ,DET_IMP_MntEch      
        ,DET_TypTaux       
        ,DET_MargeComm)      
        VALUES    
        (@DET_CE_DOSSIERALIS        
        ,@DET_TYPE_ENGA             
        ,@DET_ID_ENGA               
        ,@DET_VA_NFE                
        ,@DET_VA_DEVISEENGA         
        ,@DET_VN_AUTOR              
        ,@DET_VN_NOMPRET            
        ,@DET_VN_BILAN              
        ,@DET_VN_ICNE               
        ,@DET_VN_HORSBILAN          
        ,@DET_DT_ECHEANCE           
        ,@DET_VN_DUREEM             
        ,@DET_DT_DEBLOCAGE          
        ,@DET_VA_TYPAMORT           
        ,@DET_VA_COMPTE             
        ,@DET_VA_RUBCPT             
        ,@DET_VN_SUMBILAN           
        ,@DET_BO_ISINFINE           
        ,@DET_VA_DELEGATION       
        ,@DET_VA_REVISION    
        ,@DET_ID_CLIENT    
        ,@DET_VA_NOMCLIENT    
        ,@DET_VA_SYSSOURCE    
        ,@DET_VA_IDNEWSRTDG    
        ,@DET_VA_NEWSRTDG    
        ,@DET_LI_NETTG    
        ,@DET_VA_NETTG    
        ,@DET_VA_WLTDG    
        ,@DET_VA_NIVWLTDG    
        ,@DET_DT_NOTEGROUPE    
        ,@DET_ID_TG    
        ,@DET_VA_NOMTG    
        ,@DET_VA_LIBELLENET    
        ,@DET_VA_NETCLIENT    
        ,@DET_VA_WLCLI    
        ,@DET_VA_NIVWLCLI    
        ,@DET_DT_NOTECLIENT    
        ,@DET_VA_DIRECTION    
        ,@DET_VA_NOMEXPL    
        ,@DET_VN_MNTENGACLIENT    
        ,@DET_VN_NBENGACLIENT    
        ,@DET_VA_IDNEWSRCLIENT    
        ,@DET_VA_NEWSRCLIENT    
        ,@Det_VA_CONTRACTANT    
        ,@DET_VA_LIBENGA     
        ,@DET_VA_LIBNFE     
        ,@DET_VA_TYPENGA     
        ,@DET_LI_ImpOuDechu    
        ,@DET_LI_ObjFinance      
        ,@DET_VA_NbEchImpaye     
        ,@DET_IMP_MntEch      
        ,@DET_TypTaux       
        ,@DET_MargeComm )      
                       
                    -- Update of the compteur    
                    SET @compteur = @compteur + 1    
                        
                         -- Reset fields prêt    
                    SET @DET_CE_DOSSIERALIS    = null    
                    SET @DET_TYPE_ENGA         = null    
                    SET @DET_ID_ENGA           = null    
                    SET @DET_VA_NFE            = null    
                    SET @DET_VA_DEVISEENGA     = null    
                    SET @DET_VN_AUTOR          = null    
                    SET @DET_VN_NOMPRET        = null    
                    SET @DET_VN_BILAN          = null    
                    SET @DET_VN_ICNE           = null    
                    SET @DET_VN_HORSBILAN      = null    
                    SET @DET_VN_SUMBILAN       = null    
                    SET @DET_DT_ECHEANCE       = null    
                    SET @DET_VN_DUREEM         = null    
                    SET @DET_DT_DEBLOCAGE      = null    
                    SET @DET_VA_TYPAMORT       = null    
                    SET @DET_VA_COMPTE         = null    
                    SET @DET_VA_RUBCPT         = null    
                    SET @Det_VA_CONTRACTANT    = null    
                    SET @DET_VA_LIBNFE         = null    
                    SET @DET_VN_SUMBILAN       = null    
                    SET @DET_VA_DELEGATION     = null    
                    SET @DET_VA_TYPENGA        = null    
                    SET @DET_BO_ISINFINE       = null    
                    SET @DET_LI_ImpOuDechu = null    
                    SET @DET_LI_ObjFinance = null     
     SET @DET_VA_NbEchImpaye = null    
     SET @DET_IMP_MntEch  = null    
     SET @DET_TypTaux  = null     
     SET @DET_MargeComm  = null     
    
                    FETCH CurrentPret INTO @PRE_ID_PRET,@PRE_VA_NFE,@PRE_VA_DEVISE,@PRE_VN_MNTAUTOR    
     ,@PRE_VN_NOMINALPRET,@PRE_VN_MNTBILAN,@PRE_VN_ICNE,@PRE_VN_MNTHORSBILAN    
     ,@PRE_DT_ECHEANCE,@PRE_VN_DUREEM,@PRE_DT_DEBLOCAGE,@PRE_VA_TYPAMORT    
     ,@PRE_VA_RUBCPT,@PRE_VN_ICNE,@PRE_BO_ISINFINE,@PRE_CE_DOSSIERALIS     
     ,@PRE_LI_ImpOuDechu,@PRE_LI_ObjFinance,@PRE_VA_NbEchImpaye    
     ,@PRE_IMP_MntEch,@PRE_TypTaux,@PRE_MargeComm     
         
                -- End of the loop on the prêts    
                END    
     CLOSE CurrentPret    
           DEALLOCATE CurrentPret    
   /***************************************************************************************/    
    
    -----------------------------------------------------------------------------    
    -- Insertion in the table tra.BAL_DET_DETAIL    
    INSERT INTO tra.BAL_DET_DETAIL    
    (    
      DET_TYPE_ENGA    
     ,DET_ID_ENGA    
     ,DET_VA_DEVISEENGA    
     ,DET_VN_AUTOR    
     ,DET_VN_BILAN    
     ,DET_VN_HORSBILAN    
     ,DET_DT_ECHEANCE    
     ,DET_VA_COMPTE    
     ,DET_VA_RUBCPT    
     ,DET_VA_CONTRACTANT    
     ,DET_BO_ISDECOVER    
     ,DET_VA_TYPENGA    
     ,DET_VA_LIBENGA    
     ,DET_VA_DELEGATION    
     ,DET_VA_REVISION    
     ,DET_ID_CLIENT    
     ,DET_VA_NOMCLIENT    
     ,DET_VA_SYSSOURCE    
     ,DET_VA_IDNEWSRTDG    
     ,DET_VA_NEWSRTDG    
     ,DET_LI_NETTG    
     ,DET_VA_NETTG    
     ,DET_VA_WLTDG    
     ,DET_VA_NIVWLTDG    
     ,DET_DT_NOTEGROUPE    
     ,DET_ID_TG    
     ,DET_VA_NOMTG    
     ,DET_VA_LIBELLENET    
     ,DET_VA_NETCLIENT    
     ,DET_VA_WLCLI    
     ,DET_VA_NIVWLCLI    
     ,DET_DT_NOTECLIENT    
     ,DET_VA_DIRECTION    
     ,DET_VA_NOMEXPL    
     ,DET_VN_MNTENGACLIENT    
     ,DET_VN_NBENGACLIENT    
     ,DET_VA_IDNEWSRCLIENT    
     ,DET_VA_NEWSRCLIENT    
     ,DET_VN_SUMBILAN    
     ,DET_CodComExtCpt    
     ,DET_LibComCpt    
    )     
    VALUES    
    (    
      @DET_TYPE_ENGA    
  ,@DET_ID_ENGA    
     ,@DET_VA_DEVISEENGA    
     ,@DET_VN_AUTOR    
     ,@DET_VN_BILAN    
     ,@DET_VN_HORSBILAN    
     ,@DET_DT_ECHEANCE    
     ,@DET_VA_COMPTE    
     ,@DET_VA_RUBCPT    
     ,@DET_VA_CONTRACTANT    
     ,@DET_BO_ISDECOVER    
     ,@DET_VA_TYPENGA    
     ,@DET_VA_LIBENGA    
     ,@DET_VA_DELEGATION    
     ,@DET_VA_REVISION    
     ,@DET_ID_CLIENT    
     ,@DET_VA_NOMCLIENT    
     ,@DET_VA_SYSSOURCE    
     ,@DET_VA_IDNEWSRTDG    
     ,@DET_VA_NEWSRTDG    
     ,@DET_LI_NETTG    
     ,@DET_VA_NETTG    
     ,@DET_VA_WLTDG    
     ,@DET_VA_NIVWLTDG    
     ,@DET_DT_NOTEGROUPE    
     ,@DET_ID_TG    
     ,@DET_VA_NOMTG    
     ,@DET_VA_LIBELLENET    
     ,@DET_VA_NETCLIENT    
     ,@DET_VA_WLCLI    
     ,@DET_VA_NIVWLCLI    
     ,@DET_DT_NOTECLIENT    
     ,@DET_VA_DIRECTION    
     ,@DET_VA_NOMEXPL    
     ,@DET_VN_MNTENGACLIENT    
     ,@DET_VN_NBENGACLIENT    
     ,@DET_VA_IDNEWSRCLIENT    
     ,@DET_VA_NEWSRCLIENT    
     ,@DET_VN_SUMBILAN    
     ,@CMT_CodComExt    
     ,@CMT_LibCom    
    )    
    
    -- Update the compteur    
    SET @compteur = @compteur + 1    
    
    -- Resetting Compte Fields     
    SET @DET_TYPE_ENGA      = null    
    SET @DET_ID_ENGA        = null    
    SET @DET_VA_DEVISEENGA  = null    
    SET @DET_VN_AUTOR       = null    
    SET @DET_VN_BILAN       = null    
    SET @DET_VN_HORSBILAN   = null    
    SET @DET_DT_ECHEANCE    = null    
    SET @DET_VA_COMPTE      = null    
    SET @DET_VA_RUBCPT      = null    
    SET @DET_VA_CONTRACTANT = null    
    SET @DET_BO_ISDECOVER   = null    
    SET @DET_VA_TYPENGA     = null    
    SET @DET_VA_LIBENGA     = null    
    SET @DET_VA_DELEGATION  = null             
    
    FETCH CurrentCompte INTO @CMT_VA_RUBCPT,@CMT_ID_COMPTE,@CMT_VA_DEVISE,@CMT_VN_MNTAUTOR    
     ,@CMT_VN_SOLDE,@CMT_VA_RUBCPT,@CMT_BO_ISDECOVER,@CMT_CodComExt,@CMT_LibCom    
    
    -- End of the loop comptes       
   END    
   CLOSE CurrentCompte    
   DEALLOCATE CurrentCompte    
    
               
   FETCH CurrentClient INTO @CUS_ID_CLIENT,@CUS_CE_EXPL,@CUS_VA_TG,@CUS_VA_NOM,@CUS_DT_NOTE    
        -- End of the loop découverts    
        END    
    CLOSE CurrentClient    
    DEALLOCATE CurrentClient    
    
    
    

Your help will be really appricated.

Thanks in advance

Leave a Comment