Click here to Skip to main content
16,020,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
John d. Bartels 7-Dec-12 18:29pm    
Is there another table that you can inner join with to obtain additional information?
Joy1979 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

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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900