call spQuery_Sistema_Menu_Acceso(2, 1, '010301');

DROP PROCEDURE IF EXISTS spQuery_Sistema_Menu_Acceso;
CREATE PROCEDURE spQuery_Sistema_Menu_Acceso(
	varSistema  int,
  varUsuario  int,  
  varMenu     varchar(10)
)
BEGIN
  DECLARE varRol int;   
  select id_rol into varRol from _sys_usuario_sistema_rol where id_usuario=varUsuario and id_sistema=varSistema;  
  
  select A.CMEN, A.MENU, B.PADRE
  from
  (
  select RM.id_menu as CMEN, UPPER(M.nombre) as MENU, 
  substring(RM.id_menu,1,LENGTH(varMenu)-2) as CPAD
  from _sys_rol_menu RM
  inner join _sys_menu M ON RM.id_sistema = M.id_sistema AND RM.id_menu = M.id_menu
  where RM.id_rol   = varRol
  and RM.id_menu    = varMenu
  and RM.id_sistema = varSistema
  ) A
  left join
  (
  select id_menu as CPAD, upper(nombre) as PADRE
  from _sys_menu M
  where id_menu = substring(varMenu,1,LENGTH(varMenu)-2)
  and M.id_sistema = varSistema
  ) B
  ON A.CPAD = B.CPAD;
END;