-- PASO 8: Generamos el universo de los clientes y sus representantes Legales
--DROP TABLE ......................LD_VECTOR_VPC_PERSONA;
CREATE MULTISET TABLE  ......................LD_VECTOR_VPC_PERSONA
   (DOCUMENTO_FINAL  varchar(25),
   RUC  varchar(25),
   TIPID_BDRELE  varchar(25),
   NUMID_BDRELE  varchar(25)
   ) PRIMARY INDEX (DOCUMENTO_FINAL);
    
  INSERT INTO ......................LD_VECTOR_VPC_PERSONA ( 
  DOCUMENTO_FINAL,
  RUC,
  TIPID_BDRELE,
  NUMID_BDRELE) 
  SELECT DISTINCT
    X.DOCUMENTO_FINAL,
    X.RUC,
    D.TIPDOC TIPID_BDRELE,
    D.CODDOC NUMID_BDRELE
  FROM ......................LVL_VECTOR_VPC_4 X 
  LEFT JOIN ..........V_REP_LEGALES_SUNAT  D  ON D.NUMRUC = X.RUC
  WHERE  SUBSTR(TRIM(X.RUC),1,2) <> &#039;10&#039; AND D.CODDOC IS NOT NULL AND D.ESTADO = &#039;0&#039;  

  UNION
  SELECT DISTINCT
    X.DOCUMENTO_FINAL,
    X.RUC,
    D.TIPDOC TIPID_BDRELE,
    D.CODDOC NUMID_BDRELE
  FROM ......................LVL_VECTOR_VPC_4 X 
  LEFT JOIN ..........V_REP_LEGALES_SUNAT  D  ON D.NUMRUC = X.RUC
  WHERE  SUBSTR(TRIM(X.RUC),1,2) = &#039;10&#039;  AND LENGTH(TRIM(X.RUC)) = 11;

Add a code snippet to your website: www.paste.org