Check for duplicates in the table before inserting data into SQL Server with ASP.NET

ado.netasp.netinsertsql-server-2000

I have got 3 columns in the table, I need to check the email field before insertion of new row, so if the email exist, it should not insert that row. Do I have to go through 2 queries to achieve that, so for instance first check :

Select count(*) FROM PRODUCT WHERE email = @email

AND THEN RUN AN INSERT

Insert INTO PRODUCT (....) VALUES (....)

Or is there any better way to achieve that ,

Any suggestions or advice will be appreciated .

Thanks

Best Solution

You can have an INSERT statement that checks for the condition, and only inserts something, if it doesn't already exist:

IF NOT EXISTS (SELECT * FROM dbo.Product WHERE email = @email)
   INSERT INTO dbo.Product(list of columns)
   VALUES(list of values)
Related Question