我应该在 T-SQL 中使用 != 还是 <> 表示不相等?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/723195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:37:58  来源:igfitidea点击:

Should I use != or <> for not equal in T-SQL?

sqlsql-servertsql

提问by Bob The Janitor

I have seen SQLthat uses both !=and <>for not equal. What is the preferred syntax and why?

我已经看到SQL使用 both!=<>for not equal。什么是首选语法,为什么?

I like !=, because <>reminds me of Visual Basic.

我喜欢!=,因为<>让我想起Visual Basic

采纳答案by DBAndrew

Technically they function the same if you're using SQL Server AKA T-SQL. If you're using it in stored procedures there is no performance reason to use one over the other. It then comes down to personal preference. I prefer to use <> as it is ANSI compliant.

从技术上讲,如果您使用 SQL Server AKA T-SQL,它们的功能相同。如果您在存储过程中使用它,则没有性能上的理由使用一个而不是另一个。然后归结为个人喜好。我更喜欢使用 <>,因为它符合 ANSI 标准。

You can find links to the various ANSI standards at...

您可以在以下位置找到各种 ANSI 标准的链接...

http://en.wikipedia.org/wiki/SQL

http://en.wikipedia.org/wiki/SQL

回答by Bill Karwin

Most databases support !=(popular programming languages) and <>(ANSI).

大多数数据库支持!=(流行的编程语言)和<>(ANSI)。

Databases that support both !=and <>:

支持!=和 的数据库<>

Databases that support the ANSI standard operator, exclusively:

支持ANSI标准算,数据库独家

  • IBM DB2 UDB 9.5: <>
  • Microsoft Access 2010: <>
  • IBM DB2 UDB 9.5: <>
  • 微软访问 2010: <>

回答by Adam Lassek

'<>'is from the SQL-92 standardand '!='is a proprietaryT-SQL operator. It's available in other databases as well, but since it isn't standard you have to take it on a case-by-case basis.

'<>'来自SQL-92 标准'!='专有的T-SQL 运算符。它也可以在其他数据库中使用,但由于它不是标准的,因此您必须根据具体情况进行处理。

In most cases, you'll know what database you're connecting to so this isn't really an issue. At worst you might have to do a search and replace in your SQL.

在大多数情况下,您会知道要连接到哪个数据库,因此这不是真正的问题。在最坏的情况下,您可能不得不在 SQL 中进行搜索和替换。

回答by Mehrdad Afshari

The ANSI SQL Standard defines <>as the "not equal to" operator,

ANSI SQL 标准定义<>为“不等于”运算符,

http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt(5.2 <token> and <separator>)

http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt( 5.2 <token> and <separator>)

There is no !=operator according to the ANSI/SQL 92 standard.

!=根据 ANSI/SQL 92 标准,没有运算符。

回答by Justin Niessner

<>is the valid SQL according to the SQL-92 standard.

<>是符合 SQL-92 标准的有效 SQL。

http://msdn.microsoft.com/en-us/library/aa276846(SQL.80).aspx

http://msdn.microsoft.com/en-us/library/aa276846(SQL.80).aspx

回答by Kyle

It seems that Microsoft themselves prefer <>to !=as evidenced in their table constraints. I personally prefer using !=because I clearly read that as "not equal", but if you enter [field1 != field2]and save it as a constrait, the next time you query it, it will show up as [field1 <> field2]. This says to me that the correct way to do it is <>.

看来微软自己更喜欢<>!=证明其表约束。我个人更喜欢使用,!=因为我清楚地将其读为“不等于”,但是如果您输入[field1 != field2]并将其保存为约束,则下次查询时,它将显示为[field1 <> field2]. 这对我说,正确的做法是<>.

回答by Fat Albert

!=, despite being non-ANSI, is more in the true spirit of SQL as a readable language. It screams not equal. <>says it's to me (less than, greater than) which is just weird. I know the intention is that it's either less than or greater than hence not equal, but that's a really complicated way of saying something really simple.

!=,尽管不是 ANSI,但更符合 SQL 作为一种可读语言的真正精神。它尖叫着不平等。 <>说这对我来说(小于,大于)这很奇怪。我知道意图是它要么小于要么大于因此不等于,但这是一种非常复杂的表达方式,非常简单。

I've just had to take some long SQL queries and place them lovingly into an XML file for a whole bunch of stupid reasons I won't go into.

我只需要进行一些长 SQL 查询并将它们亲切地放入一个 XML 文件中,因为我不会讨论的一大堆愚蠢的原因。

Suffice to say XML is not down with <>at all and I had to change them to !=and check myself before I riggedy wrecked myself.

可以说 XML 完全没有问题<>,我必须将它们更改为!=并检查自己,然后才能操纵自己。

回答by Steve

You can use whichever you like in T-SQL. The documentationsays they both function the same way. I prefer !=, because it reads "not equal" to my (C/C++/C# based) mind, but database gurus seem to prefer <>.

你可以在 T-SQL 中使用任何你喜欢的。文档说它们的功能相同。我更喜欢!=,因为它读起来“不等于”我(基于 C/C++/C#)的想法,但数据库专家似乎更喜欢<>.

回答by Karl

I understand that the C syntax !=is in SQL Server due to its Unix heritage (back in the Sybase SQL Server days, pre Microsoft SQL Server 6.5).

我知道 C 语法!=在 SQL Server 中是由于其 Unix 传统(回到 Sybase SQL Server 时代,Microsoft SQL Server 6.5 之前)。