我有一个数据库查询:
DECLARE @Pager_PageNumber AS INT, @Pager_PageSize AS INT; SET @Pager_PageNumber = 1; SET @Pager_PageSize = 12; SELECT [Name], [Description], [Table1ID], [VersionNo], [Status] FROM (SELECT CAST(Table1.name AS VARCHAR(MAX)) As [Name], CAST(Table1.description AS VARCHAR(MAX)) AS [Description], CAST(CAST(Table1.Table1_ID AS DECIMAL(18,0)) AS VARCHAR(MAX)) AS [Table1ID], CAST(CAST(Table1.VERSION_NO AS DECIMAL(18,0)) AS VARCHAR(MAX)) AS [VersionNo], CAST(Table2.br_status AS VARCHAR(MAX)) AS [Status] FROM Table1 WITH (NOLOCK) INNER JOIN (SELECT Table1_id, MAX(version_no) as version_no FROM Table1 WHERE Table1.status = '00002' GROUP BY Table1_id) AS BR WITH (NOLOCK) ON Table1.Table1_id = BR.Table1_id AND BR.version_no = Table1.version_no INNER JOIN Table2 WITH (NOLOCK) ON Table1.status = Table2.br_status_code) A ORDER BY [Name], [Description], [Table1ID], [VersionNo], [Status] OFFSET ((@Pager_PageNumber - 1) * @Pager_PageSize) ROWS FETCH NEXT @Pager_PageSize ROWS ONLY; SELECT COUNT(*) FROM (SELECT CAST(Table1.name AS VARCHAR(MAX)) AS [Name], CAST(Table1.description AS VARCHAR(MAX)) AS [Description], CAST(CAST(Table1.Table1_ID AS DECIMAL(18,0)) AS VARCHAR(MAX)) AS [Table1ID], CAST(CAST(Table1.VERSION_NO AS DECIMAL(18,0)) AS VARCHAR(MAX)) As [VersionNo], CAST(Table2.br_status AS VARCHAR(MAX)) AS [Status] FROM Table1 WITH (NOLOCK) INNER JOIN (SELECT Table1_id, MAX(version_no) as version_no FROM Table1 WHERE Table1.status = '00002' GROUP BY Table1_id) AS BR WITH (NOLOCK) ON Table1.Table1_id = BR.Table1_id AND BR.version_no = Table1.version_no INNER JOIN Table2 WITH (NOLOCK) ON Table1.status = Table2.br_status_code) A;
在SQL Server中,我得到附近的错误BR WITH (NOLOCK)
:
关键字“ WITH”附近的语法不正确。关键字“ with”附近的语法不正确。如果此语句是公用表表达式,xmlnamespaces子句或更改跟踪上下文子句,则前一条语句必须以分号终止。
但是据我从语法这样的来源了解到
SELECT first_name, last_name, FROM dbo.person p WITH (NOLOCK) JOIN dbo.employee e WITH (NOLOCK) ON e.person_id = p.person_id WHERE p.person_id = 1;
因此,我的查询看起来差不多正确。
另外,当我删除BR WITH (NOLOCK)
内部联接查询旁边的WITH(NOLOCK)时,查询运行良好。关于我可能会缺少的任何想法?
PS:我的数据库兼容性级别是110。
您适用with (nolock)
于表,而不适用于子查询。因此,代替:
(SELECT Table1_id, MAX(version_no) as version_no FROM Table1 where Table1.status='00002' GROUP BY Table1_id ) as BR WITH (NOLOCK)
你会这样写:
(SELECT Table1_id, MAX(version_no) as version_no FROM Table1 WITH (NOLOCK) where Table1.status='00002' GROUP BY Table1_id ) BR