MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 18th, 2004, 12:23 AM
LingLing LingLing is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 19 LingLing User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 24 sec
Reputation Power: 0
Exclamation Importing of data, resulting in duplicates, HELP!

hi all,

I have a problem with SQL 2000 here...
I juz imported the same set of data to my database table and it gave me duplicated records as all the data is imported although there is same existing data.

Can anyone help me with this?
How can i import the data such that the data which already exists in the database will not be imported in again?

Thanks in advance.

Reply With Quote
  #2  
Old October 18th, 2004, 07:27 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
well, if there are dup records now you can eliminate 'em through querying on dup fields and the primary key.

Else, when I do an import on data that has subsets of data that are already in the DB, I do an insert on join conditions to eliminate inserting the data already in there.

Reply With Quote
  #3  
Old October 18th, 2004, 12:10 PM
BlueIrishs2k BlueIrishs2k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 7 BlueIrishs2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Username=NULL
well, if there are dup records now you can eliminate 'em through querying on dup fields and the primary key.

Else, when I do an import on data that has subsets of data that are already in the DB, I do an insert on join conditions to eliminate inserting the data already in there.


Can you elaborate on this with an example? I'm working through a problem right now that could potentially be solved by this.

Reply With Quote
  #4  
Old October 18th, 2004, 10:39 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Sorry, but I don't have too much time right now to show you all of the 2 ways I mentioned. You should note before reading any further that you can avoid this problem almost entirely by re-thinking you're primary key field(s). So instead of having to continuously go through the trouble, think about how your key is defined. Mainly I've found that using Identity fields as a primary key will result in dup records @ some time or another.

The simplest way to explain is deleting them from the DB once they're in there, but I think a more solid method is to avoid them getting in there in the 1st place. When they're already in there, you first have to be able to grab the dup records, from there you can join back on the table to do your delete. Let's assume this table w/the following data...
Code:
tEmployee
-------
ID	[PK]
Name
Address
Phone
Position

 ID	Name	Address	  Phone	  	Position
-----   ----	-------	  -----	  	--------
1	Joe	 TX	  445-3948	ProjMgr
2	Andy	 CA	  485-0094	ProjMgr
3	Kelly	 MI	  445-0049	Support
4	Dale	 LA	  449-3111	Dev
5	Andy	 CA	  485-0094	ProjMgr
6	Kelly	 MI	  445-0049	Support
7	Andy	 CA	  485-0094	ProjMgr
8	Jimmy		  499-0011	Dev
9	Seth	 TX	  466-0194	Dev
10	Tim	 here	  994-0023	DBA
11	Dave	 there	  338-9935	Support
12	Tim	 here	  994-0023	DBA

Surely you see that Andy, Kelly, and Tim have 'duplicate' rows...duplicate being defined as every field having the same value as another record w/the exception of the primary key. Now you can query on a count for the primary key to grab the dups...
Code:
select	Name,
	Address,
	Phone,
	Position,
	count(distinct ID) as cntID
from	tEmployee 
group	by Name, Address, Phone, Position
having	count(distinct ID) > 1

Name	Address	  Phone	  	Position    cntID
----	-------	  -----	  	--------    -----
Andy	 CA	  485-0094	ProjMgr      3
Kelly	 MI	  445-0049	Support      2
Tim	 here	  994-0023	DBA 	     2

You can do this much, ya? I'll try to post more later, but from those records you can join back on the table to do your delete. Since you still want one record left, from the above records select the name, address, phone, and the max(ID) from each of the dup groups and delete all their other records that match on everything BUT the max ID. I can explain more later. Btw, my use of Distinct in the count(distinct ID) isn't totally necessary, if ID is the PK then by definition it will always be distinct.

Last edited by Username=NULL : October 19th, 2004 at 03:46 PM.

Reply With Quote
  #5  
Old October 19th, 2004, 07:55 AM
BlueIrishs2k BlueIrishs2k is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 7 BlueIrishs2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I agree that the best way to not have to do this is to keep them from happening. That is what I am trying to work towards right now. I have primary keys identified as a date and want to import records for dates that are not already in the table. Each time I run just a regular DTS package, though, it errors out and doesn't add any records. I'm sure it is something simple to fix but I can't figure it out.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Importing of data, resulting in duplicates, HELP!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT