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 923457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:11:48+00:00 2026-05-15T19:11:48+00:00

I am converting a stored procedure which I had previously written as a string

  • 0

I am converting a stored procedure which I had previously written as a string then, using BIT parameters I decided whether to append certain WHERE/ON clauses

This sp is passed a number of comma-separated strings and then some of the dynamic WHERE clauses are like:

IF @pUse_Clause_A THEN SET @WhereClause = @WhereClause + ' AND [FIELD_A] IN (' + @pComma_Separated_List_A + ')'

In this case, @pComma_Separated_List_A is something like ‘1,3,6,66,22’ … a list of the things I want included.

Now I am changing these from strings into TVP,s so I can just use “real” SQL like
AND [FIELD_A] IN (SELECT [TVP_FIELD] FROM @pTVP_A)

When I do this, I don’t like the string-building method

However, I also don’t like having to nest the IF statements.

IF A 
    ENTIRE SQL WHERE A
ELSE
    ENTIRE SQL WITHOUT WHERE CLAUSE

The more parameters I add, the more complicated it gets:

IF A
    IF B
        SQL WHERE A AND B
    ELSE
        SQL WHERE A
ELSE
    IF B
        SQL WHERE B
    ELSE
        SQL

What I would rather do is something like this:

SELECT * FROM TABLE
WHERE 1=1
CASE USE_A WHEN 1 THEN 
     AND [FIELD_A] IN (SELECT A FROM TBP_A)
END
CASE USE_B WHEN 1 THEN
     AND [FIELD_B] IN (SELECT B FROM TVP_B)
END

I know it ignored SQL outside the chosen “IF” result, but having all that duplicated statement seems sloppy

  • 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-15T19:11:49+00:00Added an answer on May 15, 2026 at 7:11 pm

    Dynamically changing searches based on the given parameters is a complicated subject and doing it one way over another, even with only a very slight difference, can have massive performance implications. The key is to use an index, ignore compact code, ignore worrying about repeating code, you must make a good query execution plan (use an index).

    Read this and consider all the methods. Your best method will depend on your parameters, your data, your schema, and your actual usage:

    Dynamic Search Conditions in T-SQL by by Erland Sommarskog

    The Curse and Blessings of Dynamic SQL by Erland Sommarskog

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

Sidebar

Related Questions

I am using one stored procedure in which I send @columnname whose type in
I have a stored procedure which accepts two parameters of type UniqueIdentifier When I
Is there a way to write SQL Server Stored Procedure which to be strongly
I have a stored procedure which calculates the distance between two coordinate pairs as
I have this SQL function which is wrapped by a stored procedure: ALTER FUNCTION
I'm trying to return 'ItemId' column value of 'item' table using stored procedure based
When I try to run a stored procedure using EXECUTE, the proc runs fine.
I'm converting a stored procedure in some software I'm maintaining from SQL Server SQL
If I have a parametrized stored procedure which takes a varchar(10) value and converts
I have a stored procedure. One of its input parameters is expecting a char(8).

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.