Discussion:
XLOCK and ROWLOCK
(too old to reply)
slonk0
2004-07-02 07:08:50 UTC
Permalink
Hello,

I need to lock only one row with exclusive lock (nobody else can see or
modify this row), but when I use "with (xlock, rowlock)" it doesn't work.
XLOCK always locks whole table.

Can anybody help me?

Thanks in advance.
Magda
Erland Sommarskog
2004-07-03 15:37:49 UTC
Permalink
[posted and mailed, please reply in news]
Post by slonk0
I need to lock only one row with exclusive lock (nobody else can see or
modify this row), but when I use "with (xlock, rowlock)" it doesn't work.
XLOCK always locks whole table.
For row locks to be possible, there must be an index that SQL Server
can use. If you say:

SELECT * FROM tbl WITH (XLOCK, ROWLOCK) WHERE col = 19

and there is no index on col, SQL Server needs to scan the whole table
to find the value.
--
Erland Sommarskog, SQL Server MVP, ***@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
Loading...