Solved

SQLServer and if/case in where clause

Posted on 2011-09-22
2
558 Views
Last Modified: 2012-05-12
Have a stored proc for displaying customer info which appears as:
create proc custinfo @nonzero smallint as
select * from customer
where...

The parm @nonzero (1 or 0) limits the display of customer's balances.  If
@nonzero = 1 then only customers with balances <> 0 display.  Otherwise
all customers display.

My issue is how best to express the where clause to accomodate @nonzero.

Currently I have: ((@nonzero = 1 and balance <> 0) or (@nonzero = 0)).  Surely there's a
better solution.
0
Comment
Question by:hawand5
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 36579748
your way is ok. there are alternatives (dynamic sql is the simple one, writing a different select for each case another one), all with other drawbacks.
0
 
LVL 6

Assisted Solution

by:kswathi
kswathi earned 125 total points
ID: 36579782
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now