Skip to content

HOLD

BEGIN TRANSACTION;
DECLARE @tempTable TABLE (line VARCHAR(255));
INSERT INTO @tempTable
EXEC ('SELECT TOP 5 name FROM sys.objects WITH (HOLDLOCK) ORDER BY name');
SELECT * FROM @tempTable;
COMMIT;
line
----
sys.all_columns
sys.all_objects
sys.all_parameters
sys.all_sql_modules
sys.all_views

In the provided example, a transaction is opened, and a hold lock is applied to the sys.objects table during a SELECT query. The top 5 records are selected and stored in a temporary table. The contents of this table are then displayed, and the transaction is committed. The HOLDLOCK ensures the associated resources are held for the duration of the transaction.