call spQuery_Lista_Sub_Area(3, NULL,NULL);

DROP PROCEDURE IF EXISTS spQuery_Lista_Sub_Area;
CREATE PROCEDURE spQuery_Lista_Sub_Area(
  varArea     int,
  varCodigo   int,
  varNombre   varchar(80)
  )
BEGIN 
  DECLARE varCadCod, varCadNom text default '';
  if ifnull(varCodigo,'')<>'' then
    set varCadCod = CONCAT(" and C.id_sub_area = ", varCodigo, " ");
  end if;
  
  if ifnull(varNombre,'')<>'' then
    set varCadNom = CONCAT(" and C.nombre LIKE '%", varNombre, "%' ");
  end if;
  
  SET @sql = CONCAT("select C.id_sub_area, C.nombre, C.abrev ",
                    "from _srh_sub_area C ",
                    "where C._estado = 'A' ",
                    "and id_area = ", varArea, " ",
                    varCadCod, varCadNom,
                    "order by C.id_sub_area; ");
  PREPARE s1 FROM @sql;
  EXECUTE s1;
  DEALLOCATE PREPARE s1;
END;