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
 General SQL Server Forums
 New to SQL Server Programming
 Any?

Author  Topic 

raysefo
Constraint Violating Yak Guru

260 Posts

Posted - 2007-01-19 : 16:23:20
Hi,

I have a SQL statement. In the where clause, if user selects a choice from dropdown list box and integer comes.

...
where deptid = 1
...

but if user does NOT select a choice then i want to use a command (if there is) to get any deptid.

...
where deptid = any
...

Any suggestions?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-19 : 16:27:50
Assuming NO department selected throws a value of 0 to the variable,

select * from whatever where @variable in (0, deptid)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

raysefo
Constraint Violating Yak Guru

260 Posts

Posted - 2007-01-19 : 16:34:02
i may misguided you. What i wanna do is, if no id is selected from dropdown, to get all deptartments.
select deptproperties
from mytables
where deptid = (here i wanna put something in order to get all department properties!)
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2007-01-19 : 20:05:31
I like Peter's method, but here's another...

WHERE DeptID = CASE WHEN @DeptID IS NULL THEN DeptID ELSE @DeptID END

--Jeff Moden
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-01-20 : 00:32:17
if no id is selected from dropdown then set @DeptID to NULL


where DeptID = coalesce(@DeptID, DeptID)



KH

Go to Top of Page
   

- Advertisement -