>> 2nd record. In this situation what I would do is, add an extra identity
>> column, delete the record and remove the identity column
>
> Instead of writing a DELETE statement in a query window? I don't think
> this is a very good answer at all. What if the table is huge?
> 1 aa
> 1 aa --- i want to remove this record
> 1 aa
If you delete any of those three rows, how do you know which one was
deleted? What exactly makes you want to delete the second one, instead of
the third or the fourth? This is part of the problem with allowing
duplicate data in the first place. Anyway, the answer is simple:
SET ROWCOUNT 1;
DELETE table WHERE id = 1 AND cc = 'aa';
SET ROWCOUNT 0;
SELECT * FROM table;
> also please explain me how to remove the duplicate records from the table
> but i want to keep one record from each group, i.e after the delete the
[quoted text clipped - 6 lines]
>
> Also i am using sql 2000
Well, the easiest way for me would be to:
SELECT DISTINCT id, cc INTO dbo.newtable FROM dbo.oldtable;
DROP TABLE dbo.oldtable;
EXEC sp_rename 'dbo.newtable', 'oldtable', 'OBJECT';
Then add a PRIMARY KEY on (id, cc) if you want to not have to do this every
day (or more)!
vinu - 31 Jul 2008 14:55 GMT
Aron
> Well, the easiest way for me would be to:
>
> SELECT DISTINCT id, cc INTO dbo.newtable FROM dbo.oldtable;
> DROP TABLE dbo.oldtable;
> EXEC sp_rename 'dbo.newtable', 'oldtable', 'OBJECT';
What if dbo.oldtable has a trigger or a check constraints on it..????
Accoring to your solution you will loose the trigger/check constraints
right..???
> Then add a PRIMARY KEY on (id, cc) if you want to not have to do this
> every
> day (or more)!
As you suggested add a PRIMARY KEY , what if the is very huge
thanks
vinu
http://oneplace4sql.blogspot.com/
>> 1 aa
>> 1 aa --- i want to remove this record
[quoted text clipped - 30 lines]
> every
> day (or more)!
Aaron Bertrand [SQL Server MVP] - 31 Jul 2008 15:06 GMT
> What if dbo.oldtable has a trigger or a check constraints on it..????
> Accoring to your solution you will loose the trigger/check constraints
> right..???
Well yes, there are plenty of other things you will need to consider;
indexes, triggers; constraints; partitioning; indexed views; etc. Since I
wasn't told about other metadata, just "here is a simple two-column table
where we allow duplicates for some reason", that is the problem I solved.
>> Then add a PRIMARY KEY on (id, cc) if you want to not have to do this
>> every
>> day (or more)!
>
> As you suggested add a PRIMARY KEY , what if the is very huge
Okay, then add an identity column if you really think that is the better
answer. That will make your data unique! <shrug>