sql 中 nolock 的语法 [英] syntax for nolock in sql

查看:44
本文介绍了sql 中 nolock 的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过使用 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 的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆