Solved

SQLServer and if/case in where clause

Posted on 2011-09-22
2
575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

623 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