From 956829359412e8a37be21e65859ed640f4ee12bf Mon Sep 17 00:00:00 2001 From: Andrea Piovanelli Date: Thu, 11 Apr 2024 16:04:56 +0200 Subject: [PATCH] WITH(NOLOCK) positioned before the where clause if there is no alias for the table. --- src/Orchard/Data/NoLockInterceptor.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Orchard/Data/NoLockInterceptor.cs b/src/Orchard/Data/NoLockInterceptor.cs index 91da20f2c31..012e64c62d2 100644 --- a/src/Orchard/Data/NoLockInterceptor.cs +++ b/src/Orchard/Data/NoLockInterceptor.cs @@ -240,7 +240,13 @@ private string AddNoLockHints(string query, IEnumerable tableNames) { if (tableIndex == fromIndex + 1 || parts[tableIndex - 1].Equals(",")) { - parts.Insert(tableIndex + 2, "WITH(NOLOCK)"); + if (parts[tableIndex+1].Equals("where", StringComparison.OrdinalIgnoreCase)) { + // There is no alias in the query, so we add "WITH(NOLOCK)" immediately after table name but before the "where" clause. + parts.Insert(tableIndex + 1, "WITH(NOLOCK)"); + } else { + // We add "WITH(NOLOCK)" after the table alias + parts.Insert(tableIndex + 2, "WITH(NOLOCK)"); + } } else { // probably doing a join, so edit the next "on" and make it // "WITH (NOLOCK) on"