我在子查询中的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))
这会产生以下错误:
我很难过。这是怎么回事?
答案 0 :(得分:2)
TOP 1(第2行)正在使用ORDER BY子句,所以我怀疑你的包围是错误的,最后一行应该是:
AND VoyageLocationAttribute.AttributeInt = v.VoyageID)) ORDER BY CargoVoyageLocation.ModifiedDate DESC)