-2
Declare @ID uniqueidentifier =Null

select * from Emp where EmpID=@ID

Above query giving no results.

Dale K
  • 25,246
  • 15
  • 42
  • 71
at9063
  • 33
  • 1
  • 1
  • 8

2 Answers2

0

NULL is not a value, is a state, so you can't test for something equals to NULL using standard operators.

All expression in which is involved a NULL will result in NULL

DECLARE @I INT = NULL
DECLARE @A VARCHAR(20) = NULL

'Hello ' + @A + 'World' = NULL
127 * 54 - (32 / @i) = NULL

So your query select * from Emp where EmpID=@ID will give no results when @ID is null

You need to use the special operator IS NULL to test for your parameter (see @Sergey comment)

select * 
from Emp 
where EmpID=@ID  /* this will catch rows when @ID is not NULL */
or (EmpID IS NULL and @ID IS NULL) /* this will catch rows when @ID is NULL */
MtwStark
  • 3,866
  • 1
  • 18
  • 32
-1

Handle it using the ternery operator like this

Declare @ID nvarchar(50);
select * from Emp where EmpID=@ID is null?0:@ID;
Reza Karim
  • 24
  • 2
  • The words is "ternary", SQL Server doesn't have one, it does have `CASE` though, but you can't return a boolean out of it – Charlieface Jul 22 '22 at 09:06