问题描述
我见过使用 nolock 和 with(nolock) 的 sql 语句例如 -
I have seen sql statements using nolock and with(nolock)e.g -
select * from table1 nolock where column1 > 10
和
select * from table1 with(nolock) where column1 > 10
以上哪些说法是正确的,为什么?
Which of the above statements is correct and why?
推荐答案
第一条语句没有锁定任何东西,而第二条语句有.当我刚刚在 SQL Server 2005 上对此进行测试时,
The first statement doesn't lock anything, whereas the second one does. When I tested this out just now on SQL Server 2005, in
select * from table1 nolock where column1 > 10 --INCORRECT
nolock"成为该查询中 table1 的别名.
"nolock" became the alias, within that query, of table1.
select * from table1 with(nolock) where column1 > 10
执行所需的 nolock 功能.持怀疑态度?在单独的窗口中,运行
performs the desired nolock functionality. Skeptical? In a separate window, run
BEGIN TRANSACTION
UPDATE tabl1
set SomeColumn = 'x' + SomeColumn
锁定表,然后在其自己的窗口中尝试每个锁定语句.第一个将挂起,等待锁被释放,第二个将立即运行(并显示脏数据").不要忘记发出
to lock the table, and then try each locking statement in its own window. The first will hang, waiting for the lock to be released, and the second will run immediately (and show the "dirty data"). Don't forget to issue
ROLLBACK
完成后.
这篇关于sql 中 nolock 的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!