You can delete duplicate records in the following ways:
For example, table name: dbo. Description $. The fields include ID_PK, breed name, traits standard number, and code. First, create a table with the same structure as the original table:
Copy codeThe Code is as follows:
Select * into tmpA from dbo. breed description $ where 1 = 2 -- created
In the data table, the variety name and Character Standard Number Fields cannot have duplicate values. Execute the following script:
Declare @ VarietyName nvarchar (255 ),
@ StdCharCode nvarchar (255 ),
@ ICount int
Set @ iCount = 0;
Declare insert_distinct_cursor cursor
Select breed name, traits Standard No. from dbo. breed description $ group by breed name, traits Standard No.
Open insert_distinct_cursor
Fetch next from insert_distinct_cursor INTO @ VarietyName, @ StdCharCode
WHILE (@ fetch_status <>-1)
BEGIN
IF (@ fetch_status <>-2)
BEGIN
Insert into dbo. tmpA (breed name, traits standard number, code) select top 1 breed name, traits standard number, code from dbo. description $ where breed name = @ VarietyName and standard number of characters = @ StdCharCode;
Set @ iCount = @ iCount + 1;
END
Fetch next from insert_distinct_cursor INTO @ VarietyName, @ StdCharCode
END
CLOSE insert_distinct_cursor
DEALLOCATE insert_distinct_cursor
Print @ iCount