I have a table with a column named Time of type datetime2.
I need to run this entire query without anyone else reading or altering data in the table while its executing.
IF NOT EXISTS (SELECT Id FROM Bookings WHERE Time >= '2016-02-02 09:00:00' AND Time < '2016-02-02 11:00:00')
BEGIN
-- Insert some data
END
The query is inside a transaction in an asp.NET application, but i am not sure if that is enough. Do i need to apply locking hints or set an isolation level? If so, how would I do that?
I've read about TABLOCK, and it seems to be doing what I want, but is it overkill? And where would I specify that a TABLOCK is wanted for the entire query and not just the select statement?