Reputation: 105
I have question on lock on table in SQL Server while inserting data using multiple processes at a single time into same table.
Here are my questions on this,
Please help me to understand the same.
Upvotes: 6
Views: 17383
Reputation: 65217
It depends on the size of the table and settings on the clustered index if one exists. If the CI has page locks and row locks allowed, then those are the defaults. You will see lock escalation if multiple pages are locked, potentially up to a table lock.
If you want to explicitly lock the table, you can use the TABLOCK
hint on the insert (INSERT INTO MyTABLE WITH (TABLOCK)...
I'm not sure what you mean here.
Upvotes: 6
Reputation: 755371
Is it default behavior of SQL server to lock table while doing insert?
No. SQL Server by default locks by row - so new rows being inserted are locked - but not the whole table.
This will change if you insert more than 5000 rows in a single transaction. In that case, keeping that many individual locks just becomes too much and SQL Server will do a lock escalation and lock the entire table instead.
Upvotes: 14