This is really easy with Adam Machanic's free tool, sp_WhoIsActive.
You have to either catch a query when it's running, or in a transaction. For instance:
USE StackOverflow;
BEGIN TRAN;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
SELECT COUNT(*)
FROM dbo.Posts AS p
JOIN dbo.Users AS u
ON u.Id = p.OwnerUserId
WHERE p.OwnerUserId >= 0;
Then you want to run this command (from another tab, or use EXEC dbo.sp_WhoIsActive @get_locks = 1, @show_own_spid = 1
from the same tab):
EXEC dbo.sp_WhoIsActive @get_locks = 1
If you click on the locks column, you'll get output like this:
<Database name="StackOverflow">
<Locks>
<Lock request_mode="S" request_status="GRANT" request_count="1" />
</Locks>
<Objects>
<Object name="Posts" schema_name="dbo">
<Locks>
<Lock resource_type="OBJECT" request_mode="S" request_status="GRANT" request_count="1" />
</Locks>
</Object>
<Object name="Users" schema_name="dbo">
<Locks>
<Lock resource_type="OBJECT" request_mode="IS" request_status="GRANT" request_count="1" />
<Lock resource_type="PAGE" page_type="*" index_name="ix_Users_Helper" request_mode="S" request_status="GRANT" request_count="24725" />
</Locks>
</Object>
</Objects>
</Database>
The request_count
should tell you the number of locks.