Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
In SQL Server 2008 R2 I added two duplicate ID and record in my table. When I try to delete one of the last two records I receive the following error.
 
The row values updated or deleted either do not make the row unique or they alter multiple rows.
 
The data is:
 
ID    Name       Qty
7     ABC         6
 
7     ABC         6
 
7     ABC         6
 
8     XYZ         1
 
8     XYZ         1
 
8     XYZ         4
 
7     ABC         6
 
7     ABC         6
 
I need to delete last two records:
 
7   ABC         6
 
7   ABC         6
 
I have been trying to delete last 2 record using the feature "Edit the Top 200 rows" to delete this duplicate id but get the error above.
 
I have tried with following query but it removes entire(" 7   ABC   6") from table whereas I just need to remove last 2 rows only.
 
;with t as (
    select top(2) *
      from tbl
     where A = 7 and B = 'ABC' and C = 6
)
DELETE t;
 
Any help is appreciated. Thanks in advance:)
Posted 7-Dec-12 12:36pm
Joy1979317
Comments
John d. Bartels at 7-Dec-12 18:29pm
   
Is there another table that you can inner join with to obtain additional information?
Joy1979 at 7-Dec-12 18:37pm
   
Yes John, This table is a child table and whenever any query runs against parent table data will populate from this table. I was trying to add some data into table and by mistake add duplicate data. Thx:)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I was able to delete only 2 records using the following syntax:
 
USE Example1;
GO
DELETE TOP (2)
FROM Example1.dbo.RealTable
WHERE ID = '7' AND Name = 'ABC' AND Qty = '6';
GO
 
However, this deleted the first two records that I had added, and not the last two as one might expect. If you can join to another table and get some sort of datetime for the record insert, then you can order by and control which records are deleted. You may also want to consider adding some sort of Primary Key or Unique Index so that the rows can be uniquely indexed.
 
MSDN Transact-SQL Delete Reference: http://msdn.microsoft.com/en-us/library/ms189835(v=sql.100).aspx[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 565
1 Maciej Los 359
2 Kornfeld Eliyahu Peter 309
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100