Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6920759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:11:21+00:00 2026-05-27T10:11:21+00:00

I have a dynamic stored procedure and have to add where clause dynamically in

  • 0

I have a dynamic stored procedure and have to add where clause dynamically in case statement in SQL Server 2008.

My procedure is as below: –

CREATE PROCEDURE SPGETDATA
@STRNAME NVARCHAR(100),
@STRCODE NVARCHAR(100)

AS
BEGIN
SELECT  myTable.*
FROM myTable
WHERE 
IsDELETED = 0  
AND STRNAME LIKE CASE WHEN (RTRIM(LTRIM(@STRNAME))) <> '' THEN 
'%'+ @STRNAME + '%' ELSE '%%' END 
AND STRCODE LIKE CASE WHEN (RTRIM(LTRIM(@STRCODE)) <> '') THEN 
'%' + @STRCODE + '%'  ELSE '%%' END** 

END

The user can select either @strname or @strcode. But not both at a time.

In that case one like statement is ok but the alternative is always a burden over query because it will always be as

@STRNAME like '%%'

or as below

@STRCODE like '%%'

Now if I use this approach, will compiler cost some time to search like ‘%%’ even there is nothing to match or will it bypass it and cost nothing? I checked the execution plan also but it displays nothing for the like clause.

Hence I have to use this in webApps so speed of the sp has to consider. And the table has millions of rows.

Execution plan for both are same for both. If I use like cluase in query or remove it from query it shows – Clustred index sacn 100%.

Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:11:21+00:00Added an answer on May 27, 2026 at 10:11 am

    Firstly if either of the columns are nullable then testing col LIKE '%%' is not a No-Op but is actually equivalent to testing that the column col IS NOT NULL which may well not be the desired effect.

    Secondly if the column is not nullable so it truly is a No-Op then this is not a particularly good approach in that SQL Server will not optimise out the check. See Dynamic Search Conditions in T-SQL for better approaches.

    Chances are in your case it won’t make much difference though.

    As you are always doing a search with a leading wildcard against one or other column and you are doing SELECT * then likely you will end up with a full table scan anyway.

    One case where it could make a difference would be where you have a narrower index on one or more of the columns that could be scanned in preference to scanning the whole clustered index / table. Even there however SQL Server would still need to do book mark lookups to retrieve the * so could evaluate the residual predicate quite cheaply for that particular query.

    However the plan generated would be completely inappropriate for the invovcation with the other parameter so this attempt at a catch all query could give you a parameter sniffing issue as demonstrated below.

    Test Table

    CREATE TABLE myTable
    (
    id int primary key,
    STRNAME VARCHAR(100) NOT NULL,
    STRCODE VARCHAR(100) NOT NULL,
    IsDELETED BIT NOT NULL DEFAULT 0,
    Filler CHAR(7000) NULL,
    )
    INSERT INTO myTable(id, STRNAME, STRCODE)
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)), 
           ISNULL(name,type), 
           ISNULL(name,type)
    FROM master..spt_values
    
    CREATE INDEX ix ON myTable(STRNAME)
    

    Invoke first with name parameter (Scan count 1, logical reads 7)

    EXEC sp_executesql N'
    SELECT  myTable.*
    FROM myTable
    WHERE 
    IsDELETED = 0  
    AND STRNAME LIKE CASE WHEN (RTRIM(LTRIM(@STRNAME))) <> '''' THEN 
    ''%''+ @STRNAME + ''%'' ELSE ''%%'' END 
    AND STRCODE LIKE CASE WHEN (RTRIM(LTRIM(@STRCODE)) <> '''') THEN 
    ''%'' + @STRCODE + ''%''  ELSE ''%%'' END 
    ', 
    N'@STRNAME NVARCHAR(100),
    @STRCODE NVARCHAR(100)
    ', @STRNAME = '(rpc)', @STRCODE=''
    

    Invoke with code parameter reusing same plan (Scan count 1, logical reads 7690)

    EXEC sp_executesql N'
    SELECT  myTable.*
    FROM myTable
    WHERE 
    IsDELETED = 0  
    AND STRNAME LIKE CASE WHEN (RTRIM(LTRIM(@STRNAME))) <> '''' THEN 
    ''%''+ @STRNAME + ''%'' ELSE ''%%'' END 
    AND STRCODE LIKE CASE WHEN (RTRIM(LTRIM(@STRCODE)) <> '''') THEN 
    ''%'' + @STRCODE + ''%''  ELSE ''%%'' END 
    ', 
    N'@STRNAME NVARCHAR(100),
    @STRCODE NVARCHAR(100)
    ', @STRNAME = '', @STRCODE='(rpc)'
    

    Invoke with code parameter generate specific plan (Scan count 1, logical reads 2517)

    EXEC sp_executesql N'
    SELECT  myTable.*
    FROM myTable
    WHERE 
    IsDELETED = 0  
    AND STRNAME LIKE CASE WHEN (RTRIM(LTRIM(@STRNAME))) <> '''' THEN 
    ''%''+ @STRNAME + ''%'' ELSE ''%%'' END 
    AND STRCODE LIKE CASE WHEN (RTRIM(LTRIM(@STRCODE)) <> '''') THEN 
    ''%'' + @STRCODE + ''%''  ELSE ''%%'' END 
    OPTION (RECOMPILE)
    ', 
    N'@STRNAME NVARCHAR(100),
    @STRCODE NVARCHAR(100)
    ', @STRNAME = '', @STRCODE='(rpc)'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dynamic SQL statement I've created in a stored procedure. I need
I am building a dynamic query as a stored procedure in SQL Server 2008
I have a messy stored procedure which uses dynamic sql. I can debug it
I have a Stored Procedure which executes some dynamic SQL. I want to use
I have a stored procedure which is building a dynamic sql query and then
I have dynamic sql in stored procedure: DECLARE @sql nvarchar(1000) SET @sql = 'UPDATE
I'm trying to write a stored procedure [SQL 2008] with a dynamic, auto incrementing
Hi i have a stored procedure which will have a dynamic clause, this is
I have two sql server tables with the same structure. In a stored procedure
I have a stored procedure that contains dynamic sql to return a result set.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.