Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 avoid insert duplicate record

Author  Topic 

sebastian11c
Posting Yak Master

129 Posts

Posted - 2012-04-17 : 12:55:42
i hace a table with 4 columns one column is an identity colums the others are data




idclient (identity) city name lastname phone




how could i avoid to inser duplicate records on my table, this issue is killing me


a trigger?
any idea will be realli appreciate

many many thanks in advanced

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-17 : 13:03:33
Add a unique constraint to the columns that need to be unique.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-17 : 13:29:36
what according to your are duplicates? first explain that with sample data for us to understand

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sebastian11c
Posting Yak Master

129 Posts

Posted - 2012-04-17 : 14:47:07
HI, OBVIOUSLY the identity column cannot be duplicate, so what im trying to say is the other columns have the same data

example

idclient (identity) city name lastname phone
1 miami peter smith 7855062995
2 miami peter smith 7855062995

thats what im trying to avoid but i dont know how,,( i have 2 record with different idclient(because is identity, ) but all other columns are exactly the same

i dont want to save same records

any help will be appreciate

many many thanks

kind regards

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-17 : 14:53:52
you want to remove the existing duplicates alone or you want a logic to check for duplicates in future inserts as well?
how is the table getting populated?
for removing existing duplicates, you can do like


DELETE t
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY city, name, lastname,phone ORDER BY idclient) AS Rn FROM Table
)t
WHERE Rn >1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-17 : 15:04:12
Add a unique constraint over the columns to be unique, that way SQL protects the data.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sebastian11c
Posting Yak Master

129 Posts

Posted - 2012-04-17 : 18:20:10
hi visakh16 thanks for your help
by the way , how do you know all the answer?

also i need a logic to check for duplicates in future inserts as well


thanks in advanced
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-18 : 00:29:41
quote:
Originally posted by sebastian11c

hi visakh16 thanks for your help
by the way , how do you know all the answer?

also i need a logic to check for duplicates in future inserts as well


thanks in advanced


Mostly by experience and learning from others here

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-18 : 00:30:19
quote:
Originally posted by sebastian11c

hi visakh16 thanks for your help
by the way , how do you know all the answer?

also i need a logic to check for duplicates in future inserts as well


thanks in advanced


for that easiest way is to set a unique constraint as Tara suggested

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sebastian11c
Posting Yak Master

129 Posts

Posted - 2012-04-18 : 09:19:01
im not sure how to set a unique constraint??
could you help me to do properly

thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-18 : 13:10:20
Here's an example:

USE DB1
GO
ALTER TABLE [dbo].Tbl1 ADD CONSTRAINT [uniq_Tbl1_Col2_Col3_Col4]
UNIQUE CLUSTERED (Col2 ASC, Col3 ASC, Col4 ASC)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sebastian11c
Posting Yak Master

129 Posts

Posted - 2012-04-18 : 15:55:33
i wll add more complex,
im inserting 300 records with one query, how could i do to insert the record that arent duplicates (not exist in my table)because im trying to insert 300 records on my tabe but i get and error "cannot insert duplicated key, violation unique key constraint" and no one is instered in my table,

that i need its something to let me insert the records that dont exist in my table , and avoid to insert the duplicates, but the end user dont get any warning message

any help will be appreciate

thanks in advanced
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-18 : 16:06:53
How are you doing the insert? Is it from an app? From a file? From another table?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sebastian11c
Posting Yak Master

129 Posts

Posted - 2012-04-18 : 16:18:01
im doing the insert from an app

thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-18 : 16:19:12
Insert them into a staging table and then use INSERT/SELECT * FROM NOT EXISTS to move the data from staging table to your actual table. Or do the inserts one at a time so as the whole 300 won't fail.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sebastian11c
Posting Yak Master

129 Posts

Posted - 2012-04-18 : 18:48:26
hi, i cannot change the app, so i cant insert one by one,,
also , could you give me with my table "clients" an example hor could i do this """ Insert them into a staging table and then use INSERT/SELECT * FROM NOT EXISTS to move the data from staging table to your actual table""

table CLIENTS
idclient (identity) city name lastname phone
1 miami peter smith 7855062995
2 miami peter smith 7855062995


many many thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-18 : 18:58:18
Well if you can't change the app, then a staging table solution isn't going to help you as it requires you to write the rows to a different table first.

How about an instead of trigger then? I supposed you could process each row via a loop in the trigger and just use TRY/CATCH logic rather than having to see if the row exists already.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-04-18 : 19:20:08
Tried the "WITH IGNORE_DUP_KEY" option?
See http://blogs.msdn.com/b/craigfr/archive/2008/01/30/maintaining-unique-indexes-with-ignore-dup-key.aspx


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-19 : 01:22:59
quote:
Originally posted by sebastian11c

im doing the insert from an app

thanks


does app call insert procedure for data population? do you've any control over it?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sebastian11c
Posting Yak Master

129 Posts

Posted - 2012-04-19 : 09:10:38
the apps uses an stored procedure to insert the data into the table,
insert one by one, but send 100 rows to insert
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-19 : 12:31:06
Show us the stored procedure. How can it accept 100 rows?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
    Next Page

- Advertisement -