CREATE PROCEDURE `spAddStudent` ( IN `P_InstituteId` INTEGER , IN `P_TeacherId` INTEGER , IN `P_MobileNo` VARCHARACTER (?) , IN `P_StudentName` VARCHARACTER (?) ) BEGIN DECLARE `V_UserId` INTEGER DEFAULT ? ; IF EXISTS ( SELECT ? FROM `tstudent` INNER JOIN `tuser` ON `tuser` . `Id` = `tstudent` . `UserId` WHERE `tstudent` . `InstituteId` = `P_InstituteId` AND `tuser` . `MobileNo` = `P_MobileNo` ) THEN SELECT `tstudent` . `id` FROM `tstudent` INNER JOIN `tuser` ON `tuser` . `Id` = `tstudent` . `UserId` WHERE `tstudent` . `InstituteId` = `P_InstituteId` AND `tuser` . `MobileNo` = `P_MobileNo` ; ELSEIF EXISTS ( SELECT ? FROM `tuser` WHERE `MobileNo` = `P_MobileNo` AND `RoleTypeId` = ? ) THEN SET `V_UserId` = ( SELECT `Id` FROM `tuser` WHERE `MobileNo` = `P_MobileNo` AND `RoleTypeId` = ? ) ; UPDATE `tuser` SET NAME = `P_StudentName` , `UpdatedDateTime` = NOW ( ) WHERE `id` = `V_UserId` ; IF EXISTS ( SELECT ? FROM `tstudent` INNER JOIN `tuser` ON `tuser` . `Id` = `tstudent` . `UserId` WHERE `tstudent` . `InstituteId` = ? AND `tuser` . `MobileNo` = `P_MobileNo` ) THEN UPDATE `tstudent` SET `InstituteId` = `P_InstituteId` WHERE `UserId` = `V_UserId` AND `InstituteId` = ? ; SELECT `Id` 