?
Solved

Remove dup record from SQL

Posted on 2009-07-06
7
Medium Priority
?
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24786291
0
 
LVL 5

Accepted Solution

by:
rgc6789 earned 2000 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 42

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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 42

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

752 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