MySQL
-- Procedure "IsActionAllowed" DDL
CREATE DEFINER=`root`@`%` PROCEDURE `IsActionAllowed`(IN roleid CHAR(36), IN functionid CHAR(36))
BEGIN
DECLARE rid CHAR(36);
DECLARE base_role CHAR(36);
DECLARE exist INT;
CREATE TEMPORARY TABLE TempTable (RID CHAR(36)) TYPE=INNODB;
INSERT INTO TempTable VALUES(roleid);
SET base_role = '';
SET rid = roleid;
WHILE base_role IS NOT NULL DO
IF base_role <> '' THEN
INSERT INTO TempTable VALUES(base_role);
END IF;
SELECT
tbl_roles.BASE_ON_ROLE INTO base_role
FROM
tbl_roles
WHERE
tbl_roles.ROLE_ID = rid;
SET rid = base_role;
END WHILE;
SELECT
COUNT(tbl_role_permission.ROLE_ID) INTO exist
FROM
tbl_role_permission
WHERE
tbl_role_permission.ROLE_ID IN (SELECT * FROM TempTable)
AND tbl_role_permission.FUNCTION_ID = functionid;
DROP TEMPORARY TABLE IF EXISTS TempTable;
IF exist > 0 THEN
SELECT true AS Allowed;
ELSE
SELECT false AS Allowed;
END IF;
END;
Procedure tutorial :
Stored Procedure Parameters