If Variable is Blank Then No Where Clause

2019-08-01 06:17发布

问题:

I am trying to do what the below pseuedo code infers:

WHERE 
CASE 
WHEN @test <> '' THEN Agent = @test
ELSE --no where clause 
END

What is the correct structure for this?

回答1:

use OR:

select * from yourTable
where @test = '' OR Agent = @test

if @test coming with null value (instead of ''), you must use:

select * from yourTable
where @test is null OR Agent = @test