Delete duplicate records with one

Source: Internet
Author: User
Tags traits

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.