views:

43

answers:

2

I have an SQL Function with the following SQL within:

SELECT StockID FROM (SELECT DISTINCT StockID, 
ROW_NUMBER() OVER(ORDER BY DateAdded DESC) AS RowNum
FROM Stock
WHERE CategoryCode LIKE @CategoryID) AS Info
WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex + @maximumRows) - 1

I have a Parameter @CategoryID - however I need to take in a category ID such as "BA" and translated this to a list of Category IDs such as "IE","EG" etc so my WHERE clause looks like:

WHERE (CategoryCode LIKE 'IE' OR CategoryCode LIKE 'EG') AS Info

I have a Lookup Table which contains the "BA" code and then all the real category codes this means such as "IE" and "EG". How do I have the CategoryID expand to multiple "OR" statements in my SQL Function? I am unsure how to do this, can anyone solve this problem?

At the moment the query as shown can cope with one CategoryID such as "IE", this is done as I want a category page such as category.aspx where a parameter "BA" is passed such as category.aspx?category=BA and this page will list all items with the category codes "EG" and "IE".

The reason I need this is there is a "parent" category code which has multiple "children" category codes which are different to the parent code. I am using ASP.NET and .NET 3.5 on the front-end if this helps.

A: 

Assuming the parameter is a common delimited list of categoryID's

Try

WHERE charindex(','+CategoryCode+',',','+@CatParam+',') > 0

Performance won't be great, but it should do the trick for you

Sparky
Performance is a requirement - this is for production code.
RoguePlanetoid
OK, the above works for small tables and is an easy solution, but using functions like above in a WHERE clause shouldn't be used for an sizable database. Looks like you got some additional answers, good luck with your application.
Sparky
+5  A: 

Try using

WHERE CategoryCode IN (
    SELECT LookupCategoryCode 
    FROM LookupTable 
    WHERE LookupCategoryId = @CategoryId
)

Replacing "LookupCategoryCode", "LookupTable", and "LookupCategoryId" for the respective values in your lookup table.

Obalix
+1, I was just typing that up, beat me to it!
KM
I'm sure I tried something like this, but will try it again. Most combinations I was trying resulted in increased read times, I'm trying to keep these to a minimum.
RoguePlanetoid
With indexes in place it should be ok. However, if the performance *really* becomes critical, you could use a stored procedure to build the sql query and then execute it via sp_executesql [http://msdn.microsoft.com/de-de/library/ms188001.aspx]. But be sure to test first as you have to take into account the overhead for writing and maintaining the stored procedure.
Obalix
I have the example posted - it does work but my Database Reads are 10x higher than they should be - I'm still missing something, but it does return the behaviour I need - just need to fix the tables so I can get the performance I need.
RoguePlanetoid
Just thought I'd update that it works now - looks like I missed an Index, did not have a Clustered Index where I should have.
RoguePlanetoid