Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Proc: Parameter As Filter with Multiple Values

Status
Not open for further replies.

MomentSurfer

IS-IT--Management
Mar 7, 2006
13
CA
Greetings everyone,

I wish to seek some advice and perhaps gain some insight towards how other people are approaching a similar task.

I frequently use stored procedures to provide "list data" for my applications. The stored procedures usually have parameters to allow the aplication with a way of filter the data, server side. The filters are optional. If no parameter is specified all data is returned. If a parameter is specified only the matching data is returned. Here is an example of my typical stored proc for this:

....
@CustomerIDFilter int
......

SELECT Customer.CustomerID, Customer.CompanyName
FROM Customer
WHERE
@CustomerIDFilter IS NULL OR Customer.CustomerID = @CustomerIDFilter

This approach has been working great, and I really enjoy how clean and flexible the structure is.

My challenge is I sometimes encounter situations where the application needs to specify a filter but for *MULTIPLE* records.

e.g. WHERE CustomerID = 1 AND CustomerID = 5

If this were the case, I would use an nvarchar parameter called @CustomerIDWhereFilter and then have the application pass in the WHERE clause. I would then use another nvarchar variable to dynamically generate my entire SELECT / WHERE clause. This approach is far less cleaner because it deals with a whole lot of string concatenation.

Do you have any suggestions for how I could easily accomodate multiple values in a more clean and elegant manner?

Any insight you can provide would be greatly appreciated.

Thanks for your time.

David
 
Split string into temp table/table @variable, then join on that table... check FAQ section for this forum.

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Not if customer has some split-personality issues (a la Dr Jekyll) :)

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Thank you for your prompt response. FAQ #faq183-3132 was extremely helpful! So elegant.

David

PS: My customers do not have split personalities... it was in fact a type-o. :)
 
Er... not this faq... see faq183-3979 and faq183-5207

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top