How do the SQL “IS” and “=” operators differ?

后端 未结 5 1965
粉色の甜心
粉色の甜心 2021-01-03 21:59

I am building some prepared statements that use parametrized values. As an example:

SELECT * FROM \"Foo\" WHERE \"Bar\"=@param

Sometimes

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 22:11

    You may be thinking about this incorrectly. If you're talking about SQL Server, for example (since that's what I have to hand), your second example will result in a syntax error. The value on the right-hand side of IS cannot be 5.

    To explain, consider MSDN's explanation of these two operators in T-SQL (note that asking about "SQL" and about "SQL Server" are not necessarily the same).

    Equals (=) operator

    IS NULL operator

    Notice something important, there. There is no such thing as the "IS" operator in T-SQL. There is specifically the IS [NOT] NULL operator, which compares a single expression to NULL.

    That's not the same thing as the = operator, which compares two expressions to each other, and has certain behavior when one or both of the expressions happens to be NULL!

提交回复
热议问题