My manager and I where looking at some complex code to eliminate duplicate records in a database table.
then it hit me how its done easily... so i thought i'd share it...
In English:
--------------------------------
-- Eliminating Duplicate rows --
--------------------------------
-- select all into a temp table.
-- truncate origional table.
-- select only unique rows back into origional table.
In Code sql server 2000:
--------------------------------
-- Eliminating Duplicate rows --
-------------------------------- -
-- select all into a temp table.
-
-
select * into temptable1 from <OrigionalTable>
-
-- truncate origional table.
-
delete <OrigionalTable>
-
-
-- select only unique rows back inot origional table.
-
--this usues the selected columns to see if its unique
-
insert into <OrigionalTable> select Distinct <columnName> from TempTable1
-
-
--this uses all columnsto see if its unique
-
insert into <OrigionalTable> select Distinct * from TempTable1
-
5 8430
But wen im having multiple column duplication the above code doesnt works. In that situation wat can i do???
Regards,
Jeen
But wen im having multiple column duplication the above code doesnt works. In that situation wat can i do???
Regards,
Jeen
DELETE FROM table_name where column_name in
(select column_name from table_name GROUP BY column_name HAVING COUNT(*)>1)
This is what i've done .. creating an cursor and I've used it many times, hope it can help you.
--Declare all column names in table
DECLARE @Column1 decimal(9), @Column2 varchar(50), ect .....
DECLARE duplicate_curso r CURSOR FOR
-- select all columns in table bit you must have an count column
select Column1,Column2 , count(*) Cnt, ect ...
from tblTable
group by Column1, Column2
Having count(*) > 1
OPEN duplicate_curso r
FETCH NEXT FROM duplicate_curso r
INTO @Column1, @Column2, ect ...
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Cnt = @Cnt - 1
SET ROWCOUNT @Cnt
DELETE tblTable
WHERE @Column1 = Column1 AND @Column2 = Column1 AND ect ....
FETCH NEXT FROM duplicate_curso r
INTO @Column1, @Column2, ect ...
END
CLOSE duplicate_curso r
DEALLOCATE duplicate_curso r
-- dont forget to set rowcount to 0
SET ROWCOUNT 0
Most easy and fast.
Aply only for duplicate rows in all columns.
be carefully with the indexes and foreign key before to drop the original table.
In Code sql server 2000: -
select distinct * into temp_table from <original_table>
-
drop <OrigionalTable>
-
sp_rename temp_table, original_table
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Gary Lundquest |
last post by:
I have an application with MS Access 2000 as the front end and MySQL as the
back end. All was well until I upgraded the MySQL (Linux) server.
The Problem: I insert data into a cumulative table. Before when I did
this, and there were duplicate entries, the duplicate entries were rejected
and I got a return code with the number of affected rows (number added).
Now, I get a MyODBC error and the application stops when trying to add...
|
by: gordy |
last post by:
edit: this came out longer than I thought, any comments about anything
here is greatly appreciated. thank you for reading
My system stores millions of records, each with fields like firstname,
lastname, email address, city, state, zip, along with any number of user
defined fields. The application allows users to define message templates
with variables. They can then select a template, and for each variable
in the template, type in a...
|
by: medhanush |
last post by:
I have data in table as follows,
Num Category ProductName
---- -------- -----------
100 A Product1
100 B Product1
101 A Product2
101 B Product2
101 A Product2
|
by: Matt B |
last post by:
Can somebody help me with this please...
I have an xml file along the lines of
<results>
<result status="Pass">
<person name="Fred"/>
<subject name="English" />
</result>
<result status="Pass">
|
by: dale.zjc |
last post by:
I've got the following table data:
11652 5.99
11652 0.14
12996 5.03
12996 0.12
12996 7.00
And I need to write a query to return only rows 2 and 4, since the
remaining rows have duplicate IDs. I've tried the Group By, but am
| |
by: Rich |
last post by:
Hello,
I am just checking if there is a property or technique for displaying or
retrieving from a dataTable the top 1 row(s) for rows containing duplicate
keys (IDs). I have to pull data from a sql server DB. The resultset
contains 10 distinct rows but the keys are duplicate - like record 12345 is
listed twice but distinct because some of the other data for each field is
different. I only need to see one of the 2 rows for ID 12345...
|
by: nano |
last post by:
Does sql server have a way to handle errors in a sproc which would allow
one to insert rows, ignoring rows which would create a duplicate key
violation? I know if one loops one can handle the error on a row by row
basis. But is there a way to skip the loop and do it as a bulk insert?
It's easy to do in Access, but I'm curious to know if SQL Server proper
can handle like this. I am guessing that a looping operation would be
slower to...
|
by: shahjapan |
last post by:
create table temp(comp_id int,branch_id int);
insert into temp values (1000,1);
insert into temp values (1000,2);
insert into temp values (1000,3);
insert into temp values (1000,1);
insert into temp values (1000,2);
insert into temp values (2000,1);
insert into temp values (2000,2);
insert into temp values (2000,3);
insert into temp values (2000,1);
|
by: rajeshkrsingh |
last post by:
Hi friends,
Step1-
create table duplicate
(
intId int, varName varchar(50)
)
insert into duplicate(intId,varName) values(1,'rajesh')
insert into duplicate(intId,varName) values(2,'raj12')
insert into duplicate(intId,varName) values(1,'rajesh')
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |