CREATE PROCEDURE `spDeleteUser` ( IN `P_UserId` INTEGER ) BEGIN DECLARE `roleid` INTEGER ; SET `roleid` = ( SELECT `roletypeid` FROM `tuser` WHERE `id` = `P_UserId` ) ; START TRANSACTION ; IF `roleid` = ? THEN DELETE `tuser` , `tstudent` , `tassignbatch` FROM `tuser` LEFT JOIN `tstudent` ON `tuser` . `id` = `tstudent` . `Userid` LEFT JOIN `tassignbatch` ON `tstudent` . `id` = `tassignbatch` . `studentid` WHERE `tuser` . `id` = `P_UserId` ; ELSEIF `roleid` = ? THEN DELETE `tuser` , `tinstitute` , `tteacher` , `tbatch` FROM `tuser` LEFT JOIN `tteacher` ON `tuser` . `id` = `tteacher` . `Userid` LEFT JOIN `tinstitute` ON `tinstitute` . `id` = `tteacher` . `InstituteId` LEFT JOIN `tbatch` ON `tteacher` . `id` = `tbatch` . `TeacherId` WHERE `tuser` . `id` = `P_UserId` ; ELSE SELECT ? ; END IF ; COMMIT ; SELECT ? ; END 