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)
 How to test if data exists?

Author  Topic 

brendalisalowe
Constraint Violating Yak Guru

269 Posts

Posted - 2004-08-09 : 16:32:34
How do you test to see if a CaseNumber already exists in the table or not?

This is what I have so far:

INSERT INTO tblHUDdata (CaseNumber,HoldingMor,ServingMor) Values ('" & HUDCaseNumber & "','" & HUDHoldMor & "','" & HUDServeMor & "'"

Do I use an IF Statement? Please help!

Brenda

Pat Phelan
Posting Yak Master

187 Posts

Posted - 2004-08-09 : 16:42:09
There are a couple of ways to skin that cat. The first way that I'd suggest is to use:
IF EXISTS (SELECT * // fill in the blanks)
INSERT INTO ( //more blanks
If you want something a bit more aggressive, I'd declare a UNIQUE constraint on the table, which will prevent duplicates from being entered (trying to insert them will cause a runtime error). I'd do both.

-PatP
Go to Top of Page
   

- Advertisement -