CREATE DEFINER = `testdb` @`%` PROCEDURE `spAcceptDecline` ( IN `P_BatchId` INTEGER , IN `P_StudentId` INTEGER , IN `P_IsActive` BIT , IN `P_InstituteId` INTEGER ) BEGIN DECLARE `new_student_id` INTEGER DEFAULT ? ; DECLARE `l_UserId` INTEGER DEFAULT ? ; DECLARE `l_TeacherId` INTEGER DEFAULT ? ; DECLARE `l_new_student_Id` INTEGER DEFAULT ? ; IF ( `P_IsActive` = ? ) THEN SET `l_UserId` = ( SELECT `UserId` FROM `tstudent` WHERE `id` = `P_StudentId` ) ; SET `l_TeacherId` = ( SELECT `t` . `UserId` FROM `tbatch` `B` INNER JOIN `tteacher` `t` ON `B` . `TeacherId` = `t` . `id` WHERE `B` . `Id` = `P_BatchId` ) ; IF NOT EXISTS ( SELECT ? FROM `tbatch` `B` INNER JOIN `tteacher` `t` ON `B` . `TeacherId` = `t` . `id` INNER JOIN `tstudent` `s` ON `S` . `InstituteId` = `t` . `InstituteId` WHERE `B` . `Id` = `P_BatchId` AND `s` . `Id` = `P_StudentId` ) THEN IF NOT EXISTS ( SELECT ? FROM `tstudent` WHERE `UserId` = `l_UserId` AND `InstituteId` = `P_InstituteId` ) THEN INSERT INTO `tstudent` ( `UserId` , `StatusId` , `InstituteId` , `CreatedUserId` , `CreatedDateTime` ) VALUES ( `l_UserId` , ? , `P_InstituteId` , `l_TeacherId` , NOW ( ) ) ; SET `new_student_id` = `Last_Insert_Id` ( ) ; UPDATE 