call spQuery_Lista_Ubigeo_Dis('01','1401');

DROP PROCEDURE IF EXISTS spQuery_Lista_Ubigeo_Dis;
CREATE PROCEDURE spQuery_Lista_Ubigeo_Dis(
  varPai  varchar(3),
  varPro  varchar(4)
  )
BEGIN
  DECLARE varLongDep, varLongPro  int;
  select dig_dep, dig_pro  
  into varLongDep, varLongPro 
  from _sv_pais where id_pais = varPai;
  
  select id_ubigeo as CDIS, DISTRITO
  from _sv_ubigeo 
  WHERE id_pais = varPai
  and substring(id_ubigeo,1,varLongDep+varLongPro) = varPro
  GROUP BY id_ubigeo, DISTRITO
  ORDER BY DISTRITO;
END;
