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

  • Home
  • SEARCH
  • 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 5840779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:47:56+00:00 2026-05-22T11:47:56+00:00

I need to create a stored procedure which takes 12 arguments and the query

  • 0

I need to create a stored procedure which takes 12 arguments and the query is filtered with a different combination of this arguments. All 12 arguments are not mandatory as if I pass 3 or 5 or 12 arguments depends on search inputs entered by user.

I can create 2 ways, either using a dynamic SQL query or using ‘Case where’ statements. Example of these queries are as below:

  1. Dynamic Query

    DECLARE @sql VARCHAR(MAX) 
    DECLARE @condition VARCHAR(MAX)=''
    Declare @var1 varchar(10)
    Declare @var2 varchar(10)
    Declare @var3 varchar(10) 
    SET  @sql='SELECT * FROM TableDemo1 TD1 WITH(NOLOCK)
     INNER JOIN TableDemo2 TD2 ON TD1.Column1=TD2.Column2'
    if(@var1 <>0 and @var1 is not null)
    begin
        if(@condition<>'')
        begin
            set @condition=@condition + ' and TD1.columnTest1='+@var1)
        end
        else
        begin
            set @condition=' where TD1.columnTest1='+@var1
        end
    end
    if(@var2 <>0 and @var2 is not null)
    begin
        if(@condition<>'')
        begin
            set @condition=@condition + ' and TD2.columnTest2='+@var2)
        end
        else
        begin
            set @condition=' where TD2.columnTest2='+@var2
        end
    end
    
    if(@var3 <>0 and @var3 is not null)
    begin
        if(@condition<>'')
        begin
            set @condition=@condition + ' and TD1.columnTest3='+@var3)
        end
        else
        begin
            set @condition=' where TD1.columnTest3='+@var3
        end
    end
    SET @sql=@sql+@condition
    EXEC(@sql)
    
  2. Query with CASE WHERE

    Declare @var1 varchar(10)
    Declare @var2 varchar(10)
    Declare @var3 varchar(10)
    SELECT *
    FROM TableDemo1 TD1 WITH(NOLOCK)
        INNER JOIN TableDemo2 TD2 ON TD1.Column1=TD2.Column2
    WHERE
      (CASE WHEN   
        (@var1<>0 and @var1 is not null)   
        THEN  
        CASE WHEN TD1.columnTest1=@var1 THEN 1 ELSE 0 END  
        ELSE 1 END)=1  
      AND   
      (CASE WHEN  
        (@var2<>0 and @var2 is not null)   
        THEN  
        CASE WHEN TD2.columnTest2=@var2 THEN 1 ELSE 0 END  
        ELSE 1 END)=1  
      AND
      (CASE WHEN
        (@var3<>0 AND @var3 IS NOT NULL)
        THEN
        CASE WHEN TD1.columnTest3 = @var3
        THEN 1 ELSE 0 END
        ELSE 1 END) =1
    

This is just part of my stored procedure, there are 7-8 tables with joins and as above in query variety in conditions.

If I use a dynamic query, SQL Server will have to create an execution plan every time, but if I use ‘case where’ it also makes the query slow.

I am aware of disadvantages of dynamic SQL, but which technique I should use?

  • 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-22T11:47:56+00:00Added an answer on May 22, 2026 at 11:47 am

    Generally it depends, but most often I use dynamic queries as a last resort. Regarding your question, I would most probably go with the CASE solution, but I think your CASE expressions are unnecessarily complicated. I would replace the WHERE clause with something like this:

    ...
    WHERE
        TD1.columnTest1 = COALESCE(NULLIF(@var1, 0), TD1.columnTest1)
        AND   
        TD2.columnTest2 = COALESCE(NULLIF(@var2, 0), TD2.columnTest2)
        AND   
        TD1.columnTest3 = COALESCE(NULLIF(@var3, 0), TD1.columnTest3)
    

    With proper indexing this shouldn’t be too slow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a stored procedure that upon exceution checks if any new
I need to create an ASP page (classic, not ASP.NET) which runs remote shell
I need to get stored procedure (not standart, only custom) to delete unused rows
Let's say that I have a form which runs a stored procedure. This stored
I need to create a database table to store different changelog/auditing (when something was
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I have a number of search functions (stored procedures) which need to return results
I need to create a configuration section, that is able to store key-value pairs
I need to create a historical timeline starting from 1600's to the present day.
I need to create a linked server to a DB2 database on a mainframe.

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.