SQL Server中的一个奇怪的操作问题:-100/-100 * 10 = 0 [英] A strange operation problem in SQL Server: -100/-100*10 = 0

查看:119
本文介绍了SQL Server中的一个奇怪的操作问题:-100/-100 * 10 = 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 如果执行SELECT -100/-100*10,则结果为0.
  • 如果执行SELECT (-100/-100)*10,则结果为10.
  • 如果执行SELECT -100/(-100*10),则结果为0.
  • 如果执行SELECT 100/100*10,则结果为10.
  • If you execute SELECT -100/-100*10 the result is 0.
  • If you execute SELECT (-100/-100)*10 the result is 10.
  • If you execute SELECT -100/(-100*10) the result is 0.
  • If you execute SELECT 100/100*10 the result is 10.

BOL 状态:

当表达式中的两个运算符具有相同的运算符优先级时,将根据它们在表达式中的位置从左到右进行求值.

When two operators in an expression have the same operator precedence level, they are evaluated left to right based on their position in the expression.

还有

Level   Operators
  1     ~ (Bitwise NOT)
  2     * (Multiplication), / (Division), % (Modulus)
  3     + (Positive), - (Negative), + (Addition), + (Concatenation), - (Subtraction), & (Bitwise AND), ^ (Bitwise Exclusive OR), | (Bitwise OR)

BOL是错了,还是我错过了什么?似乎-放弃了(预期的)优先级.

Is BOL wrong, or am I missing something? It seems the - is throwing the (expected) precedence off.

推荐答案

根据优先级表,此预期的行为.优先级较高的运算符(/*)在优先级较低的运算符(一元-)之前进行评估.因此:

According to the precedence table, this is the expected behavior. The operator with higher precedence (/ and *) is evaluated before operator with lower precedence (unary -). So this:

-100 / -100 * 10

被评估为:

-(100 / -(100 * 10))

请注意,此行为与大多数编程语言不同,在大多数编程语言中,一元求反的优先级高于乘法和除法,例如 VB JavaScript .

Note that this behavior is different from most programming languages where unary negation has higher precedence than multiplication and division e.g. VB, JavaScript.

这篇关于SQL Server中的一个奇怪的操作问题:-100/-100 * 10 = 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆