call spQuery_Lista_Rol(3,NULL, NULL, 1);

DROP PROCEDURE IF EXISTS spQuery_Lista_Rol;
CREATE PROCEDURE spQuery_Lista_Rol(
  varSistema  int,
  varCodigo   int,
  varNombre   varchar(80),
  varUsuario  int
)
BEGIN
  DECLARE varCadCod, varCadNom, varCadUsr text default '';
  if ifnull(varCodigo,'')<>'' then
    set varCadCod = CONCAT(" and id_rol = ", varCodigo, " ");
  end if;
  
  if ifnull(varNombre,'')<>'' then
    set varCadNom = CONCAT(" and nombre LIKE '%", varNombre, "%' ");
  end if;
  
  if varUsuario <> 1 then
    set varCadUsr = CONCAT(" and id_rol <> 1 ");
  end if;
  SET @sql = CONCAT("select id_rol, nombre ",
                    "from _sys_rol ",
                    "where _estado = 'A'",
                    "and id_sistema = ", varSistema, " ",
                    varCadCod, varCadNom, varCadUsr,
                    "order by id_rol; ");
  PREPARE s1 FROM @sql;
  EXECUTE s1;
  DEALLOCATE PREPARE s1;
END;