title: Check if a given ID exists using Stored Procedure by If Exists or If Not Exists?
body: <p>I am using stored procedures for insert, update, delete operations in database. To check if a given <code>ID</code> exists, I use if exists condition in my code. Here's my issue: I am given the <code>ID</code> which is in database, but the condition checks and allows duplicate values to be stored in database.</p>
  
  <p>My stored procedure code:</p>
  
  <pre><code>SET NOCOUNT ON;
  
  If @Action = 'Insert'   --used to Insert records
  Begin
      If Exists(select BatchNo from Work_In_Progress where BatchNo = '@BatchNo')
      Begin
          set @Error='Already Exists'     
      End     
      Else 
      Begin                  
          insert into Work_In_Progress 
          values (@BatchNo, @ISR_No, @CatalogNo, @Brandname, @Productname, @Operation_Code, @Operation_Name, @Machine_Id, @Received_Qty, @Finished_Qty, @Balance_Qty, @Accepted_Qty, @Rejected_Qty, @Startdate, @Starttime, @Enddate, @Endtime, @Accp_MTS_No, @Rej_MTS_No, @Remarks, @Work_Done_By, @Approved_By, @Updated_Date, @Updated_By, @InspectionStatus);
  
          insert into Sample_Inspection   
          values(@BatchNo,@Operation_Code,@Inspection_Name,@Insp_Parameter,@Insp_Tolerance,@Observation,@Sample_Rcd_Qty,@Sample_Accepted_Qty,@Sample_Rejected_Qty,@Sample_Rework_Qty,@Sample_Accp_MTS_No,@Sample_Rej_MTS_No,@Sample_Rework_MTS_No,@Sample_Remarks,@Sample_WorkDone_By,@Sample_WorkDone_Date,@Sample_Approved_By,@Sample_Updated_Date,@Sample_Updated_By);
     End
  End
  </code></pre>
  
  <p>How can I overcome this issue?</p>