0

I have an attendance that is not supposed to check a user in more than once so before I check in a user I run the following query:

Cmd.CommandText = "SELECT * FROM [Attendance] 
WHERE [EID] = @EID AND 
[Location] = @loc AND 
[StormEvent] = @storm AND 
(
 (DATEDIFF(hh, [CheckIn], @checkIn)< 24) AND 
 (DATEDIFF(hh, [CheckIn], @checkIn)> -1) AND
 [CheckOut] Is NULL
)"

Most time it works but I find that when I have several users checking in employees at once I get duplicate records. How can I avoid this? I'm using transactions but that doesn't seem to do the trick.

3
  • Set transaction isolation as serializable. Sep 19, 2014 at 18:50
  • The select appears to be ascertaining if they have records in the past 24, but how are you doing the insert/update? Here's an example of a transaction, but may not fit your scenario Sep 19, 2014 at 18:52
  • 6
    You shouldn't do this with querying before, because between the time you query and the time you insert, another user could do the insert. It's best to have a UNIQUE constraint on what makes the record unique; that way the database enforces the rule. Then you can go ahead and do the INSERT without the pre-check. If the INSERT works you're all set. If there's a duplicate, the INSERT will throw and you can catch the exception and report a friendly "user is already checked in" message.
    – Ed Gibbs
    Sep 19, 2014 at 18:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.