CREATE PROCEDURE `spAddViewHistory` ( IN `P_StatusId` INTEGER , IN `P_DocumentId` INTEGER , IN `P_StudentId` INTEGER ) BEGIN IF NOT EXISTS ( SELECT ? FROM `tviewhistory` WHERE `StatusId` = `P_StatusId` AND `DocumentId` = `P_DocumentId` AND `StudentId` = `P_StudentId` ) THEN INSERT INTO `vida_lms` . `tviewhistory` ( `StatusId` , `DocumentId` , `StudentId` , `ViewCount` , `CreatedDateTime` ) VALUES ( `P_StatusId` , `P_DocumentId` , `P_StudentId` , ? , NOW ( ) ) ; SELECT ? ; ELSE UPDATE `tviewhistory` SET `ViewCount` = `ViewCount` + ? WHERE `StatusId` = `P_StatusId` AND `DocumentId` = `P_DocumentId` AND `StudentId` = `P_StudentId` ; SELECT ? ; END IF ; END 