Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 24th, 2005, 07:34 PM
Authorized User
 
Join Date: Jun 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default Write stored procs to accept 'all' values in a col

I have a stored procedure that takes 5 parameters:

1. start date
2. end date
3. call center
4. revenue band
5. product line

The procedure works great, and returns the write data for what a user has entered. What I am having trouble with is when I try to code for a user running the proc and wanting ALL the values in a column, for instance:

1. start date = january 1, 2005
2. end date = today
3. call center = calgary
4. revenue band = any of them
5. product line = pcs

To code this, I have to check to see if revenue band is 'ALL', and if it is I run a query that has no WHERE clause for revenue band, as opposed to if it wasn't 'ALL' I would run one that had a query for revenue band.

To check all possible combinations of 5 parameters is a ton of nested IF's, and no fun at all to code as it's massive. Surely this is a fairly common thing, what do I do here? I'm looking into building a dynamic SQL statement and running that - is that the best way to go?

Pat
 
Old May 24th, 2005, 09:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Use what I've called the COALESCE trick.

Set the value of the parameter to NULL to mean "Don't care", i.e. you want any (or all) values, otherwise set it to the value you want to compare to.

In your example, then, the parameter value for 'Revenue Band' would be set equal to NULL, and all the rest set to whatever you want to test against.

Then, your WHERE clause uses COALESCE, as:

    ... WHERE RevenueBand = COALESCE(@RevenueBand, RevenueBand) ...

The way this works is that if the parameter @RevenueBand has a nonnull value, COALESCE returns the parameter value, so the result of the WHERE clause is to test the column value against the parameter value. But, if the parameter has the value NULL, then the COALESCE returns the column value. Thus, the WHERE clause tests the column to see if it is equal to itself, which of course is always true, so this is in effect a no-op, and all values are selected.

Note that if the column can have a NULL value, this won't work - you'll have to COALESCE both sides to some appropriate value outside the domain of possible values for the column if you want a NULL parameter to select NULL column values.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old May 25th, 2005, 01:14 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

And if your column does contain null values, I think this will work as well:

WHERE RevenueBand = @RevenueBand OR @RevenueBand IS NULL

Here you check whether the column searched in contains the value of @RevenueBand, or the entire parameter @RevenueBand is null (which is the case when you don't supply an explicit value)

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Stored procs with ASP tdaustin Classic ASP Databases 4 June 17th, 2008 01:20 AM
O/R mappers vs stored procs rocco50 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 13 June 9th, 2007 07:49 AM
undocumented stored procs priyaram SQL Server 2005 0 February 21st, 2007 12:24 PM
Informix stored procs from ADO tomRA Access 0 October 28th, 2004 09:45 AM
Transactions in C#, not stored procs organicglenn BOOK: ASP.NET Website Programming Problem-Design-Solution 6 October 10th, 2004 09:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.