Click here to Skip to main content
13,145,608 members (36,250 online)
Rate this:
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
Any help is appreciated. Thanks in advance:)
Posted 7-Dec-12 11:36am
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

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

Solution 1

I was able to delete only 2 records using the following syntax:

USE Example1;
FROM Example1.dbo.RealTable
WHERE ID = '7' AND Name = 'ABC' AND Qty = '6';

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:[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2017
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