CREATE PROCEDURE `spUserLogOff` ( IN `P_UserId` INTEGER , IN `P_InstituteId` INTEGER , IN `P_hostAddress` VARCHARACTER (?) ) BEGIN DECLARE `P_DateTime` DATETIME ; DECLARE `roletype` INTEGER ; SET `P_DateTime` = ( SELECT MAX ( `LoggedInDateTime` ) FROM `tUserLog` WHERE `UserId` = `P_UserId` AND `InstituteId` = `P_InstituteId` AND `LoggedInIp` = `P_hostAddress` ) ; UPDATE `tUserLog` SET `LoggedOutDateTime` = NOW ( ) WHERE `UserId` = `P_UserId` AND `InstituteId` = `P_InstituteId` AND `LoggedInDateTime` = `P_DateTime` ; SET `roletype` = ( SELECT `RoletypeId` FROM `tuser` WHERE `Id` = `P_UserId` ) ; IF ( `roletype` = ? ) THEN DELETE `TR` FROM `tregisteredusers` `TR` LEFT JOIN `tstudent` `S` ON `S` . `Id` = `TR` . `SourceId` WHERE `S` . `UserId` = `P_UserId` AND `S` . `InstituteId` = `P_InstituteId` ; ELSEIF ( `roletype` = ? ) THEN DELETE `TR` FROM `tregisteredusers` `TR` LEFT JOIN `tteacher` `T` ON `T` . `Id` = `TR` . `SourceId` WHERE `T` . `UserId` = `P_UserId` AND `T` . `InstituteId` = `P_InstituteId` ; END IF ; SELECT `P_DateTime` ; END 