我收到“')'附近的语法不正确”

时间:2019-08-01 13:28:29

标签: sql-server select

我在存储过程中编写了此查询,然后从C#调用它。很好。

WITH SelectedRows AS
(
     SELECT 
         ROW_NUMBER() OVER (ORDER BY InnerDataTable.DepotCode ASC) AS RowNo, 
         InnerDataTable.* 
     FROM
         (SELECT DISTINCT 
              Date, No, Person.Title AS PersonTitle, Person.Name AS PersonName, 
              (... some other columns from tables ...)
          FROM 
              SellInvoice
          JOIN 
              SellInvoiceDetail AS Detail ON Detail.SellInvoiceGuid = SellInvoice.Guid
          JOIN 
              Depot ON Depot.Guid = SellInvoice.DepotGuid
          JOIN 
              Item ON Item.Guid = Detail.ItemGuid
          ( ... and something else... )) AS InnerDataTable  
     WHERE 
         InnerDataTable.Company = '391891BA-DCC9-4B76-9588-9C97E0C2071A' 
         AND InnerDataTable.Branch = 'A2C9055A-7DDD-41AF-9A2F-DFC42D6C2B14'
)

但是当我在SQL Server Management Studio中对其进行测试时,执行失败并且显示了错误:

  

')'附近的语法不正确

是指最后一行。那是什么错误..?

1 个答案:

答案 0 :(得分:2)

您缺少CTE中的实际SELECT。有关如何使用CTE(公用表表达式)的更多指导,请参见the Microsoft documentation

WITH SelectedRows AS
(
     SELECT 
         ROW_NUMBER() OVER (ORDER BY InnerDataTable.DepotCode ASC) AS RowNo, 
         InnerDataTable.* 
     FROM
         (SELECT DISTINCT 
              Date, No, Person.Title AS PersonTitle, Person.Name AS PersonName, 
              (... some other columns from tables ...)
          FROM 
              SellInvoice
          JOIN 
              SellInvoiceDetail AS Detail ON Detail.SellInvoiceGuid = SellInvoice.Guid
          JOIN 
              Depot ON Depot.Guid = SellInvoice.DepotGuid
          JOIN 
              Item ON Item.Guid = Detail.ItemGuid
          ( ... and something else... )) AS InnerDataTable  
     WHERE 
         InnerDataTable.Company = '391891BA-DCC9-4B76-9588-9C97E0C2071A' 
         AND InnerDataTable.Branch = 'A2C9055A-7DDD-41AF-9A2F-DFC42D6C2B14'
)
SELECT * -- Added
FROM SelectedRows -- Added

也许您只是没有在上面的查询中包含此内容,但这似乎是SQL Server在最后一行返回Incorrect syntax near ')'的充分理由。