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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:35:28+00:00 2026-06-06T01:35:28+00:00

Now I have a long query and I’d like to alternate with such a

  • 0

Now I have a long query and I’d like to alternate with such a statement

where userid=@userid

But I have other parameters which change flow, so, I want to have two options, selecting one user or any user

Can I send a username so that It can select and user? like we do in LIKE statements, we send

where username LIKE "%%"

and we get all records, how can I do that with INT?

Because my query is parameterized, I can’t add “OR 1=1” like

where userid=1 or 1=1

should I use BETWEEN and get two parameters, so when I select one record, I’ll enter userid-1 and userid+1 as two paramters, and get one user, and when I wand to select all, I’ll enter 1 and 1 million.

Is it a logical way for parameterized queries or is there another way, with one parameter?

SOLUTION
This is how I use the solution, when you use the or column on the right, your sql will be broken.

  • 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-06-06T01:35:30+00:00Added an answer on June 6, 2026 at 1:35 am

    Typically this is done using

    WHERE userid = @userid OR @userid IS NULL
    

    Another variation (if the userid column is not nullable):

    WHERE userid = COALESCE(@userid, userid)
    

    Yet another (for ints) is to say

    WHERE userid BETWEEN COALESCE(@userid, 1)
        AND COALESCE(@userid, 2000000000); -- well, 2147whatever
    

    But if you have a lot of these conditions and every time the query is run the parameters change, you could be locking yourself into bad plans that aren’t always appropriate. So sometimes the answer is to generate dynamic SQL:

    SET @sql = @sql + CASE WHEN @userid IS NOT NULL THEN 
        N' AND userid = ' + CONVERT(VARCHAR(12), @userid);
    

    Couple this with OPTION RECOMPILE and/or the instance setting “optimize for ad hoc workloads” you’ll only stuff up the plan cache with plans for versions of the query that are executed more than once.

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

Sidebar

Related Questions

I have very long integer sequences that look like this (arbitrary length!): 0000000001110002220033333 Now
I'm aware of server-side javascript for a long time now, but I don't have
I have long used the mysql_query() to do my stuff but now I am
Say I have long running update query update some_table set modification_time = now() where
For a long time now I have noticed something annoying when working on Web
I have the code below. It looks long and complicated and now I have
I have tried for a very long time now to figure out, why this
I have searched for this for a long time now... I am writing an
I have a function that convert ticks to time_t format long ticks = DateTime.Now.Ticks;
I now have a running Java program which only lacks of the final step,that

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.