I have a table like below:
EmpId DateTimeIn DateTimeOut Effort
1030 2016-12-01 07:30:00.000 2016-12-01 12:30:00.000 1030
1030 2016-12-01 13:30:00.000 2016-12-01 16:30:00.000 1531
The employee 1030
has inserted his effort on 2016-12-01 for the time ranges 07:30 to 12:30 and 13:30 to 16:30.
After this he should not enter his effort on these time ranges. He can enter effort prior 07:30 or 12:30 to 13:30 or beyond 16:30 for 2016-12-01 or can enter for any other day.
To achieve this I write a below query but it is always throwing error and not allowing users to insert data. Please help.
IF EXISTS (SELECT 1 FROM TimesheetEntries
WHERE EmpId = @EmpId AND (@DateTimeIn >= DateTimeIn AND @DateTimeIn < DateTimeOut)
OR (@DateTimeOut >= DateTimeOut AND @DateTimeOut < DateTimeIn))
BEGIN
RAISERROR ('You already input your effort for the given time range.',16,1)
RETURN
END