Home > Blockchain >  Return all results (including null) when SQL stored procedure parameter is null
Return all results (including null) when SQL stored procedure parameter is null

Time:03-17

I have SQL table that contains null values for ex:

ID NAME CODE
1 Joe 123
2 Doe 456
3 Soe NULL

I want to check if the where clause parameter is null then return all table, what I tried:

@devCode int = NULL

SELECT * FROM myTable WHERE CODE = ISNULL(@devCode, CODE)

But the last row (ID 3) gets excluded when @devCode = NULL. I want that to be included too in result when the parameter is null.

p.s. I also tried IIF and CASE but the result is the same.

CodePudding user response:

I think it will work

SELECT * FROM myTable WHERE CODE = @devCode OR @devCode IS Null
  • Related