SQL Server – Lock a Table Without Returning Results

blockingsql serversql-server-2008-r2

In an update script I'm locking several tables.

BEGIN TRANSACTION
    SELECT Top 0 Null FROM TableA with (holdlock, tablockx)
    SELECT Top 0 Null FROM TableB with (holdlock, tablockx)
    ...

I want to suppress results for it in order to focus on real script results. Is there an elegant way to xlock a table without getting a result set?

empty result in ssms

Best Answer

There may be other ways, but this technique seems simple enough:

SET NOCOUNT ON
BEGIN TRANSACTION
declare @DummyVariable INT
SET @DummyVariable = (SELECT  Top 0 Null FROM Test1 with (holdlock, tablockx))