Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Select 'All'

Author  Topic 

SQLBoy14
Yak Posting Veteran

70 Posts

Posted - 2014-08-17 : 23:52:08
Hello,

I have a select statament:
Declare @BusColor varchar(10)


Select BusColor,
BusDriver
From BusDatabase
Where BusColor = @BusColor

These are what in BusDatabase:
BusColor BusDriver
Yellow Andy
Yellow Bryan
Yellow Jimmy
Blue Tony
Blue Jean

I know how to set @BusColor to either Yellow Blue but how do make the parameter will select all instead Yellow or Blue?

Thank you all










SQLBoy

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-08-18 : 03:48:25
Your WHERE clause should be

Where (BusColor = @BusColor or @BusColor ='All')


and you need to pass 'All' as a value if you want data for all colors

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SQLBoy14
Yak Posting Veteran

70 Posts

Posted - 2014-08-18 : 20:27:17
Thank you Madhivana

SQLBoy
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-08-19 : 08:33:45
I used something like this before:


WHERE @BusColor in (BusColor, '%')


using a percent sign as a wildcard (since that is what it means in the SQL LIKE operator). So, if you want 'all', pass a percent sign.
Go to Top of Page
   

- Advertisement -