SQL Server 2008查询编辑器改变查询逻辑

发布于 2024-11-17 07:57:06 字数 490 浏览 3 评论 0原文

我在 SQL Server 2008 中手工编写了一个简单的 SQL,如下所示;

从 Tab1 WHERE 中选择 * A='1' AND (B='1' OR C='1');

由于懒惰,我在查询编辑器中打开此查询来验证语法,然后在对话框中按“确定”,而不进行任何更改。

我注意到查询编辑器已将我的查询更改为:

SELECT * FROM Tab1 WHERE A='1' AND (B='1') OR (C='1');

显然,这改变了SQL 逻辑并根据您执行的 SQL 逻辑返回不同的结果。

我经常使用查询编辑器来验证复杂查询的语法。所以有点担心这样的微妙变化会被忽视,但却会改变结果。

这是设计师的特色吗?我可以做些什么来改变这种行为吗?

编辑:感谢您指出编辑器所做的更改与上面不太一样,但尽管结果相同,但仍然修改了查询。

谢谢

I hand coded a simple SQL in SQL Server 2008 as below;

SELECT * FROM Tab1 WHERE
A='1' AND (B='1' OR C='1');

Being lazy I opened this query in the Query Editor to validate the syntax and pressed OK on the dialog without making any changes.

I noticed that the Query Editor had changed my query to:

SELECT * FROM Tab1 WHERE A='1' AND (B='1') OR (C='1');

clearly this changes the logic of the SQL and returns different results depending on which one you execute.

I routinely use the Query Editor to validate my syntax on complex queries. So a little worried that the a subtle change like this would go unotice, but would change the outcome.

Is this a feature of the designer? Is there something I can do to change this behavior?

EDIT: Thanks for pointing out that the changes made by the editor is not quite the same as above, but still the query is modified although the results are the same.

Thanks

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

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

发布评论

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

评论(2

淡紫姑娘! 2024-11-24 07:57:06

我尝试在查询设计器中复制此内容,但结果略有不同。我输入的内容与您相同:

SELECT * FROM Tab1 WHERE A='1' AND (B='1' OR C='1');

得到的结果是:

SELECT     *
FROM         Tab1
WHERE     (A = '1') AND (B = '1') OR
                      (A = '1') AND (C = '1')

我不得不说结果是相同的,但我们都可以看到这里有一条危险路。另外,我不喜欢 (A = '1') 复制。哎呀,我想要我编码的代码!

明智的一句话:我从不在 SQL Server Management Studio 中格式化我的查询。您是否看到它对您的视图代码做了什么?我恨它。我只是在其他地方编码并在完成后粘贴到短信中。

I tried to replicate this in the Query Designer and had a slightly different result. I typed the same as you:

SELECT * FROM Tab1 WHERE A='1' AND (B='1' OR C='1');

And got this:

SELECT     *
FROM         Tab1
WHERE     (A = '1') AND (B = '1') OR
                      (A = '1') AND (C = '1')

I have to say that the result is the same, but we can all see a dangerous road here. Also, I did not like the (A = '1') replication. Heck, I want the code how I coded it!

A word to the wise: I never format my queries in SQL Server Management Studio. Have you seen what it does to your view's code? I hate it. I just code somewhere else and paste in SMS when done.

记忆之渊 2024-11-24 07:57:06

该语句

SELECT * FROM Tab1 WHERE A='1' AND (B='1' OR C='1')

对我的解析是:

SELECT * FROM Tab1 WHERE (A='1') AND (B='1') OR (A='1') AND (C='1')

这是令人惊讶的正确,因为在 SQL Server TSQL 中,AND 运算符优先于 OR。这意味着上面的内容与下面的内容相同,因为 AND 运算符在 OR 运算符之前求值:

SELECT * FROM Tab1 WHERE ((A='1') AND (B='1')) OR ((A='1') AND (C='1'))

这与中使用的初始语句相同的问题。

有关详细信息,请参阅运算符优先级 (Transact-SQL)

The statement

SELECT * FROM Tab1 WHERE A='1' AND (B='1' OR C='1')

resolves for me to:

SELECT * FROM Tab1 WHERE (A='1') AND (B='1') OR (A='1') AND (C='1')

This is surprisingly correct, as in SQL Server TSQL the AND operator has precedence over OR. That means the above is the same like the following, because the AND-operator gets evaluated before the OR-operator:

SELECT * FROM Tab1 WHERE ((A='1') AND (B='1')) OR ((A='1') AND (C='1'))

And this is the same like the initial statement being used in the question.

See Operator Precedence (Transact-SQL) for details.

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