Alex
Alex

Reputation: 73

tsql select all or use a where condition

I am making an application which has a simple UI to show a database table. It allows user to enter a value to filter the rows or tick a checkbox to display the whole table.

Is it possible to make a where condition which return all rows My target is to use a SQL "select blah, blah2, blah3 from tbl1 where blah = @anInputValue". But this SQL cannot return whole table for whatever value I passed.

blah blah2 blah3
1 2 3
4 5 6
7 8 9

When user check "select all", it returns 3 rows. When user enter 4, it returns the middle row

My problem is I have 4 "Select all" checkboxes, I do not want to make many queries and many "if then else" to select the right query.

Upvotes: 0

Views: 50

Answers (1)

juergen d
juergen d

Reputation: 204794

Like this you can leave the input parameter empty to get all values.

select *
from tbl1 
where @anInputValue is null 
   or blah = @anInputValue

You can use a different value to indicate getting all values like for instance

where @anInputValue = -1
   or blah = @anInputValue

Upvotes: 4

Related Questions