MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old March 4th, 2004, 04:31 PM
scienco scienco is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 44 scienco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 9 sec
Reputation Power: 5
STORED PROCS with optional parameters

I was wondering if anyone could shed some light on this for me....?

I want a user to be able to perform a search on my database. However, I want to make the various search parameters in the stored proc optional. Take the following:

CREATE PROCEDURE sp_Accounts_Search_FullSearch

@user_age tinyint = NULL,
@user_sex varchar (6) = NULL,
/* Null as default values */
@user_country varchar (20) = NULL


AS

SELECT * FROM TBL_ACCOUNTS_USERS u

WHERE u.user_age = @user_age
AND u.user_sex = @user_sex
AND u.user_country = @user_country

GO

So basically, if user_sex or whatnot is not specified when the stored proc is executed, then it takes a null value. - So I'm basically trying to get it to ignore any parameter that's set to null when performing the search and return the results, regardless..... But it doesn't seem to be working :S

Any suggestions?

Thanks in advance!
__________________
<%Questions of Science%>

Reply With Quote
  #2  
Old March 7th, 2004, 12:44 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 26 m 13 sec
Reputation Power: 797
You can use case statements to handle the null parameters, something like this:
Code:
CREATE PROCEDURE sp_Accounts_Search_FullSearch

@user_age tinyint = NULL,
@user_sex varchar (6) = NULL,
/* Null as default values */
@user_country varchar (20) = NULL 


AS

SELECT * FROM TBL_ACCOUNTS_USERS u
WHERE u.user_age = CASE WHEN @user_age IS NULL
                                   THEN u.user_age
                                   ELSE @user_age
                             END
AND u.user_sex = CASE WHEN @user_sex IS NULL
                                  THEN u.user_sex
                                  ELSE @user_sex
                         END
AND u.user_country = CASE WHEN @user_country IS NULL
                                  THEN u.user_country
                                  ELSE @user_country
                               END 
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month

Reply With Quote
  #3  
Old March 9th, 2004, 12:19 PM
scienco scienco is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 44 scienco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 9 sec
Reputation Power: 5
Thankyou!

Thank you very much indeed! Those case statments did the trick!

Cheers

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > STORED PROCS with optional parameters


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway