SQL Server:WHERE 子句中带有子查询的布尔表达式
我在子查询的 WHERE 子句中有一个子查询。尽管存在数据库的设计问题(不是我的工作),但在尝试扩展此表达式中的顶级 WHERE 子句时,我遇到了一些奇怪的错误。下面的例子有效。
LEFT OUTER JOIN CargoVoyageLocation on CargoVoyageLocation.VoyageLocationID
= (SELECT TOP 1 CargoVoyageLocation.VoyageLocationID FROM CargoVoyageLocation
JOIN Cargo on CargoVoyageLocation.CargoID = Cargo.CargoID
JOIN CargoType on Cargo.CargoTypeID = CargoType.CargoTypeID
WHERE
(CargoType.SystemName = 'HAZMAT' OR CargoType.SystemName = 'BUNKERS' OR Cargo.IsBunkers = 1)
AND
CargoVoyageLocation.VoyageLocationID
= (SELECT VoyageLocationAttribute.VoyageLocationID FROM VoyageLocationAttribute
JOIN VoyageLocationAttributeName
ON VoyageLocationAttribute.VoyageLocationAttributeNameID = VoyageLocationAttributeName.VoyageLocationAttributeNameID
WHERE VoyageLocationAttribute.VoyageLocationAttributeNameID = (SELECT VoyageLocationAttributeNameID FROM VoyageLocationAttributeName WHERE SystemName = 'PREVIOUS_VOYAGE_ID')
AND VoyageLocationAttribute.AttributeInt = v.VoyageID) ORDER BY CargoVoyageLocation.ModifiedDate DESC)
然后我添加一个简单的布尔表达式和括号:
LEFT OUTER JOIN CargoVoyageLocation on CargoVoyageLocation.VoyageLocationID
= (SELECT TOP 1 CargoVoyageLocation.VoyageLocationID FROM CargoVoyageLocation
JOIN Cargo on CargoVoyageLocation.CargoID = Cargo.CargoID
JOIN CargoType on Cargo.CargoTypeID = CargoType.CargoTypeID
WHERE
(CargoType.SystemName = 'HAZMAT' OR CargoType.SystemName = 'BUNKERS' OR Cargo.IsBunkers = 1)
AND
Here ---> (1=0 OR CargoVoyageLocation.VoyageLocationID
= (SELECT VoyageLocationAttribute.VoyageLocationID FROM VoyageLocationAttribute
JOIN VoyageLocationAttributeName
ON VoyageLocationAttribute.VoyageLocationAttributeNameID = VoyageLocationAttributeName.VoyageLocationAttributeNameID
WHERE VoyageLocationAttribute.VoyageLocationAttributeNameID = (SELECT VoyageLocationAttributeNameID FROM VoyageLocationAttributeName WHERE SystemName = 'PREVIOUS_VOYAGE_ID')
AND VoyageLocationAttribute.AttributeInt = v.VoyageID) ORDER BY CargoVoyageLocation.ModifiedDate DESC))
这会产生以下错误:
- 关键字“ORDER”附近的语法不正确。
- “v”附近的语法不正确。
我很困惑。这是怎么回事?
I have a subquery within a WHERE-clause within a subquery. Notwithstanding the design issues of the database (not my job), I am getting some strange errors when trying to extend the top level WHERE-clause in this expression. The example below WORKS.
LEFT OUTER JOIN CargoVoyageLocation on CargoVoyageLocation.VoyageLocationID
= (SELECT TOP 1 CargoVoyageLocation.VoyageLocationID FROM CargoVoyageLocation
JOIN Cargo on CargoVoyageLocation.CargoID = Cargo.CargoID
JOIN CargoType on Cargo.CargoTypeID = CargoType.CargoTypeID
WHERE
(CargoType.SystemName = 'HAZMAT' OR CargoType.SystemName = 'BUNKERS' OR Cargo.IsBunkers = 1)
AND
CargoVoyageLocation.VoyageLocationID
= (SELECT VoyageLocationAttribute.VoyageLocationID FROM VoyageLocationAttribute
JOIN VoyageLocationAttributeName
ON VoyageLocationAttribute.VoyageLocationAttributeNameID = VoyageLocationAttributeName.VoyageLocationAttributeNameID
WHERE VoyageLocationAttribute.VoyageLocationAttributeNameID = (SELECT VoyageLocationAttributeNameID FROM VoyageLocationAttributeName WHERE SystemName = 'PREVIOUS_VOYAGE_ID')
AND VoyageLocationAttribute.AttributeInt = v.VoyageID) ORDER BY CargoVoyageLocation.ModifiedDate DESC)
Then I add a simple boolean expression and parenthesis:
LEFT OUTER JOIN CargoVoyageLocation on CargoVoyageLocation.VoyageLocationID
= (SELECT TOP 1 CargoVoyageLocation.VoyageLocationID FROM CargoVoyageLocation
JOIN Cargo on CargoVoyageLocation.CargoID = Cargo.CargoID
JOIN CargoType on Cargo.CargoTypeID = CargoType.CargoTypeID
WHERE
(CargoType.SystemName = 'HAZMAT' OR CargoType.SystemName = 'BUNKERS' OR Cargo.IsBunkers = 1)
AND
Here ---> (1=0 OR CargoVoyageLocation.VoyageLocationID
= (SELECT VoyageLocationAttribute.VoyageLocationID FROM VoyageLocationAttribute
JOIN VoyageLocationAttributeName
ON VoyageLocationAttribute.VoyageLocationAttributeNameID = VoyageLocationAttributeName.VoyageLocationAttributeNameID
WHERE VoyageLocationAttribute.VoyageLocationAttributeNameID = (SELECT VoyageLocationAttributeNameID FROM VoyageLocationAttributeName WHERE SystemName = 'PREVIOUS_VOYAGE_ID')
AND VoyageLocationAttribute.AttributeInt = v.VoyageID) ORDER BY CargoVoyageLocation.ModifiedDate DESC))
This produces the following error:
- Incorrect syntax near the keyword 'ORDER'.
- Incorrect syntax near 'v'.
I'm stumped. What is going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ORDER BY 子句正在被 TOP 1 (第 2 行)使用,所以我怀疑你的括号是错误的,最后一行应该是:
That ORDER BY clause is being used by the TOP 1 (line 2), so I suspect your bracketing is wrong, and the last line should be: