CREATE PROCEDURE `spAddRegisterUser` ( IN `P_UserId` INTEGER , IN `P_RoleTypeId` INTEGER , IN `P_SourceId` INTEGER , IN `P_OSTypeId` SMALLINT , IN `P_DeviceToken` VARCHARACTER (?) ) BEGIN IF NOT EXISTS ( SELECT ? FROM `tregisteredusers` WHERE `UserId` = `P_UserId` AND `SourceId` = `P_SourceId` AND `RoleTypeId` = `P_RoleTypeId` ) THEN START TRANSACTION ; INSERT INTO `tregisteredusers` ( `UserId` , `SourceId` , `RoleTypeId` , `DeviceToken` , `OSTypeId` , `CreatedDateTime` ) VALUES ( `P_UserId` , `P_SourceId` , `P_RoleTypeId` , `P_DeviceToken` , `P_OSTypeId` , NOW ( ) ) ; COMMIT ; ELSE START TRANSACTION ; UPDATE `tregisteredusers` SET `DeviceToken` = `P_DeviceToken` WHERE `UserId` = `P_UserId` AND `SourceId` = `P_SourceId` AND `RoleTypeId` = `P_RoleTypeId` ; COMMIT ; END IF ; END 