CREATE PROCEDURE `spCreateAdmin` ( IN `P_Code` VARCHARACTER (?) , IN `P_UserId` INTEGER ) BEGIN DECLARE `TeacherId` INTEGER DEFAULT ? ; DECLARE `TeacherCode` VARCHARACTER (?) DEFAULT ? ; DECLARE `IsSuperAdmin` INTEGER DEFAULT ? ; IF EXISTS ( SELECT * FROM `tadmin` WHERE `UserId` = `P_UserId` ) THEN SELECT `id` AS `AdminId` , CODE AS `AdminCode` FROM `tadmin` WHERE `UserId` = `P_UserId` ; ELSE IF ( SELECT COUNT ( * ) FROM `tuser` ) = ? THEN INSERT INTO `tteacher` ( CODE , `UserId` , `CreatedDateTime` , `UpdatedDateTime` , `InstituteId` ) VALUES ( `P_Code` , `P_UserId` , NOW ( ) , NOW ( ) , ? ) ; SET `TeacherId` = `LAST_INSERT_ID` ( ) ; SET `TeacherCode` = `P_Code` ; SET `IsSuperAdmin` = ? ; END IF ; INSERT INTO `tadmin` ( CODE , `UserId` , `IsSuperAdmin` , `InstituteId` , `CreatedDateTime` , `UpdatedDateTime` ) VALUES ( `P_Code` , `P_UserId` , `IsSuperAdmin` , ? , NOW ( ) , NOW ( ) ) ; SELECT `LAST_INSERT_ID` ( ) AS `AdminId` , `P_Code` AS `AdminCode` , `TeacherId` , `TeacherCode` ; END IF ; SELECT ? ; END 