April 12, 2010 at 7:53 am
Another way of deleting duplicates is to use the hidden row identifier %%LockRes%% to identify the individual rows
using the test data post previously
DELETE FROM #Duplicates
WHERE %%LockRes%%
NOT IN (
SELECT MIN(%%LockRes%%) FROM #Duplicates
GROUP BY somevalue)
April 12, 2010 at 8:14 am
Hey Steve,
I'm guessing that you are not serious here - but you should have posted a warning not to use that code! NEVER use %% functions for anything but exploration and fun - they are undocumented might therefore do anything!
As it happens, %%LockRes%% appears to identify a locking hash - which isn't guaranteed to be unique, or relate to a single row - bad news here!
(%%PhysLoc%% in 2008 is different, but equally not to be relies on for anything ever)
Also be careful with NOT IN and NULLs - and remember that these %% functions aren't guaranteed to do anything at all - it might return NULL one day, for all we know 😉
Summary: if you use %% functions, your shoes might catch fire or your fridge might float away. 😀
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
April 12, 2010 at 8:19 am
Thanks Paul,
We were playing around with them today and thought they may be useful, i guess it can be dangerous to use undocumented features and i would never use it in production.
I was also hoping to hear what other more experienced people had to say about them as i couldn't find much info on google, so i am glad you posted.
April 12, 2010 at 1:12 pm
When I have this problem, I "cheat" if I can.
I create a temp table, with a unique index on the column containing duplicates.
Then set "ignore duplicates" on the index.
Then copy from your live table containing duplicates into the temp table. It will only allow one record and delete the others.
Then truncate the live data & copy back in from the temp table.
This may not work if you have huge tables, or cannot truncate your live table for a variety of reasons, but it is an option.
(Maybe we should merge the 2 duplicate threads ? :-D)
April 14, 2010 at 1:27 pm
Thanks all for your reply......
Thanks [/font]
April 15, 2010 at 2:26 am
HI , JUST TRY TO THIS
alter table <TABLENAME> add gid timestamp
delete from <TABLENAME> where gid not in(select max(gid) from <TABLENAME> group by Field1,Field2)
alter table <TABLENAME> drop column gid
Note: Field1,Field2 are repeating allfields except that gid
gid is nothid that is uniqu field
i can fix lot of issue in production database in this way........
April 16, 2010 at 12:23 am
cheyursenthil (4/15/2010)
HI , JUST TRY TO THISalter table <TABLENAME> add gid timestamp
delete from <TABLENAME> where gid not in(select max(gid) from <TABLENAME> group by Field1,Field2)
alter table <TABLENAME> drop column gid
Note: Field1,Field2 are repeating allfields except that gid
gid is nothid that is uniqu field
i can fix lot of issue in production database in this way........
That's a great way to fragment your table, and is a lot less efficient than the direct methods (e.g. using ROW_NUMBER).
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 7 posts - 16 through 21 (of 21 total)
You must be logged in to reply to this topic. Login to reply