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 2000 Forums
 SQL Server Development (2000)
 Avoiding duplicate values

Author  Topic 

herbjorn
Starting Member

8 Posts

Posted - 2005-04-14 : 08:30:34
Hi,

I need to make sure that duplicate values doesn't get inserted into a table. The duplicate value is rather a pair of values.

Type Name
1 Green
2 Green * Not duplicate values
3 Green * Duplicate values

How can I do this?

I'm thinking about having a stored procedure for insert and checking that the value doesn't exist and then directly do the insert:

IF NOT EXISTS(SELECT * FROM Table WHERE Type=1 AND Name=Green)
INSERT

Will this be enough? Can another insert happen between my SELECT and the INSERT? If so How do I do?

sincerely Herbjörn

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-04-14 : 08:56:00
that's ok... you can also put constraints on the columns you want unique.

i'd say it's very unlikely that it does. how many user inserts per second do you ecpect???

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2005-04-14 : 10:20:29
I'd say it's highly unlikely if not impossible that anything will be inserted between the two statements but to make sure you can put an explicit transaction around it:

BEGIN TRAN
IF NOT EXISTS(SELECT * FROM Table WHERE Type=1 AND Name=Green)
INSERT
COMMIT TRAN

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page

mfemenel
Professor Frink

1421 Posts

Posted - 2005-04-14 : 11:33:22
Lumbago, that is one of the best signature quotes I've seen in a long time.

Mike
"oh, that monkey is going to pay"
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2005-04-14 : 11:56:27
Thanx

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page
   

- Advertisement -