Go Premium for a chance to win a PS4. Enter to Win


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
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
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…
Suggested Courses

926 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