错误:以...开头的标识符最大长度为128

时间:2015-03-30 16:17:14

标签: sql sql-server

当我在指向SQL Server Express时尝试执行下面的SQL语句时出现错误

  

以'Select * FROM AvailabilityBlocks LEFT JOIN开头的标识符Location ON AvailabilityBlocks.LocationID = Location.LocationID WHERE((AvailabilityBlo'太长。最大长度为128.“错误。

SQL代码:

SELECT * 
FROM 
    (Resources 
LEFT JOIN 
    [Select * 
     FROM AvailabilityBlocks 
     LEFT JOIN 
         Location ON AvailabilityBlocks.LocationID = Location.LocationID 
     WHERE 
         ((AvailabilityBlocks.LocationID IN (8, 14, 16, 31, 1, 15, 17, 10, 9, 19, 12, 30, 5, 18, 13, 20, 3, 26, 2, 25, 28, 27, 32, 33) 
         AND (AvailabilityBlocks.Type = 3 OR AvailabilityBlocks.Type = 4)) 
          OR AvailabilityBlocks.Type = 2) 
         AND [Begin] < '06-Apr-2015 12:00:00 AM' 
         AND [Begin] >= '30-Mar-2015 12:00:00 AM' 
         AND (WeekDay([Begin]) = 2 OR WeekDay([Begin]) = 3 
          OR WeekDay([Begin]) = 4 OR WeekDay([Begin]) = 5 
          OR WeekDay([Begin]) = 6 OR WeekDay([Begin]) = 7)]. AS FilteredTable ON Resources.ResourceID = FilteredTable.ResourceID) 
LEFT JOIN 
   EmployeeTypes ON EmployeeTypes.TypeID = Resources.EmployeeType 
ORDER BY 
   RClass, Resources.LastName ASC, Resources.FirstName ASC, 
   Resources.ResourceID ASC, [AvailabilityBlocks.Begin] ASC, 
   [AvailabilityBlocks.End] Desc, Location.SubType DESC

当针对Access DB执行时,此SQL工作正常。任何人都可以提出任何建议吗?

提前致谢

2 个答案:

答案 0 :(得分:0)

您正在使用[而不是(对于已命名的子查询。我在下面修复它并添加了一些结构以便于阅读。

SELECT * FROM 
Resources 
  LEFT JOIN 
    (Select * FROM AvailabilityBlocks 
      LEFT JOIN Location ON AvailabilityBlocks.LocationID=Location.LocationID 
      WHERE ((AvailabilityBlocks.LocationID IN (8, 14, 16, 31, 1, 15, 17, 10, 9, 19, 12, 30, 5, 18, 13, 20, 3, 26, 2, 25, 28, 27, 32, 33) 
        AND (AvailabilityBlocks.Type = 3 OR AvailabilityBlocks.Type = 4)) OR AvailabilityBlocks.Type = 2) 
        AND [Begin] < '06-Apr-2015 12:00:00 AM' 
        And [Begin] >= '30-Mar-2015 12:00:00 AM' 
        AND (WeekDay([Begin])=2 
          OR WeekDay([Begin])=3 
          OR WeekDay([Begin])=4 
          OR WeekDay([Begin])=5 
          OR WeekDay([Begin])=6 
          OR WeekDay([Begin])=7)) AS FilteredTable 
        ON Resources.ResourceID=FilteredTable.ResourceID 
    LEFT JOIN EmployeeTypes ON EmployeeTypes.TypeID=Resources.EmployeeType 
ORDER BY RClass, Resources.LastName ASC, Resources.FirstName ASC, Resources.ResourceID ASC, AvailabilityBlocks.[Begin] ASC, AvailabilityBlocks.[End] Desc, Location.SubType DESC

答案 1 :(得分:0)

您的查询有一些问题。我会告诉你我认为你的查询应该是什么样子。但我相信您还需要弄清楚如何分解查询以找出错误

首先,确保左连接中的内部查询确实有效:

    SELECT ab.*  -- Note Here I only select ab.*, if you need any columns from Location then do loc. "column needed"
    FROM AvailabilityBlocks ab
    LEFT JOIN Location loc ON ab.LocationID = loc.LocationID 
    WHERE 
         ((ab.LocationID IN (8, 14, 16, 31, 1, 15, 17, 10, 9, 19, 12, 30, 5, 18, 13, 20, 3, 26, 2, 25, 28, 27, 32, 33) 
           AND (ab.Type = 3 OR ab.Type = 4)) 
           OR ab.Type = 2) 
         AND ab.Begin BETWEEN CAST('20150330' AS DATE)  AND CAST('20150406' AS DATE)
         AND (WeekDay([Begin]) IN ( 2,3,4,5,6,7)

现在,一旦验证了上层查询的工作原理,就可以将内部工作查询嵌入到左连接中(我假设大多数列都在可用性块内,如果ResourceId不是,则通过Loc.ResourceID添加它)。

SELECT r.*,et.* , FilteredTable.Begin, FilteredTable.End, FilteredTable.SubType
FROM Resources r
LEFT JOIN 
   (
        SELECT ab.* 
        FROM AvailabilityBlocks ab
        LEFT JOIN Location loc ON ab.LocationID = loc.LocationID 
        WHERE 
             ((ab.LocationID IN (8, 14, 16, 31, 1, 15, 17, 10, 9, 19, 12, 30, 5, 18, 13, 20, 3, 26, 2, 25, 28, 27, 32, 33) 
               AND (ab.Type = 3 OR ab.Type = 4)) 
               OR ab.Type = 2) 
             AND ab.Begin BETWEEN CAST('20150330' AS DATE)  AND CAST('20150406' AS DATE)
             AND (WeekDay([Begin]) IN ( 2,3,4,5,6,7)
    )AS FilteredTable ON r.ResourceID = FilteredTable.ResourceID 
LEFT JOIN EmployeeTypes et ON et.TypeID = r.EmployeeType 

最后在“order by”子句中使用适当的别名而不是表名

ORDER BY 
   RClass, -- What table has this?
   r.LastName ASC, 
   r.FirstName ASC, 
   r.ResourceID ASC, 
   FilteredTable.Begin ASC, 
   FilteredTable.End Desc, 
   FilteredTable.SubType DESC

能够突破您的查询以查看导致问题的原因是确定存在其他问题的关键(意味着不要指望人们的解决方案能给您答案,而是指导您找出解决方案在你自己)