Saturday, March 24, 2012

What is the best way to check for dupicate record before inserting?

I have a list of data. And I have some feilds and a ADD button.

I do not want the user to insert for example the same MONTH and YEAR as a existing
record that has that MONTH and YEAR already.

Should I just INSERT and will SQL send back a error? Or should I create a seperate SQL stored proedure to check if the MONTH/YEAR values are already in the database? Or is there a better way ?You could really do it either way. For SQL to return an error you'd need to have a unique index set up on the fields that can't have duplicates. Then, you'd want to make sure and use a try/catch when you execute the stored procedure so if it throws an error you could handle it more elegantly.

If it were me, I'd probably just run a seperate query like you stated and do a check first. I'm not sure which would yield the best performance.
Write a stored procedure which will return a boolean value
if value = 1 then inserted if value = -1 then not.

then while inserting use the exist statement while inserting. if it exist returns true there is a row if not no row and then it gets updated.

-jai
I went ahead and went with using th eexists in the insert stored procedure. This way saves writting a new SP.

0 comments:

Post a Comment