2021年4月11日星期日

A SELECT inside of a transaction could lock the table?

I would like to know if it's possible that a select is blocking a table if it's inside a transaction.

It's something like this:

CREATE PROCEDURE InsertClient (@name   NVARCHAR(256))  AS  BEGIN    DECLARE @id INT = 0;      BEGIN TRY       BEGIN TRAN InsertingClient        SELECT @id = MAX(ID) + 1 FROM Clients;          INSERT INTO Clients(Id, Name) VALUES(@id, @name);          SELECT id, name FROM Clients;      COMMIT TRAN InsertingClient    END TRY    BEGIN CATCH             ROLLBACK TRAN InsertingClient    END CATCH;  END  

It's a dummy example, but if there's a lot of records in that table, and an API is receiving a lot of requests and calling this SP, could be blocked by the initial and final select? Should I use the begin and commit only in the insert to avoid the block?

Thanks!

https://stackoverflow.com/questions/67051443/a-select-inside-of-a-transaction-could-lock-the-table April 12, 2021 at 08:44AM

没有评论:

发表评论