syntax for nolock in sql

前端 未结 5 854
感动是毒
感动是毒 2020-12-30 01:15

I have seen sql statements using nolock and with(nolock) e.g -

select * from table1 nolock where column1 > 10

AND

selec         


        
5条回答
  •  滥情空心
    2020-12-30 01:59

    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" became the alias, within that query, of table1.

    select * from table1 with(nolock) where column1 > 10
    

    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
    

    when you're done.

提交回复
热议问题