T-SQL Pattern and Exact Match Searches

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
LVL 1
jtrembackAsked:
Who is Participating?
 
ala_frostyCommented:
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
 
ala_frostyCommented:
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
 
jtrembackAuthor Commented:
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
 
Anthony PerkinsCommented:
>>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
 
Anthony PerkinsCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.