SQL Multiple primary key tables, when the inserted data is duplicated, are prompted to violate the primary KEY constraint that cannot be inserted by the error. So, how do I find duplicate values for the inserted data?
Workaround: Use Group by
Suppose there is a table #a , there are saleid,vendorid,comid,price,saleprice,quantity and other fields.
The primary key is: Saleid,vendorid,comid three of them. Assume that the data source that you insert #a may be duplicated.
namely: Saleid,vendorid,comid Three fields are the same field, then insert #a The primary key violation is reported, and the primary key constraint is violated.
If you want to find out #a The values for inserting duplicates in the table can be queried using the following statement:
Select Saleid,vendorid,comid,count (*) from#a GROUP by Saleid,vendorid,comid have Count (*) >1
The above statement will follow three primary keys Saleid,vendorid,comid Group, if Saleid,vendorid,comid The same records are counted in the same number of rows. the having condition behind Group By is the query that the same record is greater than 1 rows.
This article is from the "Remote _chen" blog, make sure to keep this source http://chenshirong.blog.51cto.com/3945427/1884101
SQL multiple primary key tables, duplicate values of the query data when inserting data is duplicated?