title: Unique constraint is conflict select and update in SQL Server
body: <p>We have a Unique key on a Document Image upload table. While update a path of Doc's image, its violating the Unique. But <code>select</code> statement returns zero for same values. </p>
  
  <p>Queries: </p>
  
  <pre><code>select * from uploadeddocmaster 
  where EmployeeCode='VSSPL-0402' and VendorCode='VEND-0001' -- 3 rows 
  
  select * from uploadeddocmaster 
  where EmployeeCode='VSSPL-0402' and VendorCode='VEND-0001' and Documentname='blurred-bg-f4mmedia-2.jpg' -- 0 row
  
  Update uploadeddocmaster set Documentname='blurred-bg-f4mmedia-2.jpg',
  ImagePath='http://ip addrs/testingimages/VSSPL-04034049489910055332026.jpg',
  ModifiedBy='TNPL-0003',ActiveYn='Yes',DeleteYn='No'
  where EmployeeCode='VSSPL-0402' and VendorCode='VEND-0001' -- constraint vialotion 
  </code></pre>
  
  <p>Result set of Selects: </p>
  
  <p><a href="https://i.stack.imgur.com/xrT3Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xrT3Q.png" alt="edit 1:"></a></p>
  
  <p>OP window of above queries and image of <a href="https://i.stack.imgur.com/CUqiN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CUqiN.png" alt="error"></a>:</p>
  
  <blockquote>
    <p>(3 row(s) affected)</p>
    
    <p>(0 row(s) affected) </p>
    
    <p>Msg 2627, Level 14, State 1, Line 26 Violation of UNIQUE KEY
    constraint 'UK_UploadedDocMaster_Code_Name_Side_Vendor'. Cannot insert
    duplicate key in object 'dbo.UploadedDocMaster'. The duplicate key
    value is (VSSPL-0402, blurred-bg-f4mmedia-2.jpg, , VEND-0001).
    The statement has been terminated.</p>
    
    <hr>
  </blockquote>
  
  <p>Question is, there are no records found for the where clause of update. Then why unique constraint is violated. Here my table structure. </p>
  
  <p><code>Alt + F1</code> of table and Column table:</p>
  
  <pre><code>Column_name     Type    Computed    Length  Prec    Scale   Nullable    TrimTrailingBlanks  FixedLenNullInSource    Collation
  SearialNo       int         no      4       10      0       no          (n/a)               (n/a)                   NULL
  EmployeeCode    varchar     no      15                      no          no                  no                      SQL_Latin1_General_CP1_CI_AS
  DocumentName    varchar     no      100                     no          no                  no                      SQL_Latin1_General_CP1_CI_AS
  Side            varchar     no      10                      yes         no                  yes                     SQL_Latin1_General_CP1_CI_AS
  ImagePath       varchar     no      -1                      no          no                  no                      SQL_Latin1_General_CP1_CI_AS
  VendorCode      varchar     no      15                      no          no                  no                      SQL_Latin1_General_CP1_CI_AS
  ActiveYn        varchar     no      3                       yes         no                  yes                     SQL_Latin1_General_CP1_CI_AS
  DeleteYn        varchar     no      3                       yes         no                  yes                     SQL_Latin1_General_CP1_CI_AS
  CreatedDate     datetime    no      8                       yes         (n/a)               (n/a)                   NULL
  CreatedBy       varchar     no      15                      no          no                  no                      SQL_Latin1_General_CP1_CI_AS
  ModifiedDate    datetime    no      8                       yes         (n/a)               (n/a)                   NULL
  ModifiedBy      varchar     no      15                      no          no                  no                      SQL_Latin1_General_CP1_CI_AS
  </code></pre>
  
  <p><code>Alt + F1</code> of table and Index table: </p>
  
  <pre><code>index_name                                  index_description                                       index_keys
  UK_UploadedDocMaster_Code_Name_Side_Vendor  nonclustered, unique, unique key located on PRIMARY     EmployeeCode, DocumentName, Side, VendorCode
  </code></pre>
  
  <p><code>Alt + F1</code> of table and Constraint table: </p>
  
  <pre><code>constraint_type         constraint_name                             delete_action   update_action   status_enabled  status_for_replication  constraint_keys
  UNIQUE (non-clustered)  UK_UploadedDocMaster_Code_Name_Side_Vendor  (n/a)           (n/a)           (n/a)           (n/a)                   EmployeeCode, DocumentName, Side, VendorCode
  </code></pre>
  
  <p>Thanks, </p>
  
  <p>Pugal. </p>