CREATE PROCEDURE `spCreateSubmitHomeWork` ( IN `P_HomeWorkId` INTEGER , IN `P_StudentId` INTEGER ) BEGIN IF EXISTS ( SELECT ? FROM `thomework` WHERE `Id` = `P_HomeWorkId` AND `FIND_IN_SET` ( `P_StudentId` , `StudentId` ) > ? ) THEN IF NOT EXISTS ( SELECT ? FROM `thomeworksubmittedanswer` WHERE `HomeworkId` = `P_HomeWorkId` AND `StudentId` = `P_StudentId` ) THEN START TRANSACTION ; INSERT INTO `thomeworksubmittedanswer` ( `HomeWorkId` , `StudentId` ) VALUES ( `P_HomeWorkId` , `P_StudentId` ) ; SELECT ? ; COMMIT ; ELSE SELECT ? ; END IF ; ELSE IF NOT EXISTS ( SELECT ? FROM `thomework` WHERE `Id` = `P_HomeWorkId` AND `FIND_IN_SET` ( `P_StudentId` , `StudentId` ) > ? ) THEN SELECT ? ; ELSE SELECT ? ; END IF ; END IF ; END 