Delete duplicate records with one _mssql left

Source: Internet
Author: User
We can delete duplicate records in the following ways:
Example: Table name: dbo. Variety Description $, field includes: ID_PK, breed name, trait standard number, code, first create a table that is the same as the original table structure:
Copy Code code as follows:

Select * into Tmpa from dbo. Breed Description $ where 1=2--created
in datasheet, breed name, trait standard number These two fields cannot be With duplicate values, execute the following script:

declare @VarietyName nvarchar (255),
@StdCharCode nvarchar (255),
@iCount int
Set @iCount = 0;
Declare insert_distinct_cursor cursor for
Select breed name, trait standard number from dbo. Breed Description $ GROUP by breed name, trait standard number
Open Inse Rt_distinct_cursor
FETCH NEXT from Insert_distinct_cursor to @VarietyName, @StdCharCode
while (@ @fetch_status <>-1
Begin
IF (@ @fetch_status <>-2)
begin
INSERT INTO Dbo.tmpa (breed name, trait standard number, code) SE Lect top 1 Breed name, trait standard number, code from dbo. Breed description $ where breed name = @VarietyName and trait standard number = @StdCharCode;
Set @iCount = @iCount +1;
End
FETCH NEXT from Insert_distinct_cursor to @VarietyName, @StdCharCode
End

Close Insert_dis Tinct_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.