TABLOCK for both truncate and subsequent bulk insert I'd like to truncate a table and then run a bulk insert on it with some new data.
I tried a query something like this:
BEGIN TRANSACTION; 

TRUNCATE TABLE db.dbo.table;

BULK INSERT db.dbo.table 
FROM 'file.txt'
WITH (TABLOCK, FIELDTERMINATOR = ',', ROWTERMINATOR = '0x0a');

COMMIT TRANSACTION;

However it seems like I can query a time between the truncate and the insert. Granted this is a small time window but I would like to eliminate it entirely.
How can I edit the query so the TABLOCK (or better TABLOCKX?) locks the table from when the truncate begins to when the bulk insert ends?
The table lock is not taken until a statement containing that hint is executed. Since TRUNCATE cannot be hinted we must add another statement that can be. A SELECT will suffice. We want it to be fast so a restrictive predicate is good. Something like
begin transaction;

select primary_key
from dbo.table (tablockx)
where 1=2;

.. etc.

It would be nice if we could use sp_getapplock for this but it cannot, at the time of writing, address schema objects.