Jes Sie

Jes Sie

  • 741
  • 1.2k
  • 282k

How To Avoid Duplicate Entries in SQL Server 2014 Using SP

Oct 29 2017 2:44 AM
I have an online insurance application which is being used nationwide by less than 200 users. In rare cases, for some reason, while clicking the submit button, it saves the duplicate entry in my database. Below is the stored procedure of my insert statement:
 
  1. ALTER PROCEDURE [dbo].[spInsertMotorInsuranceShortTerm_transaction]  
  2.     -- Add the parameters for the stored procedure here  
  3.           
  4.           (  
  5.                 @MotorInsuranceID int,  
  6.                 @RefNumber nvarchar(15),  
  7.                 @IssuanceType nvarchar(50),  
  8.                 @CustomerNo nvarchar(50),  
  9.                 @VehicleType nvarchar(50),  
  10.                 @VehicleBrand nvarchar(50),  
  11.                 @VehiclePlateNo nvarchar(50),  
  12.                 @VehicleEngineNo nvarchar(50),  
  13.                 @VehicleChasisNo nvarchar(50),  
  14.                 @VehiclePower nvarchar(50),  
  15.                 @InsuranceType nvarchar(50),  
  16.                 @InsuranceOption nvarchar(50),  
  17.                 @PeriodFrom date,  
  18.                 @PeriodTo date,  
  19.                 @ApplicationTime nvarchar(50),  
  20.                 @DateIssued date,  
  21.                 @NetPremium decimal(18,2),  
  22.                 @RegistrationFee decimal(18,2),  
  23.                 @VAT decimal(18,2),  
  24.                 @TotalPremium decimal(18,2),  
  25.                 @BranchCode varchar(50),  
  26.                 @AgentID nvarchar(50),  
  27.                 @Username varchar(50),  
  28.                 @AgentGrossCommission decimal(18,2),  
  29.                 @AgentTax decimal(18,2),  
  30.                 @AgentNetCommission decimal(18,2),  
  31.                 @ProductType varchar(50),  
  32.                 @Seller nvarchar(50),  
  33.                 @Remarks nvarchar(50),  
  34.                 @CustomerID int,  
  35.                 @ReferenceNo nvarchar(50),  
  36.                 @CustomerName nvarchar(255),  
  37.                 @TaxPayersID nvarchar(50),  
  38.                 @Village nvarchar(50),  
  39.                 @District nvarchar(50),  
  40.                 @Province nvarchar(50),  
  41.                 @MobilePhone nvarchar(50),  
  42.                 @CustTelephone nvarchar(50),  
  43.                 @CustEmail nvarchar(50),  
  44.                 @Agent nvarchar(50),  
  45.                 @Status nvarchar(50),  
  46.                 @TrailDate nvarchar(50),  
  47.                 @TrailTime nvarchar(50))  
  48.       
  49. AS  
  50. BEGIN  
  51.       
  52.     BEGIN TRY  
  53.   
  54.         BEGIN TRANSACTION  
  55.   
  56.             INSERT INTO [dbo].[tblMotorInsurance_eCI]  
  57.                        (  
  58.                         [RefNumber]  
  59.                        ,[IssuanceType]  
  60.                        ,[CustomerNo]  
  61.                        ,[VehicleType]  
  62.                        ,[VehicleBrand]  
  63.                        ,[VehiclePlateNo]  
  64.                        ,[VehicleEngineNo]  
  65.                        ,[VehicleChasisNo]  
  66.                        ,[VehiclePower]  
  67.                        ,[InsuranceType]  
  68.                        ,[InsuranceOption]  
  69.                        ,[PeriodFrom]  
  70.                        ,[PeriodTo]  
  71.                        ,[ApplicationTime]  
  72.                        ,[DateIssued]  
  73.                        ,[NetPremium]  
  74.                        ,[RegistrationFee]  
  75.                        ,[VAT]  
  76.                        ,[TotalPremium]  
  77.                        ,[BranchCode]  
  78.                        ,[AgentID]  
  79.                        ,[Username]  
  80.                        ,[AgentGrossCommission]  
  81.                        ,[AgentTax]  
  82.                        ,[AgentNetCommission]  
  83.                        ,[ProductType]  
  84.                        ,[Seller]  
  85.                        ,[Remarks]  
  86.                        )  
  87.                  VALUES  
  88.                        (  
  89.                        @RefNumber,   
  90.                        @IssuanceType,   
  91.                        @CustomerNo,   
  92.                        @VehicleType,   
  93.                        @VehicleBrand,   
  94.                        @VehiclePlateNo,   
  95.                        @VehicleEngineNo,   
  96.                        @VehicleChasisNo,   
  97.                        @VehiclePower,   
  98.                        @InsuranceType,   
  99.                        @InsuranceOption,   
  100.                        @PeriodFrom,   
  101.                        @PeriodTo,   
  102.                        @ApplicationTime,  
  103.                        @DateIssued,   
  104.                        @NetPremium,   
  105.                        @RegistrationFee,   
  106.                        @VAT,   
  107.                        @TotalPremium,   
  108.                        @BranchCode,   
  109.                        @AgentID,   
  110.                        @Username,   
  111.                        @AgentGrossCommission,  
  112.                        @AgentTax,  
  113.                        @AgentNetCommission,  
  114.                        @ProductType,  
  115.                        @Seller,  
  116.                        @Remarks  
  117.                        )  
  118.   
  119.             IF NOT EXISTS (select CustomerNo from tblCustomers where CustomerNo=@CustomerNo)  
  120.             INSERT INTO [dbo].[tblCustomers]  
  121.                       (  
  122.                         [ReferenceNo]  
  123.                         ,[CustomerNo]  
  124.                         ,[CustomerName]  
  125.                         ,[TaxPayersID]  
  126.                         ,[Village]  
  127.                         ,[District]  
  128.                         ,[Province]  
  129.                         ,[CustTelephone]  
  130.                         ,[MobilePhone]  
  131.                         ,[CustEmail]  
  132.                         ,[AgentID]  
  133.                         ,[Status]  
  134.                         ,[Username]  
  135.                         ,[TrailDate]  
  136.                         ,[TrailTime])  
  137.                      VALUES  
  138.                      (  
  139.                          @ReferenceNo,  
  140.                          @CustomerNo,  
  141.                          @CustomerName,  
  142.                          @TaxPayersID,  
  143.                          @Village,  
  144.                          @District,  
  145.                          @Province,  
  146.                          @MobilePhone,  
  147.                          @CustTelephone,  
  148.                          @CustEmail,  
  149.                          @Agent,  
  150.                          @Status,  
  151.                          @Username,  
  152.                          @TrailDate,  
  153.                          @TrailTime)  
  154.               
  155.         COMMIT TRANSACTION  
  156.   
  157.     END TRY  
  158.   
  159.     BEGIN CATCH  
  160.             ROLLBACK TRANSACTION  
  161.             RAISERROR('Duplicate record found. All entries are reverted to its original state.',16,1)  
  162.     END CATCH     
  163. END  
Please note that I cannot make a condition for the first table like what I did in the second table.
 
Well, actually, it is not really duplicate. It's just that when they click the submit button, somehow it sends 2 entries in my database. 
 
Below is the duplicate entries:
 

Attachment: duplicate_entries.rar

Answers (3)