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.
| Author |
Topic |
|
kaine
Starting Member
2 Posts |
Posted - 2009-01-16 : 05:45:39
|
Hi,Having some trouble trying to work out the best way to combat a duplicating row problem.I have attached screenshot of my table layout.When a user adds a selected product, it is added along with their userid to the wishlist table. Example below.WishID UserID ProductID1 11-22-33 12 11-22-33 43 11-22-33 14 22-33-44 2The problem as you can see is that userid and productid columns contain duplicate entries.Can I somehow check both the UserID and ProductID columns on the insert for duplicates and if so to simple not add that entry? any help would be appreciated,thanks |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-01-16 : 05:46:58
|
| Use IF EXISTS OR IF NOT EXISTSIF NOT EXISTS(SELECT Statement)BEGIN INSERT STATEMENTENDELSEBEGIN SELECT 'Record Already Exists'ENDJai Krishna |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-01-16 : 05:49:08
|
| declare @cnt intselect @cnt = count(*) from urtable where ProductID = @ProductID and UserID =@UserID insert into urtableselect statement where @cnt = 0 |
 |
|
|
kaine
Starting Member
2 Posts |
Posted - 2009-01-16 : 07:08:33
|
thanks, this is exactly what I was looking for. |
 |
|
|
|
|
|