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
 General SQL Server Forums
 New to SQL Server Programming
 two columns with unique entries?

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 ProductID
1 11-22-33 1
2 11-22-33 4
3 11-22-33 1
4 22-33-44 2

The 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 EXISTS

IF NOT EXISTS(SELECT Statement)
BEGIN
INSERT STATEMENT
END

ELSE
BEGIN
SELECT 'Record Already Exists'
END


Jai Krishna
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-01-16 : 05:49:08
declare @cnt int
select @cnt = count(*) from urtable where ProductID = @ProductID and UserID =@UserID
insert into urtable
select statement where @cnt = 0
Go to Top of Page

kaine
Starting Member

2 Posts

Posted - 2009-01-16 : 07:08:33
thanks, this is exactly what I was looking for.
Go to Top of Page
   

- Advertisement -