Solved

SQLServer and if/case in where clause

Posted on 2011-09-22
2
560 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…

914 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

21 Experts available now in Live!

Get 1:1 Help Now