What is the meaning of this SQL statement?

I am new to SQL translation. What is the meaning of the following statement?

BEGIN TRAN
-- xlock the transaction
IF EXISTS (SELECT 1 FROM dbo.ActiveTransaction WITH (XLOCK) WHERE TransactionId = @transactionId)
BEGIN
(Omitted)
END
COMMIT TRAN

Thank!

+3
source share
1 answer

What's going on here:

  • Sql transaction started
  • You are checking to see if there is a table dbo.ActiveTransactioncontaining the record where TransactionIdalue is in the @transactionid variable.
    • If yes, you do the "(omitted)" code
  • Any COMMIT'ed changes made to the database

"XLOCK" means that :

Indicates that exclusive locks should be taken and held until the transaction is completed. If set using ROWLOCK, PAGLOCK or TABLOCK, exclusive locks apply to the appropriate level of detail.

+7
source

Source: https://habr.com/ru/post/1792997/


All Articles