Solved

Remove dup record from SQL

Posted on 2009-07-06
7
282 Views
Last Modified: 2012-05-07
I have an sql server 2005 database table.
the first column is a uniqueidentifier but there is no primary key.
I did a copy paste on a row thinking it would paste everything ACCEPT the uniqueidentifier coumn.
However, it copied everything... so now I have an exact dup record.

I cannot delete the dup.
I get the "The row value(s) updated or deleted either do not make the row unique..." msg

I tried to add a field and give it a separate value for the two records... but it will not let me edit the record to add data to the new column (same msg as above).

The following causes sql to crash
set rowcount 1
delete from mytable
where mycolumn = 'myvalue'
0
Comment
Question by:santaspores1
7 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24786291
0
 
LVL 5

Accepted Solution

by:
rgc6789 earned 500 total points
ID: 24786294
Yes, once you have 2 records that are the same, SQL cannot do anything that will then make 1 unique because it can't distinguish one from the other.

Run a delete statement that will delete both of them and then re-insert it back.
0
 
LVL 41

Expert Comment

by:pcelba
ID: 24786332
You may copy distinct records into temp table:

SELECT DISTINCT * INTO #TempTable FROM YourTable

TRUNCATE YourTable

INSERT INTO YourTable SELECT * FROM #TempTable

DROP #TempTable
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 41

Expert Comment

by:pcelba
ID: 24786341
You may execute above code just for your two duplicate records by adding appropriate WHERE clause.
0
 

Author Closing Comment

by:santaspores1
ID: 31600208
easy enough - thanks!
0
 
LVL 7

Expert Comment

by:wilje
ID: 24786568
You can use a CTE to identify your duplicate rows and delete them.
Use tempdb;

Go
 

If object_id('tempdb..#temp', 'U') Is Not Null

   Begin;

        Drop Table dbo.#temp;

   End;

   

Create Table #temp (id uniqueidentifier, data varchar(max));
 

Declare @myId uniqueidentifier;

Set @myId = newid();
 

Select @myId;
 

Insert Into #temp (id, data) Values(@myId, 'mydata');

Insert Into #temp (id, data) Values(@myId, 'mydata');
 

Select * From #temp;
 

;With cteDUPS (id, rn)

As (Select id

           ,row_number() over(partition By id Order By id) rn

      From #temp

)

Delete 

  From cteDUPS

 Where rn > 1;

 

Select * From #temp;

Open in new window

0
 
LVL 5

Expert Comment

by:rgc6789
ID: 24786587
The other options also work, but for 2 rows I find it easier to just duplicate it manually.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now