SQL Server:WHERE 子句中带有子查询的布尔表达式

发布于 2024-09-27 07:10:13 字数 2945 浏览 4 评论 0原文

我在子查询的 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))

这会产生以下错误:

  1. 关键字“ORDER”附近的语法不正确。
  2. “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:

  1. Incorrect syntax near the keyword 'ORDER'.
  2. Incorrect syntax near 'v'.

I'm stumped. What is going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽清风入怀 2024-10-04 07:10:13

ORDER BY 子句正在被 TOP 1 (第 2 行)使用,所以我怀疑你的括号是错误的,最后一行应该是:

AND VoyageLocationAttribute.AttributeInt = v.VoyageID)) ORDER BY CargoVoyageLocation.ModifiedDate DESC)

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:

AND VoyageLocationAttribute.AttributeInt = v.VoyageID)) ORDER BY CargoVoyageLocation.ModifiedDate DESC)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文