conditional stored procedure with/without passing parameter

后端 未结 3 1828
Happy的楠姐
Happy的楠姐 2020-12-21 12:15

I created a stored procedure which when passed nothing as parameter should return the entire table. But if the studentId is passed, then return her details. Something like t

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-21 13:18

    You're trying to test for null using =, a comparison operator. If you're using ANSI nulls, any comparison against null is false.

    Where @studentId is any value (or null) the following expressions are all false:

    @studentId = null  -- false
    @studentId > null  -- false
    @studentId >= null  -- false
    @studentId < null  -- false
    @studentId <= null  -- false
    @studentId <> null -- false
    

    So, in order to test for null you must use a special predicate, is null, i.e.:

    @studentId is null
    

提交回复
热议问题