[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

T-SQL Pattern and Exact Match Searches

Posted on 2006-06-21
5
Medium Priority
?
3,768 Views
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:
Table_Application
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
AS
  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
0
Comment
Question by:jtremback
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:ala_frosty
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
else
    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.
0
 
LVL 7

Accepted Solution

by:
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.
0
 
LVL 1

Author Comment

by:jtremback
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.

Thanks
0
 
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.
0
 
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
AS

SET NOCOUNT ON

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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

Exchange database can often fail to mount thereby halting the work of all users connected to it. Finding out why database isn’t mounting is crucial and getting the server back online. Stellar Phoenix Mailbox Exchange Recovery is a champion product t…
Blockchain technology enhances society similar to the Internet. Its effects are broad, disruptive, and will boost global productivity.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

590 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