CREATE PROCEDURE `spCreateInstitute` ( IN `P_UserId` INTEGER , IN `P_UserName` VARCHARACTER (?) , IN `P_Code` VARCHARACTER (?) , IN `P_Title` VARCHARACTER (?) , IN `P_RoleTypeId` INTEGER ) BEGIN DECLARE `l_InstituteId` INTEGER DEFAULT ? ; IF EXISTS ( SELECT ? FROM `tuser` WHERE `id` = `P_UserId` AND `RoleTypeId` = `P_RoleTypeId` ) THEN IF `P_UserName` IS NOT NULL THEN UPDATE `tuser` SET NAME = `P_UserName` , `UpdatedDateTime` = NOW ( ) WHERE `id` = `P_UserId` ; END IF ; IF EXISTS ( SELECT ? FROM `tinstitute` WHERE `LOWER` ( `Title` ) = `LOWER` ( TRIM ( `P_Title` ) ) AND `P_UserId` = `CreatedUserId` ) THEN SELECT ? AS `StatusCode` ; ELSEIF ( `P_RoleTypeId` = ? OR `P_RoleTypeId` = ? ) AND EXISTS ( SELECT ? FROM `tteacher` WHERE `UserId` = `P_UserId` ) THEN INSERT INTO `tinstitute` ( `CreatedUserId` , CODE , `Title` , `CreatedDateTime` ) VALUES ( `P_UserId` , `P_Code` , `P_Title` , NOW ( ) ) ; SET `l_InstituteId` = `LAST_INSERT_ID` ( ) ; IF EXISTS ( SELECT ? FROM `tteacher` WHERE `P_UserId` = `UserId` AND `InstituteId` = ? ) THEN UPDATE `tteacher` SET `InstituteId` = `LAST_INSERT_ID` ( ) , `StatusId` = ? , `DateOfJoining` = NOW ( ) WHERE `UserId` = `P_UserId` AND `InstituteId` = ? ; ELSE INSERT INTO `tteacher` ( `StatusId` , 