Hi!
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?