CREATE PROCEDURE `spAssignBatch` ( IN `P_BatchId` INTEGER , IN `P_StudentId` VARCHARACTER (?) , IN `P_StatusId` INTEGER ) BEGIN DECLARE `P_Values` VARCHARACTER (?) DEFAULT ? ; DECLARE `P_Insert` VARCHARACTER (?) DEFAULT ? ; DECLARE `V_Count` INTEGER DEFAULT ? ; DECLARE `S_Count` INTEGER DEFAULT ? ; DECLARE `V_i` INTEGER DEFAULT ? ; DECLARE `S_i` INTEGER DEFAULT ? ; DECLARE `V_StudentId` INTEGER DEFAULT ? ; DECLARE `StudentIdConcate` LONGTEXT DEFAULT ? ; IF NOT EXISTS ( SELECT ? FROM `tbatch` , `tstudent` WHERE `tbatch` . `Id` = `P_BatchId` ) THEN SELECT ? ; ELSE BEGIN DROP TABLE IF EXISTS `t` ; CREATE TEMPORARY TABLE `t` ( `txt` TEXT ) ; INSERT INTO `t` VALUES ( `P_StudentId` ) ; DROP TEMPORARY TABLE IF EXISTS `temp` ; CREATE TEMPORARY TABLE `temp` ( `Id` INTEGER NOT NULL AUTO_INCREMENT , PRIMARY KEY ( `Id` ) , INDEX ( `Id` ) , `val` CHARACTER (?) ) ; SET @? = `CONCAT` ( ? , REPLACE ( ( SELECT GROUP_CONCAT ( DISTINCTROW `txt` ) AS DATA FROM `t` ) , ?, ... ) , ? ) ; PREPARE `stmt1` FROM @? ; EXECUTE `stmt1` ; SET `S_Count` = ( SELECT COUNT ( `IFNULL` ( `val` , ? ) ) FROM `temp` WHERE `IFNULL` ( `val` , ? ) != ? ) ; DROP TABLE IF EXISTS `tempdata` ; CREATE TEMPORARY TABLE `tempdata` ( `TeacherId` INTEGER , `BatchId` INTEGER , `StudentId` INTEGER ) ; WHILE `S_i` <= `S_Count` DO SET `V_i` = ? ; SET `V_StudentId` = ( SELECT `IFNULL` ( `t` . `val` , ? ) FROM `temp` `t` WHERE `t` . `Id` = `S_i` ) ; IF NOT EXISTS ( SELECT ? FROM `tAssignBatch` WHERE `BatchId` = `P_BatchId` AND 