Solved

Remove dup record from SQL

Posted on 2009-07-06
7
287 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 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ms sql + top 1 for each customer 3 62
Query to Add Late Tolerance 10 82
Sql query 107 85
Using this function 4 38
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

680 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