2017-05-24 102 views
0

如何使用WITH(NOLOCK)查询此查询。我知道如何使用它来进行正常的选择查询。但对于连接查询?任何一个引导我如何在查询中使用WITH(NOLOCK)加入

SELECT 
    DISTINCT Amendmentdetails.BadgeNo, ContractNo,isnull(Amendmentdetails.ContractAmendmentNo,'')AS ContractAmendmentNo, 
    value As AnnualSalary, 
    Amendmentdetails.ContractType,TimesheetCategory,Rotation,RM.CRotDayOn,RM.CRotDayOff ,TSCatDays       
    from Amendmentdetails 
    Left Join 
    RotationMaster RM 
    On 
    Amendmentdetails.Rotation =RM.CRotCode 
    Left Join 
    TimesheetCategoryMaster TM 
    On 
    Amendmentdetails.TimesheetCategory=TM.TSCatCode 
    Left Join 
    SalaryDetails 
    On 
    SalaryDetails.contractAmendmentNo =Amendmentdetails.ContractAmendmentNo AND 
    Paycode in(1001,1002,1003,1004,1005) 
+3

'随着(NOLOCK)'别名后在'JOIN'的'ON'之前去。任何MS文档都会告诉你。 –

回答

1

与(nolock)如果没有别名就会在表名后面。如果有一个别名,它会在那之后进行。

SELECT 
    DISTINCT Amendmentdetails.BadgeNo, ContractNo,isnull(Amendmentdetails.ContractAmendmentNo,'')AS ContractAmendmentNo, 
    value As AnnualSalary, 
    Amendmentdetails.ContractType,TimesheetCategory,Rotation,RM.CRotDayOn,RM.CRotDayOff ,TSCatDays       
    from Amendmentdetails with (nolock) 
    Left Join RotationMaster RM with (nolock) On Amendmentdetails.Rotation =RM.CRotCode 
    Left Join TimesheetCategoryMaster TM with (nolock) On Amendmentdetails.TimesheetCategory=TM.TSCatCode 
    Left Join SalaryDetails with (nolock) On 
    SalaryDetails.contractAmendmentNo =Amendmentdetails.ContractAmendmentNo AND 
    Paycode in(1001,1002,1003,1004,1005) 

你可以阅读更多有关表提示的位置:https://docs.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-table

0

您可以将表/别名,之后添加NOLOCK。

SELECT t1.col1, t2.col1 
FROM table1 t1 NOLOCK 
INNER JOIN table2 t2 NOLOCK ON t1.x = t2.x 
WHERE t1.col2 = 42 

关于NOLOCK的事情是,它不是它似乎是魔法子弹。它基本上是READUNCOMMITTED,所以你可以读取在你选择它后立即改变的脏数据。如果这不是问题,那么继续使用它,但如果脏数据会影响您的最终结果,那么您可能需要以其他方式来处理阻止问题。

另见:

https://blogs.msdn.microsoft.com/davidlean/2009/04/05/sql-server-nolock-hint-other-poor-ideas/

http://itknowledgeexchange.techtarget.com/sql-server/what-does-with-nolock-actually-mean/

相关问题