CREATE PROCEDURE `spAddParaQuestion` ( IN `P_TestId` INTEGER , IN `P_ParaId` INTEGER , IN `P_Title` LONGTEXT , IN `P_Code` VARCHARACTER (?) ) BEGIN DECLARE `V_OrderIndex` INTEGER DEFAULT ? ; IF `P_ParaId` = ? THEN SET `V_OrderIndex` = ( SELECT `IFNULL` ( COUNT ( * ) + ?, ... ) FROM `tparaquestions` WHERE `TestId` = `P_TestId` ) ; INSERT INTO `vida_lms` . `tparaquestions` ( `TestId` , `Title` , `Code` , `OrderIndex` ) VALUES ( `P_TestId` , `P_Title` , `P_Code` , `V_OrderIndex` ) ; SELECT `LAST_INSERT_ID` ( ) ; ELSEIF `P_ParaId` > ? THEN UPDATE `vida_lms` . `tparaquestions` SET `Title` = `P_Title` , `Code` = `P_Code` WHERE `Id` = `P_ParaId` AND `TestId` = `P_TestId` ; END IF ; END 