Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


T-SQL Pattern and Exact Match Searches

Posted on 2006-06-21
Medium Priority
Last Modified: 2012-06-27
I am developing a stored procedure that allows users to search by a variety of criteria. Let's say, for the sake of brevitiy, I only search one table with three fields:
ApplicationID  INT Identity,
DecisionID INT,    -- FK
ResponseID INT   -- FK

The stored procedure looks something like:
CREATE PROCEDURE sp_ApplicationList
   @DecisionID    INT = 0,  -- an ID of zero indicates all decisions
   @ResponseID  INT = 0   -- an ID of zero indicates all response codes
  SELECT ApplicationID
    FROM Table_Application
  WHERE DecisionID = CASE @DecisionID WHEN 0 THEN DecisionID ELSE @DecisionID END
      AND ResponseID = CASE @ResponseID WHEN 0 THEN ResponseID ELSE @ResponseID END

All records in the referenced Type and Decision tables have an ID of 1 or greater. Knowing that I set it up so that if the passed in parameters equal zero all values are returned (at least for that parameter). This allows me to use a CASE statement to search for all matching records.

The problem I have is what if I have Varchar column and I allow users to do exact as well as pattern searches. In one case I have to use LIKE in the other the equal sign. Is there still a way to use a CASE statement (or any other statement to search for either exact matches or wildcards without having to resort to an IF statement?

Any help is appreciated
Question by:jtremback
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 16956767
1. SQL Server will pattern match on exact values.
2. Your procedure will be longer, but will execute a LOT faster if you ditch the single-query-does-everything approach.

if @decisionID = 0 and responseID = 0
  SELECT ApplicationID FROM Table_Application
else if @decisionID = 0
    SELECT ApplicationID FROM Table_Application where responseID = @responseID
else if @responseID = 0
    SELECT ApplicationID FROM Table_Application where decisionID = @decisionID
    SELECT ApplicationID FROM Table_Application where decisionID = @decisionID AND ResponseID = @ResponseID

Yes, it means you have to deal with maintaining four queries for one change, but that's the price you pay to get speed from a flexible solution.

Accepted Solution

ala_frosty earned 500 total points
ID: 16956775
declare @a varchar(20)
set @a = 'somevalue'

select * from sometable where somefield like @a

will return the exact same response as

select * from sometable where somefield = @a

though perhaps slightly slower for the former query.

Author Comment

ID: 16962187
while having different queries for different parameter values is in theory a good way to go it is impractical to use when you have more than two possible search criteria. The example that I have given is vastly simplified from the real problem. The query has to be set up to allow users to use up to 9 different search criteria which means that you would have an enormous number of different Select statements. Plus, each select statement draws in data from a number of tables because the search result displays some summary information (from where users can then drill down but that is kinda irrelevant for my problem).

The second post might actually help and I have to do some perfomance testing to make sure that always using LIKE does not degrade the performance too much.

LVL 75

Expert Comment

by:Anthony Perkins
ID: 16963837
>>and I have to do some perfomance testing to make sure that always using LIKE does not degrade the performance too much.<<
It will.  The question is whether it is acceptable.  You need to understand that LIKE forces a table scan (indexes are ignored).  As an alternative consider using Full-Text Search.
LVL 75

Expert Comment

by:Anthony Perkins
ID: 16963871
Also, instead of using "an ID of zero indicates all decisions", it is standard practice to use Null.  So that your query would look like this:

CREATE PROCEDURE sp_ApplicationList
         @DecisionID INT = Null,
         @ResponseID INT = Null


SELECT      ApplicationID
FROM      Table_Application
WHERE      (@DecisionID Is Null Or DecisionID = @DecisionID)
      AND (@ResponseID Is Null Or ResponseID = @ResponseID)

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question