call spQuery_Lista_Ubigeo_Dep('02');

DROP PROCEDURE IF EXISTS spQuery_Lista_Ubigeo_Dep;
CREATE PROCEDURE spQuery_Lista_Ubigeo_Dep(
  varPais varchar(3)
  )
BEGIN
  DECLARE varLongDep  int;
  select dig_dep  into varLongDep from _sv_pais where id_pais = varPais;
  
  select substring(id_ubigeo,1,varLongDep) as CDEP, DEPARTAMENTO 
  from _sv_ubigeo 
  where id_pais = varPais
  GROUP BY substring(id_ubigeo,1,varLongDep), DEPARTAMENTO
  ORDER BY DEPARTAMENTO;
END;