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

后端 未结 5 2068
粉色の甜心
粉色の甜心 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

    Edit: (Update from OP: This doesn't do what I If @param is 5, then I want to see only records where Bar is 5. I want to see records where Bar is NULL if, and only if, @param is NULL. I apologize if my question didn't make that clear.)

    In that case, I think you should try something like this:

    SELECT * FROM Foo WHERE Bar=@param OR (Bar IS NULL AND @param IS NULL)
    

    Previous post:

    Why not simply use OR ?

    SELECT * FROM "Foo" WHERE "Bar"=@param OR "Bar" IS NULL
    

    In SQL Server, you can use ISNULL:

    SELECT * FROM "Foo" WHERE ISNULL("Bar",@param)=@param
    

提交回复
热议问题