8

This is what I'm trying to achieve in it's simplest form:

SELECT 
    p.ProductId,
    p.ProductName,
    p.SKU
FROM tbl_Product p
WHERE (p.ProductName LIKE '%white%' OR p.SKU LIKE '%white%')
AND (p.ProductName LIKE '%cup%' OR p.SKU LIKE '%cup%')

I'm trying to do this in a UDF, which accepts a comma separated parameter of all the search terms.

I tried splitting that parameter into a temporary table and trying a join, like this:

DECLARE @SearchText nvarchar(1000) SELECT @SearchText='white,cup'

DECLARE @SearchTerms TABLE (String nvarchar(200))
INSERT INTO @SearchTerms (String)
SELECT '%' + String + '%' FROM dbo.CsvSplitString(@SearchText)

SELECT 
    p.ProductId,
    p.ProductName,
    p.SKU
FROM tbl_Product p
JOIN @SearchTerms s ON (p.ProductName LIKE s.String OR p.SKU LIKE s.String)

But that doesn't return what I want - it returns any records where the Name or SKU matches either of the search terms. I need it to return like the first query, where the Name or SKU matches all of the search terms (I think that makes sense).

Would be massively appreciative of a push in the right direction - let me know if you need me to be more specific.

Note: full text searching is not a viable option at the moment.

Thanks!

8
  • If you need to "match all of the search terms" doesn't that mean that you can only have two(2) search terms, since you only have two columns to match against? Apr 24, 2012 at 14:39
  • the number of search terms can be infinite. for example i could search for "white plastic cup". So for each term, either ProductName or SKU must match. Hope that makes sense.
    – royse41
    Apr 24, 2012 at 14:40
  • It does not make sense. How could only two columns match all of three distinct search terms? It's not possible the way that you have described it. Apr 24, 2012 at 14:43
  • @seanxe, ignore him it makes perfect sense.
    – Blindy
    Apr 24, 2012 at 14:44
  • 1
    @RBarryYoung, it's not =, it's like '%...%', which means "contains". He's basically doing a normal textual search in 2 fields with multiple keywords. Go look at Google if you want an example of searching for multiple words in two fields (page title and page contents).
    – Blindy
    Apr 24, 2012 at 15:11

2 Answers 2

4

The query below should do it but it may not be the fastest!

DECLARE @SearchText nvarchar(1000) SELECT @SearchText='white,cup'
DECLARE @keywords TABLE (keyword nvarchar(255))
DECLARE @keywordCount int

INSERT INTO @keywords (keyword) SELECT * FROM dbo.CsvSplitString(@SearchText)
SET @keywordCount = (SELECT COUNT(*) FROM @keywords)


SELECT *
FROM tbl_Product p
WHERE EXISTS
    (SELECT *
    FROM
        (SELECT productId
        FROM tbl_Product, @keywords
        WHERE productname like '%' + keyword + '%' or sku like '%' + keyword + '%' 
        GROUP BY productid
        HAVING COUNT(*) = @keywordCount
        ) matches 
    WHERE p.ProductId=matches.ProductId
    )
0
0

If all else fails, you can run a cursor over @SearchTerms and loop over the results, checking for each item if it's present in your results.

You most likely won't have many search items (5-6 would be so rare), so the cursor cost should be negligible compared to running like over and over on your text. And that shouldn't be too expensive either.

Edit: What I've done in the past for searches is send the query in plain text to the server instead of relying on stored procedures, so I could piece together my conditions myself. You can do the same with dynamic queries and exec on the server if you so wish. This way seems pretty hacky and it's not obvious it's a performance improvement. Food for thought.

3
  • thanks for the suggestion. i've always tried to steer clear from cursors because i always seem to read bad things about performance, but i can see where you're coming from re: number of search terms. i'll definitely have a look into it now.
    – royse41
    Apr 24, 2012 at 14:58
  • the site was originally in linq to sql, and was just being punished under the load so we decided to move it all in to sql - the peformance gains have been huge. it may be worth pointing out as well that this question is a small part of a much larger query. thanks for your answer though, food for thought with cursors.
    – royse41
    Apr 24, 2012 at 15:31
  • It's at least a quick to implement solution while you look into something else (if you still feel the need for it), should be like 6 lines of code.
    – Blindy
    Apr 24, 2012 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.