Solved

T-SQL Pattern and Exact Match Searches

Posted on 2006-06-21
5
3,690 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 125 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now