SQL Server 2008查询编辑器改变查询逻辑
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试在查询设计器中复制此内容,但结果略有不同。我输入的内容与您相同:
得到的结果是:
我不得不说结果是相同的,但我们都可以看到这里有一条危险路。另外,我不喜欢
(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:
And got this:
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.
该语句
对我的解析是:
这是令人惊讶的正确,因为在 SQL Server TSQL 中,
AND
运算符优先于OR
。这意味着上面的内容与下面的内容相同,因为AND
运算符在OR
运算符之前求值:这与中使用的初始语句相同的问题。
有关详细信息,请参阅运算符优先级 (Transact-SQL)。
The statement
resolves for me to:
This is surprisingly correct, as in SQL Server TSQL the
AND
operator has precedence overOR
. That means the above is the same like the following, because theAND
-operator gets evaluated before theOR
-operator:And this is the same like the initial statement being used in the question.
See Operator Precedence (Transact-SQL) for details.